Skip to content

Commit ca51f2d

Browse files
authored
Merge pull request #5 from FoamyGuy/8byte_mouse_data
update dual mouse example to support 8 byte data packets
2 parents be9aa14 + fea74c6 commit ca51f2d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

examples/usb_host_descriptors_two_boot_mice.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,23 @@
7878
BUTTONS = ["left", "right", "middle"]
7979

8080
mouse_bufs = []
81+
8182
for mouse_tg in mouse_tgs:
8283
# Buffer to hold data read from the mouse
8384
# Boot mice have 4 byte reports
84-
mouse_bufs.append(array.array("b", [0] * 4))
85+
mouse_bufs.append(array.array("b", [0] * 8))
86+
87+
88+
def get_mouse_deltas(buffer, read_count):
89+
if read_count == 4:
90+
delta_x = buffer[1]
91+
delta_y = buffer[2]
92+
elif read_count == 8:
93+
delta_x = buffer[2]
94+
delta_y = buffer[4]
95+
else:
96+
raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8")
97+
return delta_x, delta_y
8598

8699

87100
while True:
@@ -92,12 +105,12 @@
92105
)
93106
except usb.core.USBTimeoutError:
94107
continue
95-
108+
mouse_deltas = get_mouse_deltas(mouse_bufs[mouse_index], count)
96109
mouse_tgs[mouse_index].x = max(
97-
0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_bufs[mouse_index][1])
110+
0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_deltas[0])
98111
)
99112
mouse_tgs[mouse_index].y = max(
100-
0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_bufs[mouse_index][2])
113+
0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_deltas[1])
101114
)
102115

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

0 commit comments

Comments
 (0)