forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
206 lines (181 loc) · 4.94 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - 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;
template <class DEVTYPE,uint8_t OFFSTATE=HIGH,uint8_t ONSTATE=LOW,WiringPinMode MODE=INPUT_PULLUP>
class ConfigButton : public StateButton<HIGH,LOW,INPUT_PULLUP> {
DEVTYPE& device;
public:
typedef StateButton<HIGH,LOW,INPUT_PULLUP> ButtonType;
ConfigButton (DEVTYPE& dev,uint8_t longpresstime=3) : device(dev) {
setLongPressTime(seconds2ticks(longpresstime));
}
virtual void state (uint8_t s) {
uint8_t old = ButtonType::state();
ButtonType::state(s);
if( s == ButtonType::released ) {
device.startPairing();
}
else if( s == ButtonType::longpressed ) {
if( old == ButtonType::longpressed ) {
device.reset(); // long pressed again - reset
}
else {
device.led().set(LedStates::key_long);
}
}
}
};
template <class DEVTYPE,uint8_t OFFSTATE=HIGH,uint8_t ONSTATE=LOW,WiringPinMode MODE=INPUT_PULLUP>
class ConfigToggleButton : public StateButton<HIGH,LOW,INPUT_PULLUP> {
DEVTYPE& device;
public:
typedef StateButton<HIGH,LOW,INPUT_PULLUP> ButtonType;
ConfigToggleButton (DEVTYPE& dev,uint8_t longpresstime=3) : device(dev) {
setLongPressTime(seconds2ticks(longpresstime));
}
virtual void state (uint8_t s) {
uint8_t old = ButtonType::state();
ButtonType::state(s);
if( s == ButtonType::released ) {
device.channel(1).toggleState();
}
else if( s == ButtonType::longreleased ) {
device.startPairing();
}
else if( s == ButtonType::longpressed ) {
if( old == ButtonType::longpressed ) {
device.reset(); // long pressed again - reset
}
else {
device.led().set(LedStates::key_long);
}
}
}
};
}
#endif