-
Notifications
You must be signed in to change notification settings - Fork 71
/
Motion.h
221 lines (191 loc) · 6.73 KB
/
Motion.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-05-10 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef LIBRARIES_ASKSINPP_MOTION_H_
#define LIBRARIES_ASKSINPP_MOTION_H_
#include "MultiChannelDevice.h"
#include "Register.h"
#include "Sensors.h"
namespace as {
DEFREGISTER(MotionReg1,CREG_EVENTFILTER,CREG_INTERVAL,CREG_AES_ACTIVE,CREG_LEDONTIME)
static const uint8_t MotionReg1Defaults[] = {0x11,0x74,0x00,0x64};
class MotionList1 : public RegList1<MotionReg1> {
public:
MotionList1 (uint16_t addr) : RegList1<MotionReg1>(addr) {}
void defaults () {
init(MotionReg1Defaults,sizeof(MotionReg1Defaults));
// clear();
// eventFilterPeriod(1);
// eventFilterNumber(1);
// minInterval(4);
// captureWithinInterval(false);
// brightnessFilter(7);
// aesActive(false);
// ledOntime(100);
}
};
class MotionEventMsg : public Message {
public:
void init(uint8_t msgcnt,uint8_t ch,uint8_t counter,uint8_t brightness,uint8_t next) {
Message::init(0xd,msgcnt,AS_MESSAGE_SENSOR_EVENT,Message::BIDI|Message::WKMEUP,ch & 0x3f,counter);
pload[0] = brightness;
pload[1] = (next+4) << 4;
}
};
template <class HalType,int PeerCount,class List0Type=List0,class BrightnessSensor=Brightness>
class MotionChannel : public Channel<HalType,MotionList1,EmptyList,DefList4,PeerCount,List0Type>, public Alarm {
class QuietMode : public Alarm {
public:
bool enabled;
bool motion;
MotionChannel& channel;
QuietMode (MotionChannel& c) : Alarm(0), enabled(false), motion(false), channel(c) {}
virtual ~QuietMode () {}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
DPRINTLN(F("minInterval End"));
enabled = false;
if( motion == true ) {
motion = false;
channel.motionDetected();
}
}
};
// send the brightness every 5 minutes to the master
#define LIGHTCYCLE seconds2ticks(5*60)
class Cycle : public Alarm {
public:
MotionChannel& channel;
Cycle (MotionChannel& c) : Alarm(LIGHTCYCLE), channel(c) {}
virtual ~Cycle () {}
virtual void trigger (AlarmClock& clock) {
tick = LIGHTCYCLE;
clock.add(*this);
channel.sendState();
}
};
// return timer ticks
uint32_t getMinInterval () {
switch( this->getList1().minInterval() ) {
case 0: return seconds2ticks(15);
case 1: return seconds2ticks(30);
case 2: return seconds2ticks(60);
case 3: return seconds2ticks(120);
default: break;
}
return seconds2ticks(240);
}
private:
uint8_t counter;
QuietMode quiet;
Cycle cycle;
volatile bool isrenabled : 1;
BrightnessSensor brightsens;
uint32_t maxbright;
public:
typedef Channel<HalType,MotionList1,EmptyList,DefList4,PeerCount,List0Type> ChannelType;
MotionChannel () : ChannelType(), Alarm(0), counter(0), quiet(*this), cycle(*this), isrenabled(true),
brightsens(), maxbright(1) {}
virtual ~MotionChannel () {}
BrightnessSensor& brightnessSensor () {
return brightsens;
}
void setup(Device<HalType,List0Type>* dev,uint8_t number,uint16_t addr) {
ChannelType::setup(dev,number,addr);
sysclock.add(cycle);
pirInterruptOn();
brightsens.init();
}
uint8_t status () {
brightsens.measure();
#if defined(__SENSORS_OPT3001_h__)
uint8_t bright = brightsens.bright2mdir();
return (uint8_t)bright;
#else
uint32_t bright = brightsens.brightness();
if( bright > maxbright ) {
maxbright = bright;
}
// scale to value between 0 - 255s
return (uint8_t)(bright * 255UL / maxbright);
#endif
}
uint8_t flags () const {
return this->device().battery().low() ? 0x80 : 0x00;
}
void sendState () {
pirInterruptOff();
typename ChannelType::DeviceType& d = this->device();
d.sendInfoActuatorStatus(d.getMasterID(),d.nextcount(),*this);
pirInterruptOn();
}
void pirInterruptOn () {
isrenabled=true;
}
void pirInterruptOff () {
isrenabled=false;
}
// this runs synch to application
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
if( quiet.enabled == false ) {
// reset state timer because motion will be send now
sysclock.cancel(cycle);
cycle.set(LIGHTCYCLE);
sysclock.add(cycle);
DPRINTLN(F("Motion"));
// start timer to end quiet interval
quiet.tick = getMinInterval();
quiet.enabled = true;
sysclock.add(quiet);
// blink led
if( this->device().led().active() == false ) {
//this->device().led().ledOn(centis2ticks(this->getList1().ledOntime()) / 2);
uint16_t ledontime = this->getList1().ledOntime();
DPRINT(F("ledontime: ")); DPRINTLN(ledontime);
this->device().led().ledOn(centis2ticks(ledontime) / 2);
}
MotionEventMsg& msg = (MotionEventMsg&)this->device().message();
msg.init(this->device().nextcount(),this->number(),++counter,status(),this->getList1().minInterval());
pirInterruptOff();
this->device().sendPeerEvent(msg,*this);
pirInterruptOn();
}
else if ( this->getList1().captureWithinInterval() == true ) {
// we have had a motion during quiet interval
quiet.motion = true;
}
}
// runs in interrupt
void motionDetected () {
if( isrenabled==true ) {
// cancel may not needed but anyway
sysclock.cancel(*this);
// activate motion message handler
sysclock.add(*this);
}
}
// we are in quiet mode - means we have a motion detected
bool isMotion () const {
return quiet.enabled == true;
}
};
#define motionISR(device,chan,pin) class device##chan##ISRHandler { \
public: \
static void isr () { device.channel(chan).motionDetected(); } \
}; \
pinMode(pin,INPUT); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,device##chan##ISRHandler::isr,RISING); \
else \
attachInterrupt(digitalPinToInterrupt(pin),device##chan##ISRHandler::isr,RISING);
#define motionChannelISR(chan,pin) class __##pin##ISRHandler { \
public: \
static void isr () { chan.motionDetected(); } \
}; \
pinMode(pin,INPUT); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,__##pin##ISRHandler::isr,RISING); \
else \
attachInterrupt(digitalPinToInterrupt(pin),__##pin##ISRHandler::isr,RISING);
} // end namespace
#endif /* LIBRARIES_ASKSINPP_MOTION_H_ */