Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions adafruit_usb_host_mouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
mouse_was_attached = None
if mouse_device is not None:
# detach the kernel driver if needed
if mouse_device.is_kernel_driver_active(0):
if mouse_device.is_kernel_driver_active(mouse_interface_index):
mouse_was_attached = True
mouse_device.detach_kernel_driver(0)
mouse_device.detach_kernel_driver(mouse_interface_index)
else:
mouse_was_attached = False

Expand All @@ -117,7 +117,13 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
else:
mouse_tg = None

return BootMouse(mouse_device, mouse_endpoint_address, mouse_was_attached, mouse_tg)
return BootMouse(
mouse_device,
mouse_interface_index,
mouse_endpoint_address,
mouse_was_attached,
tilegrid=mouse_tg,
)

# if no mouse found
return None
Expand Down Expand Up @@ -171,14 +177,14 @@ def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
except usb.core.USBError as e:
print_exception(e, e, None)

mouse_was_attached = None
mouse_was_attached = False
if mouse_device is not None:
# detach the kernel driver if needed
if mouse_device.is_kernel_driver_active(0):
mouse_was_attached = True
mouse_device.detach_kernel_driver(0)
else:
mouse_was_attached = False
# Typically HID devices have interfaces 0,1,2
for intf in range(3):
if mouse_device.is_kernel_driver_active(intf):
mouse_was_attached = True
mouse_device.detach_kernel_driver(intf)

# set configuration on the mouse so we can use it
mouse_device.set_configuration()
Expand All @@ -196,7 +202,13 @@ def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
else:
mouse_tg = None

return ReportMouse(mouse_device, mouse_endpoint_address, mouse_was_attached, mouse_tg)
return ReportMouse(
mouse_device,
mouse_interface_index,
mouse_endpoint_address,
mouse_was_attached,
tilegrid=mouse_tg,
)

# if no mouse found
return None
Expand All @@ -209,19 +221,23 @@ class BootMouse:
were pressed.

:param device: The usb device instance for the mouse
:param interface_index: The USB interface index of the mouse
:param endpoint_address: The address of the mouse endpoint
:param was_attached: Whether the usb device was attached to the kernel
:param tilegrid: The TileGrid that holds the visible mouse cursor
:param scale: The scale of the group that the Mouse TileGrid will be put into.
Needed in order to properly clamp the mouse to the display bounds
"""

def __init__(self, device, endpoint_address, was_attached, tilegrid=None, scale=1): # noqa: PLR0913, too many args
def __init__( # noqa: PLR0913, too many args
self, device, interface_index, endpoint_address, was_attached, *, tilegrid=None, scale=1
):
self.device = device

self.tilegrid = tilegrid
"""TileGrid containing the Mouse cursor graphic."""

self.interface = interface_index
self.endpoint = endpoint_address
self.buffer = array.array("b", [0] * 4)
self.was_attached = was_attached
Expand Down Expand Up @@ -337,8 +353,12 @@ def update(self):


class ReportMouse(BootMouse):
def __init__(self, device, endpoint_address, was_attached, tilegrid=None, scale=1): # noqa: PLR0913, too many args
super().__init__(device, endpoint_address, was_attached, tilegrid, scale)
def __init__( # noqa: PLR0913, too many args
self, device, interface_index, endpoint_address, was_attached, *, tilegrid=None, scale=1
):
super().__init__(
device, interface_index, endpoint_address, was_attached, tilegrid=tilegrid, scale=scale
)

def update(self):
"""
Expand Down