|
| 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 |
0 commit comments