Bounce2.h
/*
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Main code by Thomas O Fredericks (tof@t-o-f.info)
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef Bounce2_h
#define Bounce2_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Uncomment the following line for "LOCK-OUT" debounce method
//#define BOUNCE_LOCK_OUT
// Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
//#define BOUNCE_WITH_PROMPT_DETECTION
#include <inttypes.h>
class Debouncer
{
// Note : this is private as it migh change in the futur
private:
static const uint8_t DEBOUNCED_STATE = 0b00000001; // Final returned calculated debounced state
static const uint8_t UNSTABLE_STATE = 0b00000010; // Actual last state value behind the scene
static const uint8_t CHANGED_STATE = 0b00000100; // The DEBOUNCED_STATE has changed since last update()
// Note : this is private as it migh change in the futur
private:
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);}
public:
Debouncer();
void interval(uint16_t interval_millis);
bool update();
bool read() const;
bool fell() const;
bool rose() const;
public:
bool changed( ) const { return getStateFlag(CHANGED_STATE); }
unsigned long currentDuration() const;
unsigned long previousDuration() const;
[[deprecated]]
unsigned long duration() {
return currentDuration();
};
protected:
void begin();
virtual bool readCurrentState() =0;
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
unsigned long stateChangeLastTime;
unsigned long durationOfPreviousState;
};
class Bounce : public Debouncer
{
public:
Bounce();
void attach(int pin, int mode);
void attach(int pin);
Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
attach(pin);
interval(interval_millis);
}
inline int getPin() const {
return this->pin;
};
// Deprecated //
[[deprecated]]
bool risingEdge() const { return rose(); }
[[deprecated]]
bool fallingEdge() const { return fell(); }
protected:
uint8_t pin;
virtual bool readCurrentState() { return digitalRead(pin); }
virtual void setPinMode(int pin, int mode) {
#if defined(ARDUINO_ARCH_STM32F1)
pinMode(pin, (WiringPinMode)mode);
#else
pinMode(pin, mode);
#endif
}
};
namespace Bounce2 {
// code declarations
class Button : public Bounce{
protected:
bool stateForPressed = 1; //
public:
Button(){ }
void setPressedState(bool state){
stateForPressed = state;
}
inline bool getPressedState() const {
return stateForPressed;
};
inline bool isPressed() const {
return read() == getPressedState();
};
inline bool pressed() const {
return changed() && isPressed();
};
inline bool released() const {
return changed() && !isPressed();
};
};
};
#endif
Bounce2::Button
Definition: Bounce2.h:276
Debouncer::interval
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:14
Debouncer::previousDuration
unsigned long previousDuration() const
Returns the duration in milliseconds of the previous state.
Definition: Bounce2.cpp:103
Bounce2::Button::getPressedState
bool getPressedState() const
Get the electrical state (HIGH/LOW) that corresponds to a physical press.
Definition: Bounce2.h:306
Bounce::Bounce
Bounce()
Create an instance of the Bounce class.
Definition: Bounce2.cpp:138
Debouncer::duration
unsigned long duration()
DEPRECATED (i.e. do not use). Renamed currentDuration().
Definition: Bounce2.h:164
Bounce::fallingEdge
bool fallingEdge() const
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:246
Debouncer::rose
bool rose() const
Returns true if pin signal transitions from low to high.
Definition: Bounce2.cpp:123
Bounce::pin
uint8_t pin
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:255
Bounce2::Button::pressed
bool pressed() const
Returns true if the button was physically pressed
Definition: Bounce2.h:320
Debouncer::currentDuration
unsigned long currentDuration() const
Returns the duration in milliseconds of the current state.
Definition: Bounce2.cpp:107
Debouncer::update
bool update()
Updates the pin's state.
Definition: Bounce2.cpp:32
Bounce2::Button::Button
Button()
Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical...
Definition: Bounce2.h:290
Bounce
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...
Definition: Bounce2.h:184
Bounce::getPin
int getPin() const
Return pin that this Bounce is attached to.
Definition: Bounce2.h:229
Debouncer
The Debouce class. Just the deboucing code separated from all harware.
Definition: Bounce2.h:62
Bounce2::Button::setPressedState
void setPressedState(bool state)
Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default,...
Definition: Bounce2.h:299
Debouncer::fell
bool fell() const
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:128
Bounce2::Button::released
bool released() const
Returns true if the button was physically released
Definition: Bounce2.h:327
Bounce::attach
void attach(int pin, int mode)
Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
Definition: Bounce2.cpp:149
Bounce2::Button::isPressed
bool isPressed() const
Returns true if the button is currently physically pressed.
Definition: Bounce2.h:313
Debouncer::read
bool read() const
Returns the pin's state (HIGH or LOW).
Definition: Bounce2.cpp:118
Bounce::risingEdge
bool risingEdge() const
Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce ver...
Definition: Bounce2.h:241
Debouncer::changed
bool changed() const
Returns true if the state changed on last update.
Definition: Bounce2.h:130
Bounce2
The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action.
Definition: Bounce2.h:273
Debouncer::Debouncer
Debouncer()
Create an instance of the Debounce class.
Definition: Bounce2.cpp:10