|
| 1 | +// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include "SdFat.h" |
| 6 | +#include "Adafruit_TestBed.h" |
| 7 | +#include "Adafruit_TinyUSB.h" |
| 8 | + |
| 9 | +// HID report descriptor using TinyUSB's template |
| 10 | +// Single Report (no ID) descriptor |
| 11 | +uint8_t const desc_hid_report[] = { |
| 12 | + TUD_HID_REPORT_DESC_KEYBOARD() |
| 13 | +}; |
| 14 | + |
| 15 | +Adafruit_USBD_HID usb_hid; |
| 16 | + |
| 17 | +#define TIP_KEYCODE HID_KEY_SPACE |
| 18 | +#define RING_KEYCODE HID_KEY_ENTER |
| 19 | + |
| 20 | + |
| 21 | +extern Adafruit_TestBed TB; |
| 22 | +uint8_t allpins[] = {PIN_TIP, PIN_RING1, PIN_RING2, PIN_SLEEVE}; |
| 23 | + |
| 24 | +bool cableinserted = false; |
| 25 | +bool last_cablestate = false; |
| 26 | +uint32_t last_i2cscan = 0; |
| 27 | + |
| 28 | +void setup() { |
| 29 | + Serial.begin(115200); |
| 30 | + //while (!Serial) { yield(); delay(10); } // wait till serial port is opened |
| 31 | + |
| 32 | + usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD); |
| 33 | + usb_hid.setPollInterval(2); |
| 34 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 35 | + usb_hid.setStringDescriptor("TinyUSB Keyboard"); |
| 36 | + usb_hid.begin(); |
| 37 | + |
| 38 | + |
| 39 | + TB.neopixelPin = PIN_NEOPIXEL; |
| 40 | + TB.neopixelNum = NUM_NEOPIXEL; |
| 41 | + TB.begin(); |
| 42 | +} |
| 43 | + |
| 44 | +void loop() { |
| 45 | + delay(10); // sample every 10 ms |
| 46 | + |
| 47 | + uint8_t keycode[6] = { 0 }; |
| 48 | + uint8_t count = 0; |
| 49 | + // used to avoid send multiple consecutive zero report for keyboard |
| 50 | + static bool keyPressedPreviously = false; |
| 51 | + |
| 52 | + pinMode(PIN_TIP, OUTPUT); |
| 53 | + digitalWrite(PIN_TIP, LOW); |
| 54 | + pinMode(PIN_TIP_SWITCH, INPUT_PULLUP); |
| 55 | + cableinserted = digitalRead(PIN_TIP_SWITCH); |
| 56 | + |
| 57 | + if (!cableinserted) { |
| 58 | + TB.setColor(RED); |
| 59 | + } |
| 60 | + |
| 61 | + if (cableinserted && !last_cablestate) { |
| 62 | + TB.setColor(GREEN); |
| 63 | + delay(250); // give em a quarter second to plug completely |
| 64 | + } |
| 65 | + |
| 66 | + last_cablestate = cableinserted; |
| 67 | + |
| 68 | + // Wake up host if we are in suspend mode |
| 69 | + if ( TinyUSBDevice.suspended() && count ) { |
| 70 | + TinyUSBDevice.remoteWakeup(); |
| 71 | + } |
| 72 | + // skip if hid is not ready e.g still transferring previous report |
| 73 | + if ( !usb_hid.ready() ) return; |
| 74 | + |
| 75 | + if (!cableinserted) { |
| 76 | + keyPressedPreviously = false; |
| 77 | + usb_hid.keyboardRelease(0); |
| 78 | + |
| 79 | + // do an I2C scan while we're here, if we have pullups on SDA/SCL |
| 80 | + if ((millis() - last_i2cscan) > 1000) { |
| 81 | + TB.disableI2C(); |
| 82 | + if (TB.testPullup(SDA) && TB.testPullup(SCL)) { |
| 83 | + Wire.begin(); |
| 84 | + TB.printI2CBusScan(); |
| 85 | + } else { |
| 86 | + Serial.println("No pullups on SDA/SCL"); |
| 87 | + } |
| 88 | + last_i2cscan = millis(); |
| 89 | + } |
| 90 | + return; |
| 91 | + } |
| 92 | + // make two inputs |
| 93 | + pinMode(PIN_TIP, INPUT_PULLUP); |
| 94 | + pinMode(PIN_RING1, INPUT_PULLUP); |
| 95 | + |
| 96 | + // make two 'ground' pins |
| 97 | + pinMode(PIN_SLEEVE, OUTPUT); |
| 98 | + digitalWrite(PIN_SLEEVE, LOW); |
| 99 | + pinMode(PIN_RING2, OUTPUT); |
| 100 | + digitalWrite(PIN_RING2, LOW); |
| 101 | + |
| 102 | + delay(1); |
| 103 | + |
| 104 | + if (!digitalRead(PIN_TIP)) { |
| 105 | + keycode[0] = TIP_KEYCODE; |
| 106 | + count++; |
| 107 | + } |
| 108 | + if (!digitalRead(PIN_RING1)) { |
| 109 | + keycode[1] = RING_KEYCODE; |
| 110 | + count++; |
| 111 | + } |
| 112 | + |
| 113 | + if (count) { // Send report if there is key pressed |
| 114 | + uint8_t const report_id = 0; |
| 115 | + uint8_t const modifier = 0; |
| 116 | + |
| 117 | + keyPressedPreviously = true; |
| 118 | + usb_hid.keyboardReport(report_id, modifier, keycode); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + // Send All-zero report to indicate there is no keys pressed |
| 123 | + // Most of the time, it is, though we don't need to send zero report |
| 124 | + // every loop(), only a key is pressed in previous loop() |
| 125 | + if ( keyPressedPreviously ) |
| 126 | + { |
| 127 | + keyPressedPreviously = false; |
| 128 | + usb_hid.keyboardRelease(0); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments