Control Surface master
MIDI Control Surface 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:
27 Button() : pin(NO_PIN) {}
28
36 Button(pin_t pin);
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
94 static FlashString_t getName(State state);
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
115 setDebounceTime(unsigned long debounceTime = BUTTON_DEBOUNCE_TIME);
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;
136 } state;
137
140 static unsigned long debounceTime;
141};
142
144
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinStatus_t HIGH
std::remove_reference< decltype(*F(""))>::type * FlashString_t
#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
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:48
pin_t pin
Definition: Button.hpp:125
Button()
Construct a new Button object.
Definition: Button.hpp:27
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:140
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
unsigned long prevBounceTime
Definition: Button.hpp:135