Arduino Helpers
master
Utility library for Arduino
src
AH
Hardware
MultiPurposeButton.hpp
Go to the documentation of this file.
1
/* ✔ */
2
3
#pragma once
4
5
#include "
Button.hpp
"
6
7
BEGIN_AH_NAMESPACE
8
12
class
MultiPurposeButton
{
13
public
:
14
MultiPurposeButton
(
pin_t
pin) :
button
(pin) {}
15
16
enum
Event
{
17
None
,
18
PressStart
,
19
ShortPressRelease
,
20
LongPress
,
21
LongPressRelease
,
22
MultiPress
,
24
MultiPressDone
,
26
};
27
29
void
begin
() {
button
.
begin
(); }
30
32
Event
update
() {
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
:
119
Button
button
;
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
:
127
friend
FlashString_t
to_string
(
Event
ev) {
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
141
END_AH_NAMESPACE
FlashString_t
std::remove_reference< decltype(*F(""))>::type * FlashString_t
Definition:
Arduino-Wrapper.h:18
Button.hpp
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition:
Hardware-Types.hpp:14
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition:
NamespaceSettings.hpp:11
Button
A class for reading and debouncing buttons and switches.
Definition:
Button.hpp:15
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::previousBounceTime
unsigned long previousBounceTime() const
Return the time point (in milliseconds) when the button last bounced.
Definition:
Button.cpp:46
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
Button::invert
void invert()
Invert the input state of this button (button pressed is HIGH instead of LOW).
Definition:
Button.cpp:9
Button::getState
State getState() const
Get the state of the button, without updating it.
Definition:
Button.cpp:32
Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition:
Button.cpp:7
MultiPurposeButton
Class for detecting short/long button presses and double clicks.
Definition:
MultiPurposeButton.hpp:12
MultiPurposeButton::getButtonState
Button::State getButtonState() const
Definition:
MultiPurposeButton.hpp:114
MultiPurposeButton::multiPressCountNew
uint8_t multiPressCountNew
Definition:
MultiPurposeButton.hpp:123
MultiPurposeButton::stableTime
unsigned long stableTime(unsigned long now) const
Definition:
MultiPurposeButton.hpp:94
MultiPurposeButton::longPress
bool longPress
Definition:
MultiPurposeButton.hpp:122
MultiPurposeButton::previousBounceTime
unsigned long previousBounceTime() const
Definition:
MultiPurposeButton.hpp:88
MultiPurposeButton::multiPressCount
uint8_t multiPressCount
Definition:
MultiPurposeButton.hpp:124
MultiPurposeButton::getPressedTime
unsigned long getPressedTime() const
Get the number of milliseconds the button has been pressed for.
Definition:
MultiPurposeButton.hpp:100
MultiPurposeButton::update
Event update()
Read the button state and return the Event (if any).
Definition:
MultiPurposeButton.hpp:32
MultiPurposeButton::getLongPressedTime
unsigned long getLongPressedTime() const
Get the number of milliseconds the button has been pressed for.
Definition:
MultiPurposeButton.hpp:106
MultiPurposeButton::multiPressDelay
unsigned long multiPressDelay
Definition:
MultiPurposeButton.hpp:121
MultiPurposeButton::getMultiPressDelay
unsigned long getMultiPressDelay() const
Get the number of milliseconds between multipresses.
Definition:
MultiPurposeButton.hpp:81
MultiPurposeButton::MultiPurposeButton
MultiPurposeButton(pin_t pin)
Definition:
MultiPurposeButton.hpp:14
MultiPurposeButton::to_string
friend FlashString_t to_string(Event ev)
Definition:
MultiPurposeButton.hpp:127
MultiPurposeButton::Event
Event
Definition:
MultiPurposeButton.hpp:16
MultiPurposeButton::PressStart
@ PressStart
The button was just pressed.
Definition:
MultiPurposeButton.hpp:18
MultiPurposeButton::LongPressRelease
@ LongPressRelease
The button was released after a long press.
Definition:
MultiPurposeButton.hpp:21
MultiPurposeButton::None
@ None
Nothing changed.
Definition:
MultiPurposeButton.hpp:17
MultiPurposeButton::ShortPressRelease
@ ShortPressRelease
The button was released after a short press.
Definition:
MultiPurposeButton.hpp:19
MultiPurposeButton::MultiPressDone
@ MultiPressDone
The button has been released for long enough to rule out another MultiPress.
Definition:
MultiPurposeButton.hpp:24
MultiPurposeButton::LongPress
@ LongPress
The button has been pressed for some time.
Definition:
MultiPurposeButton.hpp:20
MultiPurposeButton::MultiPress
@ MultiPress
The button was pressed in quick succession of the previous release.
Definition:
MultiPurposeButton.hpp:22
MultiPurposeButton::setLongPressDelay
void setLongPressDelay(unsigned long longPressDelay)
Set the number of milliseconds after which a press is considered a long press.
Definition:
MultiPurposeButton.hpp:76
MultiPurposeButton::getName
static FlashString_t getName(Event ev)
Return the name of the event as a string.
Definition:
MultiPurposeButton.hpp:111
MultiPurposeButton::getMultiPressCount
uint8_t getMultiPressCount() const
Get the number of times the button was pressed in quick succession (after MultiPressDone),...
Definition:
MultiPurposeButton.hpp:65
MultiPurposeButton::invert
void invert()
Definition:
MultiPurposeButton.hpp:116
MultiPurposeButton::button
Button button
Definition:
MultiPurposeButton.hpp:119
MultiPurposeButton::begin
void begin()
Definition:
MultiPurposeButton.hpp:29
MultiPurposeButton::getLongPressDelay
unsigned long getLongPressDelay() const
Get the number of milliseconds after which a press is considered a long press.
Definition:
MultiPurposeButton.hpp:73
MultiPurposeButton::stableTime
unsigned long stableTime() const
Definition:
MultiPurposeButton.hpp:92
MultiPurposeButton::getCurrentMultiPressCount
uint8_t getCurrentMultiPressCount() const
Get the number of times the button was pressed in quick succession, while the button is being pressed...
Definition:
MultiPurposeButton.hpp:69
MultiPurposeButton::longPressDelay
unsigned long longPressDelay
Definition:
MultiPurposeButton.hpp:120
MultiPurposeButton::setMultiPressDelay
void setMultiPressDelay(unsigned long multiPressDelay)
Set the number of milliseconds between multipresses.
Definition:
MultiPurposeButton.hpp:83
Generated by
1.9.4