diff --git a/Software/Software.ino b/Software/Software.ino index 42e5f388..75edee02 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -14,6 +14,7 @@ #include "src/communication/can/comm_can.h" #include "src/communication/contactorcontrol/comm_contactorcontrol.h" #include "src/communication/equipmentstopbutton/comm_equipmentstopbutton.h" +#include "src/communication/nvm/comm_nvm.h" #include "src/communication/rs485/comm_rs485.h" #include "src/communication/seriallink/comm_seriallink.h" #include "src/datalayer/datalayer.h" @@ -46,7 +47,6 @@ #endif // MQTT #endif // WIFI -Preferences settings; // Store user settings // The current software version, shown on webserver const char* version_number = "8.0.dev"; @@ -283,64 +283,6 @@ void init_serial() { #endif // DEBUG_VIA_USB } -void init_stored_settings() { - static uint32_t temp = 0; - settings.begin("batterySettings", false); - - // Always get the equipment stop status - datalayer.system.settings.equipment_stop_active = settings.getBool("EQUIPMENT_STOP", false); - if (datalayer.system.settings.equipment_stop_active) { - set_event(EVENT_EQUIPMENT_STOP, 1); - } - -#ifndef LOAD_SAVED_SETTINGS_ON_BOOT - settings.clear(); // If this clear function is executed, no settings will be read from storage - - //always save the equipment stop status - settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active); -#endif // LOAD_SAVED_SETTINGS_ON_BOOT - -#ifdef WIFI - char tempSSIDstring[63]; // Allocate buffer with sufficient size - size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring)); - if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID! - ssid = tempSSIDstring; - } else { // Reading from settings failed. Do nothing with SSID. Raise event? - } - char tempPasswordString[63]; // Allocate buffer with sufficient size - size_t lengthPassword = settings.getString("PASSWORD", tempPasswordString, sizeof(tempPasswordString)); - if (lengthPassword > 7) { // Successfully read the string from memory. Set it to password! - password = tempPasswordString; - } else { // Reading from settings failed. Do nothing with SSID. Raise event? - } -#endif // WIFI - - temp = settings.getUInt("BATTERY_WH_MAX", false); - if (temp != 0) { - datalayer.battery.info.total_capacity_Wh = temp; - } - temp = settings.getUInt("MAXPERCENTAGE", false); - if (temp != 0) { - datalayer.battery.settings.max_percentage = temp * 10; // Multiply by 10 for backwards compatibility - } - temp = settings.getUInt("MINPERCENTAGE", false); - if (temp != 0) { - datalayer.battery.settings.min_percentage = temp * 10; // Multiply by 10 for backwards compatibility - } - temp = settings.getUInt("MAXCHARGEAMP", false); - if (temp != 0) { - datalayer.battery.settings.max_user_set_charge_dA = temp; - } - temp = settings.getUInt("MAXDISCHARGEAMP", false); - if (temp != 0) { - datalayer.battery.settings.max_user_set_discharge_dA = temp; - temp = settings.getBool("USE_SCALED_SOC", false); - datalayer.battery.settings.soc_scaling_active = temp; //This bool needs to be checked inside the temp!= block - } // No way to know if it wasnt reset otherwise - - settings.end(); -} - #ifdef DOUBLE_BATTERY void check_interconnect_available() { if (datalayer.battery.status.voltage_dV == 0 || datalayer.battery2.status.voltage_dV == 0) { @@ -495,29 +437,6 @@ void update_values_inverter() { #endif // CAN_INVERTER_SELECTED } -void store_settings_equipment_stop() { - settings.begin("batterySettings", false); - settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active); - settings.end(); -} - -void storeSettings() { - settings.begin("batterySettings", false); -#ifdef WIFI - settings.putString("SSID", String(ssid.c_str())); - settings.putString("PASSWORD", String(password.c_str())); -#endif - settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh); - settings.putUInt("MAXPERCENTAGE", - datalayer.battery.settings.max_percentage / 10); // Divide by 10 for backwards compatibility - settings.putUInt("MINPERCENTAGE", - datalayer.battery.settings.min_percentage / 10); // Divide by 10 for backwards compatibility - settings.putUInt("MAXCHARGEAMP", datalayer.battery.settings.max_user_set_charge_dA); - settings.putUInt("MAXDISCHARGEAMP", datalayer.battery.settings.max_user_set_discharge_dA); - settings.putBool("USE_SCALED_SOC", datalayer.battery.settings.soc_scaling_active); - settings.end(); -} - /** Reset reason numbering and description * typedef enum { diff --git a/Software/src/communication/nvm/comm_nvm.cpp b/Software/src/communication/nvm/comm_nvm.cpp new file mode 100644 index 00000000..7b9a9d5a --- /dev/null +++ b/Software/src/communication/nvm/comm_nvm.cpp @@ -0,0 +1,88 @@ +#include "comm_nvm.h" +#include "../../include.h" + +// Parameters +Preferences settings; // Store user settings + +// Initialization functions + +void init_stored_settings() { + static uint32_t temp = 0; + settings.begin("batterySettings", false); + + // Always get the equipment stop status + datalayer.system.settings.equipment_stop_active = settings.getBool("EQUIPMENT_STOP", false); + if (datalayer.system.settings.equipment_stop_active) { + set_event(EVENT_EQUIPMENT_STOP, 1); + } + +#ifndef LOAD_SAVED_SETTINGS_ON_BOOT + settings.clear(); // If this clear function is executed, no settings will be read from storage + + //always save the equipment stop status + settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active); +#endif // LOAD_SAVED_SETTINGS_ON_BOOT + +#ifdef WIFI + char tempSSIDstring[63]; // Allocate buffer with sufficient size + size_t lengthSSID = settings.getString("SSID", tempSSIDstring, sizeof(tempSSIDstring)); + if (lengthSSID > 0) { // Successfully read the string from memory. Set it to SSID! + ssid = tempSSIDstring; + } else { // Reading from settings failed. Do nothing with SSID. Raise event? + } + char tempPasswordString[63]; // Allocate buffer with sufficient size + size_t lengthPassword = settings.getString("PASSWORD", tempPasswordString, sizeof(tempPasswordString)); + if (lengthPassword > 7) { // Successfully read the string from memory. Set it to password! + password = tempPasswordString; + } else { // Reading from settings failed. Do nothing with SSID. Raise event? + } +#endif // WIFI + + temp = settings.getUInt("BATTERY_WH_MAX", false); + if (temp != 0) { + datalayer.battery.info.total_capacity_Wh = temp; + } + temp = settings.getUInt("MAXPERCENTAGE", false); + if (temp != 0) { + datalayer.battery.settings.max_percentage = temp * 10; // Multiply by 10 for backwards compatibility + } + temp = settings.getUInt("MINPERCENTAGE", false); + if (temp != 0) { + datalayer.battery.settings.min_percentage = temp * 10; // Multiply by 10 for backwards compatibility + } + temp = settings.getUInt("MAXCHARGEAMP", false); + if (temp != 0) { + datalayer.battery.settings.max_user_set_charge_dA = temp; + } + temp = settings.getUInt("MAXDISCHARGEAMP", false); + if (temp != 0) { + datalayer.battery.settings.max_user_set_discharge_dA = temp; + temp = settings.getBool("USE_SCALED_SOC", false); + datalayer.battery.settings.soc_scaling_active = temp; //This bool needs to be checked inside the temp!= block + } // No way to know if it wasnt reset otherwise + + settings.end(); +} + +void store_settings_equipment_stop() { + settings.begin("batterySettings", false); + settings.putBool("EQUIPMENT_STOP", datalayer.system.settings.equipment_stop_active); + settings.end(); +} + +void storeSettings() { + settings.begin("batterySettings", false); +#ifdef WIFI + settings.putString("SSID", String(ssid.c_str())); + settings.putString("PASSWORD", String(password.c_str())); +#endif + settings.putUInt("BATTERY_WH_MAX", datalayer.battery.info.total_capacity_Wh); + settings.putUInt("MAXPERCENTAGE", + datalayer.battery.settings.max_percentage / 10); // Divide by 10 for backwards compatibility + settings.putUInt("MINPERCENTAGE", + datalayer.battery.settings.min_percentage / 10); // Divide by 10 for backwards compatibility + settings.putUInt("MAXCHARGEAMP", datalayer.battery.settings.max_user_set_charge_dA); + settings.putUInt("MAXDISCHARGEAMP", datalayer.battery.settings.max_user_set_discharge_dA); + settings.putBool("USE_SCALED_SOC", datalayer.battery.settings.soc_scaling_active); + settings.end(); +} diff --git a/Software/src/communication/nvm/comm_nvm.h b/Software/src/communication/nvm/comm_nvm.h new file mode 100644 index 00000000..eaeafca5 --- /dev/null +++ b/Software/src/communication/nvm/comm_nvm.h @@ -0,0 +1,16 @@ +#ifndef _COMM_NVM_H_ +#define _COMM_NVM_H_ + +#include "../../include.h" + +#include "../../datalayer/datalayer.h" +#include "../../devboard/utils/events.h" +#include "../../devboard/wifi/wifi.h" + +void init_stored_settings(); + +void store_settings_equipment_stop(); + +void storeSettings(); + +#endif