|
| 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 | + |
| 25 | +bool selftest = false; |
| 26 | +bool cableinserted = false; |
| 27 | +bool last_cablestate = false; |
| 28 | +uint32_t last_i2cscan = 0; |
| 29 | + |
| 30 | +void setup() { |
| 31 | + Serial.begin(115200); |
| 32 | + //while (!Serial) { yield(); delay(10); } // wait till serial port is opened |
| 33 | + |
| 34 | + usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD); |
| 35 | + usb_hid.setPollInterval(2); |
| 36 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 37 | + usb_hid.setStringDescriptor("TinyUSB Keyboard"); |
| 38 | + usb_hid.begin(); |
| 39 | + |
| 40 | + |
| 41 | + TB.neopixelPin = PIN_NEOPIXEL; |
| 42 | + TB.neopixelNum = NUM_NEOPIXEL; |
| 43 | + TB.begin(); |
| 44 | + |
| 45 | + for (int d=0; d < 100; d++) { |
| 46 | + if (Serial.available() && (Serial.read() == 0xAF)) { |
| 47 | + selftest = true; |
| 48 | + } |
| 49 | + delay(1); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + delay(10); // sample every 10 ms |
| 55 | + |
| 56 | + if (Serial.available() && (Serial.read() == 0xAF)) { |
| 57 | + selftest = true; |
| 58 | + } |
| 59 | + |
| 60 | + if (selftest) { |
| 61 | + Serial.println("TRRS Trinkey self-tester!"); |
| 62 | + |
| 63 | + // check tied pins |
| 64 | + if (! TB.testpins(PIN_RING1, PIN_RING2, allpins, sizeof(allpins))) return; |
| 65 | + if (! TB.testpins(PIN_SLEEVE, PIN_TIP, allpins, sizeof(allpins))) return; |
| 66 | + |
| 67 | + pinMode(PIN_RING1, OUTPUT); |
| 68 | + digitalWrite(PIN_RING1, LOW); |
| 69 | + pinMode(PIN_RING1_SWITCH, INPUT_PULLUP); |
| 70 | + delay(10); |
| 71 | + if (!digitalRead(PIN_RING1_SWITCH)) { |
| 72 | + Serial.println("Ring1 switch not floating"); |
| 73 | + return; |
| 74 | + } |
| 75 | + pinMode(PIN_TIP, OUTPUT); |
| 76 | + digitalWrite(PIN_TIP, LOW); |
| 77 | + pinMode(PIN_TIP_SWITCH, INPUT_PULLUP); |
| 78 | + delay(10); |
| 79 | + if (!digitalRead(PIN_TIP_SWITCH)) { |
| 80 | + Serial.println("Tip switch not floating"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + Serial.println("**TEST OK!**"); |
| 85 | + |
| 86 | + delay(100); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + uint8_t keycode[6] = { 0 }; |
| 91 | + uint8_t count = 0; |
| 92 | + // used to avoid send multiple consecutive zero report for keyboard |
| 93 | + static bool keyPressedPreviously = false; |
| 94 | + |
| 95 | + pinMode(PIN_TIP, OUTPUT); |
| 96 | + digitalWrite(PIN_TIP, LOW); |
| 97 | + pinMode(PIN_TIP_SWITCH, INPUT_PULLUP); |
| 98 | + cableinserted = digitalRead(PIN_TIP_SWITCH); |
| 99 | + |
| 100 | + if (!cableinserted) { |
| 101 | + TB.setColor(RED); |
| 102 | + } |
| 103 | + |
| 104 | + if (cableinserted && !last_cablestate) { |
| 105 | + TB.setColor(GREEN); |
| 106 | + delay(250); // give em a quarter second to plug completely |
| 107 | + } |
| 108 | + |
| 109 | + last_cablestate = cableinserted; |
| 110 | + |
| 111 | + // Wake up host if we are in suspend mode |
| 112 | + if ( TinyUSBDevice.suspended() && count ) { |
| 113 | + TinyUSBDevice.remoteWakeup(); |
| 114 | + } |
| 115 | + // skip if hid is not ready e.g still transferring previous report |
| 116 | + if ( !usb_hid.ready() ) return; |
| 117 | + |
| 118 | + if (!cableinserted) { |
| 119 | + keyPressedPreviously = false; |
| 120 | + usb_hid.keyboardRelease(0); |
| 121 | + |
| 122 | + // do an I2C scan while we're here, if we have pullups on SDA/SCL |
| 123 | + if ((millis() - last_i2cscan) > 1000) { |
| 124 | + TB.disableI2C(); |
| 125 | + if (TB.testPullup(SDA) && TB.testPullup(SCL)) { |
| 126 | + Wire.begin(); |
| 127 | + TB.printI2CBusScan(); |
| 128 | + } else { |
| 129 | + Serial.println("No pullups on SDA/SCL"); |
| 130 | + } |
| 131 | + last_i2cscan = millis(); |
| 132 | + } |
| 133 | + return; |
| 134 | + } |
| 135 | + // make two inputs |
| 136 | + pinMode(PIN_TIP, INPUT_PULLUP); |
| 137 | + pinMode(PIN_RING1, INPUT_PULLUP); |
| 138 | + |
| 139 | + // make two 'ground' pins |
| 140 | + pinMode(PIN_SLEEVE, OUTPUT); |
| 141 | + digitalWrite(PIN_SLEEVE, LOW); |
| 142 | + pinMode(PIN_RING2, OUTPUT); |
| 143 | + digitalWrite(PIN_RING2, LOW); |
| 144 | + |
| 145 | + delay(1); |
| 146 | + |
| 147 | + if (!digitalRead(PIN_TIP)) { |
| 148 | + keycode[0] = TIP_KEYCODE; |
| 149 | + count++; |
| 150 | + } |
| 151 | + if (!digitalRead(PIN_RING1)) { |
| 152 | + keycode[1] = RING_KEYCODE; |
| 153 | + count++; |
| 154 | + } |
| 155 | + |
| 156 | + if (count) { // Send report if there is key pressed |
| 157 | + uint8_t const report_id = 0; |
| 158 | + uint8_t const modifier = 0; |
| 159 | + |
| 160 | + keyPressedPreviously = true; |
| 161 | + usb_hid.keyboardReport(report_id, modifier, keycode); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + // Send All-zero report to indicate there is no keys pressed |
| 166 | + // Most of the time, it is, though we don't need to send zero report |
| 167 | + // every loop(), only a key is pressed in previous loop() |
| 168 | + if ( keyPressedPreviously ) |
| 169 | + { |
| 170 | + keyPressedPreviously = false; |
| 171 | + usb_hid.keyboardRelease(0); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments