|
| 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 | + Serial.begin(115200); |
| 52 | + |
| 53 | + // configure pio-usb: defined in usbh_helper.h |
| 54 | + rp2040_configure_pio_usb(); |
| 55 | + |
| 56 | + // run host stack on controller (rhport) 1 |
| 57 | + USBHost.begin(1); |
| 58 | + delay(3000); |
| 59 | + Serial.print("USB D+ Pin:"); |
| 60 | + Serial.println(PIN_USB_HOST_DP); |
| 61 | + Serial.print("USB 5V Pin:"); |
| 62 | + Serial.println(PIN_5V_EN); |
| 63 | +} |
| 64 | + |
| 65 | +void loop() { |
| 66 | + USBHost.task(); |
| 67 | + Serial.flush(); |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +//--------------------------------------------------------------------+ |
| 72 | +// HID Host Callback Functions |
| 73 | +//--------------------------------------------------------------------+ |
| 74 | + |
| 75 | +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) |
| 76 | +{ |
| 77 | + Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance); |
| 78 | + |
| 79 | + // Start receiving HID reports |
| 80 | + if (!tuh_hid_receive_report(dev_addr, instance)) |
| 81 | + { |
| 82 | + Serial.printf("Error: cannot request to receive report\n"); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) |
| 87 | +{ |
| 88 | + Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance); |
| 89 | +} |
| 90 | + |
| 91 | +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { |
| 92 | + |
| 93 | + if (report[BYTE_DPAD_LEFT_RIGHT] != DPAD_NEUTRAL || |
| 94 | + report[BYTE_DPAD_UP_DOWN] != DPAD_NEUTRAL || |
| 95 | + report[BYTE_ABXY_BUTTONS] != BUTTON_NEUTRAL || |
| 96 | + report[BYTE_OTHER_BUTTONS] != BUTTON_MISC_NEUTRAL){ |
| 97 | + |
| 98 | + printed_blank = false; |
| 99 | + |
| 100 | + //debug print report data |
| 101 | + // Serial.print("Report data: "); |
| 102 | + // for (int i = 0; i < len; i++) { |
| 103 | + // Serial.print(report[i], HEX); |
| 104 | + // Serial.print(" "); |
| 105 | + // } |
| 106 | + // Serial.println(); |
| 107 | + |
| 108 | + if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_LEFT){ |
| 109 | + Serial.print("Left "); |
| 110 | + }else if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_RIGHT){ |
| 111 | + Serial.print("Right "); |
| 112 | + } |
| 113 | + |
| 114 | + if (report[BYTE_DPAD_UP_DOWN] == DPAD_UP){ |
| 115 | + Serial.print("Up "); |
| 116 | + }else if (report[BYTE_DPAD_UP_DOWN] == DPAD_DOWN){ |
| 117 | + Serial.print("Down "); |
| 118 | + } |
| 119 | + |
| 120 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_A) == BUTTON_A){ |
| 121 | + Serial.print("A "); |
| 122 | + } |
| 123 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_B) == BUTTON_B){ |
| 124 | + Serial.print("B "); |
| 125 | + } |
| 126 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_X) == BUTTON_X){ |
| 127 | + Serial.print("X "); |
| 128 | + } |
| 129 | + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_Y) == BUTTON_Y){ |
| 130 | + Serial.print("Y "); |
| 131 | + } |
| 132 | + |
| 133 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_LEFT_SHOULDER) == BUTTON_LEFT_SHOULDER){ |
| 134 | + Serial.print("Left Shoulder "); |
| 135 | + } |
| 136 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_RIGHT_SHOULDER) == BUTTON_RIGHT_SHOULDER){ |
| 137 | + Serial.print("Right Shoulder "); |
| 138 | + } |
| 139 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_START) == BUTTON_START){ |
| 140 | + Serial.print("Start "); |
| 141 | + } |
| 142 | + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_SELECT) == BUTTON_SELECT){ |
| 143 | + Serial.print("Select "); |
| 144 | + } |
| 145 | + Serial.println(); |
| 146 | + } else { |
| 147 | + if (! printed_blank){ |
| 148 | + Serial.println("NEUTRAL"); |
| 149 | + printed_blank = true; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Continue to receive the next report |
| 154 | + if (!tuh_hid_receive_report(dev_addr, instance)) { |
| 155 | + Serial.println("Error: cannot request to receive report"); |
| 156 | + } |
| 157 | +} |
0 commit comments