Writing Python drivers for input devices

[Stealth] put together a post explaining how he writes drivers for input peripherals. He’s using Python which makes the process fairly painless (we’ll get to that in a minute) but the value of his post is in the explanation surrounding how to interpret the data. Once you know how the communications are coming in from a device you can write the driver using any language you want. [Stealth] wrote in to let us know about this post after reading the PlayStation 3 Sixaxis controller sniffing hack. He’s pretty much doing the same thing but the background information is much more bountiful.

There are a couple of prerequisites to the process. First, [Stealth] is working in a Linux environment. That’s not to say you couldn’t do this on another OS, but you’re going to need to do some research to find out how to tap into the data stream from the device. Secondly, the input you are working with must already be set up and working on the machine. That means if there isn’t any support at all for the peripheral (in this case a USB joystick) you’re not going to be able to sniff the commands. That being said, a short Python snippet is all you need to dump the raw data coming in from the device. With data in hand it’s time to do some pattern hunting. As you start to figure out the size and scope of the incoming packets you can try out your own code to make sure you’ve got it right. Check out the demo video after the break which features a joystick button mapper written in Python.

Comments

  1. icebrain says:

    If you want to develop Python drivers for peripherals which are not support at all, try PyUSB: http://pyusb.sourceforge.net/docs/1.0/tutorial.html

  2. adam outler says:

    I wrote a driver for my bluetooth obd2 dongle in bash. You can use any language.

  3. dan says:

    I built one of those for a tv remote, but it looks like I used struct.unpack to unpack the buttons into integers rather than reading them as characters. Pretty cool, I’ve not seen very many other examples of Python drivers.

  4. Zogg says:

    This is not a “driver” as it operates in userland and nor in kernel space…

    • kandy4me says:

      In Windows at least, there are both user-mode and kernel-mode drivers — just because it doesn’t touch the kernel doesn’t mean it isn’t a driver! I’d imagine it would be the same for *nix systems too — there are aspects of a driver that do need the nitty-gritty OS to do the dirty work (Kernel), but then you just need to do some parsing, formatting, etc. that can be done anywhere (User space).

      Always good practice to separate the two, you don’t want to throw bugs into the kernel-side of things!

  5. Jorg says:

    This guy did something like this: http://www.engenhariadequintal.com/2011/03/usb-device-drivers-em-python-parte-1.html

    He`s trying to control some bogus usb-to-parallel port adaptor

    very nice!!

  6. Michael says:

    Anyone have a copy of the the article? Getting a 404 error when trying the link.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 93,770 other followers