Make state variables used by pollButtons() public

The variables currentButtonState and previousButtonState used by
pollButtons(), justPressed() and justReleased() have been made public.
This allows them to be manipulated if circumstances require it.

The documentation added for previousButtonState includes an example.
This commit is contained in:
Scott Allen 2020-09-27 14:33:00 -04:00
parent 4698dd276e
commit 98f03773b2
2 changed files with 74 additions and 8 deletions

View File

@ -14,14 +14,14 @@ uint8_t Arduboy2Base::sBuffer[];
uint16_t Arduboy2Base::frameCount = 0; uint16_t Arduboy2Base::frameCount = 0;
uint8_t Arduboy2Base::currentButtonState = 0;
uint8_t Arduboy2Base::previousButtonState = 0;
uint8_t Arduboy2Base::eachFrameMillis = 16; uint8_t Arduboy2Base::eachFrameMillis = 16;
uint8_t Arduboy2Base::thisFrameStart; uint8_t Arduboy2Base::thisFrameStart;
uint8_t Arduboy2Base::lastFrameDurationMs; uint8_t Arduboy2Base::lastFrameDurationMs;
bool Arduboy2Base::justRendered = false; bool Arduboy2Base::justRendered = false;
uint8_t Arduboy2Base::currentButtonState = 0;
uint8_t Arduboy2Base::previousButtonState = 0;
// functions called here should be public so users can create their // functions called here should be public so users can create their
// own init functions if they need different behavior than `begin` // own init functions if they need different behavior than `begin`
// provides by default. // provides by default.

View File

@ -1054,7 +1054,7 @@ class Arduboy2Base : public Arduboy2Core
* a frame rate of 60 or lower (or possibly somewhat higher), should be * a frame rate of 60 or lower (or possibly somewhat higher), should be
* sufficient. * sufficient.
* *
* \see justPressed() justReleased() * \see justPressed() justReleased() currentButtonState previousButtonState
*/ */
static void pollButtons(); static void pollButtons();
@ -1376,6 +1376,76 @@ class Arduboy2Base : public Arduboy2Core
*/ */
static uint16_t frameCount; static uint16_t frameCount;
/** \brief
* Used by `pollButtons()` to hold the current button state.
*
* \details
* Holds the last button state read by the `pollButtons()` function.
*
* A sketch normally does not need to read or manipulate this variable and
* just lets `pollButtons()` handle it. Access to it is provided for special
* circumstances. See `previousButtonState` for further discussion.
*
* \see previousButtonState pollButtons() justPressed() justReleased()
*/
static uint8_t currentButtonState;
/** \brief
* Used by `pollButtons()` to hold the previous button state.
*
* \details
* Holds the button state saved by the `pollButtons()` function from the
* previous to last call to it.
*
* A sketch normally does not need to read or manipulate this variable and
* just lets `pollButtons()` handle it. Access to it is provided for special
* circumstances.
*
* For example, the time between calls to `pollButtons()` must be long
* enough to allow sufficient time to "debounce" the buttons.
* `pollButtons()` is normally called once every frame but at a high frame
* rate the time between frames may be too short for this. Calling
* `pollButtons()` every 2nd frame could provide a long enough time but
* then a call to `justPressed()` in each frame would make it look like a
* button was pressed twice. To remedy this, after `justPressed()` detects
* a press, `previousButtonState` could be modified to acknowledge the
* button press.
*
* \code{.cpp}
* void setup() {
* arduboy.begin();
* arduboy.setFrameRate(120); // too fast for button debounce
* }
*
* void loop() {
* if (!arduboy.nextFrame()) {
* return;
* }
*
* if (arduboy.everyXFrames(2)) { // only poll every 2nd frame
* arduboy.pollButtons(); // to slow down poll frequency
* }
*
* if (justPressedOnce(A_BUTTON)) {
* // handle button press as normal...
* }
*
* // remainder of loop() code...
* }
*
* bool justPressedOnce(uint8_t button) {
* bool pressed = arduboy.justPressed(button);
* if (pressed) {
* arduboy.previousButtonState |= button; // set state as pressed
* }
* return pressed;
* }
* \endcode
*
* \see currentButtonState pollButtons() justPressed() justReleased()
*/
static uint8_t previousButtonState;
/** \brief /** \brief
* The display buffer array in RAM. * The display buffer array in RAM.
* *
@ -1440,10 +1510,6 @@ class Arduboy2Base : public Arduboy2Core
// swap the values of two int16_t variables passed by reference // swap the values of two int16_t variables passed by reference
static void swapInt16(int16_t& a, int16_t& b); static void swapInt16(int16_t& a, int16_t& b);
// For button handling
static uint8_t currentButtonState;
static uint8_t previousButtonState;
// For frame functions // For frame functions
static uint8_t eachFrameMillis; static uint8_t eachFrameMillis;
static uint8_t thisFrameStart; static uint8_t thisFrameStart;