Arduino Helpers
master
Utility library for Arduino
src
AH
Hardware
IncrementDecrementButtons.cpp
Go to the documentation of this file.
1
#include "
IncrementDecrementButtons.hpp
"
2
3
BEGIN_AH_NAMESPACE
4
5
IncrementDecrementButtons::State
6
IncrementDecrementButtons::updateImplementation
() {
7
Button::State
incrState =
incrementButton
.
update
();
8
Button::State
decrState =
decrementButton
.
update
();
9
10
if
(decrState ==
Button::Released
&& incrState ==
Button::Released
) {
11
// Both released
12
}
else
if
((decrState ==
Button::Rising
&& incrState ==
Button::Released
) ||
13
(incrState ==
Button::Rising
&& decrState ==
Button::Released
) ||
14
(incrState ==
Button::Rising
&& decrState ==
Button::Rising
)) {
15
// One released, the other rising → nothing
16
// now both released, so go to initial state
17
longPressState
=
Initial
;
18
}
else
if
(incrState ==
Button::Falling
&& decrState ==
Button::Falling
) {
19
// Both falling → reset
20
// (rather unlikely, but just in case)
21
longPressState
=
AfterReset
;
22
return
Reset
;
23
}
else
if
(incrState ==
Button::Falling
) {
24
if
(decrState ==
Button::Pressed
) {
25
// One pressed, the other falling → reset
26
longPressState
=
AfterReset
;
27
return
Reset
;
28
}
else
{
29
// Increment falling, the other released → increment
30
return
IncrementShort
;
31
}
32
}
else
if
(decrState ==
Button::Falling
) {
33
if
(incrState ==
Button::Pressed
) {
34
// One pressed, the other falling → reset
35
longPressState
=
AfterReset
;
36
return
Reset
;
37
}
else
{
38
// Decrement falling, the other released → decrement
39
return
DecrementShort
;
40
}
41
}
else
if
(incrState ==
Button::Pressed
&& decrState ==
Button::Pressed
) {
42
// Both pressed → nothing
43
}
else
if
(
longPressState
!=
AfterReset
&& incrState ==
Button::Pressed
) {
44
// Not reset and increment pressed → long press?
45
auto
now = millis();
46
if
(
longPressState
==
LongPress
) {
47
if
(now -
longPressRepeat
>=
LONG_PRESS_REPEAT_DELAY
) {
48
longPressRepeat
+=
LONG_PRESS_REPEAT_DELAY
;
49
return
IncrementHold
;
50
}
51
}
else
if
(
incrementButton
.
stableTime
() >=
LONG_PRESS_DELAY
) {
52
longPressState
=
LongPress
;
53
longPressRepeat
= now;
54
return
IncrementLong
;
55
}
56
}
else
if
(
longPressState
!=
AfterReset
&& decrState ==
Button::Pressed
) {
57
// Not reset and decrement pressed → long press?
58
auto
now = millis();
59
if
(
longPressState
==
LongPress
) {
60
if
(now -
longPressRepeat
>=
LONG_PRESS_REPEAT_DELAY
) {
61
longPressRepeat
+=
LONG_PRESS_REPEAT_DELAY
;
62
return
DecrementHold
;
63
}
64
}
else
if
(
decrementButton
.
stableTime
() >=
LONG_PRESS_DELAY
) {
65
longPressState
=
LongPress
;
66
longPressRepeat
= now;
67
return
DecrementLong
;
68
}
69
}
70
return
Nothing
;
71
}
72
73
END_AH_NAMESPACE
IncrementDecrementButtons.hpp
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:11
LONG_PRESS_REPEAT_DELAY
constexpr unsigned long LONG_PRESS_REPEAT_DELAY
The time between increments/decremnets during a long press.
Definition:
Settings.hpp:87
LONG_PRESS_DELAY
constexpr unsigned long LONG_PRESS_DELAY
The time in milliseconds before a press is registered as a long press.
Definition:
Settings.hpp:84
Button::stableTime
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
Button::update
State update()
Read the button and return its new state.
Definition:
Button.cpp:11
Button::State
State
An enumeration of the different states a button can be in.
Definition:
Button.hpp:45
Button::Pressed
@ Pressed
Input went from low to low (0,0)
Definition:
Button.hpp:46
Button::Rising
@ Rising
Input went from low to high (0,1)
Definition:
Button.hpp:49
Button::Falling
@ Falling
Input went from high to low (1,0)
Definition:
Button.hpp:48
Button::Released
@ Released
Input went from high to high (1,1)
Definition:
Button.hpp:47
IncrementDecrementButtons::incrementButton
Button incrementButton
Definition:
IncrementDecrementButtons.hpp:83
IncrementDecrementButtons::longPressState
enum IncrementDecrementButtons::@1 longPressState
IncrementDecrementButtons::updateImplementation
State updateImplementation()
Definition:
IncrementDecrementButtons.cpp:6
IncrementDecrementButtons::State
State
An enumeration of the different actions to be performed by the counter.
Definition:
IncrementDecrementButtons.hpp:49
IncrementDecrementButtons::DecrementLong
@ DecrementLong
The counter must be decremented (after long press).
Definition:
IncrementDecrementButtons.hpp:55
IncrementDecrementButtons::Nothing
@ Nothing
The counter should not be incremented.
Definition:
IncrementDecrementButtons.hpp:50
IncrementDecrementButtons::IncrementShort
@ IncrementShort
The counter must be incremented (after short press).
Definition:
IncrementDecrementButtons.hpp:51
IncrementDecrementButtons::IncrementHold
@ IncrementHold
The counter must be incremented (still pressed).
Definition:
IncrementDecrementButtons.hpp:53
IncrementDecrementButtons::DecrementHold
@ DecrementHold
The counter must be decremented (still pressed).
Definition:
IncrementDecrementButtons.hpp:56
IncrementDecrementButtons::DecrementShort
@ DecrementShort
The counter must be decremented (after short press).
Definition:
IncrementDecrementButtons.hpp:54
IncrementDecrementButtons::Reset
@ Reset
The counter should be reset to the initial value.
Definition:
IncrementDecrementButtons.hpp:57
IncrementDecrementButtons::IncrementLong
@ IncrementLong
The counter must be incremented (after long press).
Definition:
IncrementDecrementButtons.hpp:52
IncrementDecrementButtons::longPressRepeat
unsigned long longPressRepeat
Definition:
IncrementDecrementButtons.hpp:91
IncrementDecrementButtons::Initial
@ Initial
Definition:
IncrementDecrementButtons.hpp:87
IncrementDecrementButtons::AfterReset
@ AfterReset
Definition:
IncrementDecrementButtons.hpp:89
IncrementDecrementButtons::LongPress
@ LongPress
Definition:
IncrementDecrementButtons.hpp:88
IncrementDecrementButtons::decrementButton
Button decrementButton
Definition:
IncrementDecrementButtons.hpp:84
Generated by
1.9.4