diff --git a/README.md b/README.md index 0a00707..358a6ae 100644 --- a/README.md +++ b/README.md @@ -187,10 +187,7 @@ void Arduboy2Base::begin() bootLogo(); - // wait for all buttons to be released - do { - delay(50); - } while (buttonsState()); + waitNoButtons(); // wait for all buttons to be released } ``` @@ -213,13 +210,10 @@ For example: Let's say a sketch has its own code to enable, disable and save the // bootLogo(); - // wait for all buttons to be released - do { - delay(50); - } while (arduboy.buttonsState()); +// waitNoButtons(); // wait for all buttons to be released ``` -This saves whatever code *blank()*, *systemButtons()* and *bootLogo()* would use. +This saves whatever code *blank()*, *systemButtons()*, *bootLogo()* and *waitNoButtons()* would use. There are a few functions provided that are roughly equivalent to the standard functions used by *begin()* but which use less code space. diff --git a/keywords.txt b/keywords.txt index a96a6ec..62b347b 100644 --- a/keywords.txt +++ b/keywords.txt @@ -95,6 +95,7 @@ setTextWrap KEYWORD2 SPItransfer KEYWORD2 systemButtons KEYWORD2 toggle KEYWORD2 +waitNoButtons KEYWORD2 width KEYWORD2 writeShowUnitNameFlag KEYWORD2 writeUnitID KEYWORD2 diff --git a/src/Arduboy2.cpp b/src/Arduboy2.cpp index 8497f09..14a56b8 100644 --- a/src/Arduboy2.cpp +++ b/src/Arduboy2.cpp @@ -47,10 +47,7 @@ void Arduboy2Base::begin() // bootLogoSpritesSelfMasked(); // bootLogoSpritesOverwrite(); - // wait for all buttons to be released - do { - delayShort(50); - } while (buttonsState()); + waitNoButtons(); // wait for all buttons to be released } void Arduboy2Base::flashlight() @@ -174,6 +171,13 @@ void Arduboy2Base::bootLogoShell(void (*drawLogo)(int16_t)) // Virtual function overridden by derived class void Arduboy2Base::bootLogoExtra() { } +// wait for all buttons to be released +void Arduboy2Base::waitNoButtons() { + do { + delayShort(50); // simple button debounce + } while (buttonsState()); +} + /* Frame management */ void Arduboy2Base::setFrameRate(uint8_t rate) diff --git a/src/Arduboy2.h b/src/Arduboy2.h index d4e9413..89e1542 100644 --- a/src/Arduboy2.h +++ b/src/Arduboy2.h @@ -334,6 +334,25 @@ class Arduboy2Base : public Arduboy2Core // information after the logo stops scrolling down. virtual void bootLogoExtra(); + /** \brief + * Wait until all buttons have been released. + * + * \details + * This function is called by `begin()` and can be called by a sketch + * after `boot()`. + * + * It won't return unless no buttons are being pressed. A short delay is + * performed each time before testing the state of the buttons to do a + * simple button debounce. + * + * This function is called at the end of `begin()` to make sure no buttons + * used to perform system start up actions are still being pressed, to + * prevent them from erroneously being detected by the sketch code itself. + * + * \see begin() boot() + */ + void waitNoButtons(); + /** \brief * Clear the display buffer. *