BlitzMax Modules

BlitzMax Forums/BlitzMax Programming/BlitzMax Modules

MacSven(Posted 2007) [#1]
How can i create my own BlitzMax Module! My idea is to create a usb mod from libusb! Is that possible and easy or not?


Thareh(Posted 2007) [#2]
Hi,
Creating your own module shouldn't be so hard,
I've never done it myself, but I found this tutorial:
http://www.blitzbasic.com/Community/posts.php?topic=42290

Hope that helps, Good luck! :)


MacSven(Posted 2007) [#3]
I will take a look to it. But i think it is a hard work!


Brucey(Posted 2007) [#4]
Doesn't look too difficult.

If one were to do it properly of course, you might do libusb (for linux/OS X support) and libusb-win32 (for windows) wrapped up in the same module. Then you cover all the bases.

If you do go ahead, please include half-decent documentation and examples. The whole idea of having a wrapped library is that you don't want to have to go and look at the original library documentation to get an idea what such-and-such function is meant to do.
And if you can hide pointers and structs from the user, more the better.
If I have to use library pointers, via a module, directly in my app then I may as well have not bothered using the module at all and simply talked to the library instead.

Just some thoughts ;-)


MacSven(Posted 2007) [#5]
Hi Brucey,

Is it this that you meen:

http://search.cpan.org/~gwadej/Device-USB-0.21/lib/Device/USB.pm


MacSven(Posted 2007) [#6]
Again @ Brucey:

I found this:

www.math.ias.edu/doc/libusb-devel-0.1.8/manual.ps

Reward MainframeOSX


SebHoll(Posted 2007) [#7]
My idea is to create a usb mod from libusb!

I would be interested in having such a module too! Although I'm too busy at the moment to help, sorry!


MacSven(Posted 2007) [#8]
Is this the right way:

Module pub.libusb
ModuleInfo "Version: 1.00"
ModuleInfo "Modserver: Not yet implemented"
ModuleInfo "Author: Sven Schwiecker(MacOS part)"
ModuleInfo "License: Public Domain"
ModuleInfo "Contact: Sven.Schweicker@..."
ModuleInfo "Homepage: www.SvenSchwiecker.de"


Import "usb.c"
Import "linux.c"
Import "darwin.c"
Import "error.c"


Extern "C"

Function usb_descriptor_header (bLength:Int Ptr,bDescriptorType:Int Ptr)
Function usb_string_descriptor (bLength:Int Ptr,bDescriptorType:Int Ptr,wData:Int Ptr)
Function usb_hid_descriptor (bLength:Int Ptr,bDescriptorType:Int Ptr,bcdHDI:Int Ptr,bCountryCode:Int Ptr,bNumDescriptors:Int Ptr)

EndExtern

After Build Modules no errors is shown.
Is this good?


Perturbatio(Posted 2007) [#9]
Try using the functions and see what happens.


Brucey(Posted 2007) [#10]
usb_descriptor_header etc, aren't functions. They are C-structs.

This function "usb_init()" initializes the library, which, according to the docs, you call *first*.

"usb_find_busses()" looks like it returns a count of busses... and "usb_find_devices()" looks to return a count of devices... etc.

Since it's LGPL, you may wish to rather link to a DLL/.so/.dylib, rather than compile in or statically link to the library - unless your code is in-house or GPL'd.

struct usb_descriptor_header {
	u_int8_t  bLength;
	u_int8_t  bDescriptorType;
};

If you have, say, a pointer to this struct, you could retrieve the "length" field with something like :
length = Byte Ptr(headerPtr)[0]
.. and for the descriptor type :
descType = Byte Ptr(headerPtr)[1]
, since they both appear to be byte-size values.

Things like :
	struct usb_interface *interface;

inside another struct, is a pointer to a struct of "usb_interface", which itself might contain pointers to other structs.

The two ways you might go about accessing all this information is either via the Ptr access like I've mentioned above, or by writing glue c/cpp code which makes your BlitzMax code easier to manage.


MacSven(Posted 2007) [#11]
Hi,

My first test of the libusb module

Strict

Module Pub.libusb

ModuleInfo "Version: 1.00"
ModuleInfo "Modserver: Not yet implemented"
ModuleInfo "Author: Sven Schwiecker(MacOS part)"
ModuleInfo "License: Public Domain"
ModuleInfo "Contact: Sven.Schwiecker@..."
ModuleInfo "Homepage: www.SvenSchwiecker.de"

?MacOS
Import "darwin.c"
Import "error.c"
Import "usb.c"
Import "descriptors.c"


Extern "C"
Function usb_init()
Function usb_find_busses()
Function usb_find_devices()
EndExtern

Testprogramm:

'usbtest

usb_init()
Print x
x=usb_find_busses()
Print x
x=usb_find_devices()
Print x

and it works. Now it comes the next part.


Brucey(Posted 2007) [#12]
I suggest you use your own namespace, rather than Pub.

Also, you might find it easier to define the return values of the functions...
Function usb_find_busses:Int()


If you use SuperStrict, it will force you to define things like this, which in the long run should help maintainability, since you only have to look at the function definition to see what it returns, if anything.


MacSven(Posted 2007) [#13]
I change the namespace as soon as possible, at this time it is only for testings.
The problem that i had is, i have import "linux.c" and this is a problem under MacOS X.
But thanks for the help @Brucey, i hope i can make more then this.
It is my first time to make a module!


MacSven(Posted 2007) [#14]
Need a little bit more help:



All the function work in my Testprogram. The function usb_device:int() returns this: 2080899750 is this the adress of the last usb_device?
How can i get a list of all USB-Busses and USB-Devices from
this c-source (i'm working under MacOS X):



Brucey(Posted 2007) [#15]
You will find using {codebox}{/codebox} (replace curly brackets with square) for your code, takes up much less room, and keeps the formatting.


MacSven(Posted 2007) [#16]
Is this better?


Brucey(Posted 2007) [#17]
Much.

Have a look at testlibusb.c in the tests dir of libusb.
It shows how to access all the information.

Specifically :
  usb_init();

  usb_find_busses();
  usb_find_devices();

  for (bus = usb_busses; bus; bus = bus->next) {
    if (bus->root_dev && !verbose)
      print_device(bus->root_dev, 0);
    else {
      struct usb_device *dev;

      for (dev = bus->devices; dev; dev = dev->next)
        print_device(dev, 0);
    }
  }

"usb_busses" is a static variable, which you won't be able to access directly from BlitzMax. You'll need to add some C/C++ wrapping. The struct that it points to is a linked-list of buses, each of which has information on the devices that are connected to it.

:o)


MacSven(Posted 2007) [#18]
Hi Brucey,

Can you make a little example?