Adventures in xboxdrv

Okay, this post is going to get a bit geeky (well, more than normal). Now that I've got the Pi up and running I want to try and get it working with a wireless Xbox 360 controller so that I can use that to control the visuals. I don't have a problem with a good old fashioned keyboard, but game controllers are designed to take a bit of a beating and are way more comfortable than being hunched over a keyboard.



Now, because we're working in Linux we should be able to get it to behave exactly how we want... but with a fair bit of work!

The Raspberry Pi already has drivers for the Xbox 360 pad, however, it will send the button presses as actual controller button presses. That is fine if you're playing a game that has been coded to accept those inputs, but it's not very useful if you want to control something that it designed to use keyboard controls. What we want is a way to convert those button presses into keyboard commands.
To do this, we're going to use a package called xboxdrv which has a really long and quite intimidating manual page (it's intimidating to me at least). This one calls for a massive cup of tea...

The first thing to note is that xboxdrv is pretty much run and configured in the terminal. If you're planning on playing around with this too then I'd recommend brushing up on the unix command line a bit before you dive in. A good place to start would be Codecademy's course which will show you the ropes.

To install xboxdrv on the Pi (assuming you're using Raspbian) is to plug in the following in terminal:
sudo apt-get update
sudo apt-get install xboxdrv  
That should install the xboxdrv package which will let you run the thing by simply typing this into the terminal:
xboxdrv
When it came to running the thing it, of course, didn't work first time. An error message was thrown up complaining about something. It turns out that xboxdrv wasn't able to connect to the controller because something else in the OS has already claimed it. It seems that Raspbian already includes some basic drivers for the Xbox controller. So we need to disable this. This can be done with:
rmmod xpad
That will (temporarily) remove the module xpad, which is the preinstalled driver. Another error you might encounter with running xboxdrv is a message complaining about not being able to access the libusb_open() library. This is to do with file permissions. You can fix this by elevating your rights using sudo.

After a bit of faffing around xboxdrv starts to run, and it produces an output showing all of the inputs you're getting. The poorly shot video below shows the raw data output the xboxdrv gives when you run it:

video


The output itself is actually quite cool to look at. The video showed me pulling the triggers and the analogue sticks randomly as well as pressing a few buttons.

This is probably a good place to wrap up for the day. I've managed to get the wireless 360 controller to connect to the Pi and xboxdrv is receiving all of the inputs. Good stuff! Next up is diving into the manual a bit further and figuring out how to actually translate these button presses into keyboard presses so that we can start using it to control some software. But I've run out of tea, so that will have to wait for another day ;-)