Skip to content

Commit 95181eb

Browse files
committed
update for multi interface detach
1 parent 5273772 commit 95181eb

File tree

1 file changed

+36
-57
lines changed
  • Metro/Metro_RP2350_Match3/match3_game

1 file changed

+36
-57
lines changed

Metro/Metro_RP2350_Match3/match3_game/code.py

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
# USB info lists
246246
mouse_interface_indexes = []
247247
mouse_endpoint_addresses = []
248-
kernel_driver_active_flags = []
248+
detached_interfaces = []
249249
# USB device object instance list
250250
mice = []
251251
# buffers list for mouse packet data
@@ -255,76 +255,50 @@
255255
mouse_sync = []
256256

257257
# scan for connected USB devices
258-
for device in usb.core.find(find_all=True):
259-
# check if current device is has a boot mouse endpoint
260-
try:
261-
mouse_interface_index, mouse_endpoint_address = (
262-
adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
263-
)
264-
if mouse_interface_index is not None and mouse_endpoint_address is not None:
265-
# if it does have a boot mouse endpoint then add information to the
266-
# usb info lists
267-
mouse_interface_indexes.append(mouse_interface_index)
268-
mouse_endpoint_addresses.append(mouse_endpoint_address)
269-
270-
# add the mouse device instance to list
271-
mice.append(device)
272-
print(
273-
f"mouse interface: {mouse_interface_index} "
274-
+ f"endpoint_address: {hex(mouse_endpoint_address)}"
275-
)
276-
mouse_sync.append(0)
277-
278-
# detach kernel driver if needed
279-
kernel_driver_active_flags.append(
280-
device.is_kernel_driver_active(mouse_interface_index)
281-
)
282-
if device.is_kernel_driver_active(mouse_interface_index):
283-
device.detach_kernel_driver(mouse_interface_index)
284-
285-
# set the mouse configuration so it can be used
286-
device.set_configuration()
258+
for find_endpoint, default_sync in [
259+
(adafruit_usb_host_descriptors.find_boot_mouse_endpoint, 0),
260+
(adafruit_usb_host_descriptors.find_report_mouse_endpoint, -1)
261+
]:
287262

288-
except usb.core.USBError as e:
289-
# The mouse might have glitched and may not be detected but at least we don't crash
290-
print(e)
291-
292-
if len(mice) >= 2:
293-
break
294-
295-
if len(mice) < 2:
296263
for device in usb.core.find(find_all=True):
297264
if device in mice:
298265
print('found device twice')
299266
continue
300267
# check if current device is has a boot mouse endpoint
301268
try:
302-
mouse_interface_index, mouse_endpoint_address = (
303-
adafruit_usb_host_descriptors.find_report_mouse_endpoint(device)
304-
)
269+
mouse_interface_index, mouse_endpoint_address = (find_endpoint(device))
305270
if mouse_interface_index is not None and mouse_endpoint_address is not None:
306271
if mouse_interface_index in mouse_interface_indexes and mouse_endpoint_address in mouse_endpoint_addresses:
307272
print('found index/address twice')
308273
continue
309-
# if it does have a boot mouse endpoint then add information to the
274+
# if it does have a mouse endpoint then add information to the
310275
# usb info lists
311276
mouse_interface_indexes.append(mouse_interface_index)
312277
mouse_endpoint_addresses.append(mouse_endpoint_address)
313278

314279
# add the mouse device instance to list
315280
mice.append(device)
316281
print(
317-
f"mouse interface: {mouse_interface_index} "
282+
f"{default_sync} mouse interface: {mouse_interface_index} "
318283
+ f"endpoint_address: {hex(mouse_endpoint_address)}"
319284
)
320-
mouse_sync.append(-1)
285+
mouse_sync.append(default_sync)
321286

322287
# detach kernel driver if needed
323-
kernel_driver_active_flags.append(
324-
device.is_kernel_driver_active(mouse_interface_index)
325-
)
326-
if device.is_kernel_driver_active(mouse_interface_index):
327-
device.detach_kernel_driver(mouse_interface_index)
288+
detached = []
289+
290+
# Typically HID devices have interfaces 0,1,2
291+
# Trying 0..mouse_iface is safe and sufficient
292+
for intf in range(mouse_interface_index + 1):
293+
try:
294+
if device.is_kernel_driver_active(intf):
295+
device.detach_kernel_driver(intf)
296+
detached.append(intf)
297+
print(f"Detached kernel driver from interface {intf}")
298+
except usb.core.USBError as e:
299+
print(e)
300+
301+
detached_interfaces.append(detached)
328302

329303
# set the mouse configuration so it can be used
330304
device.set_configuration()
@@ -336,6 +310,9 @@
336310
if len(mice) >= 2:
337311
break
338312

313+
if len(mice) >= 2:
314+
break
315+
339316
def is_mouse1_left_clicked():
340317
"""
341318
Check if mouse 1 left click is pressed
@@ -429,14 +406,16 @@ def atexit_callback():
429406
:return:
430407
"""
431408
for _i, _mouse in enumerate(mice):
432-
if kernel_driver_active_flags[_i]:
433-
if not _mouse.is_kernel_driver_active(mouse_interface_indexes[_i]):
434-
_mouse.attach_kernel_driver(mouse_interface_indexes[_i])
435-
print(f'#{_i} Index: {mouse_interface_indexes[_i]} (reattaching)')
436-
else:
437-
print(f'#{_i} Index: {mouse_interface_indexes[_i]} (Not Attaching)')
438-
else:
439-
print(f'#{_i} Index: {mouse_interface_indexes[_i]} kernel not active (was not attached)')
409+
detached_from_device = detached_interfaces[_i]
410+
411+
if detached_from_device:
412+
for intf in detached_from_device:
413+
if not _mouse.is_kernel_driver_active(intf):
414+
_mouse.attach_kernel_driver(intf)
415+
print(f'#{_i} Index: {intf} (reattaching)')
416+
else:
417+
print(f'#{_i} Index: {intf} (Not Attaching)')
418+
440419
supervisor.runtime.autoreload = original_autoreload_val
441420

442421

0 commit comments

Comments
 (0)