Skip to content

Commit 93338d6

Browse files
authored
Added setSendBatteryLevel() to SensorBattery
1 parent ba0230b commit 93338d6

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ Each sensor class may expose additional methods.
542542
void setBatteryVoltsPerBit(float value);
543543
// [107] change battery voltage calibration factor
544544
void setBatteryCalibrationFactor(float value);
545+
// if true call sendBatteryLevel() in addition to send the measured voltage back (default: true)
546+
void setSendBatteryLevel(bool value);
545547
~~~
546548

547549
* SensorSignal

sensors/SensorBattery.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SensorBattery: public Sensor {
3434
int8_t _battery_pin = -1;
3535
float _battery_volts_per_bit = 0.003363075;
3636
float _battery_adj_factor = 1.0;
37+
bool _send_battery_level = true;
3738

3839
public:
3940
SensorBattery(uint8_t child_id = BATTERY_CHILD_ID): Sensor(-1) {
@@ -74,6 +75,11 @@ class SensorBattery: public Sensor {
7475
_battery_adj_factor = value;
7576
};
7677

78+
// if true call sendBatteryLevel() in addition to send the measured voltage back (default: true)
79+
void setSendBatteryLevel(bool value) {
80+
_send_battery_level = value;
81+
};
82+
7783
// define what to do during loop
7884
void onLoop(Child* child) {
7985
// measure the battery
@@ -91,13 +97,15 @@ class SensorBattery: public Sensor {
9197
}
9298
volt = volt * _battery_adj_factor;
9399
child->setValue(volt);
94-
// calculate the percentage
95-
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
96-
if (percentage > 100) percentage = 100;
97-
if (percentage < 0) percentage = 0;
98-
// report battery level percentage
99-
sendBatteryLevel(percentage);
100-
nodeManager.sleepBetweenSend();
100+
if (_send_battery_level) {
101+
// calculate the percentage
102+
int percentage = ((volt - _battery_min) / (_battery_max - _battery_min)) * 100;
103+
if (percentage > 100) percentage = 100;
104+
if (percentage < 0) percentage = 0;
105+
// report battery level percentage
106+
if (_send_battery_level) sendBatteryLevel(percentage);
107+
nodeManager.sleepBetweenSend();
108+
}
101109
};
102110

103111
#if NODEMANAGER_OTA_CONFIGURATION == ON

0 commit comments

Comments
 (0)