forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeState.h
236 lines (197 loc) · 6.97 KB
/
ThreeState.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-10-19 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __THREESTATE_H__
#define __THREESTATE_H__
#include "MultiChannelDevice.h"
#include "Sensors.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 ThreeStateGenericChannel : public Channel<HALTYPE,List1Type,EmptyList,List4Type,PEERCOUNT,List0Type>, public Alarm {
class EventSender : public Alarm {
public:
ThreeStateGenericChannel& channel;
uint8_t count, state;
EventSender (ThreeStateGenericChannel& 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());
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;
ThreeStateGenericChannel () : BaseChannel(), Alarm(0), sender(*this), sabpin(0), sabotage(false) {}
virtual ~ThreeStateGenericChannel () {}
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 () {
// start polling
set(possens.interval());
sysclock.add(*this);
}
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;
}
uint8_t readPin(uint8_t pinnr) {
uint8_t value=0;
pinMode(pinnr,INPUT_PULLUP);
value = digitalRead(pinnr);
pinMode(pinnr,OUTPUT);
digitalWrite(pinnr,LOW);
return value;
}
void trigger (__attribute__ ((unused)) AlarmClock& clock) {
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;
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 = (readPin(sabpin) == SABOTAGE_ACTIVE_STATE);
if( sabotage != sabstate && this->device().getList0().sabotageMsg() == true ) {
sabotage = sabstate;
this->changed(true); // trigger StatusInfoMessage to central
}
}
}
};
class TwoPinPosition : public Position {
// pin mapping can be changed by bootloader config data
// map pins to pos 00 01 10 11
uint8_t posmap[4] = {State::PosC,State::PosC,State::PosB,State::PosA};
uint8_t sens1, sens2;
public:
TwoPinPosition () : sens1(0), sens2(0) { _present = true; }
void init (uint8_t pin1,uint8_t pin2, const uint8_t* pmap) {
memcpy(posmap,pmap,4);
init(pin1,pin2);
}
void init (uint8_t pin1,uint8_t pin2) {
sens1=pin1;
sens2=pin2;
}
void measure (__attribute__((unused)) bool async=false) {
uint8_t pinstate = readPin(sens2) << 1 | readPin(sens1);
_position = posmap[pinstate & 0x03];
}
uint8_t readPin(uint8_t pinnr) {
uint8_t value=0;
pinMode(pinnr,INPUT_PULLUP);
value = digitalRead(pinnr);
pinMode(pinnr,OUTPUT);
digitalWrite(pinnr,LOW);
return value;
}
};
template <class HALTYPE,class List0Type,class List1Type,class List4Type,int PEERCOUNT>
class ThreeStateChannel : public ThreeStateGenericChannel<TwoPinPosition,HALTYPE,List0Type,List1Type,List4Type,PEERCOUNT> {
public:
typedef ThreeStateGenericChannel<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::init(sab);
BaseChannel::possens.init(pin1,pin2,pmap);
}
void init (uint8_t pin1,uint8_t pin2, const uint8_t* pmap) {
BaseChannel::init();
BaseChannel::possens.init(pin1,pin2,pmap);
}
void init (uint8_t pin1,uint8_t pin2, uint8_t sab) {
BaseChannel::init(sab);
BaseChannel::possens.init(pin1,pin2);
}
void init (uint8_t pin1,uint8_t pin2) {
BaseChannel::init();
BaseChannel::possens.init(pin1,pin2);
}
};
#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 ThreeStateDevice : public MultiChannelDevice<HalType,ChannelType,ChannelCount,List0Type> {
class CycleInfoAlarm : public Alarm {
ThreeStateDevice& dev;
public:
CycleInfoAlarm (ThreeStateDevice& 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;
ThreeStateDevice(const DeviceInfo& info,uint16_t addr) : DevType(info,addr), cycle(*this) {}
virtual ~ThreeStateDevice () {}
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);
}
}
};
}
#endif