|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import array |
| 5 | + |
| 6 | +import displayio |
| 7 | +import supervisor |
| 8 | +import terminalio |
| 9 | +import usb.core |
| 10 | +from adafruit_display_text.bitmap_label import Label |
| 11 | +from displayio import Group, OnDiskBitmap, TileGrid |
| 12 | +from tilepalettemapper import TilePaletteMapper |
| 13 | + |
| 14 | +import adafruit_usb_host_descriptors |
| 15 | + |
| 16 | +display = supervisor.runtime.display |
| 17 | + |
| 18 | +main_group = Group() |
| 19 | +display.root_group = main_group |
| 20 | + |
| 21 | +mouse_bmp = OnDiskBitmap("mouse_cursor.bmp") |
| 22 | + |
| 23 | +output_lbls = [] |
| 24 | +mouse_tgs = [] |
| 25 | +palette_mappers = [] |
| 26 | +color_converter = displayio.ColorConverter() |
| 27 | +colors = [0xFF00FF, 0x00FF00] |
| 28 | +remap_palette = displayio.Palette(3 + len(colors)) |
| 29 | +remap_palette.make_transparent(0) |
| 30 | + |
| 31 | +# copy the 3 colors from mouse palette |
| 32 | +for i in range(3): |
| 33 | + remap_palette[i] = mouse_bmp.pixel_shader[i] |
| 34 | + |
| 35 | +# add the two extra colors to the palette |
| 36 | +for i in range(2): |
| 37 | + remap_palette[i + 3] = colors[i] |
| 38 | + |
| 39 | +for i in range(2): |
| 40 | + palette_mapper = TilePaletteMapper(remap_palette, 3, 1, 1) |
| 41 | + palette_mapper[0] = [0, 1, i + 3] |
| 42 | + palette_mappers.append(palette_mapper) |
| 43 | + mouse_tg = TileGrid(mouse_bmp, pixel_shader=palette_mapper) |
| 44 | + mouse_tg.x = display.width // 2 - (i * 12) |
| 45 | + mouse_tg.y = display.height // 2 |
| 46 | + mouse_tgs.append(mouse_tg) |
| 47 | + main_group.append(mouse_tg) |
| 48 | + |
| 49 | + output_lbl = Label(terminalio.FONT, text=f"{mouse_tg.x},{mouse_tg.y}", color=colors[i], scale=1) |
| 50 | + output_lbl.anchor_point = (0, 0) |
| 51 | + output_lbl.anchored_position = (1, 1 + i * 13) |
| 52 | + output_lbls.append(output_lbl) |
| 53 | + main_group.append(output_lbl) |
| 54 | + |
| 55 | +mouse_interface_indexes = [] |
| 56 | +mouse_endpoint_addresses = [] |
| 57 | +mice = [] |
| 58 | + |
| 59 | +# scan for connected USB devices |
| 60 | +for device in usb.core.find(find_all=True): |
| 61 | + mouse_interface_index, mouse_endpoint_address = ( |
| 62 | + adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device) |
| 63 | + ) |
| 64 | + if mouse_interface_index is not None and mouse_endpoint_address is not None: |
| 65 | + mouse_interface_indexes.append(mouse_interface_index) |
| 66 | + mouse_endpoint_addresses.append(mouse_endpoint_address) |
| 67 | + |
| 68 | + mice.append(device) |
| 69 | + print(f"mouse interface: {mouse_interface_index} ", end="") |
| 70 | + print(f"endpoint_address: {hex(mouse_endpoint_address)}") |
| 71 | + if device.is_kernel_driver_active(0): |
| 72 | + device.detach_kernel_driver(0) |
| 73 | + |
| 74 | + # set the mouse configuration so it can be used |
| 75 | + device.set_configuration() |
| 76 | + |
| 77 | +# This is ordered by bit position. |
| 78 | +BUTTONS = ["left", "right", "middle"] |
| 79 | + |
| 80 | +mouse_bufs = [] |
| 81 | +for mouse_tg in mouse_tgs: |
| 82 | + # Buffer to hold data read from the mouse |
| 83 | + # Boot mice have 4 byte reports |
| 84 | + mouse_bufs.append(array.array("b", [0] * 4)) |
| 85 | + |
| 86 | + |
| 87 | +while True: |
| 88 | + for mouse_index, mouse in enumerate(mice): |
| 89 | + try: |
| 90 | + count = mouse.read( |
| 91 | + mouse_endpoint_addresses[mouse_index], mouse_bufs[mouse_index], timeout=10 |
| 92 | + ) |
| 93 | + except usb.core.USBTimeoutError: |
| 94 | + continue |
| 95 | + |
| 96 | + mouse_tgs[mouse_index].x = max( |
| 97 | + 0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_bufs[mouse_index][1]) |
| 98 | + ) |
| 99 | + mouse_tgs[mouse_index].y = max( |
| 100 | + 0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_bufs[mouse_index][2]) |
| 101 | + ) |
| 102 | + |
| 103 | + out_str = f"{mouse_tgs[mouse_index].x},{mouse_tgs[mouse_index].y}" |
| 104 | + for i, button in enumerate(BUTTONS): |
| 105 | + if mouse_bufs[mouse_index][0] & (1 << i) != 0: |
| 106 | + out_str += f" {button}" |
| 107 | + |
| 108 | + output_lbls[mouse_index].text = out_str |
0 commit comments