Accessing a USB device from Perl
Paul Archer led an interesting presentation where the group built some Perl code to interface with a USB-based device using libusb. This project was based on a pair of Linux Journal Articleswritten by Greg Kroah-Hartman: (Writing a Simple USB Driver and Writing a Real Driver -- In User Space)
The second Linux Journal article showed how to use the libusb library from C to turn lights on in the device. The device itself is kind of interesting with LEDs in three colors and a buzzer. The device is programmable so it's allow more than just turning the lights on and off.
Paul had compiled the C code from the second article to verify that it worked. He wanted to either try to get the C code from the article callable from Perl, or write an adapter from libusb to Perl. He was also interested in using Inline::C to interface with the library, rather than go the XS route. Finally, he was hoping to tap the expertise of the group as a whole to make the development easier.
Over the course of the meeting, we were able to make a very simple
Inline::C adapter to the necessary libusb functions, call those
functions from Perl and to turn the lamp on and off with different colors.
While we worked on the project, there were many side topics that were
discussed, some relating to the project and some not. We ran into problems
passing pointers to structures back and forth across the
interface. We were able to patch around that using
We also ran into a few dumb programmer stunts from several people in the
audience. In general, most people seemed to be having a good time.
Here are two stages of the prototype code we worked on. The first is the original Perl wrapper around the C code. It doesn't do anything at this point, because we weren't really sure how much of the interface was exposed. The second file was the completed prototype with the ability to turn the colored LEDs on and off. This is definitiely not production-level code. But, since we were trying to learn how to make this work, prototype code is fine.
Hopefully, we will be able to get pictures of the device in action, soon.