Author Topic: Detecting when DigiKeyboard is available?  (Read 2632 times)

captain sparky

  • Newbie
  • *
  • Posts: 1
Detecting when DigiKeyboard is available?
« on: October 08, 2014, 01:51:40 pm »
I'm new to digispark, and I'm just playing around with the basics... and running into trouble already.

I want to make a single sketch which does something different whether or not it's plugged into a computer.  How do I test whether DigiKeyboard has initialised successfully?
Code: [Select]
#include "DigiKeyboard.h"
int ledPin =  1;
void setup() {               
  pinMode(ledPin, OUTPUT);
}
void loop() {
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(1000);
  DigiKeyboard.update();
  DigiKeyboard.sendKeyStroke(0);
  DigiKeyboard.print("\ntest");
}
This works fine when plugged into my computer, but when plugged into a (disconnected) powered hub, or just a usb power adapter, it blinks once and stops - obviously because the DigiKeyboard. functions are failing.

Is there a way to test whether DigiKeyboard has initialised successfully and is available?

I've searched, both the forum, and google, and spent several hours reading everything I can find about DigiKeyboard without finding an answer.  But hopefully it's something simple I've overlooked.
« Last Edit: October 08, 2014, 01:56:32 pm by captain sparky »

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Detecting when DigiKeyboard is available?
« Reply #1 on: October 20, 2014, 06:32:15 am »
I was hoping somebody would answer this, because I got curious too, after you asked.

I tried poking around in the source code for the DigiKeyboard and USB libraries, but I'm not familiar enough with how things fit together to unravel it, myself. It did kind of look like there might be a variable in the USB code that would reflect the availability status, but I wasn't positive.