forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
357 lines (322 loc) · 9.08 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://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(50)
public:
enum States {
invalid = 0,
none = 1,
released = 2,
pressed = 3,
debounce = 4,
longpressed = 5,
longreleased = 6,
};
class CheckAlarm : public Alarm {
public:
StateButton& sb;
CheckAlarm (StateButton& _sb) : Alarm(0), sb(_sb) {}
~CheckAlarm () {}
virtual void trigger(__attribute__((unused)) AlarmClock& clock) {
sb.check();
}
};
protected:
uint8_t stat : 3;
uint8_t pinstate : 1;
uint8_t pin;
uint16_t longpresstime;
CheckAlarm ca;
public:
StateButton() :
Alarm(0), stat(none), pinstate(OFFSTATE), pin(0), longpresstime(millis2ticks(400)), ca(*this) {
}
virtual ~StateButton() {
}
void setLongPressTime(uint16_t t) {
longpresstime = t;
}
uint8_t getPin () {
return pin;
}
virtual void trigger(AlarmClock& clock) {
uint8_t nextstate = invalid;
uint16_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 irq () {
sysclock.cancel(ca);
// use alarm to run code outside of interrupt
sysclock.add(ca);
}
void check() {
uint8_t ps = digitalRead(pin);
if( pinstate != ps ) {
pinstate = ps;
uint16_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;
default:
break;
}
if( nexttick != 0 ) {
sysclock.cancel(*this);
tick = nexttick;
sysclock.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<OFFSTATE,ONSTATE,MODE> {
DEVTYPE& device;
public:
typedef StateButton<OFFSTATE,ONSTATE,MODE> ButtonType;
ConfigButton (DEVTYPE& dev,uint8_t longpresstime=3) : device(dev) {
this->setLongPressTime(seconds2ticks(longpresstime));
}
virtual ~ConfigButton () {}
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 ) {
if( device.getList0().localResetDisable() == false ) {
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<OFFSTATE,ONSTATE,MODE> {
DEVTYPE& device;
public:
typedef StateButton<OFFSTATE,ONSTATE,MODE> ButtonType;
ConfigToggleButton (DEVTYPE& dev,uint8_t longpresstime=3) : device(dev) {
this->setLongPressTime(seconds2ticks(longpresstime));
}
virtual ~ConfigToggleButton () {}
virtual void state (uint8_t s) {
uint8_t old = ButtonType::state();
ButtonType::state(s);
if( s == ButtonType::released ) {
RemoteEventMsg& msg = (RemoteEventMsg&)device.message();
uint8_t cnt = device.nextcount();
msg.init(cnt,1,cnt,false,false);
device.getDeviceID(msg.to());
device.getDeviceID(msg.from());
msg.clearAck();
if( device.process(msg) == false ) {
DPRINTLN(F("No self peer. Create internal peering to toggle state!"));
}
}
else if( s == ButtonType::longreleased ) {
device.startPairing();
}
else if( s == ButtonType::longpressed ) {
if( old == ButtonType::longpressed ) {
if( device.getList0().localResetDisable() == false ) {
device.reset(); // long pressed again - reset
}
}
else {
device.led().set(LedStates::key_long);
}
}
}
Peer peer () const {
HMID self;
device.getDeviceID(self);
return Peer(self,1);
}
};
template <class DEVTYPE,uint8_t OFFSTATE=HIGH,uint8_t ONSTATE=LOW,WiringPinMode MODE=INPUT_PULLUP>
class InternalButton : public StateButton<OFFSTATE,ONSTATE,MODE> {
DEVTYPE& device;
uint8_t num, counter;
public:
typedef StateButton<OFFSTATE,ONSTATE,MODE> ButtonType;
InternalButton (DEVTYPE& dev,uint8_t n,uint8_t longpresstime=4) : device(dev), num(n), counter(0) {
this->setLongPressTime(decis2ticks(longpresstime));
}
virtual ~InternalButton () {}
virtual void state (uint8_t s) {
ButtonType::state(s);
if( s == ButtonType::released ) {
RemoteEventMsg& msg = fillMsg(false);
device.process(msg);
counter++;
}
else if( s == ButtonType::longpressed ) {
RemoteEventMsg& msg = fillMsg(true);
device.process(msg);
}
else if( s == ButtonType::longreleased ) {
counter++;
}
}
RemoteEventMsg& fillMsg (bool lg) {
RemoteEventMsg& msg = (RemoteEventMsg&)device.message();
uint8_t cnt = device.nextcount();
msg.init(cnt,num,counter,lg,false);
device.getDeviceID(msg.to());
device.getDeviceID(msg.from());
msg.clearAck();
return msg;
}
Peer peer () const {
HMID self;
device.getDeviceID(self);
return Peer(self,num);
}
};
class BaseEncoder {
int8_t counter;
uint8_t datapin;
public:
BaseEncoder () : counter(0), datapin(0) {}
void encirq () {
uint8_t data = digitalRead(datapin);
counter += data == LOW ? 1 : -1;
}
void init (uint8_t cpin,uint8_t dpin) {
pinMode(cpin, INPUT_PULLUP);
pinMode(dpin, INPUT_PULLUP);
datapin = dpin;
}
int8_t read () {
int8_t result=0;
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
result = counter;
counter = 0;
}
return result;
}
};
template<class DeviceType>
class InternalEncoder : public InternalButton<DeviceType>, public BaseEncoder {
public:
InternalEncoder (DeviceType& dev,uint8_t num) : InternalButton<DeviceType>(dev,num), BaseEncoder() {};
virtual ~InternalEncoder () {};
void init (uint8_t sw) {
InternalButton<DeviceType>::init(sw);
}
void init (uint8_t cpin,uint8_t dpin) {
BaseEncoder::init(cpin,dpin);
}
template<class ChannelType>
void process (ChannelType& channel) {
int8_t dx = read();
if( dx != 0 ) {
typename ChannelType::List3 l3 = channel.getList3(this->peer());
if( l3.valid() ) {
if( dx > 0 ) channel.dimUp(l3.sh());
else channel.dimDown(l3.sh());
}
}
}
};
#define buttonISR(btn,pin) class btn##ISRHandler { \
public: \
static void isr () { btn.irq(); } \
}; \
btn.init(pin); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,btn##ISRHandler::isr,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),btn##ISRHandler::isr,CHANGE);
#define encoderISR(enc,clkpin,datapin) class enc##ENCISRHandler { \
public: \
static void isr () { enc.encirq(); } \
}; \
enc.init(clkpin,datapin); \
if( digitalPinToInterrupt(clkpin) == NOT_AN_INTERRUPT ) \
enableInterrupt(clkpin,enc##ENCISRHandler::isr,FALLING); \
else \
attachInterrupt(digitalPinToInterrupt(clkpin),enc##ENCISRHandler::isr,FALLING);
}
#endif