-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFancyButton.cpp
88 lines (81 loc) · 1.72 KB
/
FancyButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* FancyButton.cpp - Library for debouncing and generating long press events
* Created by Miguel Pachá, Sep 4, 2020.
*
* License: LGPL
*/
#include "Arduino.h"
#include "FancyButton.h"
FancyButton::FancyButton(int pin){
pinMode(pin, INPUT_PULLUP);
_pin = pin;
}
void FancyButton::check(){
_reading = digitalRead(_pin);
switch(_state){
case standby:
if (_reading==LOW) {
if (_reading==_last_reading) {
if (!_counting) {
_counting = true;
_last_event = millis();
} else {
if ((millis()-_last_event)>debounceDelay){
press_flag = true;
_state = pressed;
}
}
}
} else {
_counting = false;
}
break;
case pressed:
if (_reading==LOW){
if ((millis()-_last_event)>LONG_PRESS_TIME){
long_press_flag = true;
_state = long_pressed;
}
} else {
short_press_flag = true;
_state = standby;
}
break;
case long_pressed:
if (_reading==HIGH) {
release_long_flag = true;
_state = standby;
}
break;
default: break;
}
_last_reading = _reading;
}
bool FancyButton::press(){
if (press_flag){
press_flag = false;
return true;
}
return false;
}
bool FancyButton::short_press(){
if (short_press_flag){
short_press_flag = false;
return true;
}
return false;
}
bool FancyButton::long_press(){
if (long_press_flag){
long_press_flag = false;
return true;
}
return false;
}
bool FancyButton::long_released(){
if (release_long_flag){
release_long_flag = false;
return true;
}
return false;
}