Monthly Archives: January 2014

The Teesny series of USB development boards are fantastic for making your own gaming controllers. If you don’t want to get your hands dirty then Generic HID is a great GUI tool for the Teensy++ 2.0. If you have a Teensy 3, then this guide by Kenton Hamaluik is a good starting point. If you want complete control over your Teensy++ 2.0 joystick then read on…

First you will need the Arduino and Teesnyduino software installed. I would also suggest you make a backup of the arduino/teensy folder just in case. Next I would seriously recommend a decent text editor, especially if you intend on doing lots of editing, while you can obviously do it in notepad, things like custom syntax highlighting make life much easier. I have personally been using Editpad Pro for many years and wouldn’t be without it now. Lastly if you are running Windows Vista upwards, you either need to turn off UAC (not recommended), run your editor as administrator or work on the files outside of the program files directory and then copy them back after.

Ok lets get stuck in…

Add A New USB Type To The Tools Menu

We are going to add a new USB type for our new joystick and we will call it Gamepad. First open up the ‘boards.txt‘ file found in a ‘arduino install folder\hardware\teensy‘. We want to add the new USB type for the Teensy++ 2.0. (This should work for the Teensy++ 1.0 and the Teensy 2.0, just change accordingly) Look for the ‘teensypp2.menu.usb‘ entries. After the last one (teensypp2.menu.usb.flightsim.fake_serial=teensy_gateway) add the following lines.

The first line is setting the text that the menu item is going to show. The second line is setting a preprocessor directive, which is basically telling the software to use our USB code for this device. The last line is used for creating a serial port emulator. After saving the file, if you open the Arduino software, you should have Gamepad Controller listed under the Tools, USB Type: menu. (I have included the ‘boards.txt‘ in the download file for those lazy people out there)

Adding The New Includes

If you look under ‘arduino install folder\hardware\teensy\cores‘, you will see there are several folders, in fact one for each USB type and a global teensy folder. (Ignore the teensy3 folder as the code is completely different) We need to create a folder for our new USB type here. Lets make a folder called ‘usb_gamepad‘.

Now you may be wondering how does the compiler know what files to use? When we compile our sketch, it looks in the global teensy folder and reads the relevant USB files, it then checks the preprocessor directive to see what USB type is defined, and then includes the files defined by that directive.

So in the ‘arduino install folder\hardware\teensy\cores\teensy’ folder we need to edit the following files.

‘core_id.h’, ‘usb.c’, ‘usb_api.cpp’, ‘usb_api.h’ & ‘usb_private.h’

For each file we need to add a couple of lines, after the last #include to tell it where our files are, so ‘core_id.h‘ becomes…

There is an excellent guide here on the descriptors, but the key thing is to make sure the data is byte aligned. Our gamepad has 11 x 1 bit buttons and 4 x 10 bit axis, this is a total of 51 bits. 51 is not evenly divisible by 8 so we need to pad it out to 56 bits. The following part is the 5 byte padding.

The order in the hid descriptor is also very important, as this is the order the data must be laid out in the packet sent from the controller to the host. You shouldn’t have to edit anything else in this file unless you decide to change the variable names.

usb_api.h

This is the heart of the gamepad class, and is where we configure the controller data to be sent over the USB. Earlier I said the order was important, the following picture shows how the host is expecting the gamepad data to be formatted.

Gamepad USB Packet

This is the order from right to left that the buttons and axis are defined in the gamepad hid descriptor. You can see the first 11 bits are the buttons, followed by 5 bits of padding, 10 bits for the X axis, 10 bits for the Y axis, 10 bits for the RX axis and 10 bits for the RY axis.

core_id.h

I don’t think this file is even needed, as I couldn’t find out where other core defines were referenced. In fact I took it out completely and the gamepad worked fine, but I have included it here just for completeness.

The Gamepad Sketch

Here’s the actual Arduino sketch code. This is the part you will need to edit to suit your controller. You need to adjust the button and axis pins to match you physical connections to the Teensy++ 2.0. I am using pins 0 to 5 and 7 to 11 for my buttons. Pin 6 is the LED pin, which is actually flashing at 100 times per second in the code. You can’t see the individual flashes but at least you know the code is running all the time the LED is on. I am then using A0 to A3 as the analog input for the axis. I am using a MMA7361 Triple Axis Accelerometer for 2 of my axis, so you can ignore that part of the code if you want. I have included it in case anyone else wants do do something similar.

A recent project required me to print a lot of info to the immediate window via the Debug.Print command. A quick search came up with a few suggestions for clearing the output in my program but nothing that worked for me. They did point me in the right direction and I came up with the following code that seems to work in Microsoft Visual Basic 2010 Express.

The code itself is simple, find the VB window, focus it, and then send some keystrokes. The key thing is the name of the window, this must be an exact match, so you will need to adjust it for other versions of VB. It also relies on the keyboard shortcut for the immediate window, which in VB Express 2010 is Ctrl+Alt+I.