Skip to content

Commit f7159b6

Browse files
committed
AT switch demos
Adding AT switch demos for the TRRS Trinkey
1 parent 481778c commit f7159b6

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import usb_hid
9+
from adafruit_hid.keyboard import Keyboard
10+
from adafruit_hid.keycode import Keycode
11+
12+
# HID Keyboard setup
13+
keyboard = Keyboard(usb_hid.devices)
14+
15+
# Define pins for switches and grounds
16+
tip_switch = digitalio.DigitalInOut(board.TIP_SWITCH)
17+
tip_switch.direction = digitalio.Direction.INPUT
18+
tip_switch.pull = digitalio.Pull.UP
19+
20+
sleeve = digitalio.DigitalInOut(board.SLEEVE)
21+
sleeve.direction = digitalio.Direction.OUTPUT
22+
sleeve.value = False
23+
24+
ring_2 = digitalio.DigitalInOut(board.RING_2)
25+
# Set TIP and RING_1 initially as outputs to low for jack detection
26+
tip = digitalio.DigitalInOut(board.TIP)
27+
tip.direction = digitalio.Direction.OUTPUT
28+
tip.value = False
29+
30+
ring_1 = digitalio.DigitalInOut(board.RING_1)
31+
ring_1.direction = digitalio.Direction.OUTPUT
32+
ring_1.value = False
33+
34+
# Track the state of cable insertion
35+
last_cable_state = False
36+
37+
while True:
38+
# Drive TIP low and check TIP_SWITCH to detect if cable is inserted
39+
tip.direction = digitalio.Direction.OUTPUT
40+
tip.value = False
41+
time.sleep(0.001) # Wait a moment for the state to stabilize
42+
cable_inserted = tip_switch.value # Active low when cable is inserted
43+
44+
# Handle the detected state change for cable insertion
45+
if cable_inserted and not last_cable_state:
46+
print("inserted!")
47+
time.sleep(0.25) # Debounce and allow time for complete insertion
48+
49+
last_cable_state = cable_inserted
50+
51+
if cable_inserted:
52+
# Now configure TIP and RING_1 as inputs with pull-ups
53+
ring_2.direction = digitalio.Direction.OUTPUT
54+
ring_2.value = False
55+
tip.direction = digitalio.Direction.INPUT
56+
tip.pull = digitalio.Pull.UP
57+
sleeve.direction = digitalio.Direction.INPUT
58+
sleeve.pull = digitalio.Pull.UP
59+
60+
# Check the switches and send keycodes
61+
keycode = []
62+
if not tip.value:
63+
print("A")
64+
keycode.append(Keycode.A)
65+
if not sleeve.value:
66+
print("B")
67+
keycode.append(Keycode.B)
68+
69+
if keycode:
70+
keyboard.send(*keycode)
71+
else:
72+
keyboard.release_all()
73+
else:
74+
keyboard.release_all()
75+
76+
time.sleep(0.01) # Sample at 100 Hz
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)