forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
151 lines (130 loc) · 3.31 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http:https://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include "AlarmClock.h"
#include "Debug.h"
#ifdef ARDUINO_ARCH_AVR
typedef uint8_t WiringPinMode;
#endif
namespace as {
template <uint8_t OFFSTATE=HIGH,uint8_t ONSTATE=LOW,WiringPinMode MODE=INPUT_PULLUP>
class StateButton: public Alarm {
#define DEBOUNCETIME millis2ticks(200)
public:
enum States {
invalid = 0,
none = 1,
released = 2,
pressed = 3,
debounce = 4,
longpressed = 5,
longreleased = 6,
};
protected:
uint8_t stat : 3;
uint8_t pinstate : 1;
uint8_t pin;
uint16_t longpresstime;
public:
StateButton() :
Alarm(0), stat(none), pinstate(OFFSTATE), pin(0), longpresstime(millis2ticks(400)) {
}
virtual ~StateButton() {
}
void setLongPressTime(uint16_t t) {
longpresstime = t;
}
uint8_t getPin () {
return pin;
}
virtual void trigger(AlarmClock& clock) {
uint8_t nextstate = invalid;
uint8_t nexttick = 0;
switch ( state() ) {
case released:
case longreleased:
nextstate = none;
break;
case debounce:
nextstate = pressed;
if (pinstate == ONSTATE) {
// set timer for detect longpressed
nexttick = longpresstime - DEBOUNCETIME;
} else {
nextstate = released;
nexttick = DEBOUNCETIME;
}
break;
case pressed:
case longpressed:
if( pinstate == ONSTATE) {
nextstate = longpressed;
nexttick = longpresstime;
}
break;
}
// reactivate alarm if needed
if( nexttick != 0 ) {
tick = nexttick;
clock.add(*this);
}
// trigger the state change
if( nextstate != invalid ) {
state(nextstate);
}
}
virtual void state(uint8_t s) {
switch(s) {
case released: DPRINTLN(F(" released")); break;
case pressed: DPRINTLN(F(" pressed")); break;
case debounce: DPRINTLN(F(" debounce")); break;
case longpressed: DPRINTLN(F(" longpressed")); break;
case longreleased: DPRINTLN(F(" longreleased")); break;
default: DPRINTLN(F("")); break;
}
stat = s;
}
uint8_t state() const {
return stat;
}
void check() {
uint8_t ps = digitalRead(pin);
if( pinstate != ps ) {
pinstate = ps;
uint8_t nexttick = 0;
uint8_t nextstate = state();
switch ( state() ) {
case none:
nextstate = debounce;
nexttick = DEBOUNCETIME;
break;
case pressed:
case longpressed:
if (pinstate == OFFSTATE) {
nextstate = state() == pressed ? released : longreleased;
nexttick = DEBOUNCETIME;
}
break;
}
if( nexttick != 0 ) {
aclock.cancel(*this);
tick = nexttick;
aclock.add(*this);
}
if( nextstate != state () ) {
state(nextstate);
}
}
}
void init(uint8_t pin) {
this->pin = pin;
pinMode(pin, MODE);
}
};
// define standard button switches to GND
typedef StateButton<HIGH,LOW,INPUT_PULLUP> Button;
}
#endif