Arduino Helpers master
Utility library for Arduino
MultiPurposeButton.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
5#include "Button.hpp"
6
8
13 public:
15
16 enum Event {
26 };
27
29 void begin() { button.begin(); }
30
33 Event event = None;
34 auto now = millis();
35 auto stableTime = button.stableTime(now);
36 switch (button.update()) {
37 case Button::Released: {
41 event = MultiPressDone;
42 }
43 } break;
44 case Button::Falling: {
45 event = (stableTime <= multiPressDelay) ? MultiPress //
46 : PressStart;
48 } break;
49 case Button::Pressed: {
50 if (not longPress && stableTime >= longPressDelay) {
51 event = LongPress;
52 longPress = true;
53 }
54 } break;
55 case Button::Rising: {
57 longPress = false;
58 } break;
59 }
60 return event;
61 }
62
65 uint8_t getMultiPressCount() const { return multiPressCount; }
70
73 unsigned long getLongPressDelay() const { return longPressDelay; }
76 void setLongPressDelay(unsigned long longPressDelay) {
77 this->longPressDelay = longPressDelay;
78 }
79
81 unsigned long getMultiPressDelay() const { return multiPressDelay; }
83 void setMultiPressDelay(unsigned long multiPressDelay) {
84 this->multiPressDelay = multiPressDelay;
85 }
86
88 unsigned long previousBounceTime() const {
90 }
92 unsigned long stableTime() const { return button.stableTime(); }
94 unsigned long stableTime(unsigned long now) const {
95 return button.stableTime(now);
96 }
97
100 unsigned long getPressedTime() const {
101 return getButtonState() == Button::Pressed ? stableTime() : 0;
102 }
106 unsigned long getLongPressedTime() const {
107 return longPress ? stableTime() : 0;
108 }
109
111 static FlashString_t getName(Event ev) { return to_string(ev); }
112
116 void invert() { button.invert(); }
117
118 protected:
120 unsigned long longPressDelay = 1000;
121 unsigned long multiPressDelay = 400;
122 bool longPress = false;
124 uint8_t multiPressCount = 0;
125
126 public:
128 switch (ev) {
129 case None: return F("None");
130 case PressStart: return F("PressStart");
131 case ShortPressRelease: return F("ShortPressRelease");
132 case LongPress: return F("LongPress");
133 case LongPressRelease: return F("LongPressRelease");
134 case MultiPress: return F("MultiPress");
135 case MultiPressDone: return F("MultiPressDone");
136 }
137 return F("<invalid>");
138 }
139};
140
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
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:15
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:50
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition: Button.cpp:46
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
void invert()
Invert the input state of this button (button pressed is HIGH instead of LOW).
Definition: Button.cpp:9
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
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.