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