Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General bug: initialization and print issues when first connects to USB #20

Open
ElectricRCAircraftGuy opened this issue Dec 21, 2016 · 1 comment

Comments

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Dec 21, 2016

There is some sort of initialization bug preventing prints, or reading the LED states, or anything like it, from properly working when you first connect to USB.

Related issue: #12

Here is some sample code to duplicate the issue:

#include <TrinketHidCombo.h>
#include <TrinketHidComboC.h>

void setup()
{
  TrinketHidCombo.begin();  
  
  TrinketHidCompatibleDelay(0); //increase to 2000 to remove the print errors entirely; decrease to 0 to make the errors worse and more frequent; use 1000 to cause occasional initial print errors only
  
  //wait until connected to USB before continuing
  //-note that isConnected() will change to true at that point 
  unsigned long counter = 0;
  while (TrinketHidCombo.isConnected() == false)
  {
    counter++;
    TrinketHidCombo.poll(); //The poll function must be called at least once every 10 ms, or you must command a keystroke. Otherwise, the computer may think that the device has stopped working, and give errors.
  }
  
  TrinketHidCombo.print(F("counter = ")); TrinketHidCombo.print(counter); //DOESN'T PRINT QUITE RIGHT!
  TrinketHidCombo.print(F("; t_ms = ")); TrinketHidCombo.println(millis());  
}

void loop()
{

}

//--------------------------------------------------------------------------------------------
//TrinketHidCompatibleDelay
//-this is a USB-compatible delay, so that the 10ms max time between poll calls is not exceeded
//--------------------------------------------------------------------------------------------
void TrinketHidCompatibleDelay(unsigned int desiredDelay_ms)
{
  unsigned long t_start = millis(); //ms
  while (millis()-t_start<desiredDelay_ms)
  {
    TrinketHidCombo.poll(); //keep the HID USB connection to the computer alive 
  }
}

Sample output, when printing into Notepad (NOT Notepad++, whose autocomplete messes with the output).

Using TrinketHidCompatibleDelay(0) at the beginning of setup():
(notice how messed up it is)
-note: each line below is after a new power reset of the microcontroller (unplug it from USB, then plug it back in)

unter = 41584; t_ms = 596
unter = 40789; t_ms = 590
unter = 42898; t_ms = 601
er = 42847; t_ms = 600
unter = 41137; t_ms = 594
nter = 40832; t_ms = 587
ounter = 41924; t_ms = 599
ter = 180498; t_ms = 1224
nter = 179296; t_ms = 1211
er = 41012; t_ms = 590
nter = 42755; t_ms = 602
er = 182202; t_ms = 1224

Using TrinketHidCompatibleDelay(1000) at the beginning of setup():
(only one erroneous print now)
-note: each line below is after a new power reset of the microcontroller (unplug it from USB, then plug it back in)

counter = 0; t_ms = 1307
counter = 0; t_ms = 1305
counter = 0; t_ms = 1311
counter = 0; t_ms = 1308
counter = 0; t_ms = 1308
counter = 0; t_ms = 1309
counter = 0; t_ms = 1308
ter = 70173; t_ms = 1719
counter = 0; t_ms = 1307
counter = 0; t_ms = 1307
counter = 0; t_ms = 1308
counter = 0; t_ms = 1310
counter = 0; t_ms = 1303
counter = 0; t_ms = 1302
counter = 0; t_ms = 1309
counter = 0; t_ms = 1303
counter = 0; t_ms = 1304
counter = 0; t_ms = 1304
counter = 0; t_ms = 1303
counter = 0; t_ms = 1308
counter = 0; t_ms = 1306
counter = 0; t_ms = 1303
counter = 0; t_ms = 1308
counter = 0; t_ms = 1306
counter = 0; t_ms = 1308
counter = 0; t_ms = 1307

Using TrinketHidCompatibleDelay(2000) at the beginning of setup() pretty much guarantees a successful startup with good prints.

Here's some of its outputs:

counter = 0; t_ms = 2306
counter = 0; t_ms = 2306
counter = 0; t_ms = 2309
counter = 0; t_ms = 2311
counter = 0; t_ms = 2308
counter = 0; t_ms = 2309

Why? Is there a better way? Is this a bug somewhere in the USB HID code?

@comp500
Copy link

comp500 commented Sep 6, 2018

Thank you so much for this TrinketHidCompatibleDelay function. It completely fixed the connection issues I was having with TrinketKeyboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants