Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
222 lines (194 loc) · 7.4 KB
/
Copy pathcode.py
File metadata and controls
222 lines (194 loc) · 7.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import time
import board
import usb_hid
import busio
import json
import os
import supervisor
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
# UART connection to control computer (via USB-to-serial adapter).
# timeout=0 makes uart.read() return whatever has arrived instead of
# blocking up to 1 s (the default) waiting for a full 32-byte read, and the
# larger buffer holds a few queued commands without overflowing.
uart = busio.UART(board.GP0, board.GP1, baudrate=9600,
timeout=0, receiver_buffer_size=512)
buffer = bytearray()
# Reported by the "status" command so the control computer can tell when the
# target device may have changed: boot_id changes when the Pico restarts, and
# host_count goes up each time a USB host (phone, computer) connects.
boot_id = os.urandom(4).hex()
host_count = 0
host_connected = False
def poll_host():
global host_count, host_connected
connected = supervisor.runtime.usb_connected
if connected and not host_connected:
host_count += 1
host_connected = connected
def read_uart_line():
global buffer
while True:
poll_host()
data = uart.read(32)
if data:
buffer.extend(data)
if b'\n' in buffer:
line, _, remainder = buffer.partition(b'\n')
buffer = bytearray(remainder)
# Line noise (e.g. wires moved while powered) can arrive
# as invalid UTF-8; treat it as a bad command, not a crash.
try:
return line.decode("utf-8").strip()
except UnicodeError:
return ""
time.sleep(0.01)
def try_parse_json(line):
try:
return json.loads(line)
except Exception as e:
print("JSON parse error:", e)
return None
keyboard = keyboard_layout = mouse = None
def init_hid():
# Keyboard()/Mouse() raise OSError until the host has enumerated the
# Pico, e.g. while an Android phone is asleep or locked. Return False
# instead of crashing so the UART loop keeps running.
# With no host at all, Keyboard()/Mouse() wait for one forever by
# default, which would stop the bridge answering, so check first and
# pass a timeout.
global keyboard, keyboard_layout, mouse
if not supervisor.runtime.usb_connected:
mouse = None
return False
try:
keyboard = Keyboard(usb_hid.devices, timeout=1)
keyboard_layout = KeyboardLayoutUS(keyboard)
mouse = Mouse(usb_hid.devices, timeout=1)
return True
except OSError as e:
print("HID not ready:", e)
mouse = None
return False
# Wait for the host to accept the HID devices, but not forever: commands
# retry init_hid() themselves.
for _ in range(10):
time.sleep(1)
if init_hid():
break
print("HIDAgent ready")
def handle(command_json):
type_param = command_json.get("type", "")
x_param = command_json.get("x", 0)
y_param = command_json.get("y", 0)
text_param = command_json.get("text", "")
def move_in_chunks(dx, dy, chunk=10, delay=0.01):
# Move the mouse by (dx, dy) in small steps to avoid iOS
# pointer acceleration kicking in on large single moves.
rem_x, rem_y = dx, dy
while rem_x != 0 or rem_y != 0:
step_x = max(-chunk, min(chunk, rem_x))
step_y = max(-chunk, min(chunk, rem_y))
mouse.move(x=step_x, y=step_y)
rem_x -= step_x
rem_y -= step_y
time.sleep(delay)
if type_param == "click":
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
time.sleep(0.2)
mouse.click(Mouse.LEFT_BUTTON)
elif type_param == "move":
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
elif type_param == "nudge":
# Relative move from the current position, no corner reset. Small,
# slow steps keep iOS pointer acceleration out of it, so this is for
# short visual-feedback corrections rather than long moves. "step"
# (units per report) and "delay" trade speed for linearity.
move_in_chunks(x_param, y_param, chunk=command_json.get("step", 1),
delay=command_json.get("delay", 0.02))
elif type_param == "nudgeclick":
move_in_chunks(x_param, y_param, chunk=command_json.get("step", 1),
delay=command_json.get("delay", 0.02))
time.sleep(0.2)
mouse.click(Mouse.LEFT_BUTTON)
elif type_param == "type":
keyboard_layout.write(text_param)
elif type_param == "key":
# Handle key names like "enter", "tab", "space"
key_map = {
"enter": Keycode.ENTER,
"return": Keycode.ENTER,
"tab": Keycode.TAB,
"escape": Keycode.ESCAPE,
"space": Keycode.SPACEBAR,
"delete": Keycode.DELETE,
"backspace": Keycode.BACKSPACE,
"up": Keycode.UP_ARROW,
"down": Keycode.DOWN_ARROW,
"left": Keycode.LEFT_ARROW,
"right": Keycode.RIGHT_ARROW,
}
key_code = key_map.get(text_param.lower(), None)
if key_code:
keyboard.press(key_code)
keyboard.release_all()
elif type_param == "run":
# use command keyboard shortcut to run an app, works on windows/mac/ios/android
keyboard.press(Keycode.LEFT_GUI, Keycode.SPACEBAR)
keyboard.release_all()
time.sleep(1)
keyboard_layout.write(text_param)
keyboard.press(Keycode.ENTER)
keyboard.release_all()
elif type_param == "downarrow":
keyboard.press(Keycode.DOWN_ARROW)
keyboard.release_all()
elif type_param == "dragdown" or type_param == "dragup":
step = 25 if type_param == "dragdown" else -25
mouse.move(x=-1000, y=-1000)
time.sleep(0.1)
move_in_chunks(x_param, y_param)
time.sleep(0.1)
mouse.press(Mouse.LEFT_BUTTON)
for _ in range(5):
time.sleep(0.05)
mouse.move(y=step)
mouse.release(Mouse.LEFT_BUTTON)
while True:
line = read_uart_line()
print("Received:", line)
command_json = try_parse_json(line)
if command_json is None:
uart.write("Error\n".encode("utf-8"))
continue
# Doesn't need HID, so it answers even with no host attached.
if isinstance(command_json, dict) and command_json.get("type") == "status":
poll_host()
uart.write("Status {} {} {}\n".format(
boot_id, host_count, int(host_connected)).encode("utf-8"))
continue
# A sleeping or unplugged host makes HID sends raise OSError; reply
# Error and set the devices up again on the next command.
if mouse is None and not init_hid():
uart.write("Error\n".encode("utf-8"))
continue
try:
handle(command_json)
except OSError as e:
print("HID error:", e)
mouse = None
uart.write("Error\n".encode("utf-8"))
continue
except Exception as e:
# A malformed command (e.g. text the layout can't type) must not
# stop the bridge; it would go silent until power-cycled.
print("Command error:", e)
uart.write("Error\n".encode("utf-8"))
continue
uart.write("Success\n".encode("utf-8"))