Control Surface pin-t-adl
MIDI Control Surface 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:
17 MultiPurposeButton(pin_t pin) : button(pin) {}
18
19 enum Event {
25 MultiPress,
27 MultiPressDone,
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: {
41 if (multiPressCountNew > 0 && stableTime > multiPressDelay) {
42 multiPressCount = multiPressCountNew;
43 multiPressCountNew = 0;
44 event = MultiPressDone;
45 }
46 } break;
47 case Button::Falling: {
48 event = (stableTime <= multiPressDelay) ? MultiPress //
49 : PressStart;
50 multiPressCountNew += event == MultiPress;
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: {
59 event = longPress ? LongPressRelease : ShortPressRelease;
60 longPress = false;
61 } break;
62 }
63 return event;
64 }
65
68 uint8_t getMultiPressCount() const { return multiPressCount; }
72 uint8_t getCurrentMultiPressCount() const { return multiPressCountNew; }
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 {
92 return button.previousBounceTime();
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
117 Button::State getButtonState() const { return button.getState(); }
119 void invert() { button.invert(); }
120
121 protected:
123 unsigned long longPressDelay = 1000;
124 unsigned long multiPressDelay = 400;
125 bool longPress = false;
126 uint8_t multiPressCountNew = 0;
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
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
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:50
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 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.
@ ShortPressRelease
The button was released after a short press.
@ LongPress
The button has been pressed for some time.
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...
void setMultiPressDelay(unsigned long multiPressDelay)
Set the number of milliseconds between multipresses.
Type for storing pin numbers of Extended Input/Output elements.