Skip to content

Instantly share code, notes, and snippets.

@brgaulin
Last active Oct 14, 2021
Embed
What would you like to do?
ESP32-S2 Keyboard on Arduino
#include "esp32-hal.h"
#include "esp32-hal-tinyusb.h"
#include "USB.h"
#define EPNUM_HID 0x81
ESPUSB usb = ESPUSB();
uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_KEYBOARD() };
const uint8_t* tud_hid_descriptor_report_cb(void)
{
return desc_hid_report;
}
static uint16_t load_hid_descriptor(uint8_t * dst, uint8_t * itf)
{
uint8_t str_index = tinyusb_add_string_descriptor("Keypad");
uint8_t descriptor[TUD_HID_DESC_LEN] = {
TUD_HID_DESCRIPTOR(*itf, str_index, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
};
*itf+=1;
memcpy(dst, descriptor, TUD_HID_DESC_LEN);
return TUD_HID_DESC_LEN;
}
void setup() {
Serial.begin(115200);
usb.productName("Keypad");
usb.manufacturerName("Me");
tinyusb_enable_interface(USB_INTERFACE_HID, TUD_HID_DESC_LEN, load_hid_descriptor);
usb.begin();
Serial.println("[setup] Setup Done!");
}
void loop() {
if (tud_hid_ready()) {
Serial.println("HID Ready! Sending A key");
uint8_t keycode[6] = { 0 };
keycode[0] = HID_KEY_A;
tud_hid_keyboard_report(0, 0, keycode);
delay(10);
tud_hid_keyboard_report(0, 0, NULL);
Serial.println("pausing 3s...");
delay(3000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment