Arduino Helpers master
Utility library for Arduino
Button.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
7
9
15class Button {
16 public:
25
34
36 void begin();
37
42 void invert();
43
45 enum State {
46 Pressed = 0b00,
47 Released = 0b11,
48 Falling = 0b10,
49 Rising = 0b01
50 };
51
79 State update();
80
88 State getState() const;
89
92
94 unsigned long previousBounceTime() const;
95
98 unsigned long stableTime(unsigned long now) const;
99
101 unsigned long stableTime() const;
102
111 static void
113
119 static unsigned long getDebounceTime();
120
121 private:
123
126 : debounced(0b11), bouncing(true), prevInput(HIGH), invert(false),
127 prevBounceTime(0) {}
128 uint8_t debounced : 2;
129 bool bouncing : 1;
130 bool prevInput : 1;
131 bool invert : 1;
132 unsigned long prevBounceTime;
134
137 static unsigned long debounceTime;
138};
139
constexpr PinStatus_t HIGH
std::remove_reference< decltype(*F(""))>::type * FlashString_t
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
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
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:15
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition: Button.cpp:46
static void setDebounceTime(unsigned long debounceTime=BUTTON_DEBOUNCE_TIME)
Set the debounce time for all Buttons.
Definition: Button.cpp:56
struct Button::InternalState state
State update()
Read the button and return its new state.
Definition: Button.cpp:11
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:45
@ Pressed
Input went from low to low (0,0)
Definition: Button.hpp:46
@ Rising
Input went from low to high (0,1)
Definition: Button.hpp:49
@ Falling
Input went from high to low (1,0)
Definition: Button.hpp:48
@ Released
Input went from high to high (1,1)
Definition: Button.hpp:47
void invert()
Invert the input state of this button (button pressed is HIGH instead of LOW).
Definition: Button.cpp:9
static FlashString_t getName(State state)
Return the name of the state as a string.
Definition: Button.cpp:36
State getState() const
Get the state of the button, without updating it.
Definition: Button.cpp:32
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:7
pin_t pin
Definition: Button.hpp:122
static unsigned long getDebounceTime()
Get the debounce time.
Definition: Button.cpp:60
unsigned long stableTime() const
Return the time (in milliseconds) that the button has been stable for.
Definition: Button.cpp:54
Button()
Construct a new Button object.
Definition: Button.hpp:24
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:137
unsigned long prevBounceTime
Definition: Button.hpp:132