Last Saturday, Socialbomb held its first Hack Day.
I had two goals for Hack Day: (1) get a PS/2 keyboard talking to an Arduino and (2) make something interesting with processing.py. Here’s the end result (make sure to click through to the full-screen version for maximum legibility):
It’s the Unexpected Animal Stories Keyboard, a keyboard which intermittently replaces whatever you’re typing with an Unexpected Animal Story.
It turns out that the part of this project that I thought would be difficult turned out to be easy: getting a PS/2 keyboard talking to an Arduino was a piece of cake. I already had a bunch of mini-din connectors; I just soldered one up to a breadboard, hooked it up to my trusty Arduino Diecimila, put the excellent ps2keypolled library in my libraries folder, plugged in the keyboard and voila: keystrokes gettin’ read.
I’ve got big plans for the PS/2-to-Arduino data chain, involving a data logging chip and shoes made of keyboards and sledgehammers and/or yogurt. But for Hack Day, I just wanted to whip up something fun. So the next step was to get the keystrokes from the Arduino to my computer, preferably into a processing.py sketch. Much to my surprise, Processing’s serial communication libraries worked with processing.py without a hitch*, which left me free to write the tiny little generative text toy that you see in the video above.
The biggest unforeseen timesink: I spent a few hours trying to figure out the best way to send ps2keypolled’s 16-bit key codes from the Arduino to the computer, eventually settling on the stupidest possible ad-hoc protocol that could work (and porting a big chunk of C code to Python to translate the key codes to ASCII). See the source code for more details.
Most surprising happy discovery: processing.py is amazing. Being able to quickly write the text-munging code in Python while still retaining Processing’s built-in functions and easy-to-use libraries is just… a revelation. For a project that’s just a few weeks old, it feels surprisingly polished. If you’ve got Python and Processing expertise, I recommend you give it a go.
Source code for the whole shebang: crazy_animal_keyboard_source.zip
* Okay, there was a single hitch. Apparently, the serial communication library included with Processing (and, therefore, processing.py) doesn’t support 64-bit Snow Leopard (as documented e.g. here). I was able to get around this without problems by using the -d32 parameter to the java runner, i.e.
$ java -d32 -jar processing.py animal_keyboard.py