Leonardo/Micro as HID Joystick: Remove keyboard & mouse?

There are many examples of code that allow a Leonardo/Micro become a HID joystick device, but in all the cases I’ve seen, the default HID keyboard and mouse are still part of the recognized devices in the PC device manager.

It would be cleaner if the KB/mouse could be removed from the HID implementation.

Obviously, the “traditional” method of removing them with the global CDC/HID definitions won’t work since HID still needs to be present!

The example HID.cpp appears to have statements that disable the KB/mouse, but the declarations don’t appear to do anything! Maybe they just make the KB/mouse “inert”, but completely removing them would be much better!

Any help in removing them would be greatly appreciated!

FYI, I’ve added a few files that work in 1.6.5 (nice work, by RamjetX!)

For bonus points, I think I can sus it out in the HID.cpp with a lot of trial & error (more errors than trial!), but what I’m really after is a 6-button, one-axis (throttle) emulation to be hacked into an ancient Thrustmaster WCS throttle. The HID declarations are a little arcane, and a bit of direction there would also be appreciated!

Thanks! :slight_smile:

HID.cpp (18.1 KB)

USBAPI.h (7.27 KB)

leoJoy10bit_RamjetX.ino (6.58 KB)

Platform.h (402 Bytes)

To answer my own question, here’s the scoop:

Basically, regardless what you do with the joystick routines in the modified HID.cpp, the Leonardo will always appear as a game controller and a USB keyboard and mouse, even if the keyboard and mouse are basically not there! This is caused by the Windows “drivers” for Leonardo/Micro being explicitly told to expect a keyboard and mouse.

Generic HID devices do not need any external drivers, so the misapplication of the Leonardo drivers can be fixed by using a generic (open-source) USB VID and PID in the BOARDS.TXT file. In the case of a generic HID device, the VID and PID are essentially just placeholders and could be just about anything, but the open-source ones are a good bet to be truly compliant.

If you do this alone, you will still be faced with an “unknown device” for the Leonardo bootloader and CDC serial port, but those can be eliminated in step 1 below:

  1. arduino/hardware/arduino/cores/arduino/USBDesc.h is where you remove the CDC serial define to get rid of the serial port, but be warned that that also gets rid of the ability to program over USB! (use a super-cheap USBasp to program via the Arduino IDE using the ISP header*)

  2. Update the BOARDS.TXT file Leonardo entry to a unique VID and PID, and re-name it if you like.

  3. For bonus points you can edit the HID.cpp file to remove the game controller features you don’t want in the function: void Joystick_::setState(JoyState_t *joySt) Be sure to correctly change the number of bytes the function is expected to return (#define joyBytes).

I modified the Joystick_::setState to be eight buttons and a throttle and rudder only.

*Be aware that the Arduino “Upload via Programmer” option is a bit odd. Although it does work, it’s a kludgy work-around that still has the AVR starting in a bootloader “mode”, but with no bootloader present, and the code counter wraps around to the start of the “non-bootloader” code.

To be really efficient you can revise the AVR fuses to remove the bootloader reset vector, and revise to BOARDS.TXT maximum sketch size to the full capacity of the AVR.