diff --git a/usermods/Battery/battery.h b/usermods/Battery/battery.h index ad3fb2703..3a792aadb 100644 --- a/usermods/Battery/battery.h +++ b/usermods/Battery/battery.h @@ -30,14 +30,7 @@ class Battery } - virtual void update(batteryConfig cfg) - { - if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); - if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); - if(cfg.calibration) this->setCapacity(cfg.calibration); - if(cfg.level) this->setLevel(cfg.level); - if(cfg.calibration) this->setCalibration(cfg.calibration); - } + virtual void update(batteryConfig cfg) = 0; /** * Corresponding battery curves diff --git a/usermods/Battery/lion.h b/usermods/Battery/lion.h index 4016af7e3..17a4b3593 100644 --- a/usermods/Battery/lion.h +++ b/usermods/Battery/lion.h @@ -22,6 +22,15 @@ class Lion : public Battery this->setCalibration(USERMOD_BATTERY_LION_CALIBRATION); } + void update(batteryConfig cfg) + { + if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); + if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); + if(cfg.calibration) this->setCapacity(cfg.calibration); + if(cfg.level) this->setLevel(cfg.level); + if(cfg.calibration) this->setCalibration(cfg.calibration); + } + float mapVoltage(float v, float min, float max) override { return this->linearMapping(v, min, max); // basic mapping diff --git a/usermods/Battery/lipo.h b/usermods/Battery/lipo.h index 03eed7b86..dcd44567f 100644 --- a/usermods/Battery/lipo.h +++ b/usermods/Battery/lipo.h @@ -22,6 +22,15 @@ class Lipo : public Battery this->setCalibration(USERMOD_BATTERY_LIPO_CALIBRATION); } + void update(batteryConfig cfg) + { + if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); + if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); + if(cfg.calibration) this->setCapacity(cfg.calibration); + if(cfg.level) this->setLevel(cfg.level); + if(cfg.calibration) this->setCalibration(cfg.calibration); + } + /** * LiPo batteries have a differnt dischargin curve, see * https://blog.ampow.com/lipo-voltage-chart/ diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index ab2ab908d..5cf6ac792 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -132,6 +132,7 @@ class UsermodBattery : public Usermod } bat->update(bcfg); + nextReadTime = millis() + readingInterval; lastReadTime = millis(); @@ -316,7 +317,7 @@ class UsermodBattery : public Usermod battery[F("pin")] = batteryPin; #endif - battery[F("type")] = (String)bcfg.type; + battery[F("type")] = (String)bcfg.type; // has to be a String otherwise it won't get converted to a Dropdown battery[F("min-voltage")] = bat->getMinVoltage(); battery[F("max-voltage")] = bat->getMaxVoltage(); battery[F("capacity")] = bat->getCapacity(); @@ -404,9 +405,6 @@ class UsermodBattery : public Usermod getJsonValue(battery[F("capacity")], bcfg.capacity); getJsonValue(battery[F("calibration")], bcfg.calibration); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); - - // JsonArray type = battery[F("Type")]; - // batteryType = type["bt"] | btype; JsonObject ao = battery[F("auto-off")]; setAutoOffEnabled(ao[FPSTR(_enabled)] | autoOffEnabled); @@ -444,7 +442,10 @@ class UsermodBattery : public Usermod } #endif - return !battery[F("min-voltage")].isNull(); + if(initDone) + bat->update(bcfg); + + return !battery[FPSTR(_readInterval)].isNull(); } /*