Skip to content

Instantly share code, notes, and snippets.

@halferty
Created November 6, 2017 04:37
Show Gist options
  • Save halferty/729b68d6e2cd08fc3479fede2590caca to your computer and use it in GitHub Desktop.
Save halferty/729b68d6e2cd08fc3479fede2590caca to your computer and use it in GitHub Desktop.
Arduino DUE Gamepad
#include "HID.h"
static const uint8_t _hidReportDescriptor[] PROGMEM = {
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, 0x10, // USAGE_MAXIMUM (Button 16)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x10, // REPORT_COUNT (16)
0x81, 0x02, // INPUT (Data, Var, Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x32, // USAGE (Z)
0x09, 0x33, // USAGE (Rx)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x04, // REPORT_COUNT (4)
0x81, 0x02, // INPUT (Data, Var, Abs)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
};
typedef struct {
uint16_t buttons;
int8_t leftXaxis;
int8_t leftYaxis;
int8_t rightXaxis;
int8_t rightYaxis;
} PadReport;
class Gamepad {
private:
PadReport padReport;
public:
Gamepad(void) {
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID().AppendDescriptor(&node);
padReport.buttons = 0;
}
void begin(void) {}
void end(void) {}
void sendUpdate(void) { HID().SendReport(0x03, &padReport, sizeof(PadReport)); }
void setButtonState(uint8_t button, bool state) {
(state == true) ? bitSet(padReport.buttons, button) : bitClear(padReport.buttons, button);
sendUpdate();
}
};
Gamepad gamepad;
void setup() {
pinMode(51, INPUT);
}
void loop() {
gamepad.setButtonState(0, !digitalRead(51));
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment