-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
130 lines (111 loc) · 3.77 KB
/
Copy pathcode.py
File metadata and controls
130 lines (111 loc) · 3.77 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
import time
import board
import usb_hid
import busio
import json
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)
uart = busio.UART(board.GP0, board.GP1, baudrate=9600)
buffer = bytearray()
def read_uart_line():
global buffer
while True:
data = uart.read(32)
if data:
buffer.extend(data)
if b'\n' in buffer:
line, _, remainder = buffer.partition(b'\n')
buffer = bytearray(remainder)
return line.decode("utf-8").strip()
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
# Initialize HID devices
time.sleep(1)
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
mouse = Mouse(usb_hid.devices)
time.sleep(1)
print("HIDAgent ready")
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
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 == "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":
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)
uart.write("Success\n".encode("utf-8"))