diff --git a/usermods/Battery/battery.h b/usermods/Battery/battery.h index c678f775d..ad3fb2703 100644 --- a/usermods/Battery/battery.h +++ b/usermods/Battery/battery.h @@ -12,12 +12,12 @@ class Battery private: protected: - float minVoltage = USERMOD_BATTERY_MIN_VOLTAGE; - float maxVoltage = USERMOD_BATTERY_MAX_VOLTAGE; - unsigned int capacity = USERMOD_BATTERY_TOTAL_CAPACITY; // current capacity - float voltage = this->maxVoltage; // current voltage - int8_t level = 100; // current level - float calibration = USERMOD_BATTERY_CALIBRATION; // offset or calibration value to fine tune the calculated voltage + float minVoltage; + float maxVoltage; + unsigned int capacity; + float voltage; + int8_t level = 100; + float calibration; // offset or calibration value to fine tune the calculated voltage float linearMapping(float v, float min, float max, float oMin = 0.0f, float oMax = 100.0f) { @@ -30,6 +30,15 @@ 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); + } + /** * Corresponding battery curves * calculates the capacity in % (0-100) with given voltage and possible voltage range diff --git a/usermods/Battery/battery_defaults.h b/usermods/Battery/battery_defaults.h index 4a04ac352..f4060ca60 100644 --- a/usermods/Battery/battery_defaults.h +++ b/usermods/Battery/battery_defaults.h @@ -1,3 +1,6 @@ +#ifndef UMBDefaults_h +#define UMBDefaults_h + // pin defaults // for the esp32 it is best to use the ADC1: GPIO32 - GPIO39 // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html @@ -16,38 +19,70 @@ /* Default Battery Type + * 0 = unkown * 1 = Lipo * 2 = Lion */ -#ifndef USERMOB_BATTERY_DEFAULT_TYPE - #define USERMOB_BATTERY_DEFAULT_TYPE 1 +#ifndef USERMOD_BATTERY_DEFAULT_TYPE + #define USERMOD_BATTERY_DEFAULT_TYPE 0 +#endif +/* + * + * Unkown 'Battery' defaults + * + */ +#ifndef USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE + #define USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE 3.3f +#endif +#ifndef USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE + #define USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE 4.2f +#endif +#ifndef USERMOD_BATTERY_UNKOWN_CAPACITY + #define USERMOD_BATTERY_UNKOWN_CAPACITY 2500 +#endif +#ifndef USERMOD_BATTERY_UNKOWN_CALIBRATION + // offset or calibration value to fine tune the calculated voltage + #define USERMOD_BATTERY_UNKOWN_CALIBRATION 0 +#endif +/* + * + * Lithium polymer (Li-Po) defaults + * + */ +#ifndef USERMOD_BATTERY_LIPO_MIN_VOLTAGE + // LiPo "1S" Batteries should not be dischared below 3V !! + #define USERMOD_BATTERY_LIPO_MIN_VOLTAGE 3.2f +#endif +#ifndef USERMOD_BATTERY_LIPO_MAX_VOLTAGE + #define USERMOD_BATTERY_LIPO_MAX_VOLTAGE 4.2f +#endif +#ifndef USERMOD_BATTERY_LIPO_CAPACITY + #define USERMOD_BATTERY_LIPO_CAPACITY 5000 +#endif +#ifndef USERMOD_BATTERY_LIPO_CALIBRATION + #define USERMOD_BATTERY_LIPO_CALIBRATION 0 +#endif +/* + * + * Lithium-ion (Li-Ion) defaults + * + */ +#ifndef USERMOD_BATTERY_LION_MIN_VOLTAGE + // default for 18650 battery + #define USERMOD_BATTERY_LION_MIN_VOLTAGE 2.6f +#endif +#ifndef USERMOD_BATTERY_LION_MAX_VOLTAGE + #define USERMOD_BATTERY_LION_MAX_VOLTAGE 4.2f +#endif +#ifndef USERMOD_BATTERY_LION_CAPACITY + // a common capacity for single 18650 battery cells is between 2500 and 3600 mAh + #define USERMOD_BATTERY_LION_CAPACITY 3100 +#endif +#ifndef USERMOD_BATTERY_LION_CALIBRATION + // offset or calibration value to fine tune the calculated voltage + #define USERMOD_BATTERY_LION_CALIBRATION 0 #endif -// default for 18650 battery -// https://batterybro.com/blogs/18650-wholesale-battery-reviews/18852515-when-to-recycle-18650-batteries-and-how-to-start-a-collection-center-in-your-vape-shop -// Discharge voltage: 2.5 volt + .1 for personal safety -#ifndef USERMOD_BATTERY_MIN_VOLTAGE - #if USERMOB_BATTERY_DEFAULT_TYPE == 1 - // LiPo "1S" Batteries should not be dischared below 3V !! - #define USERMOD_BATTERY_MIN_VOLTAGE 3.2f - #else - #define USERMOD_BATTERY_MIN_VOLTAGE 2.6f - #endif -#endif - -#ifndef USERMOD_BATTERY_MAX_VOLTAGE - #define USERMOD_BATTERY_MAX_VOLTAGE 4.2f -#endif - -// a common capacity for single 18650 battery cells is between 2500 and 3600 mAh -#ifndef USERMOD_BATTERY_TOTAL_CAPACITY - #define USERMOD_BATTERY_TOTAL_CAPACITY 3100 -#endif - -// offset or calibration value to fine tune the calculated voltage -#ifndef USERMOD_BATTERY_CALIBRATION - #define USERMOD_BATTERY_CALIBRATION 0 -#endif // auto-off feature #ifndef USERMOD_BATTERY_AUTO_OFF_ENABLED @@ -74,3 +109,25 @@ #ifndef USERMOD_BATTERY_LOW_POWER_INDICATOR_DURATION #define USERMOD_BATTERY_LOW_POWER_INDICATOR_DURATION 5 #endif + +typedef enum +{ + unknown=0, + lipo=1, + lion=2 +} batteryType; + +// used for initial configuration after boot +typedef struct bconfig_t +{ + batteryType type; + float minVoltage; + float maxVoltage; + unsigned int capacity; // current capacity + float voltage; // current voltage + int8_t level; // current level + float calibration; // offset or calibration value to fine tune the calculated voltage +} batteryConfig; + + +#endif \ No newline at end of file diff --git a/usermods/Battery/lion.h b/usermods/Battery/lion.h index 69095ac09..4016af7e3 100644 --- a/usermods/Battery/lion.h +++ b/usermods/Battery/lion.h @@ -13,9 +13,13 @@ class Lion : public Battery private: public: - Lion() : Battery() + Lion() { - + this->setMinVoltage(USERMOD_BATTERY_LION_MIN_VOLTAGE); + this->setMaxVoltage(USERMOD_BATTERY_LION_MAX_VOLTAGE); + this->setCapacity(USERMOD_BATTERY_LION_CAPACITY); + this->setVoltage(this->getVoltage()); + this->setCalibration(USERMOD_BATTERY_LION_CALIBRATION); } float mapVoltage(float v, float min, float max) override diff --git a/usermods/Battery/lipo.h b/usermods/Battery/lipo.h index 92ede8f81..03eed7b86 100644 --- a/usermods/Battery/lipo.h +++ b/usermods/Battery/lipo.h @@ -5,7 +5,7 @@ #include "battery.h" /** - * Lipo Battery + * Lipo Battery * */ class Lipo : public Battery @@ -13,9 +13,13 @@ class Lipo : public Battery private: public: - Lipo() : Battery() + Lipo() { - + this->setMinVoltage(USERMOD_BATTERY_LIPO_MIN_VOLTAGE); + this->setMaxVoltage(USERMOD_BATTERY_LIPO_MAX_VOLTAGE); + this->setCapacity(USERMOD_BATTERY_LIPO_CAPACITY); + this->setVoltage(this->getVoltage()); + this->setCalibration(USERMOD_BATTERY_LIPO_CALIBRATION); } /** diff --git a/usermods/Battery/unkown.h b/usermods/Battery/unkown.h new file mode 100644 index 000000000..63b2674f3 --- /dev/null +++ b/usermods/Battery/unkown.h @@ -0,0 +1,36 @@ +#ifndef UMBUnkown_h +#define UMBUnkown_h + +#include "battery_defaults.h" +#include "battery.h" + +/** + * Lion Battery + * + */ +class Unkown : public Battery +{ + private: + + public: + Unkown() + { + this->setMinVoltage(USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE); + this->setMaxVoltage(USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE); + this->setCapacity(USERMOD_BATTERY_UNKOWN_CAPACITY); + this->setVoltage(this->getVoltage()); + this->setCalibration(USERMOD_BATTERY_UNKOWN_CALIBRATION); + } + + float mapVoltage(float v, float min, float max) override + { + return this->linearMapping(v, min, max); // basic mapping + }; + + void calculateAndSetLevel(float voltage) override + { + this->setLevel(this->mapVoltage(voltage, this->getMinVoltage(), this->getMaxVoltage())); + }; +}; + +#endif \ No newline at end of file diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index f9bfc96fb..ab2ab908d 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -3,6 +3,7 @@ #include "wled.h" #include "battery_defaults.h" #include "battery.h" +#include "unkown.h" #include "lion.h" #include "lipo.h" @@ -18,16 +19,9 @@ class UsermodBattery : public Usermod private: // battery pin can be defined in my_config.h int8_t batteryPin = USERMOD_BATTERY_MEASUREMENT_PIN; - - int8_t batteryType = USERMOB_BATTERY_DEFAULT_TYPE; - - float minVoltage = USERMOD_BATTERY_MIN_VOLTAGE; - float maxVoltage = USERMOD_BATTERY_MAX_VOLTAGE; - unsigned int capacity = USERMOD_BATTERY_TOTAL_CAPACITY; // current capacity - float voltage = this->maxVoltage; // current voltage - int8_t level = 100; // current level - float calibration = USERMOD_BATTERY_CALIBRATION; // offset or calibration value to fine tune the calculated voltage + Battery* bat = nullptr; + batteryConfig bcfg; // how often to read the battery voltage unsigned long readingInterval = USERMOD_BATTERY_MEASUREMENT_INTERVAL; @@ -127,16 +121,17 @@ class UsermodBattery : public Usermod pinMode(batteryPin, INPUT); #endif - //this could also be handled with a factory class but for only 2 types it should be sufficient for now - if(batteryType == 1) { - bat = new Lipo(); - } else - if(batteryType == 2) { - bat = new Lion(); - } else { - bat = new Lipo(); // in the future one could create a nullObject - } + //this could also be handled with a factory class but for only 2 types it should be sufficient for now + if(bcfg.type == (batteryType)lipo) { + bat = new Lipo(); + } else + if(bcfg.type == (batteryType)lion) { + bat = new Lion(); + } else { + bat = new Unkown(); // nullObject + } + bat->update(bcfg); nextReadTime = millis() + readingInterval; lastReadTime = millis(); @@ -321,12 +316,11 @@ class UsermodBattery : public Usermod battery[F("pin")] = batteryPin; #endif - if(bat) { - battery[F("min-voltage")] = bat->getMinVoltage(); - battery[F("max-voltage")] = bat->getMaxVoltage(); - battery[F("capacity")] = bat->getCapacity(); - battery[F("calibration")] = bat->getCalibration(); - } + battery[F("type")] = (String)bcfg.type; + battery[F("min-voltage")] = bat->getMinVoltage(); + battery[F("max-voltage")] = bat->getMaxVoltage(); + battery[F("capacity")] = bat->getCapacity(); + battery[F("calibration")] = bat->getCalibration(); battery[FPSTR(_readInterval)] = readingInterval; JsonObject ao = battery.createNestedObject(F("auto-off")); // auto off section @@ -344,6 +338,11 @@ class UsermodBattery : public Usermod void appendConfigData() { + oappend(SET_F("td=addDropdown('Battery', 'type');")); + oappend(SET_F("addOption(td, 'Unkown', '0');")); + oappend(SET_F("addOption(td, 'LiPo', '1');")); + oappend(SET_F("addOption(td, 'LiOn', '2');")); + oappend(SET_F("addInfo('Battery:type',1,'requires reboot');")); oappend(SET_F("addInfo('Battery:min-voltage', 1, 'v');")); oappend(SET_F("addInfo('Battery:max-voltage', 1, 'v');")); oappend(SET_F("addInfo('Battery:capacity', 1, 'mAh');")); @@ -399,16 +398,15 @@ class UsermodBattery : public Usermod newBatteryPin = battery[F("pin")] | newBatteryPin; #endif - if(bat) { - bat->setMinVoltage(battery[F("min-voltage")] | bat->getMinVoltage()); - bat->setMaxVoltage(battery[F("max-voltage")] | bat->getMaxVoltage()); - bat->setCapacity(battery[F("capacity")] | bat->getCapacity()); - bat->setCalibration(battery[F("calibration")] | bat->getCalibration()); - } + getJsonValue(battery[F("type")], bcfg.type); + getJsonValue(battery[F("min-voltage")], bcfg.minVoltage); + getJsonValue(battery[F("max-voltage")], bcfg.maxVoltage); + 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"] | batteryType; + // batteryType = type["bt"] | btype; JsonObject ao = battery[F("auto-off")]; setAutoOffEnabled(ao[FPSTR(_enabled)] | autoOffEnabled); @@ -446,7 +444,7 @@ class UsermodBattery : public Usermod } #endif - return !battery[FPSTR(_readInterval)].isNull(); + return !battery[F("min-voltage")].isNull(); } /*