Skip to content

Commit eac10af

Browse files
authored
Merge pull request #2892 from jedgarpark/hid-reporter
Hid reporter
2 parents bd9285f + cc08a54 commit eac10af

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed

HID_Reporter/USB_Host_HID_Reporter/.feather_esp32s2_tft.test.only

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
"version": "0.2.0",
5+
"configurations": []
6+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-FileCopyrightText: 2024 John Park 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) 2024 John Park for Adafruit Industries
12+
Copyright (c) 2019 Ha Thach for Adafruit Industries
13+
All text above, and the splash screen below must be included in
14+
any redistribution
15+
*********************************************************************/
16+
17+
/* Keyboard HID Keycode Reporter
18+
* - Device runs on native usb controller (roothub port0)
19+
* - esp32-s2 TFT Feather : using MAX3421e controller featherwing
20+
* - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h
21+
*/
22+
23+
// USBHost is defined in usbh_helper.h
24+
#include "usbh_helper.h"
25+
#include <Adafruit_GFX.h>
26+
#include <Adafruit_ST7789.h>
27+
#include <Fonts/FreeMono18pt7b.h>
28+
#include <Fonts/FreeMono12pt7b.h>
29+
30+
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
31+
32+
void setup() {
33+
Serial.begin(115200);
34+
// turn on backlight
35+
pinMode(TFT_BACKLITE, OUTPUT);
36+
digitalWrite(TFT_BACKLITE, HIGH);
37+
38+
// turn on the TFT / I2C power supply
39+
pinMode(TFT_I2C_POWER, OUTPUT);
40+
digitalWrite(TFT_I2C_POWER, HIGH);
41+
delay(10);
42+
43+
// initialize TFT
44+
tft.init(135, 240); // Init ST7789 240x135
45+
tft.setRotation(3);
46+
tft.fillScreen(ST77XX_BLACK);
47+
48+
tft.setFont(&FreeMono18pt7b);
49+
tft.setTextWrap(true);
50+
tft.fillScreen(ST77XX_BLACK);
51+
tft.setCursor(0, 20);
52+
tft.setTextColor(ST77XX_GREEN);
53+
tft.setTextSize(1);
54+
tft.println("HIDreporter");
55+
56+
// init host stack on controller (rhport) 1
57+
USBHost.begin(1);
58+
59+
// while ( !Serial ) delay(10); // wait for native usb
60+
Serial.println("TinyUSB Dual: HID Device Reporter");
61+
}
62+
63+
void loop() {
64+
USBHost.task();
65+
Serial.flush();
66+
}
67+
68+
extern "C" {
69+
70+
// Invoked when device with hid interface is mounted
71+
// Report descriptor is also available for use.
72+
// tuh_hid_parse_report_descriptor() can be used to parse common/simple enough
73+
// descriptor. Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE,
74+
// it will be skipped therefore report_desc = NULL, desc_len = 0
75+
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) {
76+
(void) desc_report;
77+
(void) desc_len;
78+
uint16_t vid, pid;
79+
tuh_vid_pid_get(dev_addr, &vid, &pid);
80+
81+
Serial.printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
82+
Serial.printf("VID = %04x, PID = %04x\r\n", vid, pid);
83+
tft.fillRect(0, 34, 240, 80, ST77XX_BLACK);
84+
tft.setFont(&FreeMono12pt7b);
85+
tft.setCursor(0, 50);
86+
tft.printf("VID=%04x,PID=%04x\r\n", vid, pid);
87+
if (!tuh_hid_receive_report(dev_addr, instance)) {
88+
Serial.printf("Error: cannot request to receive report\r\n");
89+
}
90+
}
91+
92+
// Invoked when device with hid interface is un-mounted
93+
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
94+
Serial.printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
95+
tft.fillRect(0, 34, 240, 140, ST77XX_BLACK);
96+
tft.setFont(&FreeMono12pt7b);
97+
tft.setTextColor(ST77XX_YELLOW);
98+
tft.setCursor(0, 50);
99+
tft.printf("-- unmounted --");
100+
tft.setTextColor(ST77XX_GREEN);
101+
102+
}
103+
104+
// Invoked when received report from device via interrupt endpoint
105+
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) {
106+
Serial.printf("HIDreport : ");
107+
tft.fillRect(0, 64, 240, 80, ST77XX_BLACK);
108+
tft.setCursor(0, 88);
109+
tft.setFont(&FreeMono18pt7b);
110+
111+
for (uint16_t i = 0; i < len; i++) {
112+
Serial.printf("0x%02X ", report[i]);
113+
tft.printf("%02X ", report[i]);
114+
}
115+
116+
Serial.println();
117+
// continue to request to receive report
118+
if (!tuh_hid_receive_report(dev_addr, instance)) {
119+
Serial.printf("Error: cannot request to receive report\r\n");
120+
}
121+
}
122+
123+
} // extern C
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-FileCopyrightText: 2024 John Park for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/*********************************************************************
5+
Adafruit invests time and resources providing this open source code,
6+
please support Adafruit and open-source hardware by purchasing
7+
products from Adafruit!
8+
9+
MIT license, check LICENSE for more information
10+
Copyright (c) 2019 Ha Thach for Adafruit Industries
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
15+
#ifndef USBH_HELPER_H
16+
#define USBH_HELPER_H
17+
18+
#ifdef ARDUINO_ARCH_RP2040
19+
// pio-usb is required for rp2040 host
20+
#include "pio_usb.h"
21+
22+
// Pin D+ for host, D- = D+ + 1
23+
#ifndef PIN_USB_HOST_DP
24+
#define PIN_USB_HOST_DP 16
25+
#endif
26+
27+
// Pin for enabling Host VBUS. comment out if not used
28+
#ifndef PIN_5V_EN
29+
#define PIN_5V_EN 18
30+
#endif
31+
32+
#ifndef PIN_5V_EN_STATE
33+
#define PIN_5V_EN_STATE 1
34+
#endif
35+
#endif // ARDUINO_ARCH_RP2040
36+
37+
#include "Adafruit_TinyUSB.h"
38+
39+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
40+
// USB Host using MAX3421E: SPI, CS, INT
41+
#include "SPI.h"
42+
43+
#if defined(ARDUINO_METRO_ESP32S2)
44+
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
45+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
46+
Adafruit_USBH_Host USBHost(&SPI, 33, 15);
47+
#else
48+
// Default CS and INT are pin 10, 9
49+
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
50+
#endif
51+
#else
52+
// Native USB Host such as rp2040
53+
Adafruit_USBH_Host USBHost;
54+
#endif
55+
56+
//--------------------------------------------------------------------+
57+
// Helper Functions
58+
//--------------------------------------------------------------------+
59+
60+
#ifdef ARDUINO_ARCH_RP2040
61+
static void rp2040_configure_pio_usb(void) {
62+
//while ( !Serial ) delay(10); // wait for native usb
63+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
64+
65+
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
66+
uint32_t cpu_hz = clock_get_hz(clk_sys);
67+
if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) {
68+
while (!Serial) {
69+
delay(10); // wait for native usb
70+
}
71+
Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
72+
Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n");
73+
while (1) {
74+
delay(1);
75+
}
76+
}
77+
78+
#ifdef PIN_5V_EN
79+
pinMode(PIN_5V_EN, OUTPUT);
80+
digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE);
81+
#endif
82+
83+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
84+
pio_cfg.pin_dp = PIN_USB_HOST_DP;
85+
86+
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
87+
// For pico-w, PIO is also used to communicate with cyw43
88+
// Therefore we need to alternate the pio-usb configuration
89+
// details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46
90+
pio_cfg.sm_tx = 3;
91+
pio_cfg.sm_rx = 2;
92+
pio_cfg.sm_eop = 3;
93+
pio_cfg.pio_rx_num = 0;
94+
pio_cfg.pio_tx_num = 1;
95+
pio_cfg.tx_ch = 9;
96+
#endif
97+
98+
USBHost.configure_pio_usb(1, &pio_cfg);
99+
}
100+
#endif
101+
102+
#endif

0 commit comments

Comments
 (0)