-1

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);
}
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Jul 7, 2023 at 17:48
3
  • 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. Commented Jul 8, 2023 at 22:12
  • 1
    If you wish to complain about the way this site is working please raise an issue on our Meta site. Commented 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. Commented Jul 8, 2023 at 22:14

2 Answers 2

1

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.

answered Jul 8, 2023 at 22:18
1

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);
 }
}
answered Apr 13 at 13:18
2
  • OT: boolean == false is a code smell, better use !boolean. Commented 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. Commented Apr 15 at 10:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.