Skip to content

Commit d33651c

Browse files
committed
Update setup method
1 parent 18e9f9c commit d33651c

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

usermods/Battery/usermod_v2_Battery.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,24 @@ class UsermodBattery : public Usermod
120120
*/
121121
void setup()
122122
{
123+
// plug in the right battery type
124+
if(cfg.type == (batteryType)lipo) {
125+
bat = new LipoUMBattery();
126+
} else if(cfg.type == (batteryType)lion) {
127+
bat = new LipoUMBattery();
128+
}
129+
130+
// update the choosen battery type with configured values
131+
bat->update(cfg);
132+
123133
#ifdef ARDUINO_ARCH_ESP32
124134
bool success = false;
125135
DEBUG_PRINTLN(F("Allocating battery pin..."));
126136
if (batteryPin >= 0 && digitalPinToAnalogChannel(batteryPin) >= 0)
127137
if (pinManager.allocatePin(batteryPin, false, PinOwner::UM_Battery)) {
128138
DEBUG_PRINTLN(F("Battery pin allocation succeeded."));
129139
success = true;
130-
voltage = readVoltage();
140+
bat->setVoltage(readVoltage());
131141
}
132142

133143
if (!success) {
@@ -138,20 +148,9 @@ class UsermodBattery : public Usermod
138148
}
139149
#else //ESP8266 boards have only one analog input pin A0
140150
pinMode(batteryPin, INPUT);
151+
bat->setVoltage(readVoltage());
141152
#endif
142153

143-
// plug in the right battery type
144-
if(cfg.type == (batteryType)lipo) {
145-
bat = new LipoUMBattery();
146-
} else if(cfg.type == (batteryType)lion) {
147-
bat = new LipoUMBattery();
148-
}
149-
150-
// update the choosen battery type with configured values
151-
bat->update(cfg);
152-
153-
bat->setVoltage(readVoltage());
154-
155154
nextReadTime = millis() + readingInterval;
156155
lastReadTime = millis();
157156

0 commit comments

Comments
 (0)