Cleaned up the bit operators and made the class extendable
This commit is contained in:
+35
-32
@@ -7,9 +7,9 @@
|
||||
#endif
|
||||
#include "Bounce2.h"
|
||||
|
||||
#define DEBOUNCED_STATE 0
|
||||
#define UNSTABLE_STATE 1
|
||||
#define STATE_CHANGED 3
|
||||
static const uint8_t DEBOUNCED_STATE = 0b00000001;
|
||||
static const uint8_t UNSTABLE_STATE = 0b00000010;
|
||||
static const uint8_t CHANGED_STATE = 0b00000100;
|
||||
|
||||
|
||||
Bounce::Bounce()
|
||||
@@ -22,8 +22,8 @@ Bounce::Bounce()
|
||||
void Bounce::attach(int pin) {
|
||||
this->pin = pin;
|
||||
state = 0;
|
||||
if (digitalRead(pin)) {
|
||||
state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE);
|
||||
if (readCurrentState()) {
|
||||
setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
|
||||
}
|
||||
#ifdef BOUNCE_LOCK_OUT
|
||||
previous_millis = 0;
|
||||
@@ -48,27 +48,27 @@ void Bounce::interval(uint16_t interval_millis)
|
||||
|
||||
bool Bounce::update()
|
||||
{
|
||||
|
||||
unsetStateFlag(CHANGED_STATE);
|
||||
#ifdef BOUNCE_LOCK_OUT
|
||||
state &= ~_BV(STATE_CHANGED);
|
||||
|
||||
// Ignore everything if we are locked out
|
||||
if (millis() - previous_millis >= interval_millis) {
|
||||
bool currentState = digitalRead(pin);
|
||||
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
|
||||
bool currentState = readCurrentState();
|
||||
if ( currentState != getStateFlag(DEBOUNCED_STATE) ) {
|
||||
previous_millis = millis();
|
||||
state ^= _BV(DEBOUNCED_STATE);
|
||||
state |= _BV(STATE_CHANGED);
|
||||
toggleStateFlag(DEBOUNCED_STATE);
|
||||
setStateFlag(CHANGED_STATE);
|
||||
}
|
||||
}
|
||||
return state & _BV(STATE_CHANGED);
|
||||
|
||||
|
||||
#elif defined BOUNCE_WITH_PROMPT_DETECTION
|
||||
// Read the state of the switch port into a temporary variable.
|
||||
bool readState = digitalRead(pin);
|
||||
bool readState = readCurrentState();
|
||||
|
||||
// Clear Changed State Flag - will be reset if we confirm a button state change.
|
||||
state &= ~_BV(STATE_CHANGED);
|
||||
|
||||
if ( readState != (bool)(state & _BV(DEBOUNCED_STATE))) {
|
||||
if ( readState != getStateFlag(DEBOUNCED_STATE) ) {
|
||||
// We have seen a change from the current button state.
|
||||
|
||||
if ( millis() - previous_millis >= interval_millis ) {
|
||||
@@ -76,55 +76,58 @@ bool Bounce::update()
|
||||
// set the STATE_CHANGED flag and the new DEBOUNCED_STATE.
|
||||
// This will be prompt as long as there has been greater than interval_misllis ms since last change of input.
|
||||
// Otherwise debounced state will not change again until bouncing is stable for the timeout period.
|
||||
state ^= _BV(DEBOUNCED_STATE);
|
||||
state |= _BV(STATE_CHANGED);
|
||||
toggleStateFlag(DEBOUNCED_STATE);
|
||||
setStateFlag(CHANGED_STATE );
|
||||
}
|
||||
}
|
||||
|
||||
// If the readState is different from previous readState, reset the debounce timer - as input is still unstable
|
||||
// and we want to prevent new button state changes until the previous one has remained stable for the timeout.
|
||||
if ( readState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
|
||||
if ( readState != getStateFlag(UNSTABLE_STATE) ) {
|
||||
// Update Unstable Bit to macth readState
|
||||
state ^= _BV(UNSTABLE_STATE);
|
||||
toggleStateFlag(UNSTABLE_STATE);
|
||||
previous_millis = millis();
|
||||
}
|
||||
// return just the sate changed bit
|
||||
return state & _BV(STATE_CHANGED);
|
||||
|
||||
|
||||
#else
|
||||
// Read the state of the switch in a temporary variable.
|
||||
bool currentState = digitalRead(pin);
|
||||
state &= ~_BV(STATE_CHANGED);
|
||||
bool currentState = readCurrentState();
|
||||
|
||||
|
||||
// If the reading is different from last reading, reset the debounce counter
|
||||
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
|
||||
if ( currentState != getStateFlag(UNSTABLE_STATE) ) {
|
||||
previous_millis = millis();
|
||||
state ^= _BV(UNSTABLE_STATE);
|
||||
toggleStateFlag(UNSTABLE_STATE);
|
||||
} else
|
||||
if ( millis() - previous_millis >= interval_millis ) {
|
||||
// We have passed the threshold time, so the input is now stable
|
||||
// If it is different from last state, set the STATE_CHANGED flag
|
||||
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
|
||||
if (currentState != getStateFlag(DEBOUNCED_STATE) ) {
|
||||
previous_millis = millis();
|
||||
state ^= _BV(DEBOUNCED_STATE);
|
||||
state |= _BV(STATE_CHANGED);
|
||||
toggleStateFlag(DEBOUNCED_STATE);
|
||||
setStateFlag(CHANGED_STATE) ;
|
||||
}
|
||||
}
|
||||
|
||||
return state & _BV(STATE_CHANGED);
|
||||
|
||||
#endif
|
||||
|
||||
return getStateFlag(CHANGED_STATE);
|
||||
|
||||
}
|
||||
|
||||
bool Bounce::read()
|
||||
{
|
||||
return state & _BV(DEBOUNCED_STATE);
|
||||
return getStateFlag(DEBOUNCED_STATE);
|
||||
}
|
||||
|
||||
bool Bounce::rose()
|
||||
{
|
||||
return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
|
||||
return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
|
||||
}
|
||||
|
||||
bool Bounce::fell()
|
||||
{
|
||||
return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
|
||||
return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
|
||||
}
|
||||
|
||||
@@ -37,9 +37,11 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
/*
|
||||
#ifndef _BV
|
||||
#define _BV(n) (1<<(n))
|
||||
#endif
|
||||
*/
|
||||
|
||||
class Bounce
|
||||
{
|
||||
@@ -83,6 +85,13 @@ class Bounce
|
||||
uint16_t interval_millis;
|
||||
uint8_t state;
|
||||
uint8_t pin;
|
||||
virtual bool readCurrentState() { return digitalRead(pin); }
|
||||
|
||||
private:
|
||||
inline void setStateFlag(const uint8_t flag) {state |= flag;}
|
||||
inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
|
||||
inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
|
||||
inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user