Skip to content

Commit bd42fcc

Browse files
committed
Use more symetric function design
1 parent 0847184 commit bd42fcc

1 file changed

Lines changed: 46 additions & 16 deletions

File tree

adafruit_usb_host_mouse/__init__.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,33 @@
4747
SUBCLASS_RESERVED = 0x00
4848

4949

50-
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): # noqa: PLR0912
50+
def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, scale=1, subclass=SUBCLASS_BOOT): # noqa: PLR0912
5151
"""
52-
Scan for an attached boot mouse connected via USB host.
53-
If one is found initialize an instance of :class:`BootMouse` class
54-
and return it.
52+
Scan for an attached mouse connected via USB host.
53+
If one is found return a tuple containing the parameters needed to initalize an
54+
instance of :class: `BootMouse` or :class: `ReportMouse` depending on the value of
55+
the subclass parameter.
5556
5657
:param cursor_image: Provide the absolute path to the desired cursor bitmap image. If set as
57-
`None`, the :class:`BootMouse` instance will not control a :class:`displayio.TileGrid` object.
58-
:return: The :class:`BootMouse` instance or None if no mouse was found.
58+
`None`, the object instance created using the returned tuple will not control
59+
a :class:`displayio.TileGrid` object.
60+
:param scale: The scale of the group that the Mouse TileGrid will be put into
61+
Needed in order to properly clamp the mouse to the display bounds
62+
:param subclass: Defines whether to search for boot or non-boot mice.
63+
`0x01`, a boot mouse will be searched for
64+
`0x02`, a non-boot (report) mouse will be searched for
65+
:return: A tupple cotaining the arguments needed to inialize a :class:`BootMouse`
66+
or `ReportMouse` instance depending on the value of subclass. If no mouse is found
67+
None is returned.
5968
"""
6069
mouse_interface_index, mouse_endpoint_address = None, None
6170
mouse_device = None
6271
if subclass == SUBCLASS_BOOT:
6372
deviceType = "boot"
6473
find_endpoint = adafruit_usb_host_descriptors.find_boot_mouse_endpoint
65-
returnClass = BootMouse
6674
else:
6775
deviceType = "report"
6876
find_endpoint = adafruit_usb_host_descriptors.find_report_mouse_endpoint
69-
returnClass = ReportMouse
7077

7178
# scan for connected USB device and loop over any found
7279
print(f"scanning usb ({deviceType})")
@@ -130,29 +137,52 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT
130137
else:
131138
mouse_tg = None
132139

133-
return returnClass(
134-
mouse_device,
135-
mouse_interface_index,
136-
mouse_endpoint_address,
137-
mouse_was_attached,
138-
tilegrid=mouse_tg,
140+
return (
141+
(mouse_device, mouse_interface_index, mouse_endpoint_address, mouse_was_attached),
142+
mouse_tg,
143+
scale,
139144
)
140145

141146
# if no mouse found
142147
return None
143148

144149

145-
def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR): # noqa: PLR0912
150+
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, scale=1): # noqa: PLR0912
151+
"""
152+
Scan for an attached boot mouse connected via USB host.
153+
If one is found initialize an instance of :class:`BootMouse` class
154+
and return it.
155+
156+
:param cursor_image: Provide the absolute path to the desired cursor bitmap image. If set as
157+
`None`, the :class:`BootMouse` instance will not control a :class:`displayio.TileGrid` object
158+
:param scale: The scale of the group that the Mouse TileGrid will be put into
159+
Needed in order to properly clamp the mouse to the display bounds
160+
:return: The :class:`BootMouse` instance or None if no mouse was found.
161+
"""
162+
found_mouse = find_and_init_mouse(cursor_image, scale, SUBCLASS_BOOT)
163+
if found_mouse is not None:
164+
return BootMouse(*found_mouse[0], tilegrid=found_mouse[1], scale=found_mouse[2])
165+
else:
166+
return None
167+
168+
169+
def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR, scale=1): # noqa: PLR0912
146170
"""
147171
Scan for an attached report mouse connected via USB host.
148172
If one is found initialize an instance of :class:`ReportMouse` class
149173
and return it.
150174
151175
:param cursor_image: Provide the absolute path to the desired cursor bitmap image. If set as
152176
`None`, the :class:`ReportMouse` will not control a :class:`displayio.TileGrid` object.
177+
:param scale: The scale of the group that the Mouse TileGrid will be put into
178+
Needed in order to properly clamp the mouse to the display bounds
153179
:return: The :class:`ReportMouse` instance or None if no mouse was found.
154180
"""
155-
return find_and_init_boot_mouse(cursor_image, SUBCLASS_RESERVED)
181+
found_mouse = find_and_init_mouse(cursor_image, scale, SUBCLASS_RESERVED)
182+
if found_mouse is not None:
183+
return ReportMouse(*found_mouse[0], tilegrid=found_mouse[1], scale=found_mouse[2])
184+
else:
185+
return None
156186

157187

158188
class BootMouse:

0 commit comments

Comments
 (0)