Monday, June 17, 2013

Joystick to JSON/HTTP in Processing

This weekend I had the urge to read the state of the game paddle connected to my PC and offer it as a JSON web service. This is but a part of my larger goal to do the same with my LEGO Power Functions through Arduino, then couple all of the aforementioned together; ie: Use a joystick to control LEGO.
I used a Java library called FF4J which in turn is based on the SDL library. I tried to make my Processing sketch as simple as possible while interfacing with the aforementioned libraries.

JSON Output mapping

You are able to define a mapping that describes the output format of your JSON web service. It would look something like this, depending on your game controller:
{
 x: 'rX',
 y: 'Y',
 z: 'Z',
 dial: 'X',
 pov: 'POV1',
 fire: 'B1',
 b2: 'B2',
 b3: 'B3',
 b4: 'B4',
 b5: 'B5',
 b6: 'B6',
 b7: 'B7',
 b8: 'B8',
 b9: 'B9',
 b10: 'B10',
 b11: 'B11',
 b12: 'B12'
}
This JSON is defined in [mapping.json]. This allows you to choose custom names for the buttons, axis, and POVs available to you. You can find the code name of the keys by running the app.

Installation

  • Download and install the SDL Library.
  • Add the SDL DLLs to processing/java/bin/ folder.
  • (Plug in your joystick)
  • Make sure your Joystick is available to SDL ~ Test with dxdiag.exe
  • Open JoystickService in Processing.

The Result

Once it's running you can view the state of your joystick in the application window. If all is well then open up the service in a browser (localhost:28080 by default). You should see something like this:

Download

You can download my processing app folder from JoystickService on GitHub.