DanceCtl.ino 2.34 KB
Newer Older
Martin Natano's avatar
Martin Natano committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#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, 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)

    0x75, 0x01,   //    REPORT_SIZE (1)
    0x95, 0x02,   //    REPORT_COUNT (2)
    0x01, 0x01,   //    INPUT (Constant)
    0xc0,         //  END_COLLECTION
    0xc0,         // END_COLLECTION
};

#define NBUTTONS 6
static const int button_pins[NBUTTONS] = {7, 8, 9, 10, 11, 12};

void setup() {
    int i;
    
    pinMode(LED_BUILTIN, OUTPUT);
    for (i = 0; i < NBUTTONS; i++)
        pinMode(button_pins[i], INPUT_PULLUP);

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

// the loop function runs over and over again forever
void loop() {
    uint8_t reportData;
Martin Natano's avatar
Martin Natano committed
42
    uint8_t debounceBuffer[15];
Martin Natano's avatar
Martin Natano committed
43
    static uint8_t previousReportData;
Martin Natano's avatar
Martin Natano committed
44
    int i, j;
Martin Natano's avatar
Martin Natano committed
45 46 47 48 49 50 51 52 53

    reportData = 0;

    for (i = 0; i < NBUTTONS; i++) {
        int pressed = !digitalRead(button_pins[i]);
        if (pressed)
            reportData |= (1 << i);
    }

Martin Natano's avatar
Martin Natano committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    for (i = 14; i > 0; i--) {
        debounceBuffer[i] = debounceBuffer[i-1];
    }
    debounceBuffer[0] = reportData;

    reportData = previousReportData;
    for (i = 0; i < NBUTTONS; i++) {
        int n = 0;
        for (j = 0; j < 15; j++) {
            if (debounceBuffer[j] & (1 << i))
                n ++;
        }

        if (reportData & (1 << i)) {
            if (n == 0)
                reportData &= ~(1 << i);
        } else {
            if (n == 15)
                reportData |= (1 << i);
        }
    }

Martin Natano's avatar
Martin Natano committed
76 77 78 79 80 81 82 83 84 85 86
    if (reportData != previousReportData) {
        HID().SendReport(0x03, &reportData, sizeof(reportData));

        if (reportData)
            digitalWrite(LED_BUILTIN, HIGH);
        else
            digitalWrite(LED_BUILTIN, LOW);

        previousReportData = reportData;
    }

Martin Natano's avatar
Martin Natano committed
87
    delayMicroseconds(100);
Martin Natano's avatar
Martin Natano committed
88
}