Skip to content

Commit fea74c6

Browse files
committed
refactor get_mouse_deltas() to accept buffer and read_count instead of index
1 parent b18147f commit fea74c6

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

examples/usb_host_descriptors_two_boot_mice.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,22 @@
7878
BUTTONS = ["left", "right", "middle"]
7979

8080
mouse_bufs = []
81-
mouse_read_counts = [0, 0]
81+
8282
for mouse_tg in mouse_tgs:
8383
# Buffer to hold data read from the mouse
8484
# Boot mice have 4 byte reports
85-
mouse_bufs.append(array.array("b", [0] * 4))
85+
mouse_bufs.append(array.array("b", [0] * 8))
8686

8787

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]
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]
9595
else:
96-
raise ValueError(
97-
f"Unsupported mouse packet size: {mouse_packet_sizes[mouse_index]}, must be 4 or 8"
98-
)
96+
raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8")
9997
return delta_x, delta_y
10098

10199

@@ -105,10 +103,9 @@ def get_mouse_deltas(mouse_index):
105103
count = mouse.read(
106104
mouse_endpoint_addresses[mouse_index], mouse_bufs[mouse_index], timeout=10
107105
)
108-
mouse_read_counts[mouse_index] = count
109106
except usb.core.USBTimeoutError:
110107
continue
111-
mouse_deltas = get_mouse_deltas(mouse_index)
108+
mouse_deltas = get_mouse_deltas(mouse_bufs[mouse_index], count)
112109
mouse_tgs[mouse_index].x = max(
113110
0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_deltas[0])
114111
)

0 commit comments

Comments
 (0)