-
Notifications
You must be signed in to change notification settings - Fork 71
/
Button.h
418 lines (375 loc) · 10.7 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//- -----------------------------------------------------------------------------------------------------------------------
// 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"
#if defined ARDUINO_ARCH_AVR
typedef uint8_t WiringPinMode;
#endif
namespace as {
class DoublePressAlarm : public Alarm {
private:
bool isNewPressAllowed;
uint16_t doublepresstime;
public:
DoublePressAlarm () : Alarm(0), isNewPressAllowed(true), doublepresstime(0) {}
virtual ~DoublePressAlarm () {}
bool newPressAllowed() {
return isNewPressAllowed;
}
void newPressAllowed(bool b) {
isNewPressAllowed = b;
if (b == false) {
sysclock.cancel(*this);
set(doublepresstime);
sysclock.add(*this);
}
}
virtual void trigger(__attribute__((unused)) AlarmClock& clock) {
isNewPressAllowed = true;
}
void setDoublePressTime(uint16_t t) {
doublepresstime = t;
}
bool canDoublePress () const { return true; }
};
class NoDoublePressAlarm {
public:
NoDoublePressAlarm () {}
~NoDoublePressAlarm () {}
bool newPressAllowed() { return false; }
void newPressAllowed(__attribute__((unused)) bool b) { }
void setDoublePressTime(__attribute__((unused)) uint16_t t) {}
bool canDoublePress () const { return false; }
};
template <uint8_t OFFSTATE=HIGH,uint8_t ONSTATE=LOW,WiringPinMode MODE=INPUT_PULLUP, class DBLPRESS=NoDoublePressAlarm>
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;
DBLPRESS dbl;
public:
StateButton() :
Alarm(0), stat(none), pinstate(OFFSTATE), pin(0), longpresstime(millis2ticks(400)), ca(*this) {
}
virtual ~StateButton() {
}
void setLongPressTime(uint16_t t) {
longpresstime = t;
}
void setDoublePressTime(uint16_t t) {
dbl.setDoublePressTime(t);
}
bool canDoublePress () const {
return dbl.canDoublePress();
}
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;
dbl.newPressAllowed(false);
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 () {
//if (dbl.newPressAllowed() == true) {
// sysclock.cancel(ca);
// use alarm to run code outside of interrupt
// sysclock.add(ca);
//}
sysclock.cancel(ca);
sysclock.add(ca);
}
void check() {
uint8_t ps = digitalRead(pin);
if( pinstate != ps ) {
pinstate = ps;
if ((pinstate == ONSTATE) && (dbl.newPressAllowed() == false) && (state() != none)) return;
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;
typedef StateButton<HIGH,LOW,INPUT_PULLUP,DoublePressAlarm> DoublePressButton;
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 ) {
shortPress();
}
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);
}
// trigger a short press event - press button by software
void shortPress () {
RemoteEventMsg& msg = fillMsg(false);
device.process(msg);
counter++;
}
};
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