forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactState.h
250 lines (208 loc) · 7.97 KB
/
ContactState.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-10-19 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __CONTACTSTATE_H__
#define __CONTACTSTATE_H__
#include "MultiChannelDevice.h"
#include "sensors/PinPosition.h"
#ifndef SABOTAGE_ACTIVE_STATE
#define SABOTAGE_ACTIVE_STATE LOW
#endif
namespace as {
template <class Sensor,class HALTYPE,class List0Type,class List1Type,class List4Type,int PEERCOUNT>
class StateGenericChannel : public Channel<HALTYPE,List1Type,EmptyList,List4Type,PEERCOUNT,List0Type>, public Alarm {
class EventSender : public Alarm {
public:
StateGenericChannel& channel;
uint8_t count, state;
EventSender (StateGenericChannel& c) : Alarm(0), channel(c), count(0), state(255) {}
virtual ~EventSender () {}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
SensorEventMsg& msg = (SensorEventMsg&)channel.device().message();
msg.init(channel.device().nextcount(),channel.number(),count++,state,channel.device().battery().low());
#ifdef CONTACT_STATE_WITH_BATTERY
msg.append(channel.device().battery().current());
// msg.append(__gb_BatCount);
#endif
channel.device().sendPeerEvent(msg,channel);
}
};
EventSender sender;
uint8_t sabpin;
bool sabotage;
protected:
Sensor possens;
public:
typedef Channel<HALTYPE,List1Type,EmptyList,List4Type,PEERCOUNT,List0Type> BaseChannel;
StateGenericChannel () : BaseChannel(), Alarm(0), sender(*this), sabpin(0), sabotage(false) {}
virtual ~StateGenericChannel () {}
void setup(Device<HALTYPE,List0Type>* dev,uint8_t number,uint16_t addr) {
BaseChannel::setup(dev,number,addr);
}
void init (uint8_t sab) {
sabpin = sab;
init();
}
void init () {
// get the current state
trigger(sysclock);
}
uint8_t status () const {
return sender.state;
}
uint8_t flags () const {
uint8_t flags = sabotage ? 0x07 << 1 : 0x00;
flags |= this->device().battery().low() ? 0x80 : 0x00;
return flags;
}
void trigger (__attribute__ ((unused)) AlarmClock& clock) {
// reactivate polling - if interval greater 0
if( possens.interval() > 0) {
set(possens.interval());
clock.add(*this);
}
uint8_t newstate = sender.state;
uint8_t msg = 0;
possens.measure();
switch( possens.position() ) {
case Sensor::State::PosA:
msg = this->getList1().msgForPosA();
break;
case Sensor::State::PosB:
msg = this->getList1().msgForPosB();
break;
case Sensor::State::PosC:
msg = this->getList1().msgForPosC();
break;
default:
break;
}
if( msg == 1) newstate = 0;
else if( msg == 2) newstate = 200;
else if( msg == 3) newstate = 100;
// lets the position sensor remap the new state
newstate = possens.remap(newstate);
if( sender.state == 255 ) {
// we are in the init stage - store state only
sender.state = newstate;
}
else if( newstate != sender.state ) {
uint8_t delay = this->getList1().eventDelaytime();
sender.state = newstate;
sysclock.cancel(sender);
if( delay == 0 ) {
sender.trigger(sysclock);
}
else {
sender.set(AskSinBase::byteTimeCvtSeconds(delay));
sysclock.add(sender);
}
uint16_t ledtime = (uint16_t)this->getList1().ledOntime() * 5;
if( ledtime > 0 ) {
this->device().led().ledOn(millis2ticks(ledtime),0);
}
}
if( sabpin != 0 ) {
bool sabstate = (possens.interval()==0 ? digitalRead(sabpin) : AskSinBase::readPin(sabpin) == SABOTAGE_ACTIVE_STATE);
if( sabotage != sabstate && this->device().getList0().sabotageMsg() == true ) {
sabotage = sabstate;
this->changed(true); // trigger StatusInfoMessage to central
}
}
}
#ifdef CONTACT_STATE_WITH_BATTERY
void patchStatus (Message& msg) {
// append current battery value to status message
msg.append(this->device().battery().current());
}
#endif
};
// alias for old code
template <class Sensor,class HALTYPE,class List0Type,class List1Type,class List4Type,int PEERCOUNT>
using ThreeStateGenericChannel = StateGenericChannel<Sensor,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT>;
template <class HALTYPE,class List0Type,class List1Type,class List4Type,int PEERCOUNT>
class ThreeStateChannel : public StateGenericChannel<TwoPinPosition,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT> {
public:
typedef StateGenericChannel<TwoPinPosition,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT> BaseChannel;
ThreeStateChannel () : BaseChannel() {};
~ThreeStateChannel () {}
void init (uint8_t pin1,uint8_t pin2, uint8_t sab,const uint8_t* pmap) {
BaseChannel::possens.init(pin1,pin2,pmap);
BaseChannel::init(sab);
}
void init (uint8_t pin1,uint8_t pin2, const uint8_t* pmap) {
BaseChannel::possens.init(pin1,pin2,pmap);
BaseChannel::init();
}
void init (uint8_t pin1,uint8_t pin2, uint8_t sab) {
BaseChannel::possens.init(pin1,pin2);
BaseChannel::init(sab);
}
void init (uint8_t pin1,uint8_t pin2) {
BaseChannel::possens.init(pin1,pin2);
BaseChannel::init();
}
};
template <class HALTYPE,class List0Type,class List1Type,class List4Type,int PEERCOUNT, uint16_t WAITMILLIS_AFTER_ENABLE=0>
class TwoStateChannel : public StateGenericChannel<OnePinPosition<WAITMILLIS_AFTER_ENABLE>,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT> {
public:
typedef StateGenericChannel<OnePinPosition<WAITMILLIS_AFTER_ENABLE>,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT> BaseChannel;
TwoStateChannel () : BaseChannel() {};
~TwoStateChannel () {}
void init (uint8_t pin, uint8_t en, uint8_t sab) {
BaseChannel::init(sab);
BaseChannel::possens.init(pin, en);
}
void init (uint8_t pin, uint8_t sab) {
BaseChannel::init(sab);
BaseChannel::possens.init(pin, 0);
}
void init (uint8_t pin) {
BaseChannel::init();
BaseChannel::possens.init(pin, 0);
}
};
#define DEFCYCLETIME seconds2ticks(60UL*60*20)
template<class HalType,class ChannelType,int ChannelCount,class List0Type,uint32_t CycleTime=DEFCYCLETIME> // at least one message per day
class StateDevice : public MultiChannelDevice<HalType,ChannelType,ChannelCount,List0Type> {
class CycleInfoAlarm : public Alarm {
StateDevice& dev;
public:
CycleInfoAlarm (StateDevice& d) : Alarm (CycleTime), dev(d) {}
virtual ~CycleInfoAlarm () {}
void trigger (AlarmClock& clock) {
set(CycleTime);
clock.add(*this);
for( uint8_t idx=1; idx<=dev.channels(); ++idx) {
dev.channel(idx).changed(true); // force StatusInfoMessage to central
}
}
} cycle;
public:
typedef MultiChannelDevice<HalType,ChannelType,ChannelCount,List0Type> DevType;
StateDevice(const DeviceInfo& info,uint16_t addr) : DevType(info,addr), cycle(*this) {}
virtual ~StateDevice () {}
virtual void configChanged () {
// activate cycle info message
if( this->getList0().cycleInfoMsg() == true ) {
DPRINTLN(F("Activate Cycle Msg"));
sysclock.cancel(cycle);
cycle.set(CycleTime);
sysclock.add(cycle);
}
else {
DPRINTLN(F("Deactivate Cycle Msg"));
sysclock.cancel(cycle);
}
}
};
// alias for old code
template<class HalType,class ChannelType,int ChannelCount,class List0Type,uint32_t CycleTime=DEFCYCLETIME>
using ThreeStateDevice = StateDevice<HalType,ChannelType,ChannelCount,List0Type,CycleTime>;
#define contactISR(pin,func) if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,func,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),func,CHANGE);
}
#endif