Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
MultiPurposeButton.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
5#include "Button.hpp"
6
8
13 public:
14 MultiPurposeButton(pin_t pin) : button(pin) {}
15
16 enum Event {
22 MultiPress,
24 MultiPressDone,
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: {
38 if (multiPressCountNew > 0 && stableTime > multiPressDelay) {
39 multiPressCount = multiPressCountNew;
40 multiPressCountNew = 0;
41 event = MultiPressDone;
42 }
43 } break;
44 case Button::Falling: {
45 event = (stableTime <= multiPressDelay) ? MultiPress //
46 : PressStart;
47 multiPressCountNew += event == MultiPress;
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: {
56 event = longPress ? LongPressRelease : ShortPressRelease;
57 longPress = false;
58 } break;
59 }
60 return event;
61 }
62
65 uint8_t getMultiPressCount() const { return multiPressCount; }
69 uint8_t getCurrentMultiPressCount() const { return multiPressCountNew; }
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 {
89 return button.previousBounceTime();
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
114 Button::State getButtonState() const { return button.getState(); }
116 void invert() { button.invert(); }
117
118 protected:
120 unsigned long longPressDelay = 1000;
121 unsigned long multiPressDelay = 400;
122 bool longPress = false;
123 uint8_t multiPressCountNew = 0;
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
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
std::remove_reference< decltype(*F(""))>::type * FlashString_t
@ None
No buffers available.
A class for reading and debouncing buttons and switches.
Definition Button.hpp:15
State
An enumeration of the different states a button can be in.
Definition Button.hpp:47
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.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
void begin() override
Initialize the shift register.
Type for storing pin numbers of Extended Input/Output elements.