DanceCtl.ino 2 KB
Newer Older
Martin Natano's avatar
Martin Natano committed
1 2 3 4 5 6 7 8
#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)
Martin Natano's avatar
Martin Natano committed
9 10

    // Buttons
Martin Natano's avatar
Martin Natano committed
11 12 13 14 15 16 17 18 19
    0x05, 0x09,   //    USAGE_PAGE (Button)
    0x19, 0x01,   //    USAGE_MINIMUM (Button 1)
    0x29, 0x06,   //    USAGE_MAXIMUM (Button 6)
    0x15, 0x00,   //    LOGICAL_MINIMUM (0)
    0x25, 0x01,   //    LOGICAL_MAXIMUM (1)
    0x75, 0x01,   //    REPORT_SIZE (1)
    0x95, 0x06,   //    REPORT_COUNT (6)
    0x81, 0x02,   //    INPUT (Data, Var, Abs)

Martin Natano's avatar
Martin Natano committed
20
    // Padding
Martin Natano's avatar
Martin Natano committed
21 22 23
    0x75, 0x01,   //    REPORT_SIZE (1)
    0x95, 0x02,   //    REPORT_COUNT (2)
    0x01, 0x01,   //    INPUT (Constant)
Martin Natano's avatar
Martin Natano committed
24

Martin Natano's avatar
Martin Natano committed
25 26 27 28
    0xc0,         //  END_COLLECTION
    0xc0,         // END_COLLECTION
};

Martin Natano's avatar
Martin Natano committed
29
#define DEBOUNCE_DELAY 2
Martin Natano's avatar
Martin Natano committed
30
#define NBUTTONS 6
Martin Natano's avatar
Martin Natano committed
31
static const int buttonPins[NBUTTONS] = {7, 8, 9, 10, 11, 12};
Martin Natano's avatar
Martin Natano committed
32 33 34 35 36 37

void setup() {
    int i;
    
    pinMode(LED_BUILTIN, OUTPUT);
    for (i = 0; i < NBUTTONS; i++)
Martin Natano's avatar
Martin Natano committed
38
        pinMode(buttonPins[i], INPUT_PULLUP);
Martin Natano's avatar
Martin Natano committed
39 40 41 42 43 44

    static HIDSubDescriptor node(hidReportDescriptor, sizeof(hidReportDescriptor));
    HID().AppendDescriptor(&node);
}

void loop() {
Martin Natano's avatar
Martin Natano committed
45 46 47 48 49
    static uint8_t reportData;
    static uint8_t buttonStates;
    static unsigned long lastButtonChange[NBUTTONS];
    unsigned long now;
    int sendReport, i;
Martin Natano's avatar
Martin Natano committed
50

Martin Natano's avatar
Martin Natano committed
51 52
    now = millis();
    sendReport = 0;
Martin Natano's avatar
Martin Natano committed
53 54

    for (i = 0; i < NBUTTONS; i++) {
Martin Natano's avatar
Martin Natano committed
55 56 57 58
        int pressed = !digitalRead(buttonPins[i]);
        if (pressed != bitRead(buttonStates, i)) {
            bitWrite(buttonStates, i, pressed);
            lastButtonChange[i] = now;
Martin Natano's avatar
Martin Natano committed
59 60
        }

Martin Natano's avatar
Martin Natano committed
61 62 63
        if (pressed != bitRead(reportData, i) && lastButtonChange[i] + DEBOUNCE_DELAY < now) {
            bitWrite(reportData, i, pressed);
            sendReport = 1;
Martin Natano's avatar
Martin Natano committed
64 65 66
        }
    }

Martin Natano's avatar
Martin Natano committed
67
    if (sendReport) {
Martin Natano's avatar
Martin Natano committed
68
        HID().SendReport(0x03, &reportData, sizeof(reportData));
Martin Natano's avatar
Martin Natano committed
69
        digitalWrite(LED_BUILTIN, reportData ? HIGH : LOW);
Martin Natano's avatar
Martin Natano committed
70 71
    }
}