Arduino Filters master
Filter library for Arduino
IncrementDecrementButtons.cpp
Go to the documentation of this file.
2
3AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
4
6
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
constexpr unsigned long LONG_PRESS_REPEAT_DELAY
The time between increments/decremnets during a long press.
Definition: Settings.hpp:82
constexpr unsigned long LONG_PRESS_DELAY
The time in milliseconds before a press is registered as a long press.
Definition: Settings.hpp:79
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
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
A class for buttons that increment and decrement some counter or setting.
State update()
Update and return the state of the increment/decrement button.