Arduino Filters master
Filter library for Arduino
MultiPurposeButton.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include "Button.hpp"
9
11
16 public:
18
19 enum Event {
29 };
30
32 void begin() { button.begin(); }
33
36 Event event = None;
37 auto now = millis();
38 auto stableTime = button.stableTime(now);
39 switch (button.update()) {
40 case Button::Released: {
44 event = MultiPressDone;
45 }
46 } break;
47 case Button::Falling: {
48 event = (stableTime <= multiPressDelay) ? MultiPress //
49 : PressStart;
51 } break;
52 case Button::Pressed: {
53 if (not longPress && stableTime >= longPressDelay) {
54 event = LongPress;
55 longPress = true;
56 }
57 } break;
58 case Button::Rising: {
60 longPress = false;
61 } break;
62 }
63 return event;
64 }
65
68 uint8_t getMultiPressCount() const { return multiPressCount; }
73
76 unsigned long getLongPressDelay() const { return longPressDelay; }
79 void setLongPressDelay(unsigned long longPressDelay) {
80 this->longPressDelay = longPressDelay;
81 }
82
84 unsigned long getMultiPressDelay() const { return multiPressDelay; }
86 void setMultiPressDelay(unsigned long multiPressDelay) {
87 this->multiPressDelay = multiPressDelay;
88 }
89
91 unsigned long previousBounceTime() const {
93 }
95 unsigned long stableTime() const { return button.stableTime(); }
97 unsigned long stableTime(unsigned long now) const {
98 return button.stableTime(now);
99 }
100
103 unsigned long getPressedTime() const {
104 return getButtonState() == Button::Pressed ? stableTime() : 0;
105 }
109 unsigned long getLongPressedTime() const {
110 return longPress ? stableTime() : 0;
111 }
112
114 static FlashString_t getName(Event ev) { return to_string(ev); }
115
119 void invert() { button.invert(); }
120
121 protected:
123 unsigned long longPressDelay = 1000;
124 unsigned long multiPressDelay = 400;
125 bool longPress = false;
127 uint8_t multiPressCount = 0;
128
129 public:
131 switch (ev) {
132 case None: return F("None");
133 case PressStart: return F("PressStart");
134 case ShortPressRelease: return F("ShortPressRelease");
135 case LongPress: return F("LongPress");
136 case LongPressRelease: return F("LongPressRelease");
137 case MultiPress: return F("MultiPress");
138 case MultiPressDone: return F("MultiPressDone");
139 }
140 return F("<invalid>");
141 }
142};
143
145
std::remove_reference< decltype(*F(""))>::type * FlashString_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#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 stableTime(unsigned long now) const
Return the time (in milliseconds) that the button has been stable for, compared to the given time poi...
Definition: Button.cpp:52
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition: Button.cpp:48
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
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
Class for detecting short/long button presses and double clicks.
Button::State getButtonState() const
unsigned long stableTime(unsigned long now) const
unsigned long previousBounceTime() const
unsigned long getPressedTime() const
Get the number of milliseconds the button has been pressed for.
Event update()
Read the button state and return the Event (if any).
unsigned long getLongPressedTime() const
Get the number of milliseconds the button has been pressed for.
unsigned long multiPressDelay
unsigned long getMultiPressDelay() const
Get the number of milliseconds between multipresses.
friend FlashString_t to_string(Event ev)
@ PressStart
The button was just pressed.
@ LongPressRelease
The button was released after a long press.
@ None
Nothing changed.
@ ShortPressRelease
The button was released after a short press.
@ MultiPressDone
The button has been released for long enough to rule out another MultiPress.
@ LongPress
The button has been pressed for some time.
@ MultiPress
The button was pressed in quick succession of the previous release.
void setLongPressDelay(unsigned long longPressDelay)
Set the number of milliseconds after which a press is considered a long press.
static FlashString_t getName(Event ev)
Return the name of the event as a string.
uint8_t getMultiPressCount() const
Get the number of times the button was pressed in quick succession (after MultiPressDone),...
unsigned long getLongPressDelay() const
Get the number of milliseconds after which a press is considered a long press.
unsigned long stableTime() const
uint8_t getCurrentMultiPressCount() const
Get the number of times the button was pressed in quick succession, while the button is being pressed...
unsigned long longPressDelay
void setMultiPressDelay(unsigned long multiPressDelay)
Set the number of milliseconds between multipresses.