Why does Keyboard HID only work when system mode is automatic?

photon
Tags: #<Tag:0x00007fe228e23c10>

#1

I have a particle photon. I tried the keyboard emulation so that it can type stuff via usb. But I need it to do it right at boot up. I don’t have time to wait for it to connect to cloud. So I changed system mode to semi automatic and manual and both times they keyboard doesn’t work. I tried this on the most recent firmware and also 0.6. It works fine on automatic.

What can I do about this. Additionally, can anyone confirm this behavior on the new mesh devices? I was looking to buy one.


#2

I’d have to try with a Photon, but AFAICT there is no HID support on mesh devices yet.
It’s a requested feature but not very high on the priority list.


Update:
I have just tried this code on a Photon 1.4.2 and it works as expected.
Each time I hit the SETUP button the expected text gety typed out

SYSTEM_MODE(SEMI_AUTOMATIC)
STARTUP(Keyboard.begin())

void setup() {
}

void loop() {
  if (!digitalRead(BTN)) {
    Keyboard.printlnf("Test %lu", millis());
    delay(1000);
  }
}

#3

Thanks for testing that. I compared it to my code and it looks like when Keyboard is in loop() it works but when it is in setup(), like in my code, it only works when system mode is automatic. The below code doesn’t type for me.

Anyway I can now work around it now that I know it works in loop().

SYSTEM_MODE(SEMI_AUTOMATIC)
STARTUP(Keyboard.begin())

void setup() {
    Keyboard.print("yellow");
}

void loop() {
  }

#4

I’d say the reason is that in non-AUTOMATIC mode the code passes setup() before the host OS is done enumerating the HID device.
So it’s not that it wouldn’t work but merely a timing issue - AUTOMATIC just has the side effect that setup() is executed later (i.e. after the cloud connection has been established).
What if you add some delay() before the first attempt to send data?