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);
}
-
I've deleted all the comments which had descended into sledging. Please remember, comments under the question are for clarifying it, not attacking each other.Nick Gammon– Nick Gammon ♦07/08/2023 22:12:20Commented Jul 8, 2023 at 22:12
-
1If you wish to complain about the way this site is working please raise an issue on our Meta site.Nick Gammon– Nick Gammon ♦07/08/2023 22:13:20Commented Jul 8, 2023 at 22:13
-
The question looks reasonable to me, however @Paperino you should not keep re-posting the same question. Try to salvage the original one where possible. As it is now deleted I suggest we run with this one.Nick Gammon– Nick Gammon ♦07/08/2023 22:14:20Commented Jul 8, 2023 at 22:14
2 Answers 2
The Digispark does not have a proper hardware USB implementation. It is possible that you simply can't do what you are attempting to do. A search I did indicated you are not the only person attempting to do this.
You may want to switch to one of the Arduinos that have a hardware USB interface like the Leonardo or Micro.
I should point out that the Digispark is not an Arduino, and it has its own forum where you may possibly get an answer from their own community.
Might be a bit late but I also had the same need for a project and managed to get it working with the usbInterruptIsReady()
macro. That macro checks a global value somewhere that is used by other things, so you need to poll it for some time before it gives a valid result.
Here's a simple sketch that seems to work reliably and detects if a host is connected to USB:
// Include the Keyboard library regardless of USB availability.
#include "DigiKeyboard.h"
// Variable to store the state of USB connectivity.
bool is_usb = false;
void setup()
{
// Start counting time.
uint32_t start = millis();
// Wait until 1000ms have passed or USB host has been detected.
// I have tested with less than 1000ms timeout but it was unreliable.
while (((millis() - start) <= 1000) && !is_usb)
{
// Check USB availability.
is_usb = usbInterruptIsReady();
// Use the Keyboard library delay function to keep calling usbPoll().
DigiKeyboard.delay(5);
}
// For all Keyboard calls, always check first if the host is connected,
// otherwise the connection might fail and you'll get in one of the many
// infinite loops in the Digispark libraries.
if (is_usb)
{
// Send the "clear" key stroke in setup() instead of loop() to allow
// better keyboard control.
DigiKeyboard.sendKeyStroke(0);
}
}
-
OT:
boolean == false
is a code smell, better use!boolean
.the busybee– the busybee04/14/2025 05:29:39Commented Apr 14 at 5:29 -
Calling that specific thing a code smell is wrong. The real issue is I didn't consistently do an explicit comparison. I will edit the answer to be more consistent.ÉmileBV– ÉmileBV04/15/2025 10:55:42Commented Apr 15 at 10:55