Building a PC Game Controller with LEDs (Arduino Micro)

I have a collection of buttons with built-in LEDs, that are perfect for building something to control the autopilot in a Flight Simulator, say a 737 NG MCP-like device.

I have:

  • [tested, works] An Arduino Micro (two actually, but we'll see how much we can control with only one), so USB HID profiles should be possible.
  • [tested, works] On the PC side, I found a library called "hidapi", which is able to enumerate HID devices and will recognize the Micro as soon as I have some kind of Joystick library on it.
  • [tested, works] I have some additional hardware ready for the actual build, and the Micro can deal fine with buttons and LEDs. (Duh)

But now the big one: I am a fairly competent developer, Java, C++, C, and a little bit of C#, but how to enable the Arduino to receive data from the PC so I can control the LEDs is running into problems. The people involved with writing the USB standard are definitely not people with a background in client-server or OS-level networking, because the terminology they use comes nowhere near to what I'm used to. Messages are suddenly called "reports" or "events". The IN and OUT channels are not named with usage in mind, they are host oriented, with the host being the PC. Well, strictly speaking, the host is the device with the host-plug, which is most often our good old USB-A plug. So if I want to report a button press, that will become an event on the IN channel.

The "hidapi" library is perfectly capable of controlling LEDs in an HID, and that this is not that unusual is evidenced by the most common one: NumLock en CAPSLock LEDs on a keyboard. My impression is that there is no direct reason why I could not support 4 bytes LED state or more, as long as the initial reports on capabilities correctly tell the PC what size messages are allowed. That said, the USB specs are close to unreadable due to the loss in translation.

I found a library called "ArduinoJoystickLibrary" that allows you to customize some things like the number of buttons, HAT switches, and axes, but has very little documentation explaining why certain numeric values have the value they have. Trying to find how to adjust this for adding LED reports is for the moment beyond me. Another library is "HID", which actually supports a TeensyKeyboard that has LEDs but does not explain how this is achieved. So basically neither help me further.

Who has the knowledge I miss and can help me with (at the least) pointers or (preferably) example code? Oh yes, and I know I can ignore the USB-HIDness of the Micro and go back a few generations to communicate through a COM port, but that feels like cheating because A HID device should be perfectly able to do this.

Cheers,
Bert Laverman

When using the Arduino joystick on a normal PC you should not need an API (hidapi) as the operating system should find and handle HID devices all on its own.

When using something like the Arduino Leonardo it enumerates as a HID device and as a serial device at the same time so you could have the Leonardo check serial for LED control commands sent from your PC program while also sending HID (keyboard/mouse/joystick) data to the PC.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.