As I’ve been hustling away at the blimp project, one of the most daunting challenges that I’m going to face is to deal with creating a user interface for the client application associated with the blimp. But then, out of nowhere, I had an epiphary. Using existing techmology, if I could offload the majority of a user interface to an easily accessible device like an XBox Controller, then the remaining tasks to place on the UI are minimal. Moreover, building and testing the blimp would suddenly become cool if I could do a sufficient amount of testing using a game controller (flying quadrocopter in the office THIS WEEK!).
I subsequently starting googling pretty hard. I found that there were off the shelf drivers for Windows, but I didn’t want to be caught on the streets writing a program for Windows. Also I don’t have a Windows computer. Most of what I found pointed me toward PyGame for Mac OS X, but not only did it seem like a pain to install to begin with, but I was unable to install it after a decent amount of effort.
Then I found SDL, but it didn’t have any Python bindings. But only yesterday, I’d written about how to call C++ functions and methods from Python. After downloading SDL, I was up and running in a matter of minutes.
I’ve posted the sample code to a GitHub repository. Simply install SDL and Boost, adjust the Makefile to your particular environment, and then you can run the sample python program.
One of the slightly interesting aspects about this small project was to deal with a function that returned two different items. In C++ you’d normally accomplish this by passing variables by reference, but you can’t count on that when you’re calling those functions from Python. Instead, I wrote a small helper function that returns a Python tuple from C++. See below:
To walk through the rest of the code, the basic C++ functionality is like so:
Keep in mind there are also some functions to initialize SDL that I’m not posting here, but they are in the sample code on Github. The code above can then be ported over to Python in two steps that I like. 1.) Create a manager class that manages the objects that are specific to SDL so that we don’t have to create custom PyObjects for them and 2.) Create python bindings using boost. The manager class looks something like this:
Finally, we need to expose that class to Python using boost. After you’ve done this once, it’s extremely easy and straightforward:
Now, in python, the usage is incredibly simple: