Merge pull request #2812 from adafruit/trrs_factoryreset
Create TRRS_Trinkey_FactoryTest.ino
This commit is contained in:
commit
6c93e1018b
1 changed files with 131 additions and 0 deletions
|
|
@ -0,0 +1,131 @@
|
|||
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "SdFat.h"
|
||||
#include "Adafruit_TestBed.h"
|
||||
#include "Adafruit_TinyUSB.h"
|
||||
|
||||
// HID report descriptor using TinyUSB's template
|
||||
// Single Report (no ID) descriptor
|
||||
uint8_t const desc_hid_report[] = {
|
||||
TUD_HID_REPORT_DESC_KEYBOARD()
|
||||
};
|
||||
|
||||
Adafruit_USBD_HID usb_hid;
|
||||
|
||||
#define TIP_KEYCODE HID_KEY_SPACE
|
||||
#define RING_KEYCODE HID_KEY_ENTER
|
||||
|
||||
|
||||
extern Adafruit_TestBed TB;
|
||||
uint8_t allpins[] = {PIN_TIP, PIN_RING1, PIN_RING2, PIN_SLEEVE};
|
||||
|
||||
bool cableinserted = false;
|
||||
bool last_cablestate = false;
|
||||
uint32_t last_i2cscan = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
//while (!Serial) { yield(); delay(10); } // wait till serial port is opened
|
||||
|
||||
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
|
||||
usb_hid.setPollInterval(2);
|
||||
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
|
||||
usb_hid.setStringDescriptor("TinyUSB Keyboard");
|
||||
usb_hid.begin();
|
||||
|
||||
|
||||
TB.neopixelPin = PIN_NEOPIXEL;
|
||||
TB.neopixelNum = NUM_NEOPIXEL;
|
||||
TB.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(10); // sample every 10 ms
|
||||
|
||||
uint8_t keycode[6] = { 0 };
|
||||
uint8_t count = 0;
|
||||
// used to avoid send multiple consecutive zero report for keyboard
|
||||
static bool keyPressedPreviously = false;
|
||||
|
||||
pinMode(PIN_TIP, OUTPUT);
|
||||
digitalWrite(PIN_TIP, LOW);
|
||||
pinMode(PIN_TIP_SWITCH, INPUT_PULLUP);
|
||||
cableinserted = digitalRead(PIN_TIP_SWITCH);
|
||||
|
||||
if (!cableinserted) {
|
||||
TB.setColor(RED);
|
||||
}
|
||||
|
||||
if (cableinserted && !last_cablestate) {
|
||||
TB.setColor(GREEN);
|
||||
delay(250); // give em a quarter second to plug completely
|
||||
}
|
||||
|
||||
last_cablestate = cableinserted;
|
||||
|
||||
// Wake up host if we are in suspend mode
|
||||
if ( TinyUSBDevice.suspended() && count ) {
|
||||
TinyUSBDevice.remoteWakeup();
|
||||
}
|
||||
// skip if hid is not ready e.g still transferring previous report
|
||||
if ( !usb_hid.ready() ) return;
|
||||
|
||||
if (!cableinserted) {
|
||||
keyPressedPreviously = false;
|
||||
usb_hid.keyboardRelease(0);
|
||||
|
||||
// do an I2C scan while we're here, if we have pullups on SDA/SCL
|
||||
if ((millis() - last_i2cscan) > 1000) {
|
||||
TB.disableI2C();
|
||||
if (TB.testPullup(SDA) && TB.testPullup(SCL)) {
|
||||
Wire.begin();
|
||||
TB.printI2CBusScan();
|
||||
} else {
|
||||
Serial.println("No pullups on SDA/SCL");
|
||||
}
|
||||
last_i2cscan = millis();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// make two inputs
|
||||
pinMode(PIN_TIP, INPUT_PULLUP);
|
||||
pinMode(PIN_RING1, INPUT_PULLUP);
|
||||
|
||||
// make two 'ground' pins
|
||||
pinMode(PIN_SLEEVE, OUTPUT);
|
||||
digitalWrite(PIN_SLEEVE, LOW);
|
||||
pinMode(PIN_RING2, OUTPUT);
|
||||
digitalWrite(PIN_RING2, LOW);
|
||||
|
||||
delay(1);
|
||||
|
||||
if (!digitalRead(PIN_TIP)) {
|
||||
keycode[0] = TIP_KEYCODE;
|
||||
count++;
|
||||
}
|
||||
if (!digitalRead(PIN_RING1)) {
|
||||
keycode[1] = RING_KEYCODE;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count) { // Send report if there is key pressed
|
||||
uint8_t const report_id = 0;
|
||||
uint8_t const modifier = 0;
|
||||
|
||||
keyPressedPreviously = true;
|
||||
usb_hid.keyboardReport(report_id, modifier, keycode);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send All-zero report to indicate there is no keys pressed
|
||||
// Most of the time, it is, though we don't need to send zero report
|
||||
// every loop(), only a key is pressed in previous loop()
|
||||
if ( keyPressedPreviously )
|
||||
{
|
||||
keyPressedPreviously = false;
|
||||
usb_hid.keyboardRelease(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue