Skip to content

Commit b18147f

Browse files
committed
update dual mouse example to support 8 byte data packets
1 parent be9aa14 commit b18147f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

examples/usb_host_descriptors_two_boot_mice.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,42 @@
7878
BUTTONS = ["left", "right", "middle"]
7979

8080
mouse_bufs = []
81+
mouse_read_counts = [0, 0]
8182
for mouse_tg in mouse_tgs:
8283
# Buffer to hold data read from the mouse
8384
# Boot mice have 4 byte reports
8485
mouse_bufs.append(array.array("b", [0] * 4))
8586

8687

88+
def get_mouse_deltas(mouse_index):
89+
if mouse_read_counts[mouse_index] == 4:
90+
delta_x = mouse_bufs[mouse_index][1]
91+
delta_y = mouse_bufs[mouse_index][2]
92+
elif mouse_read_counts[mouse_index] == 8:
93+
delta_x = mouse_bufs[mouse_index][2]
94+
delta_y = mouse_bufs[mouse_index][4]
95+
else:
96+
raise ValueError(
97+
f"Unsupported mouse packet size: {mouse_packet_sizes[mouse_index]}, must be 4 or 8"
98+
)
99+
return delta_x, delta_y
100+
101+
87102
while True:
88103
for mouse_index, mouse in enumerate(mice):
89104
try:
90105
count = mouse.read(
91106
mouse_endpoint_addresses[mouse_index], mouse_bufs[mouse_index], timeout=10
92107
)
108+
mouse_read_counts[mouse_index] = count
93109
except usb.core.USBTimeoutError:
94110
continue
95-
111+
mouse_deltas = get_mouse_deltas(mouse_index)
96112
mouse_tgs[mouse_index].x = max(
97-
0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_bufs[mouse_index][1])
113+
0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_deltas[0])
98114
)
99115
mouse_tgs[mouse_index].y = max(
100-
0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_bufs[mouse_index][2])
116+
0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_deltas[1])
101117
)
102118

103119
out_str = f"{mouse_tgs[mouse_index].x},{mouse_tgs[mouse_index].y}"

0 commit comments

Comments
 (0)