118 lines
3.7 KiB
C
118 lines
3.7 KiB
C
#include <FS.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
//shutdown suggestion level
|
|
#define SHUTDOWN_SUGGESTION_DISABLED 0
|
|
#define SHUTDOWN_SUGGESTION_ON_BATT 1
|
|
#define SHUTDOWN_SUGGESTION_ON_BATT_75 2
|
|
#define SHUTDOWN_SUGGESTION_ON_BATT_50 3
|
|
#define SHUTDOWN_SUGGESTION_ON_BATT_25 4
|
|
#define SHUTDOWN_SUGGESTION_ON_BATT_CRITICAL 5
|
|
static volatile uint8_t cfgShutdownSuggestion = SHUTDOWN_SUGGESTION_ON_BATT_25;
|
|
|
|
#define AUTO_POWER_OFF 0
|
|
#define AUTO_POWER_ON 1
|
|
#define AUTO_POWER_PERSIST 2
|
|
static volatile uint8_t cfgAutoOn = AUTO_POWER_ON;//AUTO_POWER_PERSIST;//AUTO_POWER_ON;
|
|
|
|
#define SHUTDOWN_DURATION_DEFAULT 120
|
|
static volatile uint16_t cfgShutdownInSeconds = SHUTDOWN_DURATION_DEFAULT;//default time in seconds for shutdown
|
|
|
|
//full charge trigger:
|
|
#define INPUT_LOST_TRIGGER_CHARGE_NONE 0
|
|
#define INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_ALWAYS 1 //trigger full charge if below 100 percent
|
|
#define INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_95 2 //trigger full charge if below 95 percent
|
|
#define INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_90 3 //trigger full charge if below 90 percent
|
|
#define INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_85 4 //trigger full charge if below 85 percent
|
|
#define INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_80 5 //trigger full charge if below 80 percent
|
|
static volatile uint8_t cfgInputLostTriggerChargeMode = INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_90;
|
|
|
|
#define CHARGE_MODE_ALWAYS 0
|
|
#define CHARGE_MODE_RANGE_90_100 1
|
|
#define CHARGE_MODE_RANGE_85_100 2
|
|
#define CHARGE_MODE_RANGE_80_100 3
|
|
#define CHARGE_MODE_RANGE_90_95 4
|
|
#define CHARGE_MODE_RANGE_85_95 5
|
|
#define CHARGE_MODE_RANGE_80_95 6
|
|
#define CHARGE_MODE_RANGE_85_90 7
|
|
#define CHARGE_MODE_RANGE_80_90 8
|
|
#define CHARGE_MODE_RANGE_80_85 9
|
|
static volatile uint8_t cfgChargeMode = CHARGE_MODE_RANGE_85_90;
|
|
|
|
static volatile uint8_t cfgChargeOvervoltageProtection = 1;
|
|
static volatile uint8_t cfgChargeAutomaticReducer = 1;
|
|
|
|
bool spiffsOk = false;
|
|
|
|
bool initConfigs(){
|
|
//setup spiffs
|
|
spiffsOk = SPIFFS.begin();
|
|
if (!spiffsOk){
|
|
Serial.println("SPIFFS ERROR!");
|
|
}else{
|
|
//LoadCfg();
|
|
}
|
|
return spiffsOk;
|
|
}
|
|
|
|
void loadCfg(){
|
|
if (!spiffsOk) return;
|
|
// Open file for reading
|
|
File file = SPIFFS.open("/cfg.json", "r");
|
|
if (!file) {
|
|
Serial.println("Failed to open config");
|
|
return;
|
|
}
|
|
|
|
// Parse file contents as JSON
|
|
DynamicJsonDocument jsonDoc(255);
|
|
DeserializationError error = deserializeJson(jsonDoc, file);
|
|
file.close();
|
|
|
|
if (error) {
|
|
Serial.println("Failed to parse file settings");
|
|
return;
|
|
}
|
|
|
|
uint8_t tmpAutoOnMode = jsonDoc["auto"].as<int>();
|
|
cfgAutoOn = tmpAutoOnMode;
|
|
if (cfgAutoOn>AUTO_POWER_PERSIST) cfgAutoOn = AUTO_POWER_PERSIST;
|
|
|
|
uint8_t v = jsonDoc["shut"].as<int>();
|
|
if (v>SHUTDOWN_SUGGESTION_ON_BATT_CRITICAL) v = SHUTDOWN_SUGGESTION_ON_BATT_25;
|
|
cfgShutdownSuggestion = v;
|
|
|
|
v = jsonDoc["chgMode"].as<int>();
|
|
if (v>CHARGE_MODE_RANGE_80_85) v = CHARGE_MODE_RANGE_90_100;
|
|
cfgChargeMode = v;
|
|
|
|
v = jsonDoc["chgILTC"].as<int>();
|
|
if (v>INPUT_LOST_TRIGGER_CHARGE_ACTIVE_FULL_80) v = INPUT_LOST_TRIGGER_CHARGE_NONE;
|
|
cfgInputLostTriggerChargeMode = v;
|
|
|
|
v = jsonDoc["chgOVP"].as<int>();
|
|
if (v>1) v = 1;
|
|
cfgChargeOvervoltageProtection = v;
|
|
|
|
v = jsonDoc["chgAR"].as<int>();
|
|
if (v>1) v = 1;
|
|
cfgChargeAutomaticReducer = v;
|
|
}
|
|
|
|
void saveCfg(){
|
|
if (!spiffsOk) return;
|
|
DynamicJsonDocument jsonDoc(255);
|
|
jsonDoc["auto"] = cfgAutoOn;
|
|
jsonDoc["shut"] = cfgShutdownSuggestion;
|
|
jsonDoc["chgMode"] = cfgChargeMode;
|
|
jsonDoc["chgILTC"] = cfgInputLostTriggerChargeMode;
|
|
jsonDoc["chgOVP"] = cfgChargeOvervoltageProtection;
|
|
jsonDoc["chgAR"] = cfgChargeAutomaticReducer;
|
|
File file = SPIFFS.open("/cfg.json", "w+");
|
|
if (file) {
|
|
uint32_t sz = serializeJson(jsonDoc, file);
|
|
file.close();
|
|
Serial.println("Configuration updated!");
|
|
}
|
|
}
|