forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemote.h
101 lines (82 loc) · 2.97 KB
/
Remote.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-04-12 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __REMOTE_H__
#define __REMOTE_H__
#include "Channel.h"
#include "ChannelList.h"
#include "Button.h"
#include "Message.h"
#include "Register.h"
namespace as {
DEFREGISTER(RemoteReg1,CREG_LONGPRESSTIME,CREG_AES_ACTIVE,CREG_DOUBLEPRESSTIME)
class RemoteList1 : public RegList1<RemoteReg1> {
public:
RemoteList1 (uint16_t addr) : RegList1<RemoteReg1>(addr) {}
void defaults () {
clear();
longPressTime(1);
// aesActive(false);
// doublePressTime(0);
}
};
template<class HALTYPE,int PEERCOUNT,class List0Type=List0>
class RemoteChannel : public Channel<HALTYPE,RemoteList1,EmptyList,DefList4,PEERCOUNT,List0Type>, public Button {
private:
uint8_t repeatcnt;
volatile bool isr;
public:
typedef Channel<HALTYPE,RemoteList1,EmptyList,DefList4,PEERCOUNT,List0Type> BaseChannel;
RemoteChannel () : BaseChannel(), repeatcnt(0), isr(false) {}
virtual ~RemoteChannel () {}
Button& button () { return *(Button*)this; }
uint8_t status () const {
return 0;
}
uint8_t flags () const {
return 0;
}
virtual void state(uint8_t s) {
DHEX(BaseChannel::number());
Button::state(s);
RemoteEventMsg& msg = (RemoteEventMsg&)this->device().message();
msg.init(this->device().nextcount(),this->number(),repeatcnt,(s==longreleased || s==longpressed),this->device().battery().low());
if( s == released || s == longreleased) {
// send the message to every peer
this->device().sendPeerEvent(msg,*this);
repeatcnt++;
}
else if (s == longpressed) {
// broadcast the message
this->device().broadcastPeerEvent(msg,*this);
}
}
uint8_t state() const {
return Button::state();
}
bool pressed () const {
uint8_t s = state();
return s == Button::pressed || s == Button::debounce || s == Button::longpressed;
}
};
#define remoteISR(device,chan,pin) class device##chan##ISRHandler { \
public: \
static void isr () { device.channel(chan).irq(); } \
}; \
device.channel(chan).button().init(pin); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,device##chan##ISRHandler::isr,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),device##chan##ISRHandler::isr,CHANGE);
#define remoteChannelISR(chan,pin) class __##pin##ISRHandler { \
public: \
static void isr () { chan.irq(); } \
}; \
chan.button().init(pin); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,__##pin##ISRHandler::isr,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),__##pin##ISRHandler::isr,CHANGE);
}
#endif