Skip to content
Open
Show file tree
Hide file tree
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
36 changes: 23 additions & 13 deletions adafruit_usb_host_mouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import array
from traceback import print_exception

import adafruit_imageload
import adafruit_usb_host_descriptors
import supervisor
import usb
from displayio import OnDiskBitmap, TileGrid
from displayio import Bitmap, Palette, TileGrid

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_USB_Host_Mouse.git"
Expand Down Expand Up @@ -120,13 +120,15 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT):
# load the mouse cursor bitmap
mouse_tg = None
if isinstance(cursor_image, str):
mouse_bmp = OnDiskBitmap(cursor_image)
mouse_bmp, mouse_palette = adafruit_imageload.load(
cursor_image, bitmap=Bitmap, palette=Palette
)

# make the background pink pixels transparent
mouse_bmp.pixel_shader.make_transparent(0)
mouse_palette.make_transparent(0)

# create a TileGrid for the mouse, using its bitmap and pixel_shader
mouse_tg = TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)
mouse_tg = TileGrid(mouse_bmp, pixel_shader=mouse_palette)

return (
(mouse_device, mouse_interface_index, mouse_endpoint_address, mouse_was_attached),
Expand Down Expand Up @@ -217,13 +219,18 @@ def __init__( # noqa: PLR0913, too many args
which buttons are currently pressed."""

if tilegrid is not None:
self.display_size = (
supervisor.runtime.display.width,
supervisor.runtime.display.height,
)
self.tilegrid.x, self.tilegrid.y = (
x // 2 for x in self.display_size
) # center cursor in display
try:
import supervisor # noqa: PLC0415
except ImportError:
self.tilegrid.x, self.tilegrid.y = 0
else:
self.display_size = (
supervisor.runtime.display.width,
supervisor.runtime.display.height,
)
self.tilegrid.x, self.tilegrid.y = (
x // 2 for x in self.display_size
) # center cursor in display
else:
self._x, self._y = 0, 0

Expand Down Expand Up @@ -264,7 +271,10 @@ def release(self):
# an empty list if no interfaces were detached
for intf in self.was_attached:
if not self.device.is_kernel_driver_active(intf):
self.device.attach_kernel_driver(intf)
try:
self.device.attach_kernel_driver(intf)
except usb.core.USBError:
pass

def update(self):
"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# SPDX-License-Identifier: MIT

Adafruit-Blinka
adafruit-circuitpython-imageload
adafruit-circuitpython-usb-host-descriptors