Arduino Filters master
Filter library for Arduino
Button.cpp
Go to the documentation of this file.
1#include "Button.hpp"
2
3AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
4
6
7Button::Button(pin_t pin) : pin(pin) {}
8
10
11void Button::invert() { state.invert = true; }
12
14 // Read pin state and current time
15 bool input = ExtIO::digitalRead(pin) ^ state.invert;
16 unsigned long now = millis();
17 // Check if enough time has elapsed after last bounce
18 if (state.bouncing)
20 // Shift the debounced state one bit to the left, either appending the
21 // new input state if not bouncing, or repeat the old state if bouncing
22 bool prevState = state.debounced & 0b01;
23 bool newState = state.bouncing ? prevState : input;
24 state.debounced = (prevState << 1) | newState;
25 // Check if the input changed state (button pressed, released or bouncing)
26 if (input != state.prevInput) {
27 state.bouncing = true;
28 state.prevInput = input;
30 }
31 return getState();
32}
33
35 return static_cast<State>(state.debounced);
36}
37
39 switch (state) {
40 case Button::Pressed: return F("Pressed");
41 case Button::Released: return F("Released");
42 case Button::Falling: return F("Falling");
43 case Button::Rising: return F("Rising");
44 default: return F("<invalid>"); // Keeps the compiler happy
45 }
46}
47
48unsigned long Button::previousBounceTime() const {
49 return state.prevBounceTime;
50}
51
52unsigned long Button::stableTime(unsigned long now) const {
53 return now - previousBounceTime();
54}
55
56unsigned long Button::stableTime() const { return stableTime(millis()); }
57
58void Button::setDebounceTime(unsigned long debounceTime) {
60}
61
63
65
67
constexpr PinMode_t INPUT_PULLUP
std::remove_reference< decltype(*F(""))>::type * FlashString_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: Settings.hpp:76
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition: Button.cpp:48
static void setDebounceTime(unsigned long debounceTime=BUTTON_DEBOUNCE_TIME)
Set the debounce time for all Buttons.
Definition: Button.cpp:58
struct Button::InternalState state
State update()
Read the button and return its new state.
Definition: Button.cpp:13
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:48
@ Pressed
Input went from low to low (0,0)
Definition: Button.hpp:49
@ Rising
Input went from low to high (0,1)
Definition: Button.hpp:52
@ Falling
Input went from high to low (1,0)
Definition: Button.hpp:51
@ Released
Input went from high to high (1,1)
Definition: Button.hpp:50
void invert()
Invert the input state of this button (button pressed is HIGH instead of LOW).
Definition: Button.cpp:11
static FlashString_t getName(State state)
Return the name of the state as a string.
Definition: Button.cpp:38
State getState() const
Get the state of the button, without updating it.
Definition: Button.cpp:34
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:9
pin_t pin
Definition: Button.hpp:125
static unsigned long getDebounceTime()
Get the debounce time.
Definition: Button.cpp:62
unsigned long stableTime() const
Return the time (in milliseconds) that the button has been stable for.
Definition: Button.cpp:56
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:140
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
unsigned long prevBounceTime
Definition: Button.hpp:135