Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
103 lines (79 loc) · 2.96 KB
/
Copy pathcode.py
File metadata and controls
103 lines (79 loc) · 2.96 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
# Pins the 5 buttons are wired to (update to match your board's actual pins)
BUTTON_PINS = [
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP4,
]
# Key sent for each button (same order as BUTTON_PINS)
BUTTON_KEYS = [
Keycode.X,
Keycode.C,
Keycode.V,
Keycode.B,
Keycode.N,
]
# Seconds of inactivity before the workspace-switch combo is sent
INACTIVITY_TIMEOUT = 5 * 60
# Combo sent after the inactivity timeout to switch to the game workspace
SLIDESHOW_ACTIVATE_COMBO = (Keycode.CONTROL, Keycode.ONE)
# Combo sent on the first button press after workspace switch to focus the game
GAME_ACTIVATE_COMBO = (Keycode.CONTROL, Keycode.TWO)
DEBOUNCE_DELAY = 0.02 # seconds
# ---------------------------------------------------------------------------
keyboard = Keyboard(usb_hid.devices)
buttons = []
for pin in BUTTON_PINS:
btn = digitalio.DigitalInOut(pin)
btn.direction = digitalio.Direction.INPUT
btn.pull = digitalio.Pull.UP
buttons.append(btn)
prev_states = [True] * len(buttons)
debounce_timestamps = [0.0] * len(buttons)
# States:
# active – normal operation, game keys sent directly
# needs_activation – workspace was switched; next press sends activate combo first
STATE_ACTIVE = "active"
STATE_NEEDS_ACTIVATION = "needs_activation"
state = STATE_ACTIVE
last_activity = time.monotonic()
def send_combo(*keycodes):
keyboard.press(*keycodes)
time.sleep(0.05)
keyboard.release_all()
time.sleep(0.05)
while True:
now = time.monotonic()
if state == STATE_ACTIVE and (now - last_activity) >= INACTIVITY_TIMEOUT:
print("Inactivity timeout — sending slideshow activate combo")
send_combo(*SLIDESHOW_ACTIVATE_COMBO)
state = STATE_NEEDS_ACTIVATION
print("State -> needs_activation")
for i, btn in enumerate(buttons):
current = btn.value # True = not pressed (internal pull-up)
if current != prev_states[i] and (now - debounce_timestamps[i]) >= DEBOUNCE_DELAY:
debounce_timestamps[i] = now
prev_states[i] = current
if not current: # button pressed
if state == STATE_NEEDS_ACTIVATION:
print("Sending game activate combo")
send_combo(*GAME_ACTIVATE_COMBO)
state = STATE_ACTIVE
print("State -> active")
print(f"Button {i} pressed -> key {BUTTON_KEYS[i]:#04x}")
keyboard.press(BUTTON_KEYS[i])
last_activity = now
else: # button released
print(f"Button {i} released")
keyboard.release(BUTTON_KEYS[i])
time.sleep(0.005)