Adafruit_USBD_HID usb_hid;
#define USB_VID 0xCafe
#define USB_PID 0x4011
#define USB_MANUFACTURER "ESP32S3"
#define USB_PRODUCT "ESP32 HID"
#define HID_REPORT_SIZE 64
static const uint8_t hid_report_descriptor[] = {
0x06, 0x00, 0xFF, // Usage Page (Vendor)
0x09, 0x01, // Usage
0xA1, 0x01, // Collection (Application)
0x09, 0x02,
0x15, 0x00,
0x26, 0xFF, 0x00,
0x75, 0x08,
0x95, HID_REPORT_SIZE,
0x81, 0x02, // Input
0x09, 0x03,
0x15, 0x00,
0x26, 0xFF, 0x00,
0x75, 0x08,
0x95, HID_REPORT_SIZE,
0x91, 0x02, // Output
0xC0
};
uint8_t hid_rx_buffer[HID_REPORT_SIZE];
uint8_t hid_tx_buffer[HID_REPORT_SIZE];
// PC → ESP32
void hid_set_report_cb(uint8_t report_id,
hid_report_type_t report_type,
uint8_t const* buffer,
uint16_t bufsize)
{
memcpy(hid_rx_buffer, buffer, bufsize);
Serial.print("RX: ");
Serial.print(bufsize);
Serial.print(" ");
for(int ii=0;ii
在setup 中的設定
// 初始化 HID
TinyUSBDevice.setID(USB_VID, USB_PID);
TinyUSBDevice.setManufacturerDescriptor("MattLab");
TinyUSBDevice.setProductDescriptor("ESP32S3 MSC+HID");
// ---------- HID ----------
usb_hid.setReportDescriptor(hid_report_descriptor, sizeof(hid_report_descriptor));
usb_hid.setPollInterval(2);
usb_hid.setReportCallback(NULL, hid_set_report_cb);
usb_hid.begin();
ESP32 → PC
將資料存放於 hid_tx_buffer 陣列中再送出
memset(hid_tx_buffer, 0, HID_REPORT_SIZE);
usb_hid.sendReport(0, hid_tx_buffer, HID_REPORT_SIZE);