Step 12: Joystick and buttons

Picture of Joystick and buttons
Screen Shot 2013-08-25 at 8.10.16 AM.jpg
The GPIO cobbler board allows the controls to be hooked up to the raspberry pi directly. Without going into too much detail because I'm no expert, the on/off signals from the controls are seen by the raspberry pi and with a programming script, the signals are mapped to specific keyboard functions so the controls act as a keyboard emulator. Adafruit have an excellent guide here http://learn.adafruit.com/retro-gaming-with-raspberry-pi/buttons.

It was a lot of trial and error but in the end I used this script which I modified to suit my button layout https://github.com/adafruit/Adafruit-Retrogame/blob/master/retrogame.c.

Here is my script change at line 87

//   Input    Output (from /usr/include/linux/input.h)
{ 22,      KEY_LEFT     },
{  2,      KEY_RIGHT    },
{  4,      KEY_UP       },
{  3,      KEY_DOWN     },
{ 27,      KEY_C        },
{ 23,      KEY_X        },
{ 11,      KEY_Z        },
{ 24,      KEY_A        },
{ 10,      KEY_S        },
{  9,      KEY_D        },
{  7,      KEY_5        },
{  8,      KEY_1        },
{ 25,      KEY_ESC      }

Each line in this table contains two elements. The first is a GPIO pin number (where a button or one direction from a joystick is attached), the second is the corresponding key code to be generated by this control. A list of valid key code names can be found in the file /usr/include/linux/input.h starting around line 178. Remember to enclose each pin/key pair in {curly braces} with a comma between them. Basically the controls can be programmed to do the job of any key on a keyboard.