Control Surface main
MIDI Control Surface library for Arduino
IncrementDecrementButtons.cpp
Go to the documentation of this file.
2
3AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
4
6
7IncrementDecrementButtons::State
8IncrementDecrementButtons::updateImplementation() {
9 Button::State incrState = incrementButton.update();
10 Button::State decrState = decrementButton.update();
11
12 if (decrState == Button::Released && incrState == Button::Released) {
13 // Both released
14 } else if ((decrState == Button::Rising && incrState == Button::Released) ||
15 (incrState == Button::Rising && decrState == Button::Released) ||
16 (incrState == Button::Rising && decrState == Button::Rising)) {
17 // One released, the other rising → nothing
18 // now both released, so go to initial state
19 longPressState = Initial;
20 } else if (incrState == Button::Falling && decrState == Button::Falling) {
21 // Both falling → reset
22 // (rather unlikely, but just in case)
23 longPressState = AfterReset;
24 return Reset;
25 } else if (incrState == Button::Falling) {
26 if (decrState == Button::Pressed) {
27 // One pressed, the other falling → reset
28 longPressState = AfterReset;
29 return Reset;
30 } else {
31 // Increment falling, the other released → increment
32 return IncrementShort;
33 }
34 } else if (decrState == Button::Falling) {
35 if (incrState == Button::Pressed) {
36 // One pressed, the other falling → reset
37 longPressState = AfterReset;
38 return Reset;
39 } else {
40 // Decrement falling, the other released → decrement
41 return DecrementShort;
42 }
43 } else if (incrState == Button::Pressed && decrState == Button::Pressed) {
44 // Both pressed → nothing
45 } else if (longPressState != AfterReset && incrState == Button::Pressed) {
46 // Not reset and increment pressed → long press?
47 auto now = millis();
48 if (longPressState == LongPress) {
49 if (now - longPressRepeat >= LONG_PRESS_REPEAT_DELAY) {
50 longPressRepeat += LONG_PRESS_REPEAT_DELAY;
51 return IncrementHold;
52 }
53 } else if (incrementButton.stableTime() >= LONG_PRESS_DELAY) {
54 longPressState = LongPress;
55 longPressRepeat = now;
56 return IncrementLong;
57 }
58 } else if (longPressState != AfterReset && decrState == Button::Pressed) {
59 // Not reset and decrement pressed → long press?
60 auto now = millis();
61 if (longPressState == LongPress) {
62 if (now - longPressRepeat >= LONG_PRESS_REPEAT_DELAY) {
63 longPressRepeat += LONG_PRESS_REPEAT_DELAY;
64 return DecrementHold;
65 }
66 } else if (decrementButton.stableTime() >= LONG_PRESS_DELAY) {
67 longPressState = LongPress;
68 longPressRepeat = now;
69 return DecrementLong;
70 }
71 }
72 return Nothing;
73}
74
76
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:36
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:48
A class for buttons that increment and decrement some counter or setting.
State update()
Update and return the state of the increment/decrement button.
constexpr unsigned long LONG_PRESS_REPEAT_DELAY
The time between increments/decremnets during a long press.
constexpr unsigned long LONG_PRESS_DELAY
The time in milliseconds before a press is registered as a long press.