|
47 | 47 | SUBCLASS_RESERVED = 0x00 |
48 | 48 |
|
49 | 49 |
|
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 |
51 | 51 | """ |
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. |
55 | 56 |
|
56 | 57 | :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. |
59 | 68 | """ |
60 | 69 | mouse_interface_index, mouse_endpoint_address = None, None |
61 | 70 | mouse_device = None |
62 | 71 | if subclass == SUBCLASS_BOOT: |
63 | 72 | deviceType = "boot" |
64 | 73 | find_endpoint = adafruit_usb_host_descriptors.find_boot_mouse_endpoint |
65 | | - returnClass = BootMouse |
66 | 74 | else: |
67 | 75 | deviceType = "report" |
68 | 76 | find_endpoint = adafruit_usb_host_descriptors.find_report_mouse_endpoint |
69 | | - returnClass = ReportMouse |
70 | 77 |
|
71 | 78 | # scan for connected USB device and loop over any found |
72 | 79 | print(f"scanning usb ({deviceType})") |
@@ -130,29 +137,52 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT |
130 | 137 | else: |
131 | 138 | mouse_tg = None |
132 | 139 |
|
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, |
139 | 144 | ) |
140 | 145 |
|
141 | 146 | # if no mouse found |
142 | 147 | return None |
143 | 148 |
|
144 | 149 |
|
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 |
146 | 170 | """ |
147 | 171 | Scan for an attached report mouse connected via USB host. |
148 | 172 | If one is found initialize an instance of :class:`ReportMouse` class |
149 | 173 | and return it. |
150 | 174 |
|
151 | 175 | :param cursor_image: Provide the absolute path to the desired cursor bitmap image. If set as |
152 | 176 | `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 |
153 | 179 | :return: The :class:`ReportMouse` instance or None if no mouse was found. |
154 | 180 | """ |
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 |
156 | 186 |
|
157 | 187 |
|
158 | 188 | class BootMouse: |
|
0 commit comments