forked from mysensors/NodeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeManagerLibrary.ino
5218 lines (4816 loc) · 154 KB
/
NodeManagerLibrary.ino
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NodeManager Library
*/
/***************************************
PowerManager
*/
#if FEATURE_POWER_MANAGER == ON
PowerManager::PowerManager(int ground_pin, int vcc_pin, int wait_time) {
setPowerPins(ground_pin, vcc_pin, wait_time);
}
// set the vcc and ground pin the sensor is connected to
void PowerManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
_ground_pin = ground_pin;
_vcc_pin = vcc_pin;
#if FEATURE_DEBUG == ON
Serial.print(F("PWR G="));
Serial.print(_ground_pin);
Serial.print(F(" V="));
Serial.println(_vcc_pin);
#endif
if (_ground_pin > 0) {
// configure the ground pin as output and initialize to low
pinMode(_ground_pin, OUTPUT);
digitalWrite(_ground_pin, LOW);
}
if (_vcc_pin > 0) {
// configure the vcc pin as output and initialize to high (power on)
pinMode(_vcc_pin, OUTPUT);
digitalWrite(_vcc_pin, HIGH);
}
// save wait time
_wait = wait_time;
}
// turn on the sensor by activating its power pins
void PowerManager::powerOn() {
if (_vcc_pin == -1) return;
#if FEATURE_DEBUG == ON
Serial.print(F("ON P="));
Serial.println(_vcc_pin);
#endif
// power on the sensor by turning high the vcc pin
digitalWrite(_vcc_pin, HIGH);
// wait a bit for the device to settle down
if (_wait > 0) wait(_wait);
}
// turn off the sensor
void PowerManager::powerOff() {
if (_vcc_pin == -1) return;
#if FEATURE_DEBUG == ON
Serial.print(F("OFF P="));
Serial.println(_vcc_pin);
#endif
// power off the sensor by turning low the vcc pin
digitalWrite(_vcc_pin, LOW);
}
#endif
/******************************************
Timer
*/
Timer::Timer(NodeManager* node_manager) {
_node = node_manager;
}
// start the timer
void Timer::start(int target, int unit) {
set(target,unit);
start();
}
void Timer::start() {
if (_is_configured) _is_running = true;
#if FEATURE_TIME == ON
// keep track of the time the timer has started
_last = now();
#endif
}
// stop the timer
void Timer::stop() {
_is_running = false;
}
// reset the timer
void Timer::reset() {
// reset the timer
_elapsed = 0;
_last = 0;
}
// restart the timer
void Timer::restart() {
if (! isRunning()) return;
stop();
reset();
// if using millis(), keep track of the current timestamp for calculating the difference
if (! _node->isSleepingNode()) _last = millis();
start();
}
// setup the timer
void Timer::set(int target, int unit) {
reset();
// save the settings
_target = target;
if (unit == MINUTES) _target = _target * 60;
else if (unit == HOURS) _target = _target * 60 *60;
else if (unit == DAYS) _target = _target * 60 * 60 *24;
_is_running = false;
_is_configured = true;
}
// unset the timer
void Timer::unset() {
stop();
_is_configured = true;
}
// update the timer at every cycle
void Timer::update() {
if (! isRunning()) return;
#if FEATURE_TIME == ON
// system time is available so use now() to calculated the elapsed time
_elapsed = (long)(now() - _last);
#else
#if FEATURE_SLEEP == ON
if (_node->isSleepingNode()) {
// millis() is not reliable while sleeping so calculate how long a sleep cycle would last in seconds and update the elapsed time
_elapsed += _node->getSleepSeconds();
}
#endif
if (! _node->isSleepingNode()) {
// use millis() to calculate the elapsed time in seconds
_elapsed = (long)((millis() - _last)/1000);
}
#endif
_first_run = false;
}
// return true if the time is over
bool Timer::isOver() {
if (! isRunning()) return false;
// time has elapsed
if (_elapsed >= _target) return true;
// millis has started over
if (_elapsed < 0 ) return true;
return false;
}
// return true if the timer is running
bool Timer::isRunning() {
if (! isConfigured()) return false;
return _is_running;
}
// return true if the time is configured
bool Timer::isConfigured() {
return _is_configured;
}
// return true if this is the first time the timer runs
bool Timer::isFirstRun() {
return _first_run;
}
// return elapsed seconds so far
float Timer::getElapsed() {
return _elapsed;
}
/******************************************
Request
*/
// contructor, tokenize a request in the format "child_id,function,value"
Request::Request(int recipient_child_id, const char* string) {
_recipient_child_id = recipient_child_id;
char* ptr;
// tokenize the string and get child id
_child_id = atoi(strtok_r(const_cast<char*>(string), ",", &ptr));
// tokenize the string and get function id
_function = atoi(strtok_r(NULL, ",", &ptr));
// tokenize the string and get the value
_value = atof(strtok_r(NULL, ",", &ptr));
#if FEATURE_DEBUG == ON
Serial.print(F("REQ C="));
Serial.print(_child_id);
Serial.print(F(" F="));
Serial.print(_function);
Serial.print(F(" V="));
Serial.println(_value);
#endif
}
// return the child id
int Request::getRecipientChildId() {
return _recipient_child_id;
}
// return the child id
int Request::getChildId() {
return _child_id;
}
// return the parsed function
int Request::getFunction() {
return _function;
}
// return the value as an int
int Request::getValueInt() {
return (int)_value;
}
// return the value as a float
float Request::getValueFloat() {
return _value;
}
/******************************************
Sensors
*/
/*
Child class
*/
Child::Child() {
}
// constructor
Child::Child(Sensor* sensor, int child_id, int presentation, int type, const char* description) {
_child_id = child_id;
_presentation = presentation;
_type = type;
_description = description;
_sensor = sensor;
_sensor->registerChild(this);
#if FEATURE_CONDITIONAL_REPORT == ON
// initialize the timer for forcing updates to the gateway after a given timeframe
_force_update_timer = new Timer(_sensor->_node);
#endif
}
// setter/getter
void Child::setChildId(int value) {
_child_id = value;
}
int Child::getChildId() {
return _child_id;
}
void Child::setPresentation(int value) {
_presentation = value;
}
int Child::getPresentation() {
return _presentation;
}
void Child::setType(int value) {
_type = value;
}
int Child::getType() {
return _type;
}
void Child::setFloatPrecision(int value) {
_float_precision = value;
}
void Child::setDescription(const char* value) {
_description = value;
}
const char* Child::getDescription() {
return _description;
}
#if FEATURE_CONDITIONAL_REPORT == ON
void Child::setForceUpdateMinutes(int value) {
_force_update_timer->start(value,MINUTES);
}
void Child::setMinThreshold(float value) {
_min_threshold = value;
}
void Child::setMaxThreshold(float value) {
_max_threshold = value;
}
void Child::setValueDelta(float value) {
_value_delta = value;
}
#endif
// set a value, implemented by the subclasses
void Child::sendValue(bool force) {
}
// Print the child's value (variable type depending on the child class) to the given output
void Child::print(Print& device) {
}
// reset the counters
void Child::reset() {
}
/*
ChildInt class
*/
// ChildInt class
ChildInt::ChildInt(Sensor* sensor, int child_id, int presentation, int type, const char* description): Child(sensor, child_id, presentation, type, description) {
}
// store a new value and update the total
void ChildInt::setValueInt(int value) {
_total = _total + value;
_samples++;
// averages the values
_value = (int) (_total / _samples);
}
// return the value
int ChildInt::getValueInt() {
return _value;
}
// send the value back to the controller
void ChildInt::sendValue(bool force) {
if (_samples == 0) return;
#if FEATURE_CONDITIONAL_REPORT == ON
if (! force) {
// update the force update timer if running
if (_force_update_timer->isRunning()) _force_update_timer->update();
// if below or above the thresholds, do not send the value
if (_value < _min_threshold || _value > _max_threshold) return;
// if the force update timer is over, send the value regardless and restart it
if (_force_update_timer->isRunning() && _force_update_timer->isOver()) {
_force_update_timer->restart();
} else {
// if the value does not differ enough from the previous one, do not send the value
if (_value > (_last_value - _value_delta) && _value < (_last_value + _value_delta)) {
// keep track of the previous value
_last_value = _value;
return;
}
}
}
// keep track of the previous value
_last_value = _value;
#endif
// send the value to the gateway
_sensor->_node->sendMessage(_child_id,_type,_value);
reset();
}
// Print the child's value (variable type depending on the child class) to the given output
void ChildInt::print(Print& device) {
device.print(_value);
}
// reset the counters
void ChildInt::reset() {
_total = 0;
_samples = 0;
}
/*
ChildFloat class
*/
// ChildFloat class
ChildFloat::ChildFloat(Sensor* sensor, int child_id, int presentation, int type, const char* description): Child(sensor, child_id, presentation, type, description) {
_float_precision = 2;
}
// store a new value and update the total
void ChildFloat::setValueFloat(float value) {
_total = _total + value;
_samples++;
// averages the values
_value = _total / _samples;
// round the value if float precision has been customized
if (_float_precision != 2) {
if (_float_precision == 0) _value = (int) _value;
else _value = float((int) (_value * (_float_precision*10))) / (_float_precision*10);
}
}
// return the value
float ChildFloat::getValueFloat() {
return _value;
}
// send the value back to the controller
void ChildFloat::sendValue(bool force) {
if (_samples == 0) return;
#if FEATURE_CONDITIONAL_REPORT == ON
if (! force) {
// update the force update timer if running
if (_force_update_timer->isRunning()) _force_update_timer->update();
// if below or above the thresholds, do not send the value
if (_value < _min_threshold || _value > _max_threshold) return;
// if the force update timer is over, send the value regardless and restart it
if (_force_update_timer->isRunning() && _force_update_timer->isOver()) {
_force_update_timer->restart();
} else {
// if the value does not differ enough from the previous one, do not send the value
if (_value > (_last_value - _value_delta) && _value < (_last_value + _value_delta)) {
// keep track of the previous value
_last_value = _value;
return;
}
}
}
// keep track of the previous value
_last_value = _value;
#endif
// send the value to the gateway
_sensor->_node->sendMessage(_child_id,_type,_value,_float_precision);
}
// Print the child's value (variable type depending on the child class) to the given output
void ChildFloat::print(Print& device) {
device.print(_value,_float_precision);
}
// reset the counters
void ChildFloat::reset() {
_total = 0;
_samples = 0;
}
/*
ChildDouble class
*/
// ChildDouble class
ChildDouble::ChildDouble(Sensor* sensor, int child_id, int presentation, int type, const char* description): Child(sensor, child_id, presentation, type, description) {
_float_precision = 4;
}
// store a new value and update the total
void ChildDouble::setValueDouble(double value) {
_total = _total + value;
_samples++;
// averages the values
_value = _total / _samples;
// round the value if float precision has been customized
if (_float_precision != 4) {
if (_float_precision == 0) _value = (int) _value;
else _value = double((int) (_value * (_float_precision*10))) / (_float_precision*10);
}
}
// return the value
double ChildDouble::getValueDouble() {
return _value;
}
// send the value back to the controller
void ChildDouble::sendValue(bool force) {
if (_samples == 0) return;
#if FEATURE_CONDITIONAL_REPORT == ON
if (! force) {
// update the force update timer if running
if (_force_update_timer->isRunning()) _force_update_timer->update();
// if below or above the thresholds, do not send the value
if (_value < _min_threshold || _value > _max_threshold) return;
// if the force update timer is over, send the value regardless and restart it
if (_force_update_timer->isRunning() && _force_update_timer->isOver()) {
_force_update_timer->restart();
} else {
// if the value does not differ enough from the previous one, do not send the value
if (_value > (_last_value - _value_delta) && _value < (_last_value + _value_delta)) {
// keep track of the previous value
_last_value = _value;
return;
}
}
}
// keep track of the previous value
_last_value = _value;
#endif
// send the value to the gateway
_sensor->_node->sendMessage(_child_id,_type,_value,_float_precision);
}
// Print the child's value (variable type depending on the child class) to the given output
void ChildDouble::print(Print& device) {
device.print(_value,_float_precision);
}
// reset the counters
void ChildDouble::reset() {
_total = 0;
_samples = 0;
}
/*
ChildString class
*/
// ChildString class
ChildString::ChildString(Sensor* sensor, int child_id, int presentation, int type, const char* description): Child(sensor, child_id, presentation, type, description) {
}
// store a new value and update the total
void ChildString::setValueString(const char* value) {
_value = value;
}
// return the value
const char* ChildString::getValueString() {
return _value;
}
// send the value back to the controller
void ChildString::sendValue(bool force) {
#if FEATURE_CONDITIONAL_REPORT == ON
if (! force) {
// if a delta is configured, do not report if the string is the same as the previous one
if (_value_delta > 0 && strcmp(_value, _last_value) == 0) {
// keep track of the previous value
_last_value = _value;
return;
}
}
// keep track of the previous value
_last_value = _value;
#endif
// send the value to the gateway
_sensor->_node->sendMessage(_child_id,_type,_value);
}
// Print the child's value (variable type depending on the child class) to the given output
void ChildString::print(Print& device) {
device.print(_value);
}
// reset the counters
void ChildString::reset() {
_value = "";
}
/*
Sensor class
*/
// constructor
Sensor::Sensor() {
}
Sensor::Sensor(NodeManager& node_manager, int pin) {
_node = &node_manager;
_pin = pin;
_report_timer = new Timer(_node);
_node->registerSensor(this);
}
// return the name of the sensor
const char* Sensor::getName() {
return _name;
}
// setter/getter
void Sensor::setPin(int value) {
_pin = value;
}
int Sensor::getPin() {
return _pin;
}
void Sensor::setSamples(int value) {
_samples = value;
}
void Sensor::setSamplesInterval(int value) {
_samples_interval = value;
}
#if FEATURE_POWER_MANAGER == ON
void Sensor::setPowerPins(int ground_pin, int vcc_pin, int wait_time) {
if (_powerManager == nullptr) return;
_powerManager->setPowerPins(ground_pin, vcc_pin, wait_time);
}
void Sensor::powerOn() {
if (_powerManager == nullptr) return;
_powerManager->powerOn();
}
void Sensor::powerOff() {
if (_powerManager == nullptr) return;
_powerManager->powerOff();
}
#endif
#if FEATURE_INTERRUPTS == ON
int Sensor::getInterruptPin() {
return _interrupt_pin;
}
#endif
// After how many seconds the sensor will report back its measure
void Sensor::setReportIntervalSeconds(int value) {
_report_timer->start(value,SECONDS);
}
// After how many minutes the sensor will report back its measure
void Sensor::setReportIntervalMinutes(int value) {
_report_timer->start(value,MINUTES);
}
// After how many minutes the sensor will report back its measure
void Sensor::setReportIntervalHours(int value) {
_report_timer->start(value,HOURS);
}
// After how many minutes the sensor will report back its measure
void Sensor::setReportIntervalDays(int value) {
_report_timer->start(value,DAYS);
}
// return true if the report interval has been already configured
bool Sensor::isReportIntervalConfigured() {
return _report_timer->isConfigured();
}
#if FEATURE_INTERRUPTS == ON
// listen for interrupts on the given pin so interrupt() will be called when occurring
void Sensor::setInterrupt(int pin, int mode, int initial) {
_interrupt_pin = pin;
_node->setInterrupt(pin,mode,initial);
}
#endif
// register a child
void Sensor::registerChild(Child* child) {
children.push(child);
}
// present the sensor to the gateway and controller
void Sensor::presentation() {
for (List<Child*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
Child* child = *itr;
#if FEATURE_DEBUG == ON
Serial.print(F("PRES I="));
Serial.print(child->getChildId());
Serial.print(F(" T="));
Serial.print(child->getPresentation());
Serial.print(F(" D="));
Serial.println(child->getDescription());
#endif
present(child->getChildId(), child->getPresentation(), child->getDescription(), _node->getAck());
}
}
// call the sensor-specific implementation of before
void Sensor::before() {
onBefore();
for (List<Child*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
Child* child = *itr;
#if FEATURE_DEBUG == ON
Serial.print(_name);
Serial.print(F(" I="));
Serial.print(child->getChildId());
Serial.print(F(" P="));
Serial.print(child->getPresentation());
Serial.print(F(" T="));
Serial.println(child->getType());
#endif
}
}
// call the sensor-specific implementation of setup
void Sensor::setup() {
onSetup();
#if FEATURE_HOOKING == ON
// if a hook function is defined, call it
if (_setup_hook != 0) _setup_hook(this);
#endif
}
// call the sensor-specific implementation of loop
void Sensor::loop(MyMessage* message) {
// update the timers if within a loop cycle
if (message == nullptr) {
if (_report_timer->isRunning()) {
// keep track if it is the first time
bool first_run = _report_timer->isFirstRun();
// update the timer
_report_timer->update();
// if it is not the time yet to report a new measure, just return (unless it is the first time)
if (! _report_timer->isOver() && ! first_run) return;
}
}
#if FEATURE_HOOKING == ON
// if a hook function is defined, call it
if (_pre_loop_hook != 0) _pre_loop_hook(this);
#endif
// turn the sensor on
#if FEATURE_POWER_MANAGER == ON
powerOn();
#endif
// iterates over all the children
for (List<Child*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
Child* child = *itr;
// if a specific child is requested, skip all the others
if (message != nullptr && message->sensor != child->getChildId()) continue;
// collect multiple samples if needed
for (int i = 0; i < _samples; i++) {
// we've been called from receive(), pass the message along
if (message != nullptr) onReceive(message);
// we'be been called from loop()
else onLoop(child);
// wait between samples
if (_samples_interval > 0) _node->sleepOrWait(_samples_interval);
}
// send the value back to the controller
child->sendValue(message != nullptr);
// reset the counters
child->reset();
}
#if FEATURE_HOOKING == ON
// if a hook function is defined, call it
if (_post_loop_hook != 0) _post_loop_hook(this);
#endif
// turn the sensor off
#if FEATURE_POWER_MANAGER == ON
powerOff();
#endif
// if called from loop(), restart the report timer if over
if (message == nullptr && _report_timer->isRunning() && _report_timer->isOver()) _report_timer->restart();
}
#if FEATURE_INTERRUPTS == ON
// receive and handle an interrupt
void Sensor::interrupt() {
// call the implementation of onInterrupt()
onInterrupt();
#if FEATURE_HOOKING == ON
// if a hook function is defined, call it
if (_interrupt_hook != 0) _interrupt_hook(this);
#endif
}
#endif
#if FEATURE_RECEIVE == ON
// receive a message from the radio network
void Sensor::receive(MyMessage* message) {
// a request would make the sensor executing its main task passing along the message
loop(message);
#if FEATURE_HOOKING == ON
// if a hook function is defined, call it
if (_receive_hook != 0) _receive_hook(this,message);
#endif
}
#endif
// return the requested child
Child* Sensor::getChild(int child_id) {
for (List<Child*>::iterator itr = children.begin(); itr != children.end(); ++itr) {
Child* child = *itr;
if (child->getChildId() == child_id) return child;
}
return nullptr;
}
#if FEATURE_POWER_MANAGER == ON
void Sensor::setPowerManager(PowerManager& powerManager) {
_powerManager = &powerManager;
}
#endif
#if FEATURE_HOOKING == ON
void Sensor::setSetupHook(void (*function)(Sensor* sensor)) {
_setup_hook = function;
}
void Sensor::setPreLoopHook(void (*function)(Sensor* sensor)) {
_pre_loop_hook = function;
}
void Sensor::setPostLoopHook(void (*function)(Sensor* sensor)) {
_post_loop_hook = function;
}
void Sensor::setInterruptHook(void (*function)(Sensor* sensor)) {
_interrupt_hook = function;
}
void Sensor::setReceiveHook(void (*function)(Sensor* sensor, MyMessage* message)) {
_receive_hook = function;
}
#endif
// virtual functions
void Sensor::onBefore() {}
void Sensor::onSetup(){}
void Sensor::onLoop(Child* child){}
void Sensor::onReceive(MyMessage* message){}
void Sensor::onInterrupt(){}
#ifdef USE_BATTERY
/*
SensorBattery
*/
// contructor
SensorBattery::SensorBattery(NodeManager& node_manager, int child_id): Sensor(node_manager) {
_name = "BATTERY";
children.allocateBlocks(1);
new ChildFloat(this,child_id,S_MULTIMETER,V_VOLTAGE,_name);
// report battery level every 60 minutes by default
setReportIntervalMinutes(60);
}
void SensorBattery::setMinVoltage(float value) {
_battery_min = value;
}
void SensorBattery::setMaxVoltage(float value) {
_battery_max = value;
}
void SensorBattery::setBatteryInternalVcc(bool value) {
_battery_internal_vcc = value;
}
void SensorBattery::setBatteryPin(int value) {
_battery_pin = value;
}
void SensorBattery::setBatteryVoltsPerBit(float value) {
_battery_volts_per_bit = value;
}
// what to do during setup
void SensorBattery::onSetup() {
#ifdef CHIP_AVR
// when measuring the battery from a pin, analog reference must be internal
if (! _battery_internal_vcc && _battery_pin > -1)
#ifdef CHIP_MEGA
analogReference(INTERNAL1V1);
#else
analogReference(INTERNAL);
#endif
#endif
}
// what to do during loop
void SensorBattery::onLoop(Child* child) {
// measure the board vcc
float volt = 0;
if (_battery_internal_vcc || _battery_pin == -1) volt = _node->getVcc();
else volt = analogRead(_battery_pin) * _battery_volts_per_bit;
// calculate the percentage
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
#if FEATURE_DEBUG == ON
Serial.print(_name);
Serial.print(F(" V="));
Serial.print(volt);
Serial.print(F(" %="));
Serial.println(percentage);
#endif
((ChildFloat*)child)->setValueFloat(volt);
// report battery level percentage
sendBatteryLevel(percentage);
}
// what to do as the main task when receiving a message
void SensorBattery::onReceive(MyMessage* message) {
Child* child = getChild(message->sensor);
if (child == nullptr) return;
if (message->getCommand() == C_REQ && message->type == child->getType()) onLoop(child);
}
#endif
#ifdef USE_SIGNAL
/*
SensorSignal
*/
// contructor
SensorSignal::SensorSignal(NodeManager& node_manager, int child_id): Sensor(node_manager) {
_name = "SIGNAL";
children.allocateBlocks(1);
new ChildInt(this,child_id,S_SOUND,V_LEVEL,_name);
// report signal level every 60 minutes by default
setReportIntervalMinutes(60);
}
// setter/getter
void SensorSignal::setSignalCommand(int value) {
_signal_command = value;
}
// what to do during loop
void SensorSignal::onLoop(Child* child) {
int16_t value = transportGetSignalReport((signalReport_t)_signal_command);
#if FEATURE_DEBUG == ON
Serial.print(_name);
Serial.print(F(" V="));
Serial.println(value);
#endif
((ChildInt*)child)->setValueInt(value);
}
// what to do as the main task when receiving a message
void SensorSignal::onReceive(MyMessage* message) {
Child* child = getChild(message->sensor);
if (child == nullptr) return;
if (message->getCommand() == C_REQ && message->type == child->getType()) onLoop(child);
}
#endif
#ifdef USE_ANALOG_INPUT
/*
SensorAnalogInput
*/
// contructor
SensorAnalogInput::SensorAnalogInput(NodeManager& node_manager, int pin, int child_id): Sensor(node_manager, pin) {
_name = "ANALOG_I";
children.allocateBlocks(1);
new ChildInt(this,_node->getAvailableChildId(child_id),S_CUSTOM,V_CUSTOM,_name);
}
// setter/getter
void SensorAnalogInput::setReference(int value) {
_reference = value;
}
void SensorAnalogInput::setReverse(bool value) {
_reverse = value;
}
void SensorAnalogInput::setOutputPercentage(bool value) {
_output_percentage = value;
}
void SensorAnalogInput::setRangeMin(int value) {
_range_min = value;
}
void SensorAnalogInput::setRangeMax(int value) {
_range_max = value;
}
// what to do during setup
void SensorAnalogInput::onSetup() {
// prepare the pin for input
pinMode(_pin, INPUT);
}
// what to do during loop
void SensorAnalogInput::onLoop(Child* child) {
// read the input
int adc = _getAnalogRead();
// calculate the percentage
int percentage = 0;
if (_output_percentage) percentage = _getPercentage(adc);
#if FEATURE_DEBUG == ON
Serial.print(_name);
Serial.print(F(" I="));
Serial.print(child->getChildId());
Serial.print(F(" V="));
Serial.print(adc);
Serial.print(F(" %="));
Serial.println(percentage);
#endif
// store the result
((ChildInt*)child)->setValueInt(_output_percentage ? percentage : adc);
}
// what to do during loop
void SensorAnalogInput::onReceive(MyMessage* message) {
Child* child = getChild(message->sensor);
if (child == nullptr) return;
if (message->getCommand() == C_REQ && message->type == child->getType()) onLoop(child);
}
// read the analog input
int SensorAnalogInput::_getAnalogRead() {
#ifdef CHIP_AVR
// set the reference
if (_reference != -1) {
analogReference(_reference);
wait(100);
}
#endif
// read and return the value
int value = analogRead(_pin);
if (_reverse) value = _range_max - value;
return value;
}
// return a percentage from an analog value
int SensorAnalogInput::_getPercentage(int adc) {
float value = (float)adc;
// restore the original value
if (_reverse) value = _range_max - value;
// scale the percentage based on the range provided
float percentage = ((value - _range_min) / (_range_max - _range_min)) * 100;
if (_reverse) percentage = 100 - percentage;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
return (int)percentage;
}
/*
SensorLDR
*/
// contructor
SensorLDR::SensorLDR(NodeManager& node_manager, int pin, int child_id): SensorAnalogInput(node_manager, pin, child_id) {
_name = "LDR";
children.get(1)->setPresentation(S_LIGHT_LEVEL);