-
Notifications
You must be signed in to change notification settings - Fork 72
/
robust_keyboard.py
39 lines (32 loc) · 1.27 KB
/
robust_keyboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# robust_keyboard_code.py -- demonstrate how to deal with USB disconnect on startup and while running
# 27 Jul 2022 - @todbot / Tod Kurt
import supervisor
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
def usb_connected(): return supervisor.runtime.usb_connected
if usb_connected(): # only attempt keyboard creation if USB
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
# make up some buttons. This is for a FunHouse, but could be any keys
buttons = []
for pin in (board.BUTTON_UP, board.BUTTON_SELECT, board.BUTTON_DOWN):
switch = digitalio.DigitalInOut(pin)
switch.pull = digitalio.Pull.DOWN # defaults to input
buttons.append(switch)
# what keycodes to send for each button
keys = [Keycode.A, Keycode.B, Keycode.C]
while True:
print("usb:", usb_connected(), "buttons:", [b.value for b in buttons] )
for i in range(len(buttons)):
if buttons[i].value:
if usb_connected():
try:
keyboard.send( keys[i] )
except OSError: # also if USB disconnected
pass
time.sleep(0.1)