Skip to content

Commit 694c9a9

Browse files
committed
adding tinypod code
Adding tinypod code. uses esp32-s3 feather for ble and ano seesaw encoder
1 parent 7b86eec commit 694c9a9

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

TinyPod/code.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import microcontroller
7+
import board
8+
from adafruit_hid.keyboard import Keyboard
9+
from adafruit_hid.keycode import Keycode
10+
import adafruit_ble
11+
from adafruit_ble.advertising import Advertisement
12+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
13+
from adafruit_ble.services.standard.hid import HIDService
14+
from adafruit_seesaw import seesaw, rotaryio, digitalio
15+
16+
# ios modifier
17+
mod = Keycode.CAPS_LOCK
18+
# keycodes
19+
KBD_CODES = [
20+
[Keycode.SPACE], # center
21+
[mod], # up
22+
[Keycode.LEFT_ARROW], # left
23+
[Keycode.DOWN_ARROW], # down
24+
[Keycode.RIGHT_ARROW], # right
25+
]
26+
27+
i2c = board.STEMMA_I2C()
28+
seesaw = seesaw.Seesaw(i2c, addr=0x49)
29+
30+
seesaw_product = (seesaw.get_version() >> 16) & 0xFFFF
31+
print(f"Found product {seesaw_product}")
32+
if seesaw_product != 5740:
33+
print("Wrong firmware loaded? Expected 5740")
34+
35+
buttons = []
36+
button_names = ["Select", "Up", "Left", "Down", "Right"]
37+
button_states = []
38+
39+
for s in range(1, 6):
40+
seesaw.pin_mode(s, seesaw.INPUT_PULLUP)
41+
pin = digitalio.DigitalIO(seesaw, s)
42+
pin_state = False
43+
buttons.append(pin)
44+
button_states.append(pin_state)
45+
46+
encoder = rotaryio.IncrementalEncoder(seesaw)
47+
last_position = 0
48+
49+
if not buttons[0].value and button_states[0] is False:
50+
button_states[0] = True
51+
try:
52+
import _bleio
53+
time.sleep(2)
54+
_bleio.adapter.erase_bonding()
55+
time.sleep(2)
56+
print("Last BLE bonding deleted, restarting..")
57+
time.sleep(2)
58+
microcontroller.reset()
59+
except Exception: # pylint: disable=broad-except
60+
pass
61+
62+
# BLE HID setup
63+
hid = HIDService()
64+
# keyboard & mouse HID setup
65+
kbd = Keyboard(hid.devices)
66+
67+
advertisement = ProvideServicesAdvertisement(hid)
68+
advertisement.appearance = 961
69+
scan_response = Advertisement()
70+
scan_response.complete_name = "CircuitPython HID"
71+
ble = adafruit_ble.BLERadio()
72+
73+
if not ble.connected:
74+
print("advertising")
75+
ble.start_advertising(advertisement, scan_response)
76+
else:
77+
print("already connected")
78+
print(ble.connections)
79+
time.sleep(2)
80+
81+
while True:
82+
# check for BLE connection
83+
while not ble.connected:
84+
pass
85+
# while BLE connected
86+
while ble.connected:
87+
position = encoder.position
88+
# if the encoder is turned to the right
89+
if position > last_position:
90+
kbd.send(Keycode.RIGHT_ARROW)
91+
# if the encoder is turned to the left
92+
if position < last_position:
93+
kbd.send(Keycode.LEFT_ARROW)
94+
# reset encoder position
95+
if position != last_position:
96+
last_position = position
97+
print(f"Position: {position}")
98+
for b in range(5):
99+
if not buttons[b].value and button_states[b] is False:
100+
button_states[b] = True
101+
if b != 0:
102+
kbd.press(*KBD_CODES[b])
103+
print(*KBD_CODES[b])
104+
else:
105+
kbd.press(mod)
106+
kbd.press(*KBD_CODES[b])
107+
print(f"{button_names[b]} button pressed")
108+
if buttons[b].value and button_states[b] is True:
109+
button_states[b] = False
110+
kbd.release_all()
111+
print(f"{button_names[b]} button released")
112+
# if BLE disconnects, begin advertising again
113+
ble.start_advertising(advertisement)

0 commit comments

Comments
 (0)