Untitled
#include "Adafruit_TinyUSB.h" // HID report descriptor uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_KEYBOARD() }; Adafruit_USBD_HID usb_hid; void setup() { // Initialize Serial for logging Serial.begin(115200); while (!Serial) { delay(10); // Wait for Serial connection } Serial.println("Starting TinyUSB Keyboard..."); if (!TinyUSBDevice.isInitialized()) { TinyUSBDevice.begin(0); } usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD); usb_hid.setPollInterval(2); usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); usb_hid.begin(); Serial.println("USB HID initialized."); if (TinyUSBDevice.mounted()) { TinyUSBDevice.detach(); delay(10); TinyUSBDevice.attach(); Serial.println("Device re-enumerated."); } } void loop() { #ifdef TINYUSB_NEED_POLLING_TASK TinyUSBDevice.task(); #endif if (!TinyUSBDevice.mounted()) { Serial.println("Device not mounted. Waiting..."); delay(500); return; } if (!usb_hid.ready()) { Serial.println("USB HID not ready. Waiting..."); delay(100); return; } // Send 'a' key uint8_t keycode[6] = {HID_KEY_A, 0, 0, 0, 0, 0}; usb_hid.keyboardReport(0, 0, keycode); Serial.println("Sent 'a' key."); delay(100); // Release key usb_hid.keyboardRelease(0); delay(100); }
Leave a Comment