Improved Code Readability #88

Open
ElectronicsArchiver wants to merge 9 commits from ElectronicsArchiver/master into master
4 changed files with 259 additions and 155 deletions
+32
View File
@@ -0,0 +1,32 @@
/*
* Check Bounce2.h for licensing / authors.
*/
#include "Bounce2.h"
/*
* Bounce
*/
Bounce::Bounce()
: pin(0) {}
void Bounce::attach(int pin){
this -> pin = pin;
// Set Initial State
begin();
}
void Bounce::attach(int pin,int mode){
setPinMode(pin,mode);
this -> attach(pin);
}
-155
View File
@@ -1,155 +0,0 @@
// Please read Bounce2.h for information about the liscence and authors
#include "Bounce2.h"
//////////////
// DEBOUNCE //
//////////////
Debouncer::Debouncer():previous_millis(0)
, interval_millis(10)
, state(0) {}
void Debouncer::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
void Debouncer::begin() {
state = 0;
if (readCurrentState()) {
setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
bool Debouncer::update()
{
unsetStateFlag(CHANGED_STATE);
#ifdef BOUNCE_LOCK_OUT
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
bool currentState = readCurrentState();
if ( currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
changeState();
}
}
#elif defined BOUNCE_WITH_PROMPT_DETECTION
// Read the state of the switch port into a temporary variable.
bool readState = readCurrentState();
if ( readState != getStateFlag(DEBOUNCED_STATE) ) {
// We have seen a change from the current button state.
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the time threshold, so a new change of state is allowed.
// 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.
changeState();
}
}
// 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 != getStateFlag(UNSTABLE_STATE) ) {
// Update Unstable Bit to macth readState
toggleStateFlag(UNSTABLE_STATE);
previous_millis = millis();
}
#else
// Read the state of the switch in a temporary variable.
bool currentState = readCurrentState();
// If the reading is different from last reading, reset the debounce counter
if ( currentState != getStateFlag(UNSTABLE_STATE) ) {
previous_millis = millis();
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 (currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
changeState();
}
}
#endif
return changed();
}
// WIP HELD
unsigned long Debouncer::previousDuration() const {
return durationOfPreviousState;
}
unsigned long Debouncer::currentDuration() const {
return (millis() - stateChangeLastTime);
}
inline void Debouncer::changeState() {
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE) ;
durationOfPreviousState = millis() - stateChangeLastTime;
stateChangeLastTime = millis();
}
bool Debouncer::read() const
{
return getStateFlag(DEBOUNCED_STATE);
}
bool Debouncer::rose() const
{
return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
}
bool Debouncer::fell() const
{
return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
}
////////////
// BOUNCE //
////////////
Bounce::Bounce()
: pin(0)
{}
void Bounce::attach(int pin) {
this->pin = pin;
// SET INITIAL STATE
begin();
}
void Bounce::attach(int pin, int mode){
setPinMode(pin, mode);
this->attach(pin);
}
+4
View File
@@ -69,11 +69,15 @@ private:
// Note : this is private as it migh change in the futur
private:
inline void updateTime();
inline void changeState();
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) const {return((state & flag) != 0);}
inline bool thresholdPassed() const;
inline bool isDebouncing(const bool state) const;
inline bool isUnstable(const bool state) const;
public:
/*!
+223
View File
@@ -0,0 +1,223 @@
/*
* Check Bounce2.h for licensing / authors.
*/
#include "Bounce2.h"
/*
* Debounce
*/
Debouncer::Debouncer():previous_millis(0)
, interval_millis(10)
, state(0) {}
void Debouncer::interval(uint16_t interval_millis){
this -> interval_millis = interval_millis;
}
void Debouncer::begin(){
state = 0;
if(readCurrentState())
setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
updateTime();
#endif
}
#ifdef BOUNCE_LOCK_OUT
bool Debouncer::update(){
unsetStateFlag(CHANGED_STATE);
// Ignore everything if we are locked out
if(thresholdPassed()){
const bool state = readCurrentState();
if(!isDebouncing(state)){
updateTime();
changeState();
}
}
return changed();
}
#elif defined BOUNCE_WITH_PROMPT_DETECTION
bool Debouncer::update(){
unsetStateFlag(CHANGED_STATE);
/*
* Switch Port State 🠖 Temporary Variable
*/
const bool state = readCurrentState();
// Check if the button state has changed
if(!isDebouncing(state)){
/*
* Enough time has passed
* ⤷ New state changes are allowed
*
* Set Flags:
* - STATE_CHANGED
* - DEBOUNCED_STATE
*/
if(thresholdPassed())
changeState();
}
/*
* If readState ≠ to it's previous state
* ⤷ Reset the debounce timer
*
* This is done, as input is still unstable
* and we want to prevent new button state
* changes until the previous state has
* remained stable for the timeout.
*/
if(!isUnstable(state)){
// Update unstable bit to match readState
toggleStateFlag(UNSTABLE_STATE);
updateTime();
}
return changed();
}
#else
bool Debouncer::update(){
unsetStateFlag(CHANGED_STATE);
/*
* Switch Port State 🠖 Temporary Variable
*/
const bool state = readCurrentState();
/*
* If the reading ≠ to it's previous state
* ⤷ Reset the debounce counter
*/
if(!isUnstable(state)){
updateTime();
toggleStateFlag(UNSTABLE_STATE);
return changed();
}
if(thresholdPassed()){
/*
* We have passed the threshold time
* ⤷ The input is now stable
*
* If it is ≠ to it's previous state
* ⤷ Set the STATE_CHANGED flag
*/
if(!isDebouncing(state)){
updateTime();
changeState();
}
}
return changed();
}
#endif
/*
* Work In Progress
* HELD
*/
unsigned long Debouncer::previousDuration() const {
return durationOfPreviousState;
}
unsigned long Debouncer::currentDuration() const {
return millis() - stateChangeLastTime;
}
inline void Debouncer::changeState(){
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE) ;
durationOfPreviousState = millis() - stateChangeLastTime;
stateChangeLastTime = millis();
}
bool Debouncer::read() const {
return getStateFlag(DEBOUNCED_STATE);
}
bool Debouncer::rose() const {
return getStateFlag(DEBOUNCED_STATE)
&& getStateFlag(CHANGED_STATE);
}
bool Debouncer::fell() const {
return ! getStateFlag(DEBOUNCED_STATE)
&& getStateFlag(CHANGED_STATE);
}
inline bool Debouncer::thresholdPassed() const {
return millis() - previous_millis >= interval_millis;
}
inline void Debouncer::updateTime(){
previous_millis = millis();
}
inline bool Debouncer::isDebouncing(const bool state) const {
return state == getStateFlag(DEBOUNCED_STATE);
}
inline bool Debouncer::isUnstable(const bool state) const {
return state == getStateFlag(UNSTABLE_STATE);
}