forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
128 lines (108 loc) · 2.42 KB
/
Button.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include "AlarmClock.h"
#include "Debug.h"
namespace as {
class Button: public Alarm {
#define DEBOUNCETIME 2
public:
enum States {
none = 0,
released = 1,
pressed = 2,
debounce = 3,
longpressed = 4,
longreleased = 5
};
protected:
uint8_t stat;
uint8_t longpresstime;
uint8_t pin;
uint8_t pinstate;
public:
Button() :
Alarm(0), stat(none), longpresstime(4), pin(0), pinstate(HIGH) {
}
virtual ~Button() {
}
void setLongPressTime(uint8_t t) {
longpresstime = t;
}
uint8_t getPin () {
return pin;
}
virtual void trigger(AlarmClock& clock) {
switch (state()) {
case released:
case longreleased:
state(none);
break;
case debounce:
state(pressed);
if (pinstate == LOW) {
// set timer for detect longpressed
tick = longpresstime - DEBOUNCETIME;
clock.add(*this);
} else {
state(released);
tick = DEBOUNCETIME;
aclock.add(*this);
}
break;
case pressed:
case longpressed:
if( pinstate == LOW) {
state(longpressed);
tick = longpresstime;
}
else {
state() == pressed ? state(released) : state(longreleased);
tick = DEBOUNCETIME;
}
clock.add(*this);
break;
}
}
virtual void state(uint8_t s) {
switch(s) {
case released: DPRINTLN(" released"); break;
case pressed: DPRINTLN(" pressed"); break;
case debounce: DPRINTLN(" debounce"); break;
case longpressed: DPRINTLN(" longpressed"); break;
case longreleased: DPRINTLN(" longreleased"); break;
default: DPRINTLN(""); break;
}
stat = s;
}
uint8_t state() const {
return stat;
}
void check() {
uint8_t ps = digitalRead(pin);
if( pinstate != ps ) {
pinstate = ps;
switch (state()) {
case none:
state(debounce);
tick = DEBOUNCETIME;
aclock.add(*this);
break;
case pressed:
case longpressed:
if (pinstate == HIGH) {
aclock.cancel(*this);
tick = 0;
aclock.add(*this);
}
break;
}
}
}
void init(uint8_t pin) {
this->pin = pin;
pinMode(pin, INPUT_PULLUP);
}
};
// used to register interrupt
}
#endif