DanceCtl.ino 2.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020 Martin Natano <[email protected]>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
Martin Natano's avatar
Martin Natano committed
16 17 18 19 20 21 22 23
#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
24 25

    // Buttons
Martin Natano's avatar
Martin Natano committed
26 27 28 29 30 31 32 33 34
    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
35
    // Padding
Martin Natano's avatar
Martin Natano committed
36 37 38
    0x75, 0x01,   //    REPORT_SIZE (1)
    0x95, 0x02,   //    REPORT_COUNT (2)
    0x01, 0x01,   //    INPUT (Constant)
Martin Natano's avatar
Martin Natano committed
39

Martin Natano's avatar
Martin Natano committed
40 41 42 43
    0xc0,         //  END_COLLECTION
    0xc0,         // END_COLLECTION
};

Martin Natano's avatar
Martin Natano committed
44
#define DEBOUNCE_DELAY 2
Martin Natano's avatar
Martin Natano committed
45
#define NBUTTONS 6
Martin Natano's avatar
Martin Natano committed
46
static const int buttonPins[NBUTTONS] = {7, 8, 9, 10, 11, 12};
Martin Natano's avatar
Martin Natano committed
47 48 49 50 51 52

void setup() {
    int i;
    
    pinMode(LED_BUILTIN, OUTPUT);
    for (i = 0; i < NBUTTONS; i++)
Martin Natano's avatar
Martin Natano committed
53
        pinMode(buttonPins[i], INPUT_PULLUP);
Martin Natano's avatar
Martin Natano committed
54 55 56 57 58 59

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

void loop() {
Martin Natano's avatar
Martin Natano committed
60 61 62 63 64
    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
65

Martin Natano's avatar
Martin Natano committed
66 67
    now = millis();
    sendReport = 0;
Martin Natano's avatar
Martin Natano committed
68 69

    for (i = 0; i < NBUTTONS; i++) {
Martin Natano's avatar
Martin Natano committed
70 71 72 73
        int pressed = !digitalRead(buttonPins[i]);
        if (pressed != bitRead(buttonStates, i)) {
            bitWrite(buttonStates, i, pressed);
            lastButtonChange[i] = now;
Martin Natano's avatar
Martin Natano committed
74 75
        }

Martin Natano's avatar
Martin Natano committed
76 77 78
        if (pressed != bitRead(reportData, i) && lastButtonChange[i] + DEBOUNCE_DELAY < now) {
            bitWrite(reportData, i, pressed);
            sendReport = 1;
Martin Natano's avatar
Martin Natano committed
79 80 81
        }
    }

Martin Natano's avatar
Martin Natano committed
82
    if (sendReport) {
Martin Natano's avatar
Martin Natano committed
83
        HID().SendReport(0x03, &reportData, sizeof(reportData));
Martin Natano's avatar
Martin Natano committed
84
        digitalWrite(LED_BUILTIN, reportData ? HIGH : LOW);
Martin Natano's avatar
Martin Natano committed
85 86
    }
}