diff --git a/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/.feather_rp2040_usbhost_tinyusb.test.only b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/.feather_rp2040_usbhost_tinyusb.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/hid_mouse_reports.h b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/hid_mouse_reports.h new file mode 100644 index 000000000..33ad947b1 --- /dev/null +++ b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/hid_mouse_reports.h @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// HID reports for standard boot mouse + +// Byte indices for the gamepad report +#define BYTE_BUTTONS 0 // Left, right, middle click buttons +#define BYTE_DELTA_X 1 // Mouse movement horizontal +#define BYTE_DELTA_Y 2 // Mouse movement vertical + +#define BUTTON_NEUTRAL 0x00 +#define BUTTON_LEFT 0x01 +#define BUTTON_RIGHT 0x02 +#define BUTTON_MIDDLE 0x04 diff --git a/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbh_helper.h b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbh_helper.h new file mode 100644 index 000000000..4f3cc5503 --- /dev/null +++ b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbh_helper.h @@ -0,0 +1,110 @@ +// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#ifndef USBH_HELPER_H +#define USBH_HELPER_H + +#ifdef ARDUINO_ARCH_RP2040 + // pio-usb is required for rp2040 host + #include "pio_usb.h" + + // Pin D+ for host, D- = D+ + 1 + #ifndef PIN_USB_HOST_DP + #define PIN_USB_HOST_DP 16 + #endif + + // Pin for enabling Host VBUS. comment out if not used + #ifndef PIN_5V_EN + #define PIN_5V_EN 18 + #endif + + #ifndef PIN_5V_EN_STATE + #define PIN_5V_EN_STATE 1 + #endif +#endif // ARDUINO_ARCH_RP2040 + +#ifdef ARDUINO_ARCH_RP2350 + + // pio-usb is required for rp2040 host + #include "pio_usb.h" + + // Pin D+ for host, D- = D+ + 1 + #ifndef PIN_USB_HOST_DP + #define PIN_USB_HOST_DP 32 + #endif + + // Pin for enabling Host VBUS. comment out if not used + #ifndef PIN_5V_EN + #define PIN_5V_EN 29 + #endif + + #ifndef PIN_5V_EN_STATE + #define PIN_5V_EN_STATE 1 + #endif +#endif // ARDUINO_ARCH_RP2350 + +#include "Adafruit_TinyUSB.h" + +#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 + // USB Host using MAX3421E: SPI, CS, INT + #include "SPI.h" + + #if defined(ARDUINO_METRO_ESP32S2) + Adafruit_USBH_Host USBHost(&SPI, 15, 14); + #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) + Adafruit_USBH_Host USBHost(&SPI, 33, 15); + #else + // Default CS and INT are pin 10, 9 + Adafruit_USBH_Host USBHost(&SPI, 10, 9); + #endif +#else + // Native USB Host such as rp2040 + Adafruit_USBH_Host USBHost; +#endif + +//--------------------------------------------------------------------+ +// Helper Functions +//--------------------------------------------------------------------+ + +#ifdef ARDUINO_ARCH_RP2040 +static void rp2040_configure_pio_usb(void) { + //while ( !Serial ) delay(10); // wait for native usb + Serial.println("Core1 setup to run TinyUSB host with pio-usb"); + +#ifdef PIN_5V_EN + pinMode(PIN_5V_EN, OUTPUT); + digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); +#endif + + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; + pio_cfg.pin_dp = PIN_USB_HOST_DP; + +#if defined(ARDUINO_RASPBERRY_PI_PICO_W) + // For pico-w, PIO is also used to communicate with cyw43 + // Therefore we need to alternate the pio-usb configuration + // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 + pio_cfg.sm_tx = 3; + pio_cfg.sm_rx = 2; + pio_cfg.sm_eop = 3; + pio_cfg.pio_rx_num = 0; + pio_cfg.pio_tx_num = 1; + pio_cfg.tx_ch = 9; +#endif + + USBHost.configure_pio_usb(1, &pio_cfg); +} +#endif + +#endif diff --git a/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino new file mode 100644 index 000000000..8adaaa291 --- /dev/null +++ b/USB_Host_Examples/Arduino_Feather_RP2040_USB_Host_Mouse/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino @@ -0,0 +1,121 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This example demonstrates use of usb host with a standard HID boot mouse. + * - Host depends on MCU: + * - rp2040: bit-banging 2 GPIOs with Pico-PIO-USB library (roothub port1) + * + * Requirements: + * - For rp2040: + * - Pico-PIO-USB library + * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 + * - Provide VBus (5v) and GND for peripheral + */ + +// USBHost is defined in usbh_helper.h +#include "usbh_helper.h" +#include "tusb.h" +#include "Adafruit_TinyUSB.h" +#include "hid_mouse_reports.h" + + +bool printed_blank = false; + +void setup() { + Serial.begin(115200); + + // configure pio-usb: defined in usbh_helper.h + rp2040_configure_pio_usb(); + + // run host stack on controller (rhport) 1 + // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the + // host bit-banging processing works done in core1 to free up core0 for other works + USBHost.begin(1); + delay(3000); + Serial.print("USB D+ Pin:"); + Serial.println(PIN_USB_HOST_DP); + Serial.print("USB 5V Pin:"); + Serial.println(PIN_5V_EN); +} + +void loop() { + USBHost.task(); + Serial.flush(); + +} + +//--------------------------------------------------------------------+ +// HID Host Callback Functions +//--------------------------------------------------------------------+ + +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) +{ + Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance); + + // Start receiving HID reports + if (!tuh_hid_receive_report(dev_addr, instance)) + { + Serial.printf("Error: cannot request to receive report\n"); + } +} + +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) +{ + Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance); +} + +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + + if (len > 0){ + + //debug print report data + // Serial.print("Report data: "); + // for (int i = 0; i < len; i++) { + // if (i == 0 || i == 3){ + // Serial.print(report[i], HEX); + // Serial.print(" "); + // }else { // i==1 or i==2 + // Serial.print((int8_t)report[i]); + // Serial.print(" "); + // } + // } + // Serial.println(); + + Serial.print("X: "); + Serial.print((int8_t)report[1]); + Serial.print(" "); + Serial.print("Y: "); + Serial.print((int8_t)report[2]); + Serial.print(" "); + + if (report[BYTE_BUTTONS] != BUTTON_NEUTRAL){ + if ((report[BYTE_BUTTONS] & BUTTON_LEFT) == BUTTON_LEFT){ + Serial.print("Left "); + } + if ((report[BYTE_BUTTONS] & BUTTON_RIGHT) == BUTTON_RIGHT){ + Serial.print("Right "); + } + if ((report[BYTE_BUTTONS] & BUTTON_MIDDLE) == BUTTON_MIDDLE){ + Serial.print("Middle "); + } + } + Serial.println(); + } + + // Continue to receive the next report + if (!tuh_hid_receive_report(dev_addr, instance)) { + Serial.println("Error: cannot request to receive report"); + } +} diff --git a/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/.metro_rp2350_tinyusb.test.only b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/.metro_rp2350_tinyusb.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/hid_mouse_reports.h b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/hid_mouse_reports.h new file mode 100644 index 000000000..33ad947b1 --- /dev/null +++ b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/hid_mouse_reports.h @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// HID reports for standard boot mouse + +// Byte indices for the gamepad report +#define BYTE_BUTTONS 0 // Left, right, middle click buttons +#define BYTE_DELTA_X 1 // Mouse movement horizontal +#define BYTE_DELTA_Y 2 // Mouse movement vertical + +#define BUTTON_NEUTRAL 0x00 +#define BUTTON_LEFT 0x01 +#define BUTTON_RIGHT 0x02 +#define BUTTON_MIDDLE 0x04 diff --git a/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbh_helper.h b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbh_helper.h new file mode 100644 index 000000000..4f3cc5503 --- /dev/null +++ b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbh_helper.h @@ -0,0 +1,110 @@ +// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#ifndef USBH_HELPER_H +#define USBH_HELPER_H + +#ifdef ARDUINO_ARCH_RP2040 + // pio-usb is required for rp2040 host + #include "pio_usb.h" + + // Pin D+ for host, D- = D+ + 1 + #ifndef PIN_USB_HOST_DP + #define PIN_USB_HOST_DP 16 + #endif + + // Pin for enabling Host VBUS. comment out if not used + #ifndef PIN_5V_EN + #define PIN_5V_EN 18 + #endif + + #ifndef PIN_5V_EN_STATE + #define PIN_5V_EN_STATE 1 + #endif +#endif // ARDUINO_ARCH_RP2040 + +#ifdef ARDUINO_ARCH_RP2350 + + // pio-usb is required for rp2040 host + #include "pio_usb.h" + + // Pin D+ for host, D- = D+ + 1 + #ifndef PIN_USB_HOST_DP + #define PIN_USB_HOST_DP 32 + #endif + + // Pin for enabling Host VBUS. comment out if not used + #ifndef PIN_5V_EN + #define PIN_5V_EN 29 + #endif + + #ifndef PIN_5V_EN_STATE + #define PIN_5V_EN_STATE 1 + #endif +#endif // ARDUINO_ARCH_RP2350 + +#include "Adafruit_TinyUSB.h" + +#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 + // USB Host using MAX3421E: SPI, CS, INT + #include "SPI.h" + + #if defined(ARDUINO_METRO_ESP32S2) + Adafruit_USBH_Host USBHost(&SPI, 15, 14); + #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) + Adafruit_USBH_Host USBHost(&SPI, 33, 15); + #else + // Default CS and INT are pin 10, 9 + Adafruit_USBH_Host USBHost(&SPI, 10, 9); + #endif +#else + // Native USB Host such as rp2040 + Adafruit_USBH_Host USBHost; +#endif + +//--------------------------------------------------------------------+ +// Helper Functions +//--------------------------------------------------------------------+ + +#ifdef ARDUINO_ARCH_RP2040 +static void rp2040_configure_pio_usb(void) { + //while ( !Serial ) delay(10); // wait for native usb + Serial.println("Core1 setup to run TinyUSB host with pio-usb"); + +#ifdef PIN_5V_EN + pinMode(PIN_5V_EN, OUTPUT); + digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); +#endif + + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; + pio_cfg.pin_dp = PIN_USB_HOST_DP; + +#if defined(ARDUINO_RASPBERRY_PI_PICO_W) + // For pico-w, PIO is also used to communicate with cyw43 + // Therefore we need to alternate the pio-usb configuration + // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 + pio_cfg.sm_tx = 3; + pio_cfg.sm_rx = 2; + pio_cfg.sm_eop = 3; + pio_cfg.pio_rx_num = 0; + pio_cfg.pio_tx_num = 1; + pio_cfg.tx_ch = 9; +#endif + + USBHost.configure_pio_usb(1, &pio_cfg); +} +#endif + +#endif diff --git a/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino new file mode 100644 index 000000000..8adaaa291 --- /dev/null +++ b/USB_Host_Examples/Arduino_Metro_RP2350_Mouse_USB_Host/usbhost_mouse_simpletest/usbhost_mouse_simpletest.ino @@ -0,0 +1,121 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This example demonstrates use of usb host with a standard HID boot mouse. + * - Host depends on MCU: + * - rp2040: bit-banging 2 GPIOs with Pico-PIO-USB library (roothub port1) + * + * Requirements: + * - For rp2040: + * - Pico-PIO-USB library + * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 + * - Provide VBus (5v) and GND for peripheral + */ + +// USBHost is defined in usbh_helper.h +#include "usbh_helper.h" +#include "tusb.h" +#include "Adafruit_TinyUSB.h" +#include "hid_mouse_reports.h" + + +bool printed_blank = false; + +void setup() { + Serial.begin(115200); + + // configure pio-usb: defined in usbh_helper.h + rp2040_configure_pio_usb(); + + // run host stack on controller (rhport) 1 + // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the + // host bit-banging processing works done in core1 to free up core0 for other works + USBHost.begin(1); + delay(3000); + Serial.print("USB D+ Pin:"); + Serial.println(PIN_USB_HOST_DP); + Serial.print("USB 5V Pin:"); + Serial.println(PIN_5V_EN); +} + +void loop() { + USBHost.task(); + Serial.flush(); + +} + +//--------------------------------------------------------------------+ +// HID Host Callback Functions +//--------------------------------------------------------------------+ + +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) +{ + Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance); + + // Start receiving HID reports + if (!tuh_hid_receive_report(dev_addr, instance)) + { + Serial.printf("Error: cannot request to receive report\n"); + } +} + +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) +{ + Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance); +} + +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + + if (len > 0){ + + //debug print report data + // Serial.print("Report data: "); + // for (int i = 0; i < len; i++) { + // if (i == 0 || i == 3){ + // Serial.print(report[i], HEX); + // Serial.print(" "); + // }else { // i==1 or i==2 + // Serial.print((int8_t)report[i]); + // Serial.print(" "); + // } + // } + // Serial.println(); + + Serial.print("X: "); + Serial.print((int8_t)report[1]); + Serial.print(" "); + Serial.print("Y: "); + Serial.print((int8_t)report[2]); + Serial.print(" "); + + if (report[BYTE_BUTTONS] != BUTTON_NEUTRAL){ + if ((report[BYTE_BUTTONS] & BUTTON_LEFT) == BUTTON_LEFT){ + Serial.print("Left "); + } + if ((report[BYTE_BUTTONS] & BUTTON_RIGHT) == BUTTON_RIGHT){ + Serial.print("Right "); + } + if ((report[BYTE_BUTTONS] & BUTTON_MIDDLE) == BUTTON_MIDDLE){ + Serial.print("Middle "); + } + } + Serial.println(); + } + + // Continue to receive the next report + if (!tuh_hid_receive_report(dev_addr, instance)) { + Serial.println("Error: cannot request to receive report"); + } +} diff --git a/USB_Host_Examples/CircuitPython_Keyboard_USB_Host/code.py b/USB_Host_Examples/CircuitPython_Keyboard_USB_Host/code.py index 419bf63c5..e9957f3a3 100644 --- a/USB_Host_Examples/CircuitPython_Keyboard_USB_Host/code.py +++ b/USB_Host_Examples/CircuitPython_Keyboard_USB_Host/code.py @@ -6,15 +6,14 @@ import usb import adafruit_usb_host_descriptors -# lists for mouse interface indexes, endpoint addresses, and USB Device instances -# each of these will end up with length 2 once we find both mice +#interface index, and endpoint addresses for USB Device instance kbd_interface_index = None kbd_endpoint_address = None keyboard = None # scan for connected USB devices for device in usb.core.find(find_all=True): - # check for boot mouse endpoints on this device + # check for boot keyboard endpoints on this device kbd_interface_index, kbd_endpoint_address = ( adafruit_usb_host_descriptors.find_boot_keyboard_endpoint(device) ) @@ -161,7 +160,7 @@ def print_keyboard_report(report_data): # if there is no data it will raise USBTimeoutError except usb.core.USBTimeoutError: - # Nothing to do if there is no data for this mouse + # Nothing to do if there is no data for this keyboard continue print_keyboard_report(buf) diff --git a/USB_Host_Examples/CircuitPython_Mouse_Basic/code.py b/USB_Host_Examples/CircuitPython_Mouse_Basic/code.py new file mode 100644 index 000000000..7a331523b --- /dev/null +++ b/USB_Host_Examples/CircuitPython_Mouse_Basic/code.py @@ -0,0 +1,70 @@ +# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +This example is made for a basic mouse with +two buttons and a wheel that can be pressed. + +It assumes there is a single mouse connected to USB Host. +""" +import array +import usb.core +import adafruit_usb_host_descriptors + +# button names +# This is ordered by bit position. +BUTTONS = ["left", "right", "middle"] + +# scan for connected USB device and loop over any found +for device in usb.core.find(find_all=True): + # print device info + print(f"{device.idVendor:04x}:{device.idProduct:04x}") + print(device.manufacturer, device.product) + print(device.serial_number) + + # try to find mouse endpoint on the current device. + mouse_interface_index, mouse_endpoint_address = ( + adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device) + ) + if mouse_interface_index is not None and mouse_endpoint_address is not None: + mouse = device + print( + f"mouse interface: {mouse_interface_index} " + + f"endpoint_address: {hex(mouse_endpoint_address)}" + ) + + # detach the kernel driver if needed + if mouse.is_kernel_driver_active(0): + mouse.detach_kernel_driver(0) + + # set configuration on the mouse so we can use it + mouse.set_configuration() + + break + +# buffer to hold mouse data +buf = array.array("b", [0] * 8) + +# main loop +while True: + try: + # attempt to read data from the mouse + # 20ms timeout, so we don't block long if there + # is no data + count = mouse.read(0x81, buf, timeout=20) + except usb.core.USBTimeoutError: + # skip the rest of the loop if there is no data + continue + + # string with delta x, y values to print + out_str = f"{buf[1]},{buf[2]}" + + # loop over the button names + for i, button in enumerate(BUTTONS): + # check if each button is pressed using bitwise AND shifted + # to the appropriate index for this button + if buf[0] & (1 << i) != 0: + # append the button name to the string to show if + # it is being clicked. + out_str += f" {button}" + + print(out_str) diff --git a/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/code.py b/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/code.py new file mode 100644 index 000000000..c8ab2fb05 --- /dev/null +++ b/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/code.py @@ -0,0 +1,118 @@ +# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +This example is made for a basic boot mouse with +two buttons and a wheel that can be pressed. + +It assumes there is a single mouse connected to USB Host, +and no other devices connected. +""" +import array +from displayio import Group, OnDiskBitmap, TileGrid +import supervisor +import terminalio +import usb.core +from adafruit_display_text.bitmap_label import Label +import adafruit_usb_host_descriptors + +display = supervisor.runtime.display + +# group to hold visual elements +main_group = Group() + +# make the group visible on the display +display.root_group = main_group + +# load the mouse cursor bitmap +mouse_bmp = OnDiskBitmap("mouse_cursor.bmp") + +# make the background pink pixels transparent +mouse_bmp.pixel_shader.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) + +# move it to the center of the display +mouse_tg.x = display.width // 2 +mouse_tg.y = display.height // 2 + +# text label to show the x, y coordinates on the screen +output_lbl = Label( + terminalio.FONT, text=f"{mouse_tg.x},{mouse_tg.y}", color=0xFFFFFF, scale=1 +) + +# move it to the upper left corner +output_lbl.anchor_point = (0, 0) +output_lbl.anchored_position = (1, 1) + +# add it to the main group +main_group.append(output_lbl) + +# add the mouse tile grid to the main group +main_group.append(mouse_tg) + +# button names +# This is ordered by bit position. +BUTTONS = ["left", "right", "middle"] + +# scan for connected USB device and loop over any found +for device in usb.core.find(find_all=True): + # print device info + print(f"{device.idVendor:04x}:{device.idProduct:04x}") + print(device.manufacturer, device.product) + print(device.serial_number) + + # try to find mouse endpoint on the current device. + mouse_interface_index, mouse_endpoint_address = ( + adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device) + ) + if mouse_interface_index is not None and mouse_endpoint_address is not None: + mouse = device + print( + f"mouse interface: {mouse_interface_index} " + + f"endpoint_address: {hex(mouse_endpoint_address)}" + ) + + # detach the kernel driver if needed + if mouse.is_kernel_driver_active(0): + mouse.detach_kernel_driver(0) + + # set configuration on the mouse so we can use it + mouse.set_configuration() + + break + +# buffer to hold mouse data +buf = array.array("b", [0] * 8) + +# main loop +while True: + try: + # attempt to read data from the mouse + # 20ms timeout, so we don't block long if there + # is no data + count = mouse.read(0x81, buf, timeout=20) + except usb.core.USBTimeoutError: + # skip the rest of the loop if there is no data + continue + + # update the mouse tilegrid x and y coordinates + # based on the delta values read from the mouse + mouse_tg.x = max(0, min(display.width - 1, mouse_tg.x + buf[1])) + mouse_tg.y = max(0, min(display.height - 1, mouse_tg.y + buf[2])) + + # string with updated coordinates for the text label + out_str = f"{mouse_tg.x},{mouse_tg.y}" + + # loop over the button names + for i, button in enumerate(BUTTONS): + # check if each button is pressed using bitwise AND shifted + # to the appropriate index for this button + if buf[0] & (1 << i) != 0: + # append the button name to the string to show if + # it is being clicked. + out_str += f" {button}" + + # update the text label with the new coordinates + # and buttons being pressed + output_lbl.text = out_str diff --git a/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/mouse_cursor.bmp b/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/mouse_cursor.bmp new file mode 100644 index 000000000..94ec32889 Binary files /dev/null and b/USB_Host_Examples/CircuitPython_Mouse_Displayio_Cursor/mouse_cursor.bmp differ