Arduino Helpers pin-t-adl
Utility 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
39
41 void begin();
42
47 void invert();
48
50 enum State {
51 Pressed = 0b00,
52 Released = 0b11,
53 Falling = 0b10,
54 Rising = 0b01
55 };
56
84 State update();
85
93 State getState() const;
94
97
99 unsigned long previousBounceTime() const;
100
103 unsigned long stableTime(unsigned long now) const;
104
106 unsigned long stableTime() const;
107
116 static void
118
124 static unsigned long getDebounceTime();
125
126 private:
128
131 : debounced(0b11), bouncing(true), prevInput(HIGH), invert(false),
132 prevBounceTime(0) {}
133 uint8_t debounced : 2;
134 bool bouncing : 1;
135 bool prevInput : 1;
136 bool invert : 1;
137 unsigned long prevBounceTime;
139
142 static unsigned long debounceTime;
143};
144
146
AH::function_traits< decltype(::digitalWrite)>::argument_t< 0 > ArduinoPin_t
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.
#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
Button(ArduinoPin_t pin)
Construct a new Button object.
Definition: Button.hpp:38
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:50
@ Pressed
Input went from low to low (0,0)
Definition: Button.hpp:51
@ Rising
Input went from low to high (0,1)
Definition: Button.hpp:54
@ Falling
Input went from high to low (1,0)
Definition: Button.hpp:53
@ Released
Input went from high to high (1,1)
Definition: Button.hpp:52
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:127
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:142
unsigned long prevBounceTime
Definition: Button.hpp:137
Type for storing pin numbers of Extended Input/Output elements.