Arduino Filters master
Filter library for Arduino
Button.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
10
12
18class Button {
19 public:
28
37
39 void begin();
40
45 void invert();
46
48 enum State {
49 Pressed = 0b00,
50 Released = 0b11,
51 Falling = 0b10,
52 Rising = 0b01
53 };
54
82 State update();
83
91 State getState() const;
92
95
97 unsigned long previousBounceTime() const;
98
101 unsigned long stableTime(unsigned long now) const;
102
104 unsigned long stableTime() const;
105
114 static void
116
122 static unsigned long getDebounceTime();
123
124 private:
126
129 : debounced(0b11), bouncing(true), prevInput(HIGH), invert(false),
130 prevBounceTime(0) {}
131 uint8_t debounced : 2;
132 bool bouncing : 1;
133 bool prevInput : 1;
134 bool invert : 1;
135 unsigned long prevBounceTime;
137
140 static unsigned long debounceTime;
141};
142
144
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
#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
Button()
Construct a new Button object.
Definition: Button.hpp:27
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:140
unsigned long prevBounceTime
Definition: Button.hpp:135