|
| 1 | +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +/********************************************************************* |
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 12 | + All text above, and the splash screen below must be included in |
| 13 | + any redistribution |
| 14 | +*********************************************************************/ |
| 15 | + |
| 16 | +/* This example demonstrates use of usb host with a SNES-like game controller |
| 17 | + * - Host depends on MCU: |
| 18 | + * - rp2040: bit-banging 2 GPIOs with Pico-PIO-USB library (roothub port1) |
| 19 | + * |
| 20 | + * Requirements: |
| 21 | + * - For rp2040: |
| 22 | + * - Pico-PIO-USB library |
| 23 | + * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 |
| 24 | + * - Provide VBus (5v) and GND for peripheral |
| 25 | + * - CPU Speed must be either 120 or 240 MHz. Selected via "Menu -> CPU Speed" |
| 26 | + */ |
| 27 | + |
| 28 | +// USBHost is defined in usbh_helper.h |
| 29 | +#include "usbh_helper.h" |
| 30 | +#include "tusb.h" |
| 31 | +#include "Adafruit_TinyUSB.h" |
| 32 | +#include "gamepad_reports.h" |
| 33 | + |
| 34 | +// HID report descriptor using TinyUSB's template |
| 35 | +// Single Report (no ID) descriptor |
| 36 | +uint8_t const desc_hid_report[] = { |
| 37 | + TUD_HID_REPORT_DESC_GAMEPAD() |
| 38 | +}; |
| 39 | + |
| 40 | +// USB HID object |
| 41 | +Adafruit_USBD_HID usb_hid; |
| 42 | + |
| 43 | +// Report payload defined in src/class/hid/hid.h |
| 44 | +// - For Gamepad Button Bit Mask see hid_gamepad_button_bm_t |
| 45 | +// - For Gamepad Hat Bit Mask see hid_gamepad_hat_t |
| 46 | +hid_gamepad_report_t gp; |
| 47 | + |
| 48 | +bool printed_blank = false; |
| 49 | + |
| 50 | +void setup() { |
| 51 | + if (!TinyUSBDevice.isInitialized()) { |
| 52 | + TinyUSBDevice.begin(0); |
| 53 | + } |
| 54 | + Serial.begin(115200); |
| 55 | + // Setup HID |
| 56 | + usb_hid.setPollInterval(2); |
| 57 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 58 | + usb_hid.begin(); |
| 59 | + |
| 60 | + // If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration |
| 61 | + if (TinyUSBDevice.mounted()) { |
| 62 | + TinyUSBDevice.detach(); |
| 63 | + delay(10); |
| 64 | + TinyUSBDevice.attach(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#if defined(ARDUINO_ARCH_RP2040) |
| 69 | +//--------------------------------------------------------------------+ |
| 70 | +// For RP2040 use both core0 for device stack, core1 for host stack |
| 71 | +//--------------------------------------------------------------------// |
| 72 | + |
| 73 | +//------------- Core0 -------------// |
| 74 | +void loop() { |
| 75 | +} |
| 76 | + |
| 77 | +//------------- Core1 -------------// |
| 78 | +void setup1() { |
| 79 | + // configure pio-usb: defined in usbh_helper.h |
| 80 | + rp2040_configure_pio_usb(); |
| 81 | + |
| 82 | + // run host stack on controller (rhport) 1 |
| 83 | + // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the |
| 84 | + // host bit-banging processing works done in core1 to free up core0 for other works |
| 85 | + USBHost.begin(1); |
| 86 | +} |
| 87 | + |
| 88 | +void loop1() { |
| 89 | + USBHost.task(); |
| 90 | + Serial.flush(); |
| 91 | + |
| 92 | +} |
| 93 | +#endif |
| 94 | + |
| 95 | +//--------------------------------------------------------------------+ |
| 96 | +// HID Host Callback Functions |
| 97 | +//--------------------------------------------------------------------+ |
| 98 | + |
| 99 | +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) |
| 100 | +{ |
| 101 | + Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance); |
| 102 | + |
| 103 | + // Start receiving HID reports |
| 104 | + if (!tuh_hid_receive_report(dev_addr, instance)) |
| 105 | + { |
| 106 | + Serial.printf("Error: cannot request to receive report\n"); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) |
| 111 | +{ |
| 112 | + Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { |
| 117 | + |
| 118 | + if (report[BYTE_DPAD_LEFT_RIGHT] != DPAD_NEUTRAL || |
| 119 | + report[BYTE_DPAD_UP_DOWN] != DPAD_NEUTRAL || |
| 120 | + report[BYTE_ABXY_BUTTONS] != BUTTON_NEUTRAL || |
| 121 | + report[BYTE_OTHER_BUTTONS] != BUTTON_MISC_NEUTRAL){ |
| 122 | + |
| 123 | + printed_blank = false; |
| 124 | + |
| 125 | + //debug print report data |
| 126 | + // Serial.print("Report data: "); |
| 127 | + // for (int i = 0; i < len; i++) { |
| 128 | + // Serial.print(report[i], HEX); |
| 129 | + // Serial.print(" "); |
| 130 | + // } |
| 131 | + // Serial.println(); |
| 132 | + |
| 133 | + if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_LEFT){ |
| 134 | + Serial.print("Left "); |
| 135 | + }else if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_RIGHT){ |
| 136 | + Serial.print("Right "); |
| 137 | + } |
| 138 | + |
| 139 | + if (report[BYTE_DPAD_UP_DOWN] == DPAD_UP){ |
| 140 | + Serial.print("Up "); |
| 141 | + }else if (report[BYTE_DPAD_UP_DOWN] == DPAD_DOWN){ |
| 142 | + Serial.print("Down "); |
| 143 | + } |
| 144 | + |
| 145 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_A) == BUTTON_A){ |
| 146 | + Serial.print("A "); |
| 147 | + } |
| 148 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_B) == BUTTON_B){ |
| 149 | + Serial.print("B "); |
| 150 | + } |
| 151 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_X) == BUTTON_X){ |
| 152 | + Serial.print("X "); |
| 153 | + } |
| 154 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_Y) == BUTTON_Y){ |
| 155 | + Serial.print("Y "); |
| 156 | + } |
| 157 | + |
| 158 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_LEFT_SHOULDER) == BUTTON_LEFT_SHOULDER){ |
| 159 | + Serial.print("Left Shoulder "); |
| 160 | + } |
| 161 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_RIGHT_SHOULDER) == BUTTON_RIGHT_SHOULDER){ |
| 162 | + Serial.print("Right Shoulder "); |
| 163 | + } |
| 164 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_START) == BUTTON_START){ |
| 165 | + Serial.print("Start "); |
| 166 | + } |
| 167 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_SELECT) == BUTTON_SELECT){ |
| 168 | + Serial.print("Select "); |
| 169 | + } |
| 170 | + Serial.println(); |
| 171 | + } else { |
| 172 | + if (! printed_blank){ |
| 173 | + Serial.println("NEUTRAL"); |
| 174 | + printed_blank = true; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Continue to receive the next report |
| 179 | + if (!tuh_hid_receive_report(dev_addr, instance)) { |
| 180 | + Serial.println("Error: cannot request to receive report"); |
| 181 | + } |
| 182 | +} |
0 commit comments