AT switch demos

Adding AT switch demos for the TRRS Trinkey
This commit is contained in:
Liz 2024-05-28 11:06:14 -04:00
parent 481778c4ee
commit f7159b6c23
2 changed files with 183 additions and 0 deletions

View file

@ -0,0 +1,76 @@
# SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# HID Keyboard setup
keyboard = Keyboard(usb_hid.devices)
# Define pins for switches and grounds
tip_switch = digitalio.DigitalInOut(board.TIP_SWITCH)
tip_switch.direction = digitalio.Direction.INPUT
tip_switch.pull = digitalio.Pull.UP
sleeve = digitalio.DigitalInOut(board.SLEEVE)
sleeve.direction = digitalio.Direction.OUTPUT
sleeve.value = False
ring_2 = digitalio.DigitalInOut(board.RING_2)
# Set TIP and RING_1 initially as outputs to low for jack detection
tip = digitalio.DigitalInOut(board.TIP)
tip.direction = digitalio.Direction.OUTPUT
tip.value = False
ring_1 = digitalio.DigitalInOut(board.RING_1)
ring_1.direction = digitalio.Direction.OUTPUT
ring_1.value = False
# Track the state of cable insertion
last_cable_state = False
while True:
# Drive TIP low and check TIP_SWITCH to detect if cable is inserted
tip.direction = digitalio.Direction.OUTPUT
tip.value = False
time.sleep(0.001) # Wait a moment for the state to stabilize
cable_inserted = tip_switch.value # Active low when cable is inserted
# Handle the detected state change for cable insertion
if cable_inserted and not last_cable_state:
print("inserted!")
time.sleep(0.25) # Debounce and allow time for complete insertion
last_cable_state = cable_inserted
if cable_inserted:
# Now configure TIP and RING_1 as inputs with pull-ups
ring_2.direction = digitalio.Direction.OUTPUT
ring_2.value = False
tip.direction = digitalio.Direction.INPUT
tip.pull = digitalio.Pull.UP
sleeve.direction = digitalio.Direction.INPUT
sleeve.pull = digitalio.Pull.UP
# Check the switches and send keycodes
keycode = []
if not tip.value:
print("A")
keycode.append(Keycode.A)
if not sleeve.value:
print("B")
keycode.append(Keycode.B)
if keycode:
keyboard.send(*keycode)
else:
keyboard.release_all()
else:
keyboard.release_all()
time.sleep(0.01) # Sample at 100 Hz

View file

@ -0,0 +1,107 @@
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#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_A
#define RING_KEYCODE HID_KEY_B
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("TRRS Trinkey Keyboard");
usb_hid.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 && !last_cablestate) {
Serial.println("inserted!");
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);
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);
}
}
}