1+ /* ********************************************************************
2+ Adafruit invests time and resources providing this open source code,
3+ please support Adafruit and open-source hardware by purchasing
4+ products from Adafruit!
5+
6+ MIT license, check LICENSE for more information
7+ Copyright (c) 2024 John Park for Adafruit Industries
8+ Copyright (c) 2019 Ha Thach for Adafruit Industries
9+ All text above, and the splash screen below must be included in
10+ any redistribution
11+ *********************************************************************/
12+
13+ /* Keyboard HID Keycode Reporter
14+ * - Device runs on native usb controller (roothub port0)
15+ * - esp32-s2 TFT Feather : using MAX3421e controller featherwing
16+ * - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h
17+ */
18+
19+ // USBHost is defined in usbh_helper.h
20+ #include " usbh_helper.h"
21+ #include < Adafruit_GFX.h>
22+ #include < Adafruit_ST7789.h>
23+ #include < Fonts/FreeMono18pt7b.h>
24+ #include < Fonts/FreeMono12pt7b.h>
25+
26+ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
27+
28+ void setup () {
29+ Serial.begin (115200 );
30+ // turn on backlight
31+ pinMode (TFT_BACKLITE, OUTPUT);
32+ digitalWrite (TFT_BACKLITE, HIGH);
33+
34+ // turn on the TFT / I2C power supply
35+ pinMode (TFT_I2C_POWER, OUTPUT);
36+ digitalWrite (TFT_I2C_POWER, HIGH);
37+ delay (10 );
38+
39+ // initialize TFT
40+ tft.init (135 , 240 ); // Init ST7789 240x135
41+ tft.setRotation (3 );
42+ tft.fillScreen (ST77XX_BLACK);
43+
44+ tft.setFont (&FreeMono18pt7b);
45+ tft.setTextWrap (true );
46+ tft.fillScreen (ST77XX_BLACK);
47+ tft.setCursor (0 , 20 );
48+ tft.setTextColor (ST77XX_GREEN);
49+ tft.setTextSize (1 );
50+ tft.println (" HIDreporter" );
51+
52+ // init host stack on controller (rhport) 1
53+ USBHost.begin (1 );
54+
55+ // while ( !Serial ) delay(10); // wait for native usb
56+ Serial.println (" TinyUSB Dual: HID Device Reporter" );
57+ }
58+
59+ void loop () {
60+ USBHost.task ();
61+ Serial.flush ();
62+ }
63+
64+ extern " C" {
65+
66+ // Invoked when device with hid interface is mounted
67+ // Report descriptor is also available for use.
68+ // tuh_hid_parse_report_descriptor() can be used to parse common/simple enough
69+ // descriptor. Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE,
70+ // it will be skipped therefore report_desc = NULL, desc_len = 0
71+ void tuh_hid_mount_cb (uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) {
72+ (void ) desc_report;
73+ (void ) desc_len;
74+ uint16_t vid, pid;
75+ tuh_vid_pid_get (dev_addr, &vid, &pid);
76+
77+ Serial.printf (" HID device address = %d, instance = %d is mounted\r\n " , dev_addr, instance);
78+ Serial.printf (" VID = %04x, PID = %04x\r\n " , vid, pid);
79+ tft.fillRect (0 , 34 , 240 , 80 , ST77XX_BLACK);
80+ tft.setFont (&FreeMono12pt7b);
81+ tft.setCursor (0 , 50 );
82+ tft.printf (" VID=%04x,PID=%04x\r\n " , vid, pid);
83+ if (!tuh_hid_receive_report (dev_addr, instance)) {
84+ Serial.printf (" Error: cannot request to receive report\r\n " );
85+ }
86+ }
87+
88+ // Invoked when device with hid interface is un-mounted
89+ void tuh_hid_umount_cb (uint8_t dev_addr, uint8_t instance) {
90+ Serial.printf (" HID device address = %d, instance = %d is unmounted\r\n " , dev_addr, instance);
91+ tft.fillRect (0 , 34 , 240 , 140 , ST77XX_BLACK);
92+ tft.setFont (&FreeMono12pt7b);
93+ tft.setTextColor (ST77XX_YELLOW);
94+ tft.setCursor (0 , 50 );
95+ tft.printf (" -- unmounted --" );
96+ tft.setTextColor (ST77XX_GREEN);
97+
98+ }
99+
100+ // Invoked when received report from device via interrupt endpoint
101+ void tuh_hid_report_received_cb (uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) {
102+ Serial.printf (" HIDreport : " );
103+ tft.fillRect (0 , 64 , 240 , 80 , ST77XX_BLACK);
104+ tft.setCursor (0 , 88 );
105+ tft.setFont (&FreeMono18pt7b);
106+
107+ for (uint16_t i = 0 ; i < len; i++) {
108+ Serial.printf (" 0x%02X " , report[i]);
109+ tft.printf (" %02X " , report[i]);
110+ }
111+
112+ Serial.println ();
113+ // continue to request to receive report
114+ if (!tuh_hid_receive_report (dev_addr, instance)) {
115+ Serial.printf (" Error: cannot request to receive report\r\n " );
116+ }
117+ }
118+
119+ } // extern C
0 commit comments