romu-hid-keyboard-macro.ino

Bitmerse Romu Compact USB Development Board (RP2354A) · File : romu-hid-keyboard-macro.ino · Language : C/C++ (TinyUSB) · Use case : Créer un périphérique HID clavier personnalisé qui envoie une séquence de touches.

Turns the Romu board into a USB keyboard that automatically types a phrase.

Requires TinyUSB. The board connects directly via its USB-A connector. No external GPIOs used here, everything is handled by the USB stack. Ideal for macros or simple authentication. Compile with Pico SDK and TinyUSB.

romu-hid-keyboard-macro.ino

#include "pico/stdlib.h"
#include "tusb.h"
#include "class/hid/hid_device.h"

// Descripteur HID personnalisé pour un clavier standard
uint8_t const desc_hid_report[] = {
    TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_REPORTID_KEYBOARD))
};

const uint8_t desc_configuration[] = {
    TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUD_CONFIG_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
    TUD_HID_DESCRIPTOR(0, 0, HID_ITF_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x81, 10)
};

uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance) {
    return desc_hid_report;
}

uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen) {
    return 0;
}

void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) {
    // Ignorer les rapports entrants pour cet exemple
}

int main() {
    tusb_init();

    while (true) {
        tud_task();

        if (tud_mounted()) {
            // Envoyer 'Hello' puis 'Enter'
            static bool sent = false;
            if (!sent) {
                keyboard_report_t report;
                report.modifier = 0;
                report.keycode[0] = HID_KEY_H;
                report.keycode[1] = HID_KEY_E;
                report.keycode[2] = HID_KEY_L;
                report.keycode[3] = HID_KEY_L;
                report.keycode[4] = HID_KEY_O;
                report.keycode[5] = 0;
                report.keycode[6] = 0;
                report.keycode[7] = 0;
                tud_hid_report(0, &report, sizeof(report));
                sleep_ms(100);
                
                // Release keys
                memset(&report, 0, sizeof(report));
                tud_hid_report(0, &report, sizeof(report));
                sent = true;
            }
        }
    }
}

← Back to the board