I am trying to detect if the digispark device is actually connected to a host (i.e. the USB line is "active") vs. just powered.
The issue: if I send a keypress while the data line is disconnected, the code hangs perpetually in sendKeyPress.
My theory is that it keeps looping in while waiting for USB to be ready, which makes sense.
while (!usbInterruptIsReady()) {
// Note: We wait until we can send keyPress
// so we know the previous keyPress was
// sent.
usbPoll();
_delay_ms(5);
}
I tried to use the macro usbInterruptIsReady() before calling sendKeyStroke in the main, but with no success. I would expect to see a function that keeps track of this state, but apparently there is none.
Also: it doesn't seem like what I am trying to accomplish being extremely weird, so my expectation is that someone else could have stumbled upon this and figured out a reasonable workaround. I would like to avoid setting up a watchdog timer.
Code below for example hangs when the device is powered but not connected to a PC. Thanks
Background: https://hackaday.io/project/191762-automatic-workstation-locker
#include "DigiKeyboard.h"
#include "Arduino.h"
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
DigiKeyboard.update();
DigiKeyboard.sendKeyStroke(0);
}
void loop() {
static uint8_t bit = 0;
bit = 1 - bit;
digitalWrite(LED_BUILTIN, bit);
DigiKeyboard.update();
DigiKeyboard.sendKeyStroke(KEY_Q);
delay(2000);
}