Leonardo HID Gamepad

I’m trying to implement an HID gamepad on the Leonardo. I’ve already added the following to the device descriptor in HID.cpp (4 buttons, 2 axes):

    //	Gamepad
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x05,                    // USAGE (Game Pad)
    0xa1, 0x01,                    // COLLECTION (Application)
    0xa1, 0x00,                    //   COLLECTION (Physical)
    0x85, 0x03,                    //     REPORT_ID (3)
    0x05, 0x09,                    //     USAGE_PAGE (Button)
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    0x95, 0x04,                    //     REPORT_COUNT (4)
    0x75, 0x01,                    //     REPORT_SIZE (1)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
    0x09, 0x30,                    //     USAGE (X)
    0x09, 0x31,                    //     USAGE (Y)
    0x15, 0xff,                    //     LOGICAL_MINIMUM (-1)
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    0x95, 0x02,                    //     REPORT_COUNT (2)
    0x75, 0x02,                    //     REPORT_SIZE (2)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    0xc0,                          //   END_COLLECTION
    0xc0                           // END_COLLECTION

I’ve also added the following class declaration to USBAPI.h:

class Gamepad_
{
private:
	uint8_t _buttons;
public:
	Gamepad_(void);
	void begin(void);
	void end(void);
	void press(uint8_t b);
	void release(uint8_t b);
	bool isPressed(uint8_t b);
};
extern Gamepad_ Gamepad;

However, when I try to call any of the Gamepad functions (Gamepad.begin(), Gamepad.press(), etc.), it throws an undefined reference to Gamepad. So apparently, I’m missing something. Can anybody with knowledge of the Arduino HID implementation help point me in the right direction? If I comment out any calls to the Gamepad class, I can compile it and upload the sketch, and it shows up as a gamepad in Windows with 2 axes and 4 buttons, so the device descriptor is good. I’m just missing a reference to my newly defined class. Any ideas?

LeonardoGamepad.png

HID.cpp (15.4 KB)

USBAPI.h (5.64 KB)

Nevermind, I missed the singleton instantiations at the top of HID.cpp. IT WORKS :smiley: :smiley: :smiley:

If anyone's interested (as I haven't found anyone else that's implemented the HID gamepad profile on the Arduino yet... I looked for one before trying to do it myself), I have a write-up on my blog: Forty-two: Arduino HID Gamepad, part 1