Skip to content

Commit f520408

Browse files
Akubiuser2684
authored andcommitted
Timer rollover and other fixes (#440)
1 parent d4c4c34 commit f520408

File tree

9 files changed

+69
-16
lines changed

9 files changed

+69
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ The following methods are available for all the child:
493493
float getValueFloat();
494494
double getValueDouble();
495495
const char* getValueString();
496+
// check if the value must be sended back to the controller
497+
bool valueReadyToSend();
496498
// send the current value to the gateway
497499
void sendValue(bool force = 0);
498500
// print the current value on a LCD display
@@ -508,6 +510,8 @@ The following methods are available for all the child:
508510
void setMaxThreshold(float value);
509511
// do not report values if too close to the previous one (default: 0)
510512
void setValueDelta(float value);
513+
// set when the last value is updated. Possible values are UPDATE_ALWAYS (at every cycle), UPDATE_ON_SEND (only after sending) (default: UPDATE_ON_SEND)
514+
void setUpdateLastValue(last_value_mode value);
511515
// get the last value of the child
512516
int getLastValueInt();
513517
float getLastValueFloat();

nodemanager/Child.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,38 +136,60 @@ const char* Child::getValueString(){
136136
return _value_string;
137137
}
138138

139-
// send the value back to the controller
140-
void Child::sendValue(bool force) {
139+
140+
// check if the value must be sended back to the controller
141+
bool Child::valueReadyToSend() {
141142
// do not send if no samples have been collected, unless instructed otherwise
142-
if (! _send_without_value && _samples == 0) return;
143+
if (!_send_without_value && _samples == 0) return false;
143144
#if NODEMANAGER_CONDITIONAL_REPORT == ON
144-
// ignore conditional reporting settings if forced to report or if it is the first run
145-
if (! force || ! _sensor->getFirstRun()) {
145+
// ignore conditional reporting settings if it is the first run
146+
if (!_sensor->getFirstRun()) {
146147
// if the value is a number
147148
if (_format != STRING) {
148149
// if below or above the thresholds, do not send the value
149-
if (_value < _min_threshold || _value > _max_threshold) return;
150-
// if the force update timer is over, send the value regardless and restart it
151-
if (_force_update_timer->isOver()) _force_update_timer->start();
152-
else {
150+
if (_value < _min_threshold || _value > _max_threshold) return false;
151+
// if the force update timer is over, send the value
152+
if (!_force_update_timer->isOver()) {
153153
// if the value does not differ enough from the previous one, do not send the value
154154
if (_value > (_last_value - _value_delta) && _value < (_last_value + _value_delta)) {
155-
return;
155+
return false;
156156
}
157157
}
158158
}
159159
// if the value is a string
160160
else {
161161
// if a delta is configured, do not report if the string is the same as the previous one
162162
if (_value_delta > 0 && strcmp(_value_string, _last_value_string) == 0) {
163-
return;
163+
return false;
164164
}
165165
}
166166
}
167+
#endif
168+
return true;
169+
}
170+
171+
172+
// send the value back to the controller
173+
void Child::sendValue(bool force) {
174+
175+
#if NODEMANAGER_CONDITIONAL_REPORT == ON
176+
// update last value if mode = UPDATE_ALWAYS
177+
if (_last_value_mode == UPDATE_ALWAYS) _last_value = _value;
178+
#endif
179+
180+
// ignore conditional reporting settings if forced to report
181+
if (!force && !valueReadyToSend()) return;
182+
183+
// we report value
184+
#if NODEMANAGER_CONDITIONAL_REPORT == ON
185+
// if the force update timer is over restart it
186+
if (_force_update_timer->isOver()) _force_update_timer->start();
187+
167188
// keep track of the previous value
168189
if (_format != STRING) _last_value = _value;
169190
else _last_value_string = _value_string;
170191
#endif
192+
171193
// send the value to the gateway
172194
if (_format == INT) nodeManager.sendMessage(_child_id,_type,(int)_value);
173195
if (_format == FLOAT) nodeManager.sendMessage(_child_id,_type,(float)_value,_float_precision);
@@ -217,6 +239,9 @@ void Child::setMaxThreshold(float value) {
217239
void Child::setValueDelta(float value) {
218240
_value_delta = value;
219241
}
242+
void Child::setUpdateLastValue(last_value_mode value) {
243+
_last_value_mode = value;
244+
}
220245

221246
// return the last value of the child in the requested format
222247
int Child::getLastValueInt() {
@@ -232,6 +257,7 @@ const char* Child::getLastValueString(){
232257
return _last_value_string;
233258
}
234259
#endif
260+
235261
#if NODEMANAGER_EEPROM == ON
236262
// setter/getter
237263
void Child::setPersistValue(bool value) {

nodemanager/Child.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class Child {
7373
float getValueFloat();
7474
double getValueDouble();
7575
const char* getValueString();
76+
// check if the value must be sended back to the controller
77+
bool valueReadyToSend();
7678
// send the current value to the gateway
7779
void sendValue(bool force = 0);
7880
// print the current value on a LCD display
@@ -88,6 +90,8 @@ class Child {
8890
void setMaxThreshold(float value);
8991
// do not report values if too close to the previous one (default: 0)
9092
void setValueDelta(float value);
93+
// set when the last value is updated. Possible values are UPDATE_ALWAYS (at every cycle), UPDATE_ON_SEND (only after sending) (default: UPDATE_ON_SEND)
94+
void setUpdateLastValue(last_value_mode value);
9195
// get the last value of the child
9296
int getLastValueInt();
9397
float getLastValueFloat();
@@ -125,6 +129,7 @@ class Child {
125129
float _min_threshold = -FLT_MAX;
126130
float _max_threshold = FLT_MAX;
127131
float _value_delta = 0;
132+
last_value_mode _last_value_mode = UPDATE_ON_SEND;
128133
#endif
129134
#if NODEMANAGER_EEPROM == ON
130135
bool _persist_value = false;

nodemanager/Constants.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ enum value_format {
6767
STRING
6868
};
6969

70+
#if NODEMANAGER_CONDITIONAL_REPORT == ON
71+
// define update last value modes
72+
enum last_value_mode {
73+
UPDATE_ALWAYS,
74+
UPDATE_ON_SEND
75+
};
76+
#endif
77+
7078
/***********************************
7179
Chip type
7280
*/

nodemanager/Node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ NodeManager::NodeManager(uint8_t sensor_count) {
3030
// allocate block for all the sensors if sensor_count is provided
3131
if (sensor_count > 0) sensors.allocateBlocks(sensor_count);
3232
// setup serial port baud rate
33+
#ifndef CHIP_NRF5
3334
MY_SERIALDEVICE.begin(MY_BAUD_RATE);
35+
#endif
3436
// print out the version
3537
debug(PSTR(LOG_INIT "VER=" VERSION "\n"));
3638
// print out sketch name

nodemanager/Timer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ Timer::Timer() {
2727
nodeManager.registerTimer(this);
2828
}
2929

30+
// set the timer mode
3031
void Timer::setMode(timer_mode mode) {
3132
_mode = mode;
3233
}
34+
35+
// get the timer mode
3336
timer_mode Timer::getMode() {
3437
return _mode;
3538
}
3639

40+
// set the timer value in seconds
3741
void Timer::setValue(unsigned long value) {
3842
_value = value;
3943
}
4044

45+
// get the timer value in seconds
4146
unsigned long Timer::getValue() {
4247
return _value;
4348
}
@@ -104,8 +109,8 @@ bool Timer::isOver() {
104109
}
105110
#else
106111
if (_mode == TIME_INTERVAL) {
107-
// check if the current millis() is greater than the target
108-
if (millis() > _target) return true;
112+
// check for a millis() rollover. The difference between millis() and the target (in seconds) cannot be less than value
113+
if ( (_target - millis()) >= (_value * 1000UL) ) return true;
109114
return false;
110115
}
111116
#endif

nodemanager/Timer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Timer {
4242
bool isOver();
4343
private:
4444
timer_mode _mode = NOT_CONFIGURED;
45-
unsigned long _value = 0;
46-
unsigned long _target = 0;
45+
unsigned long _value = 0; // s
46+
unsigned long _target = 0; // ms
4747
bool _is_running = false;
4848
#if NODEMANAGER_TIME == ON
4949
bool _already_reported = false;

sensors/Display.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class Display: public Sensor {
3535
new Child(this,STRING,nodeManager.getAvailableChildId(child_id), S_INFO, V_TEXT,_name);
3636
// prevent reporting to the gateway at each display update
3737
setReportTimerMode(DO_NOT_REPORT);
38+
// refresh the display every minute by default
39+
setMeasureTimerValue(TIME_INTERVAL);
40+
setMeasureTimerValue(60UL);
3841
};
3942

4043
// set a static caption text on header of the display

sensors/SensorBattery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class SensorBattery: public Sensor {
9797
}
9898
volt = volt * _battery_adj_factor;
9999
child->setValue(volt);
100-
if (_send_battery_level) {
100+
if (_send_battery_level && child->valueReadyToSend()) {
101101
// calculate the percentage
102102
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
103103
if (percentage > 100) percentage = 100;

0 commit comments

Comments
 (0)