Skip to content

Commit 0847184

Browse files
committed
Combine find_and_init functions
1 parent 92e7c76 commit 0847184

1 file changed

Lines changed: 26 additions & 87 deletions

File tree

adafruit_usb_host_mouse/__init__.py

Lines changed: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
BUTTONS = ["left", "right", "middle"]
4444
DEFAULT_CURSOR = "/".join(__file__.split("/")[:-1]) + "/mouse_cursor.bmp"
4545

46+
SUBCLASS_BOOT = 0x01
47+
SUBCLASS_RESERVED = 0x00
4648

47-
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
49+
50+
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): # noqa: PLR0912
4851
"""
4952
Scan for an attached boot mouse connected via USB host.
5053
If one is found initialize an instance of :class:`BootMouse` class
@@ -56,9 +59,17 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
5659
"""
5760
mouse_interface_index, mouse_endpoint_address = None, None
5861
mouse_device = None
62+
if subclass == SUBCLASS_BOOT:
63+
deviceType = "boot"
64+
find_endpoint = adafruit_usb_host_descriptors.find_boot_mouse_endpoint
65+
returnClass = BootMouse
66+
else:
67+
deviceType = "report"
68+
find_endpoint = adafruit_usb_host_descriptors.find_report_mouse_endpoint
69+
returnClass = ReportMouse
5970

6071
# scan for connected USB device and loop over any found
61-
print("scanning usb (boot)")
72+
print(f"scanning usb ({deviceType})")
6273
for device in usb.core.find(find_all=True):
6374
# print device info
6475
try:
@@ -76,9 +87,7 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
7687
)
7788
print(config_descriptor)
7889

79-
_possible_interface_index, _possible_endpoint_address = (
80-
adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
81-
)
90+
_possible_interface_index, _possible_endpoint_address = find_endpoint(device)
8291
if _possible_interface_index is not None and _possible_endpoint_address is not None:
8392
mouse_device = device
8493
mouse_interface_index = _possible_interface_index
@@ -88,16 +97,22 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
8897
+ f"endpoint_address: {hex(mouse_endpoint_address)}"
8998
)
9099
break
91-
print("was not a boot mouse")
100+
print(f"was not a {deviceType} mouse")
92101
except usb.core.USBError as e:
93102
print_exception(e, e, None)
94103

95104
mouse_was_attached = []
96105
if mouse_device is not None:
97106
# detach the kernel driver if needed
98-
if mouse_device.is_kernel_driver_active(mouse_interface_index):
99-
mouse_was_attached = [mouse_interface_index]
100-
mouse_device.detach_kernel_driver(mouse_interface_index)
107+
if subclass == SUBCLASS_BOOT:
108+
possible_interfaces = [mouse_interface_index]
109+
else:
110+
possible_interfaces = [0, 1, 2]
111+
112+
for intf in possible_interfaces:
113+
if mouse_device.is_kernel_driver_active(intf):
114+
mouse_was_attached.append(intf)
115+
mouse_device.detach_kernel_driver(intf)
101116

102117
# set configuration on the mouse so we can use it
103118
mouse_device.set_configuration()
@@ -115,7 +130,7 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
115130
else:
116131
mouse_tg = None
117132

118-
return BootMouse(
133+
return returnClass(
119134
mouse_device,
120135
mouse_interface_index,
121136
mouse_endpoint_address,
@@ -137,83 +152,7 @@ def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
137152
`None`, the :class:`ReportMouse` will not control a :class:`displayio.TileGrid` object.
138153
:return: The :class:`ReportMouse` instance or None if no mouse was found.
139154
"""
140-
mouse_interface_index, mouse_endpoint_address = None, None
141-
mouse_device = None
142-
143-
# scan for connected USB device and loop over any found
144-
print("scanning usb (report)")
145-
for device in usb.core.find(find_all=True):
146-
# print device info
147-
try:
148-
try:
149-
print(f"{device.idVendor:04x}:{device.idProduct:04x}")
150-
except usb.core.USBError as e:
151-
print_exception(e, e, None)
152-
try:
153-
print(device.manufacturer, device.product)
154-
except usb.core.USBError as e:
155-
print_exception(e, e, None)
156-
print()
157-
config_descriptor = adafruit_usb_host_descriptors.get_configuration_descriptor(
158-
device, 0
159-
)
160-
print(config_descriptor)
161-
162-
_possible_interface_index, _possible_endpoint_address = (
163-
adafruit_usb_host_descriptors.find_report_mouse_endpoint(device)
164-
)
165-
if _possible_interface_index is not None and _possible_endpoint_address is not None:
166-
mouse_device = device
167-
mouse_interface_index = _possible_interface_index
168-
mouse_endpoint_address = _possible_endpoint_address
169-
print(
170-
f"mouse interface: {mouse_interface_index} "
171-
+ f"endpoint_address: {hex(mouse_endpoint_address)}"
172-
)
173-
break
174-
print("was not a report mouse")
175-
except usb.core.USBError as e:
176-
print_exception(e, e, None)
177-
178-
mouse_was_attached = []
179-
_attach_list = []
180-
if mouse_device is not None:
181-
# detach the kernel driver if needed
182-
# Typically HID devices have interfaces 0,1,2
183-
for intf in range(3):
184-
if mouse_device.is_kernel_driver_active(intf):
185-
_attach_list.append(intf)
186-
mouse_device.detach_kernel_driver(intf)
187-
188-
if len(_attach_list) > 0:
189-
mouse_was_attached = _attach_list
190-
191-
# set configuration on the mouse so we can use it
192-
mouse_device.set_configuration()
193-
194-
# load the mouse cursor bitmap
195-
if isinstance(cursor_image, str):
196-
mouse_bmp = OnDiskBitmap(cursor_image)
197-
198-
# make the background pink pixels transparent
199-
mouse_bmp.pixel_shader.make_transparent(0)
200-
201-
# create a TileGrid for the mouse, using its bitmap and pixel_shader
202-
mouse_tg = TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)
203-
204-
else:
205-
mouse_tg = None
206-
207-
return ReportMouse(
208-
mouse_device,
209-
mouse_interface_index,
210-
mouse_endpoint_address,
211-
mouse_was_attached,
212-
tilegrid=mouse_tg,
213-
)
214-
215-
# if no mouse found
216-
return None
155+
return find_and_init_boot_mouse(cursor_image, SUBCLASS_RESERVED)
217156

218157

219158
class BootMouse:

0 commit comments

Comments
 (0)