From a13f1a9bee52ad637cf6903546f1bac4ef3df3da Mon Sep 17 00:00:00 2001 From: Maximilian Mewes Date: Tue, 30 Apr 2024 15:24:02 +0200 Subject: [PATCH] bug fixes --- usermods/Battery/battery.h | 61 +++++++++++++++------------ usermods/Battery/types/lion.h | 8 ---- usermods/Battery/types/lipo.h | 8 ---- usermods/Battery/usermod_v2_Battery.h | 43 ++++++++++++++++++- 4 files changed, 75 insertions(+), 45 deletions(-) diff --git a/usermods/Battery/battery.h b/usermods/Battery/battery.h index 4cdfb035f..084e6c0aa 100644 --- a/usermods/Battery/battery.h +++ b/usermods/Battery/battery.h @@ -32,7 +32,14 @@ class Battery this->setCalibration(USERMOD_BATTERY_CALIBRATION); } - virtual void update(batteryConfig cfg) = 0; + virtual void update(batteryConfig cfg) + { + if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); + if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); + if(cfg.level) this->setLevel(cfg.level); + if(cfg.calibration) this->setCalibration(cfg.calibration); + if(cfg.voltageMultiplier) this->setVoltageMultiplier(cfg.voltageMultiplier); + } /** * Corresponding battery curves @@ -49,10 +56,10 @@ class Battery /* - * - * Getter and Setter - * - */ + * + * Getter and Setter + * + */ /* * Get lowest configured battery voltage @@ -63,26 +70,26 @@ class Battery } /* - * Set lowest battery voltage - * can't be below 0 volt - */ + * Set lowest battery voltage + * can't be below 0 volt + */ virtual void setMinVoltage(float voltage) { this->minVoltage = max(0.0f, voltage); } /* - * Get highest configured battery voltage - */ + * Get highest configured battery voltage + */ virtual float getMaxVoltage() { return this->maxVoltage; } /* - * Set highest battery voltage - * can't be below minVoltage - */ + * Set highest battery voltage + * can't be below minVoltage + */ virtual void setMaxVoltage(float voltage) { this->maxVoltage = max(getMinVoltage()+.5f, voltage); @@ -110,43 +117,43 @@ class Battery void setLevel(float level) { - this->level = constrain(level, 0.0f, 110.0f);; + this->level = constrain(level, 0.0f, 110.0f); } /* - * Get the configured calibration value - * a offset value to fine-tune the calculated voltage. - */ + * Get the configured calibration value + * a offset value to fine-tune the calculated voltage. + */ virtual float getCalibration() { return calibration; } /* - * Set the voltage calibration offset value - * a offset value to fine-tune the calculated voltage. - */ + * Set the voltage calibration offset value + * a offset value to fine-tune the calculated voltage. + */ virtual void setCalibration(float offset) { calibration = offset; } /* - * Get the configured calibration value - * a value to set the voltage divider ratio - */ + * Get the configured calibration value + * a value to set the voltage divider ratio + */ virtual float getVoltageMultiplier() { return voltageMultiplier; } /* - * Set the voltage multiplier value - * a value to set the voltage divider ratio. - */ + * Set the voltage multiplier value + * a value to set the voltage divider ratio. + */ virtual void setVoltageMultiplier(float multiplier) { - voltageMultiplier = voltageMultiplier; + voltageMultiplier = multiplier; } }; diff --git a/usermods/Battery/types/lion.h b/usermods/Battery/types/lion.h index b3641e263..e77266164 100644 --- a/usermods/Battery/types/lion.h +++ b/usermods/Battery/types/lion.h @@ -19,14 +19,6 @@ class Lion : public Battery this->setMaxVoltage(USERMOD_BATTERY_LION_MAX_VOLTAGE); } - void update(batteryConfig cfg) - { - if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); - if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); - 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/types/lipo.h b/usermods/Battery/types/lipo.h index 1deb6e7d3..d732cf4da 100644 --- a/usermods/Battery/types/lipo.h +++ b/usermods/Battery/types/lipo.h @@ -19,14 +19,6 @@ class Lipo : public Battery this->setMaxVoltage(USERMOD_BATTERY_LIPO_MAX_VOLTAGE); } - void update(batteryConfig cfg) - { - if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); - if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); - if(cfg.level) this->setLevel(cfg.level); - if(cfg.calibration) this->setCalibration(cfg.calibration); - } - /** * LiPo batteries have a differnt discharge 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 a82a46667..b9631d6db 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -264,6 +264,7 @@ class UsermodBattery : public Usermod battery[F("min-voltage")] = bat->getMinVoltage(); battery[F("max-voltage")] = bat->getMaxVoltage(); battery[F("calibration")] = bat->getCalibration(); + battery[F("voltage-multiplier")] = bat->getVoltageMultiplier(); battery[FPSTR(_readInterval)] = readingInterval; JsonObject ao = battery.createNestedObject(F("auto-off")); // auto off section @@ -283,6 +284,7 @@ class UsermodBattery : public Usermod getJsonValue(battery[F("min-voltage")], cfg.minVoltage); getJsonValue(battery[F("max-voltage")], cfg.maxVoltage); getJsonValue(battery[F("calibration")], cfg.calibration); + getJsonValue(battery[F("voltage-multiplier")], cfg.voltageMultiplier); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); @@ -459,6 +461,7 @@ class UsermodBattery : public Usermod setMinBatteryVoltage(battery[F("min-voltage")] | bat->getMinVoltage()); setMaxBatteryVoltage(battery[F("max-voltage")] | bat->getMaxVoltage()); setCalibration(battery[F("calibration")] | calibration); + setVoltageMultiplier(battery[F("voltage-multiplier")] | voltageMultiplier); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); getUsermodConfigFromJsonObject(battery); @@ -537,7 +540,25 @@ class UsermodBattery : public Usermod return USERMOD_ID_BATTERY; } + /** + * get currently active battery type + */ + batteryType getBatteryType() + { + return cfg.type; + } + /** + * Set currently active battery type + */ + batteryType setBatteryType(batteryType type) + { + cfg.type = type; + } + + /** + * + */ unsigned long getReadingInterval() { return readingInterval; @@ -561,7 +582,7 @@ class UsermodBattery : public Usermod /** * Set lowest battery voltage - * cant be below 0 volt + * can't be below 0 volt */ void setMinBatteryVoltage(float voltage) { @@ -622,6 +643,24 @@ class UsermodBattery : public Usermod bat->setCalibration(offset); } + /** + * Set the voltage multiplier value + * A multiplier that may need adjusting for different voltage divider setups + */ + void setVoltageMultiplier(float multiplier) + { + bat->setVoltageMultiplier(multiplier); + } + + /* + * Get the voltage multiplier value + * A multiplier that may need adjusting for different voltage divider setups + */ + float getVoltageMultiplier() + { + return bat->getVoltageMultiplier(); + } + /** * Get auto-off feature enabled status * is auto-off enabled, true/false @@ -727,7 +766,7 @@ class UsermodBattery : public Usermod } /** - * Get low-power-indicator status when the indication is done thsi returns true + * Get low-power-indicator status when the indication is done this returns true */ bool getLowPowerIndicatorDone() {