Long story short I am trying to turn several XIAO boards into HID keyboards.
Previously I wrote a sketch using the Keyboard library from Arduino, but these boards are not supported. I found that if I use specifically the "Adafruit TinyUSB v0.10.5" the board are supported and compiles/uploads using the example "hid_keyboard". I had trouble understanding how the TinyUSB example operates and hoped I could adapt my old sketch to use the the TinyUSB library, but am having trouble getting it to compile.
a seeeduino XIAO is a microcontroller with native USB-support.
You don't need the Tiny-USB library
here is a code that I was quickly digging out where I used a XIAO for receiving bytes over serial interface and then let them send as keystrokes to the computer
You have to install the SafeString-library if you want to use this code
be careful with sending keystrokes. You should make the sending conditional to a hardware button and not immidiately start sending keystrokes
This would happen even in case you want to modify your source-code and then your source-code gets screwed up
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
do { \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
} while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
//Test
#include "Keyboard.h"
#include <SafeString.h>
createSafeString(myDemo_SS, 32);
createSafeString(mySecondDemo_SS, 32);
#define KEY_7 0x24 // Keyboard 7 and &
const byte D0 = 0;
const byte D1 = 1;
const byte D2 = 2;
const byte OnBoard_LED = 13;
byte Button0;
byte Button1;
byte Button2;
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
unsigned long MyButtonTimer = 0; // variables MUST be of type unsigned long
unsigned long KeyTimer = 0;
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
digitalWrite(IO_Pin,!digitalRead(IO_Pin) );
}
}
int count = 0;
void PrintFileNameDateTime() {
Serial.println("Code running comes from file ");
Serial.println(__FILE__);
Serial.print(" compiled ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.println(__TIME__);
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
Keyboard.begin();
pinMode(D0, INPUT_PULLUP);
pinMode(D1, INPUT_PULLUP);
pinMode(D2, INPUT_PULLUP);
myDemo_SS = "";
}
void ReceiveBytes() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
dbgi("RcvBytes",myDemo_SS,1000);
if (Serial.available() > 0) { // whenever a byte is received
rc = Serial.read(); // read byte from receive-buffer
if (rc != endMarker) { // if it is not the endmarker
if (myDemo_SS.length() == 0 ) {
myDemo_SS = rc;
}
else {
myDemo_SS += rc; // store byte
ndx++; // increase position by one
}
}
else {
Serial.print(myDemo_SS);
myDemo_SS = "";
}
}
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,100);
Button0 = digitalRead(D0);
Button1 = digitalRead(D1);
Button2 = digitalRead(D2);
/*
if ( TimePeriodIsOver(MyButtonTimer,500) ) {
debugTxtVar(D0,Button0);
debugTxtVar(D1,Button1);
debugTxtVar(D2,Button2);
Serial.println();
Serial.println();
}
*/
if ( TimePeriodIsOver(KeyTimer,2000) ) {
if (Button0 == LOW) {
Serial.println("Button0 is pressed");
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('l');
Keyboard.release('l');
Keyboard.release(KEY_LEFT_GUI);
Keyboard.release(KEY_LEFT_ALT);
}
}
// dbgi("0:",count,1000);
ReceiveBytes();
if (count < 1) {
if ( TimePeriodIsOver(MyTestTimer,10000) ) {
dbg("10 seconds over",0);
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
Keyboard.print("Hello");
Keyboard.press(KEY_RIGHT_ALT);
Keyboard.press('q');
Keyboard.releaseAll();
Keyboard.print("world.com");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
Keyboard.print("some keystrokes");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
count++;
}
}
}
The Adafruit HID keyboard example has a much more complicated constructor for the device than you do:
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
The Xiao uses the same processor as the the Arduino Zero (and several of the MKR boards.) The "Arduino Keyboard" library OUGHT to be supported - are you sure that your version is up-to-date?