Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Button.cpp
Go to the documentation of this file.
1#include "Button.hpp"
2
4
5Button::Button(pin_t pin) : pin(pin) {}
6
8
9void Button::invert() { state.invert = true; }
10
12 // Read pin state and current time
13 bool input = ExtIO::digitalRead(pin) ^ state.invert;
14 unsigned long now = millis();
15 // Check if enough time has elapsed after last bounce
16 if (state.bouncing)
17 state.bouncing = now - state.prevBounceTime <= debounceTime;
18 // Shift the debounced state one bit to the left, either appending the
19 // new input state if not bouncing, or repeat the old state if bouncing
20 bool prevState = state.debounced & 0b01;
21 bool newState = state.bouncing ? prevState : input;
22 state.debounced = (prevState << 1) | newState;
23 // Check if the input changed state (button pressed, released or bouncing)
24 if (input != state.prevInput) {
25 state.bouncing = true;
26 state.prevInput = input;
27 state.prevBounceTime = now;
28 }
29 return getState();
30}
31
33 return static_cast<State>(state.debounced);
34}
35
37 switch (state) {
38 case Button::Pressed: return F("Pressed");
39 case Button::Released: return F("Released");
40 case Button::Falling: return F("Falling");
41 case Button::Rising: return F("Rising");
42 default: return F("<invalid>"); // Keeps the compiler happy
43 }
44}
45
46unsigned long Button::previousBounceTime() const {
47 return state.prevBounceTime;
48}
49
50unsigned long Button::stableTime(unsigned long now) const {
51 return now - previousBounceTime();
52}
53
54unsigned long Button::stableTime() const { return stableTime(millis()); }
55
56void Button::setDebounceTime(unsigned long debounceTime) {
58}
59
61
63
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinMode_t INPUT_PULLUP
std::remove_reference< decltype(*F(""))>::type * FlashString_t
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
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
struct AH::Button::InternalState state
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
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition Button.hpp:137
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.
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).
An array wrapper for easy copying, comparing, and iterating.
Definition Array.hpp:32