Skip to content

Commit 3378ed9

Browse files
committed
give each device their own .test.only file
1 parent 74b401d commit 3378ed9

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// HID reports for USB SNES-like controller
6+
7+
// Byte indices for the gamepad report
8+
#define BYTE_DPAD_LEFT_RIGHT 0 // D-Pad left and right
9+
#define BYTE_DPAD_UP_DOWN 1 // D-Pad up and down
10+
// bytes 2,3,4 unused
11+
#define BYTE_ABXY_BUTTONS 5 // A, B, X, Y
12+
#define BYTE_OTHER_BUTTONS 6 // Shoulders, start, and select
13+
14+
15+
#define DPAD_NEUTRAL 0x7F
16+
// D-Pad directions
17+
#define DPAD_UP 0x00
18+
#define DPAD_RIGHT 0xFF
19+
#define DPAD_DOWN 0xFF
20+
#define DPAD_LEFT 0x00
21+
22+
23+
// Face buttons (Byte[5])
24+
#define BUTTON_NEUTRAL 0x0F
25+
#define BUTTON_X 0x1F
26+
#define BUTTON_A 0x2F
27+
#define BUTTON_B 0x4F
28+
#define BUTTON_Y 0x8F
29+
30+
31+
// Miscellaneous buttons (Byte[6])
32+
#define BUTTON_MISC_NEUTRAL 0x00
33+
#define BUTTON_LEFT_SHOULDER 0x01
34+
#define BUTTON_RIGHT_SHOULDER 0x02
35+
#define BUTTON_SELECT 0x10
36+
#define BUTTON_START 0x20
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
58+
// host bit-banging processing works done in core1 to free up core0 for other works
59+
USBHost.begin(1);
60+
delay(3000);
61+
Serial.print("USB D+ Pin:");
62+
Serial.println(PIN_USB_HOST_DP);
63+
Serial.print("USB 5V Pin:");
64+
Serial.println(PIN_5V_EN);
65+
}
66+
67+
void loop() {
68+
USBHost.task();
69+
Serial.flush();
70+
71+
}
72+
73+
//--------------------------------------------------------------------+
74+
// HID Host Callback Functions
75+
//--------------------------------------------------------------------+
76+
77+
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
78+
{
79+
Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance);
80+
81+
// Start receiving HID reports
82+
if (!tuh_hid_receive_report(dev_addr, instance))
83+
{
84+
Serial.printf("Error: cannot request to receive report\n");
85+
}
86+
}
87+
88+
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
89+
{
90+
Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance);
91+
}
92+
93+
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {
94+
95+
if (report[BYTE_DPAD_LEFT_RIGHT] != DPAD_NEUTRAL ||
96+
report[BYTE_DPAD_UP_DOWN] != DPAD_NEUTRAL ||
97+
report[BYTE_ABXY_BUTTONS] != BUTTON_NEUTRAL ||
98+
report[BYTE_OTHER_BUTTONS] != BUTTON_MISC_NEUTRAL){
99+
100+
printed_blank = false;
101+
102+
//debug print report data
103+
// Serial.print("Report data: ");
104+
// for (int i = 0; i < len; i++) {
105+
// Serial.print(report[i], HEX);
106+
// Serial.print(" ");
107+
// }
108+
// Serial.println();
109+
110+
if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_LEFT){
111+
Serial.print("Left ");
112+
}else if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_RIGHT){
113+
Serial.print("Right ");
114+
}
115+
116+
if (report[BYTE_DPAD_UP_DOWN] == DPAD_UP){
117+
Serial.print("Up ");
118+
}else if (report[BYTE_DPAD_UP_DOWN] == DPAD_DOWN){
119+
Serial.print("Down ");
120+
}
121+
122+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_A) == BUTTON_A){
123+
Serial.print("A ");
124+
}
125+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_B) == BUTTON_B){
126+
Serial.print("B ");
127+
}
128+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_X) == BUTTON_X){
129+
Serial.print("X ");
130+
}
131+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_Y) == BUTTON_Y){
132+
Serial.print("Y ");
133+
}
134+
135+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_LEFT_SHOULDER) == BUTTON_LEFT_SHOULDER){
136+
Serial.print("Left Shoulder ");
137+
}
138+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_RIGHT_SHOULDER) == BUTTON_RIGHT_SHOULDER){
139+
Serial.print("Right Shoulder ");
140+
}
141+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_START) == BUTTON_START){
142+
Serial.print("Start ");
143+
}
144+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_SELECT) == BUTTON_SELECT){
145+
Serial.print("Select ");
146+
}
147+
Serial.println();
148+
} else {
149+
if (! printed_blank){
150+
Serial.println("NEUTRAL");
151+
printed_blank = true;
152+
}
153+
}
154+
155+
// Continue to receive the next report
156+
if (!tuh_hid_receive_report(dev_addr, instance)) {
157+
Serial.println("Error: cannot request to receive report");
158+
}
159+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// SPDX-FileCopyrightText: 2024 Ha Thach 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+
#ifndef USBH_HELPER_H
17+
#define USBH_HELPER_H
18+
19+
#ifdef ARDUINO_ARCH_RP2040
20+
// pio-usb is required for rp2040 host
21+
#include "pio_usb.h"
22+
23+
// Pin D+ for host, D- = D+ + 1
24+
#ifndef PIN_USB_HOST_DP
25+
#define PIN_USB_HOST_DP 16
26+
#endif
27+
28+
// Pin for enabling Host VBUS. comment out if not used
29+
#ifndef PIN_5V_EN
30+
#define PIN_5V_EN 18
31+
#endif
32+
33+
#ifndef PIN_5V_EN_STATE
34+
#define PIN_5V_EN_STATE 1
35+
#endif
36+
#endif // ARDUINO_ARCH_RP2040
37+
38+
#ifdef ARDUINO_ARCH_RP2350
39+
40+
// pio-usb is required for rp2040 host
41+
#include "pio_usb.h"
42+
43+
// Pin D+ for host, D- = D+ + 1
44+
#ifndef PIN_USB_HOST_DP
45+
#define PIN_USB_HOST_DP 32
46+
#endif
47+
48+
// Pin for enabling Host VBUS. comment out if not used
49+
#ifndef PIN_5V_EN
50+
#define PIN_5V_EN 29
51+
#endif
52+
53+
#ifndef PIN_5V_EN_STATE
54+
#define PIN_5V_EN_STATE 1
55+
#endif
56+
#endif // ARDUINO_ARCH_RP2350
57+
58+
#include "Adafruit_TinyUSB.h"
59+
60+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
61+
// USB Host using MAX3421E: SPI, CS, INT
62+
#include "SPI.h"
63+
64+
#if defined(ARDUINO_METRO_ESP32S2)
65+
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
66+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
67+
Adafruit_USBH_Host USBHost(&SPI, 33, 15);
68+
#else
69+
// Default CS and INT are pin 10, 9
70+
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
71+
#endif
72+
#else
73+
// Native USB Host such as rp2040
74+
Adafruit_USBH_Host USBHost;
75+
#endif
76+
77+
//--------------------------------------------------------------------+
78+
// Helper Functions
79+
//--------------------------------------------------------------------+
80+
81+
#ifdef ARDUINO_ARCH_RP2040
82+
static void rp2040_configure_pio_usb(void) {
83+
//while ( !Serial ) delay(10); // wait for native usb
84+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
85+
86+
#ifdef PIN_5V_EN
87+
pinMode(PIN_5V_EN, OUTPUT);
88+
digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE);
89+
#endif
90+
91+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
92+
pio_cfg.pin_dp = PIN_USB_HOST_DP;
93+
94+
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
95+
// For pico-w, PIO is also used to communicate with cyw43
96+
// Therefore we need to alternate the pio-usb configuration
97+
// details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46
98+
pio_cfg.sm_tx = 3;
99+
pio_cfg.sm_rx = 2;
100+
pio_cfg.sm_eop = 3;
101+
pio_cfg.pio_rx_num = 0;
102+
pio_cfg.pio_tx_num = 1;
103+
pio_cfg.tx_ch = 9;
104+
#endif
105+
106+
USBHost.configure_pio_usb(1, &pio_cfg);
107+
}
108+
#endif
109+
110+
#endif

0 commit comments

Comments
 (0)