Skip to content

Commit 7425b43

Browse files
committed
(WIP) adjust Battery usermod for "MM Style"
seems to work, but still needs more testing.
1 parent e28d303 commit 7425b43

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

usermods/Battery/usermod_v2_Battery.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class UsermodBattery : public Usermod
6060
bool initializing = true;
6161

6262
// strings to reduce flash memory usage (used more than twice)
63-
static const char _name[];
63+
// static const char _name[];
6464
static const char _readInterval[];
65-
static const char _enabled[];
65+
// static const char _enabled[];
6666
static const char _threshold[];
6767
static const char _preset[];
6868
static const char _duration[];
@@ -117,6 +117,7 @@ class UsermodBattery : public Usermod
117117
float readVoltage()
118118
{
119119
#ifdef ARDUINO_ARCH_ESP32
120+
if ((batteryPin <0) || !pinManager.isPinAnalog(batteryPin)) return(-1.0f); // WLEDMM avoid reading from invalid pin
120121
// use calibrated millivolts analogread on esp32 (150 mV ~ 2450 mV default attentuation) and divide by 1000 to get from milivolts to volts and multiply by voltage multiplier and apply calibration value
121122
return (analogReadMilliVolts(batteryPin) / 1000.0f) * voltageMultiplier + calibration;
122123
#else
@@ -127,26 +128,29 @@ class UsermodBattery : public Usermod
127128

128129
public:
129130
//Functions called by WLED
131+
UsermodBattery(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM: this shouldn't be necessary (passthrough of constructor), maybe because Usermod is an abstract class
130132

131133
/*
132134
* setup() is called once at boot. WiFi is not yet connected at this point.
133135
* You can use it to initialize variables, sensors or similar.
134136
*/
135137
void setup()
136138
{
139+
if (!enabled) return; // WLEDMM
137140
#ifdef ARDUINO_ARCH_ESP32
138141
bool success = false;
139142
DEBUG_PRINTLN(F("Allocating battery pin..."));
140-
if (batteryPin >= 0 && digitalPinToAnalogChannel(batteryPin) >= 0)
143+
if ((batteryPin >= 0) && (digitalPinToAnalogChannel(batteryPin) >= 0))
141144
if (pinManager.allocatePin(batteryPin, false, PinOwner::UM_Battery)) {
142145
DEBUG_PRINTLN(F("Battery pin allocation succeeded."));
143146
success = true;
144147
voltage = readVoltage();
145148
}
146149

147150
if (!success) {
148-
DEBUG_PRINTLN(F("Battery pin allocation failed."));
151+
USER_PRINTLN(F("Battery pin allocation failed."));
149152
batteryPin = -1; // allocation failed
153+
enabled = false;
150154
} else {
151155
pinMode(batteryPin, INPUT);
152156
}
@@ -178,7 +182,13 @@ class UsermodBattery : public Usermod
178182
*/
179183
void loop()
180184
{
181-
if(strip.isUpdating()) return;
185+
// WLEDMM begin
186+
if (!enabled) return;
187+
if (batteryPin < 0) return;
188+
unsigned long currentTime = millis(); // get the current elapsed time
189+
if (strip.isUpdating() && (currentTime - lastTime < 30000)) return; // WLEDMM: be nice
190+
lastTime = currentTime;
191+
// WLEDMM end
182192

183193
lowPowerIndicator();
184194

@@ -252,6 +262,7 @@ class UsermodBattery : public Usermod
252262
JsonObject user = root["u"];
253263
if (user.isNull()) user = root.createNestedObject("u");
254264

265+
if (!enabled) return; // WLEDMM
255266
if (batteryPin < 0) {
256267
JsonArray infoVoltage = user.createNestedArray(F("Battery voltage"));
257268
infoVoltage.add(F("n/a"));
@@ -360,6 +371,8 @@ class UsermodBattery : public Usermod
360371
void addToConfig(JsonObject& root)
361372
{
362373
JsonObject battery = root.createNestedObject(FPSTR(_name)); // usermodname
374+
battery[F("enabled")] = enabled; // WLEDMM
375+
363376
#ifdef ARDUINO_ARCH_ESP32
364377
battery[F("pin")] = batteryPin;
365378
#endif
@@ -373,11 +386,11 @@ class UsermodBattery : public Usermod
373386
battery[FPSTR(_readInterval)] = readingInterval;
374387

375388
JsonObject ao = battery.createNestedObject(F("auto-off")); // auto off section
376-
ao[FPSTR(_enabled)] = autoOffEnabled;
389+
ao[F("auto-off-enabled")] = autoOffEnabled;
377390
ao[FPSTR(_threshold)] = autoOffThreshold;
378391

379392
JsonObject lp = battery.createNestedObject(F("indicator")); // low power section
380-
lp[FPSTR(_enabled)] = lowPowerIndicatorEnabled;
393+
lp[F("indicator-enabled")] = lowPowerIndicatorEnabled;
381394
lp[FPSTR(_preset)] = lowPowerIndicatorPreset; // dropdown trickery (String)lowPowerIndicatorPreset;
382395
lp[FPSTR(_threshold)] = lowPowerIndicatorThreshold;
383396
lp[FPSTR(_duration)] = lowPowerIndicatorDuration;
@@ -436,6 +449,10 @@ class UsermodBattery : public Usermod
436449
#endif
437450

438451
JsonObject battery = root[FPSTR(_name)];
452+
453+
bool configComplete = !battery.isNull(); // WLEDMM
454+
configComplete &= getJsonValue(battery[F("enabled")], enabled, true); // WLEDMM
455+
439456
if (battery.isNull())
440457
{
441458
DEBUG_PRINT(FPSTR(_name));
@@ -455,11 +472,11 @@ class UsermodBattery : public Usermod
455472
setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval);
456473

457474
JsonObject ao = battery[F("auto-off")];
458-
setAutoOffEnabled(ao[FPSTR(_enabled)] | autoOffEnabled);
475+
setAutoOffEnabled(ao[F("auto-off-enabled")] | autoOffEnabled); // WLEDMM
459476
setAutoOffThreshold(ao[FPSTR(_threshold)] | autoOffThreshold);
460477

461478
JsonObject lp = battery[F("indicator")];
462-
setLowPowerIndicatorEnabled(lp[FPSTR(_enabled)] | lowPowerIndicatorEnabled);
479+
setLowPowerIndicatorEnabled(lp[F("indicator-enabled")] | lowPowerIndicatorEnabled); // WLEDMM
463480
setLowPowerIndicatorPreset(lp[FPSTR(_preset)] | lowPowerIndicatorPreset); // dropdown trickery (int)lp["preset"]
464481
setLowPowerIndicatorThreshold(lp[FPSTR(_threshold)] | lowPowerIndicatorThreshold);
465482
lowPowerIndicatorReactivationThreshold = lowPowerIndicatorThreshold+10;
@@ -490,7 +507,7 @@ class UsermodBattery : public Usermod
490507
}
491508
#endif
492509

493-
return !battery[FPSTR(_readInterval)].isNull();
510+
return configComplete && (!battery[FPSTR(_readInterval)].isNull()); // WLEDMM
494511
}
495512

496513
/*
@@ -780,9 +797,9 @@ class UsermodBattery : public Usermod
780797
};
781798

782799
// strings to reduce flash memory usage (used more than twice)
783-
const char UsermodBattery::_name[] PROGMEM = "Battery";
800+
// const char UsermodBattery::_name[] PROGMEM = "Battery";
784801
const char UsermodBattery::_readInterval[] PROGMEM = "interval";
785-
const char UsermodBattery::_enabled[] PROGMEM = "enabled";
802+
//const char UsermodBattery::_enabled[] PROGMEM = "enabled";
786803
const char UsermodBattery::_threshold[] PROGMEM = "threshold";
787804
const char UsermodBattery::_preset[] PROGMEM = "preset";
788805
const char UsermodBattery::_duration[] PROGMEM = "duration";

wled00/usermods_list.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void registerUsermods()
222222
*/
223223
//usermods.add(new MyExampleUsermod());
224224
#ifdef USERMOD_BATTERY
225-
usermods.add(new UsermodBattery());
225+
usermods.add(new UsermodBattery("Battery", true));
226226
#endif
227227

228228
#ifdef USERMOD_DALLASTEMPERATURE

0 commit comments

Comments
 (0)