-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
576 lines (443 loc) · 25.2 KB
/
application.cpp
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/**
*******************************************************************************
* @file application.cpp
* @authors chrono
* @version V1.0.0 (codename Aziz 3.0)
* @date 2014-10-15
* @brief spark-lighter - Robot /w 4ch (RGBW) LED + PIR & Ambient Light Sensor
*******************************************************************************
Copyright (c) 2014 Apollo-NG - https://apollo.open-resource.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/// Includes ///////////////////////////////////////////////////////////////////
#include "application.h"
#include "lib/DS18B20.h"
#include "lib/OneWire.h"
////////////////////////////////////////////////////////////////////////////////
/// Robot Configuration (Change your local settings here) //////////////////////
////////////////////////////////////////////////////////////////////////////////
#define VERBOSE
// Inputs (DYP-ME003 PIR Sensor -> D2 & TEMT6000 Ambient Light Sensor -> A0) ///
const uint8_t pinPIR = 2 ;
const uint8_t pinAMB = 10 ;
const uint8_t pinTMP = 4 ;
// Outputs (RGBW Channels -> [A4:A7] -> MOSFET/Gatedriver inputs ) /////////////
const uint8_t pinR = 15 ;
const uint8_t pinG = 14 ;
const uint8_t pinB = 17 ;
const uint8_t pinW = 16 ;
/// Time mapping ///////////////////////////////////////////////////////////////
const uint8_t GPB = 30; // Grace Period Baselength in Seconds
const uint8_t GPM = 90; // Maximum Grace Period length in Seconds
const uint8_t bNight = 26; // Begin of Night hours
const uint8_t eNight = 6; // End of Night hours
////////////////////////////////////////////////////////////////////////////////
/// Init ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Elastic Grace Period length (variable - starts with baselength) /////////////
uint8_t EGP = GPB ;
// LEDs ////////////////////////////////////////////////////////////////////////
uint8_t ledR = 0 ;
uint8_t ledG = 0 ;
uint8_t ledB = 0 ;
uint8_t ledW = 0 ;
// Time ////////////////////////////////////////////////////////////////////////
uint16_t timeDiff = 0 ;
uint32_t lastMotion = 0 ;
uint32_t lastTSync = 0 ;
// Environment ////////////////////////////////////////////////////////////////
uint16_t ambLux = 0 ;
float ambTmp = 0 ;
char tmpData[64] ;
// Bitwise State Table /////////////////////////////////////////////////////////////
/*
0x1 : Online & Ready
0x2 : PIR Motion triggered
0x4 : Human Presence
0x8 : Grace Period
0x16 : Event Notification
0x32 : Night
*/
uint8_t state = 0x0 ;
// Function prototypes /////////////////////////////////////////////////////////
int setRGBW (String rgbwInt) ;
void setPWM (uint8_t pin, uint8_t value) ;
void fadeTo (long rgbw, int delaytime) ;
void autolight (int target) ;
void motionISR (void) ;
void alertESR (const char *event, const char *data) ;
uint16_t readT6K (void) ;
float readDS18B20 (void) ;
////////////////////////////////////////////////////////////////////////////////
/// Setup //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
DS18B20 ds18b20 = DS18B20 (pinTMP) ;
SYSTEM_MODE (AUTOMATIC) ;
void setup ()
{
#ifdef VERBOSE
Serial.begin (9600) ;
#endif
////////////////////////////////////////////////////////////////////////////
/// Pre-Define port direction & attach Interrupts //////////////////////////
// Set up DYP-ME003 Passive Infrared Sensor ////////////////////////////////
pinMode (pinPIR, INPUT_PULLDOWN) ;
attachInterrupt (pinPIR, motionISR, RISING) ;
// Set up MOSFET Gate Driver output lines //////////////////////////////////
pinMode (pinR, OUTPUT) ;
pinMode (pinG, OUTPUT) ;
pinMode (pinB, OUTPUT) ;
pinMode (pinW, OUTPUT) ;
/// Close all PWM valves, to keep time of uncontrolled state at minimum. Also
/// in this context: I had to add 4 pull-down resistors (10k), each between
/// one of the Core's PWM output pins and the gate driver's input pin or all
/// hell would break loose (especially when waving my hands over it) :)
setPWM (pinR, 0) ;
setPWM (pinG, 0) ;
setPWM (pinB, 0) ;
setPWM (pinW, 0) ;
////////////////////////////////////////////////////////////////////////////
/// Expose variables & function through spark-server API ///////////////////
Spark.variable ("ledr", &ledR, INT) ;
Spark.variable ("ledg", &ledG, INT) ;
Spark.variable ("ledb", &ledB, INT) ;
Spark.variable ("ledw", &ledW, INT) ;
Spark.variable ("amblux", &ambLux, INT) ;
Spark.variable ("ambtmp", &ambTmp, DOUBLE) ;
Spark.function ("setrgbw", setRGBW ) ;
Spark.subscribe ("alerts", alertESR ) ;
////////////////////////////////////////////////////////////////////////////
/// Set Ready-State bit ////////////////////////////////////////////////////
state |= 0x1 ;
}
////////////////////////////////////////////////////////////////////////////////
/// MAIN LOOP //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void loop ()
{
////////////////////////////////////////////////////////////////////////////
/// Already time to request time synchronization (1h)? /////////////////////
if (millis() - lastTSync > 3600000)
{
Spark.syncTime () ;
lastTSync = millis () ;
}
////////////////////////////////////////////////////////////////////////////
/// Is the motion trigger state bit set (new motion detected)? /////////////
if ((state & 0x2) == 0x2)
{
#ifdef VERBOSE
Serial.println ("Motion Detected") ;
#endif
// Clear motion trigger state bit //////////////////////////////////////
state &= ~0x2 ;
// Publish our motion event through our spark-server's event firehose //
Spark.publish ("motion", NULL, 60, PRIVATE) ;
// Remember the timestamp of this event ////////////////////////////////
lastMotion = millis () ;
// New presence detected (presence state bit not set)? /////////////////
if ((state & 0x4) == 0)
{
#ifdef VERBOSE
Serial.println ("New presence detected") ;
#endif
// Set presence state bit (4) //////////////////////////////////////
state |= 0x4 ;
// Let there be light //////////////////////////////////////////////
autolight (1) ;
}
// Are we already in grace period? /////////////////////////////////////
else if ((state & 0x8) == 0x8)
{
// Presence confidence restored, remove grace status bit ///////////
state &= ~0x8 ;
autolight (1) ;
}
// Honor current presence's movement by increasing time to GP //////////
else if ((state & 0x4) == 0x4)
{
if(EGP < GPM)
{
#ifdef VERBOSE
Serial.println (" -> Boosting GP +10...") ;
#endif
EGP = EGP+10 ;
}
}
// Slow down so that the RGB LED state is observable to humans /////////
delay (250) ;
// Release control of RGB status Led ///////////////////////////////////
RGB.control (false) ;
}
////////////////////////////////////////////////////////////////////////////
/// Is there really anyone left present? ///////////////////////////////////
if ((state & 0x4) == 0x4)
{
timeDiff = (int) (millis() - lastMotion)/1000 ;
#ifdef VERBOSE
Serial.print (" -> Last Motion: ") ;
Serial.println (timeDiff) ;
#endif
// Compare last motion time distance for graceful auto powerdown ///////
if (timeDiff > EGP+30)
{
// I'm confident no one is any longer present //////////////////////
#ifdef VERBOSE
Serial.println ("No one present - Shutting down") ;
#endif
// Unset presence (4) and grace-period (8) status bits /////////////
state &= ~0x4 ;
state &= ~0x8 ;
// Reset accumulated Elastic Grace Period boni /////////////////////
EGP = GPB ;
// Let there be darkness ///////////////////////////////////////////
autolight (0) ;
}
else if (timeDiff > EGP)
{
if ((state & 0x8) == 0)
{
#ifdef VERBOSE
Serial.println ("Grace Period started - Fading down") ;
#endif
// Set grace period state bit (8) //////////////////////////////
state |= 0x8 ;
// Fade the light down a little to remind the human to move ////
autolight (2) ;
}
}
}
ambLux = readT6K () ;
ambTmp = readDS18B20 () ;
sprintf (tmpData, "{ 'C': %4.1f }", ambTmp) ;
Spark.publish ("temperature", tmpData, 60, PRIVATE) ;
#ifdef VERBOSE
Serial.print (" -> Timestamp: ") ;
Serial.println (millis()) ;
Serial.print (" -> State: ") ;
Serial.println (state, BIN) ;
Serial.print (" -> Ambient Light: ") ;
Serial.println (ambLux, DEC) ;
Serial.print (" -> Ambient Temp: ") ;
Serial.println (tmpData) ;
Serial.print (" -> RSSI: ") ;
Serial.println (WiFi.RSSI()) ;
Serial.println ("------------------------------------");
#endif
}
/// END MAIN LOOP //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void autolight (int target)
{
if (target == 1)
{
////////////////////////////////////////////////////////////////////////
// Ramp up to Maximum, depending on time/environment ///////////////////
if ( Time.hour() < eNight
|| Time.hour() > bNight)
{
// Night mode //////////////////////////////////////////////////////
while (ledR < 128)
{
ledR++ ;
setPWM (pinR, ledR) ;
delay (40) ;
}
}
else
{
// Day mode ////////////////////////////////////////////////////////
while (ledW < 255 && ambLux < 250)
{
ledW ++ ;
setPWM (pinW, ledW) ;
delay (20) ;
ambLux = readT6K () ;
}
}
}
else if (target == 2)
{
////////////////////////////////////////////////////////////////////////
// Fade Down a little to notifiy present humans to move ////////////////
if ( Time.hour() < eNight
|| Time.hour() > bNight)
{
// Night mode //////////////////////////////////////////////////////
while (ledR > 64)
{
ledR-- ;
setPWM (pinR, ledR) ;
delay (20) ;
}
}
else
{
// Day mode ////////////////////////////////////////////////////////
while (ledW > 128)
{
ledW-- ;
setPWM (pinW, ledW) ;
delay (20) ;
}
}
}
else
{
////////////////////////////////////////////////////////////////////////
// Fade Down all
// FIXME: This is still buggy, there has to be some more thought about
// collisions between autolight and user/event overrides.
if ( Time.hour() < eNight
|| Time.hour() > bNight)
{
// Night mode //////////////////////////////////////////////////////
while (ledR > 0)
{
ledR-- ;
setPWM (pinR, ledR) ;
delay (20) ;
}
}
else
{
// Day mode ////////////////////////////////////////////////////////
while (ledW > 0)
{
ledW-- ;
setPWM (pinW, ledW) ;
delay (20) ;
}
}
}
}
void fadeTo (long rgbw, int delaytime)
{
uint8_t completed = 0x0 ;
#ifdef VERBOSE
Serial.println ("Fading to new target") ;
#endif
// Separate colors from combined 32bit RGBA long ///////////////////////////
uint8_t newR = (rgbw >> 24) & 0xFF ;
uint8_t newG = (rgbw >> 16) & 0xFF ;
uint8_t newB = (rgbw >> 8) & 0xFF ;
uint8_t newW = (int) ((rgbw >> 0) & 0xFF) ;
while (completed != 15)
{
// Red Channel /////////////////////////////////////////////////////////
if (ledR < newR)
{
ledR++ ;
setPWM (pinR, ledR) ;
}
else if (ledR > newR)
{
ledR-- ;
setPWM (pinR, ledR) ;
}
else { completed |= 0x1; }
// Green Channel ///////////////////////////////////////////////////////
if (ledG < newG)
{
ledG++ ;
setPWM (pinG, ledG) ;
}
else if (ledG > newG)
{
ledG-- ;
setPWM (pinG, ledG) ;
}
else { completed |= 0x2; }
// Blue Channel ////////////////////////////////////////////////////////
if (ledB < newB)
{
ledB++ ;
setPWM (pinB, ledB) ;
}
else if (ledB > newB)
{
ledB-- ;
setPWM (pinB, ledB) ;
}
else { completed |= 0x4; }
// White Channel ///////////////////////////////////////////////////////
if (ledW < newW)
{
ledW++ ;
setPWM (pinW, ledW) ;
}
else if (ledW > newW)
{
ledW-- ;
setPWM (pinW, ledW) ;
}
else { completed |= 0x8; }
delay (delaytime) ;
}
#ifdef VERBOSE
Serial.print ("R: ") ;
Serial.println (ledR) ;
Serial.print ("G: ") ;
Serial.println (ledG) ;
Serial.print ("B: ") ;
Serial.println (ledB) ;
Serial.print ("W: ") ;
Serial.println (ledW) ;
#endif
}
uint16_t readT6K (void)
{
uint16_t D = analogRead (pinAMB) ;
/*
float U = D * 3.3 / 4095.0 ;
float I = U / 10000.0 ;
uint16_t lux = I * 1000000 * 2 ;
*/
return (D * 0.161172) ;
}
float readDS18B20 (void)
{
float temp = 0 ;
ds18b20.search () ;
temp = ds18b20. getTemperature() ;
ds18b20.resetsearch () ;
return temp ;
}
void motionISR (void)
{
// Set motion state bit (2) ////////////////////////////////////////////////
state |= 0x2 ;
RGB.control (true) ;
RGB.color (30, 255, 5) ;
}
void alertESR (const char *event, const char *data)
{
// Set event notification state bit (16) ///////////////////////////////////
state |= 0x16 ;
#ifdef VERBOSE
Serial.print (" -> Event Received: ") ;
Serial.println (data) ;
#endif
//FIXME: Well, do something with it
//setPWM (pinB, 255) ;
}
int setRGBW (String rgbwInt)
{
#ifdef VERBOSE
Serial.print ("setRGBW Called: ") ;
Serial.println (rgbwInt) ;
#endif
fadeTo (rgbwInt.toInt(), 20) ;
return 1 ;
}