-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,184
  • 13
  • 65
  • 124
Paperino
  • 99
  • 2
  • 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 Jul 08 '23 at 22:12
  • 1
    If you wish to complain about the way this site is working please raise an issue on our Meta site. – Nick Gammon Jul 08 '23 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 Jul 08 '23 at 22:14

1 Answers1

0

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.

Nick Gammon
  • 38,184
  • 13
  • 65
  • 124