|
245 | 245 | # USB info lists |
246 | 246 | mouse_interface_indexes = [] |
247 | 247 | mouse_endpoint_addresses = [] |
248 | | -kernel_driver_active_flags = [] |
| 248 | +detached_interfaces = [] |
249 | 249 | # USB device object instance list |
250 | 250 | mice = [] |
251 | 251 | # buffers list for mouse packet data |
|
255 | 255 | mouse_sync = [] |
256 | 256 |
|
257 | 257 | # 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 | +]: |
287 | 262 |
|
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: |
296 | 263 | for device in usb.core.find(find_all=True): |
297 | 264 | if device in mice: |
298 | 265 | print('found device twice') |
299 | 266 | continue |
300 | 267 | # check if current device is has a boot mouse endpoint |
301 | 268 | 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)) |
305 | 270 | if mouse_interface_index is not None and mouse_endpoint_address is not None: |
306 | 271 | if mouse_interface_index in mouse_interface_indexes and mouse_endpoint_address in mouse_endpoint_addresses: |
307 | 272 | print('found index/address twice') |
308 | 273 | 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 |
310 | 275 | # usb info lists |
311 | 276 | mouse_interface_indexes.append(mouse_interface_index) |
312 | 277 | mouse_endpoint_addresses.append(mouse_endpoint_address) |
313 | 278 |
|
314 | 279 | # add the mouse device instance to list |
315 | 280 | mice.append(device) |
316 | 281 | print( |
317 | | - f"mouse interface: {mouse_interface_index} " |
| 282 | + f"{default_sync} mouse interface: {mouse_interface_index} " |
318 | 283 | + f"endpoint_address: {hex(mouse_endpoint_address)}" |
319 | 284 | ) |
320 | | - mouse_sync.append(-1) |
| 285 | + mouse_sync.append(default_sync) |
321 | 286 |
|
322 | 287 | # 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) |
328 | 302 |
|
329 | 303 | # set the mouse configuration so it can be used |
330 | 304 | device.set_configuration() |
|
336 | 310 | if len(mice) >= 2: |
337 | 311 | break |
338 | 312 |
|
| 313 | + if len(mice) >= 2: |
| 314 | + break |
| 315 | + |
339 | 316 | def is_mouse1_left_clicked(): |
340 | 317 | """ |
341 | 318 | Check if mouse 1 left click is pressed |
@@ -429,14 +406,16 @@ def atexit_callback(): |
429 | 406 | :return: |
430 | 407 | """ |
431 | 408 | 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 | + |
440 | 419 | supervisor.runtime.autoreload = original_autoreload_val |
441 | 420 |
|
442 | 421 |
|
|
0 commit comments