diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index 349a7c8..8af05a7 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -222,17 +222,17 @@ struct LockscreenPinDigitAccessor : public NewSettingsAccessor { ConfigW struct LockscreenKeepLockedAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.lockscreen.keepLockedAfterReboot; } }; // Handbremse -struct HandbremsEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.enable; } }; -struct HandbremsModeAccessor : public RefAccessorSaveSettings { HandbremseMode &getRef() const override { return settings.handbremse.mode; } }; -struct HandbremsTimeoutAccessor : public RefAccessorSaveSettings { uint16_t &getRef() const override { return settings.handbremse.triggerTimeout; } }; -struct HandbremsAutomaticAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.automatic; } }; -struct HandbremsVisualizeAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.handbremse.visualize; } }; +struct HandbremsEnabledAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.handbremse.enable; } }; +struct HandbremsModeAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.handbremse.mode; } }; +struct HandbremsTimeoutAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.handbremse.triggerTimeout; } }; +struct HandbremsAutomaticAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.handbremse.automatic; } }; +struct HandbremsVisualizeAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.handbremse.visualize; } }; // ESP Now #ifdef FEATURE_ESPNOW -struct ESPNowSyncTimeEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTime; } }; -struct ESPNowSyncTimeWithOthersEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncTimeWithOthers; } }; -struct ESPNowSyncBlinkEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.espnow.syncBlink; } }; +struct ESPNowSyncTimeEnabledAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.espnow.syncTime; } }; +struct ESPNowSyncTimeWithOthersEnabledAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.espnow.syncTimeWithOthers; } }; +struct ESPNowSyncBlinkEnabledAccessor : public NewSettingsAccessor { ConfigWrapper &getConfig() const override { return configs.espnow.syncBlink; } }; #endif // Button Mapping accessors diff --git a/main/bobbybuttons.cpp b/main/bobbybuttons.cpp index 8b35a19..262ae4d 100644 --- a/main/bobbybuttons.cpp +++ b/main/bobbybuttons.cpp @@ -102,7 +102,7 @@ void buttonPressedCommon(espgui::Button button) #endif break; case BobbyButton::Up2: - if (settings.handbremse.enable) + if (configs.handbremse.enable.value) { using namespace handbremse; if (stateWish == StateWish::brake || angezogen) diff --git a/main/configutils_bobby.h b/main/configutils_bobby.h index 2f242d8..b145153 100644 --- a/main/configutils_bobby.h +++ b/main/configutils_bobby.h @@ -6,5 +6,7 @@ // local includes #include "ledstrip.h" +#include "handbremse.h" IMPLEMENT_NVS_GET_SET_ENUM(OtaAnimationModes) +IMPLEMENT_NVS_GET_SET_ENUM(HandbremseMode) diff --git a/main/configwrapper_bobby.cpp b/main/configwrapper_bobby.cpp index 62b5412..46bef2f 100644 --- a/main/configwrapper_bobby.cpp +++ b/main/configwrapper_bobby.cpp @@ -5,3 +5,4 @@ #include INSTANTIATE_CONFIGWRAPPER_TEMPLATES(OtaAnimationModes) +INSTANTIATE_CONFIGWRAPPER_TEMPLATES(HandbremseMode) diff --git a/main/displays/menus/handbremssettingsmenu.cpp b/main/displays/menus/handbremssettingsmenu.cpp index 3c39799..cb85968 100644 --- a/main/displays/menus/handbremssettingsmenu.cpp +++ b/main/displays/menus/handbremssettingsmenu.cpp @@ -42,7 +42,7 @@ class HandBremsModeText : public virtual espgui::TextInterface public: std::string text() const override { - return fmt::format("Mode: &2{}", toString(settings.handbremse.mode)); + return fmt::format("Mode: &2{}", configs.handbremse.mode.valueAsString()); } }; } diff --git a/main/displays/menus/statisticsmenu.cpp b/main/displays/menus/statisticsmenu.cpp index 44b0c90..a58a139 100644 --- a/main/displays/menus/statisticsmenu.cpp +++ b/main/displays/menus/statisticsmenu.cpp @@ -104,7 +104,7 @@ public: class SavedTotalCentimetersText : public virtual espgui::TextInterface { public: std::string text() const override { - return fmt::format("saved: {}cm", settings.savedStatistics.totalCentimeters ); + return fmt::format("saved: {}cm", configs.savedStatistics.totalCentimeters.value ); } }; @@ -140,8 +140,7 @@ public: void triggered() override { drivingStatistics.last_cm_written = drivingStatistics.totalMeters * 100; - settings.savedStatistics.totalCentimeters = drivingStatistics.last_cm_written; - saveSettings(); + configs.write_config(configs.savedStatistics.totalCentimeters, drivingStatistics.last_cm_written); } }; diff --git a/main/displays/statusdisplay.cpp b/main/displays/statusdisplay.cpp index 51f37d5..0506ac4 100644 --- a/main/displays/statusdisplay.cpp +++ b/main/displays/statusdisplay.cpp @@ -85,9 +85,9 @@ void StatusDisplay::redraw() lastRedraw = now; } - if (settings.handbremse.enable && settings.handbremse.visualize && handbremse::angezogen) + if (configs.handbremse.enable.value && configs.handbremse.visualize.value && handbremse::angezogen) tft.fillRect(0, tft.height()-6, tft.width(), 6, TFT_RED); - else if (settings.handbremse.enable && settings.handbremse.visualize && handbremse::stateWish == handbremse::StateWish::brake) + else if (configs.handbremse.enable.value && configs.handbremse.visualize.value && handbremse::stateWish == handbremse::StateWish::brake) tft.fillRect(0, tft.height()-6, tft.width(), 6, TFT_YELLOW); else tft.fillRect(0, tft.height()-6, tft.width(), 6, TFT_BLACK); diff --git a/main/drivingstatistics.cpp b/main/drivingstatistics.cpp index 22a5809..df66b24 100644 --- a/main/drivingstatistics.cpp +++ b/main/drivingstatistics.cpp @@ -59,10 +59,10 @@ void calculateStatistics() EVERY_N_MILLIS( 10 ) { static bool saveTotal = false; - if ((settings.savedStatistics.totalCentimeters / 100.f) > drivingStatistics.totalMeters) + if ((configs.savedStatistics.totalCentimeters.value / 100.f) > drivingStatistics.totalMeters) { - drivingStatistics.totalMeters = settings.savedStatistics.totalCentimeters / 100.f; - drivingStatistics.last_cm_written = settings.savedStatistics.totalCentimeters; + drivingStatistics.totalMeters = configs.savedStatistics.totalCentimeters.value / 100.f; + drivingStatistics.last_cm_written = configs.savedStatistics.totalCentimeters.value; } static auto last_km_calculation = espchrono::millis_clock::now(); @@ -106,8 +106,7 @@ void calculateStatistics() saveTotal = false; } drivingStatistics.last_cm_written = drivingStatistics.totalMeters * 100; // Save total Meters - settings.savedStatistics.totalCentimeters = drivingStatistics.last_cm_written; - saveSettings(); + configs.write_config(configs.savedStatistics.totalCentimeters, drivingStatistics.last_cm_written); } } } diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index 2dc7111..96c9cd6 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -188,7 +188,7 @@ void handle() { if (msg.type == "T") { - if (!receiveTimeStamp || !settings.espnow.syncTime) + if (!receiveTimeStamp || !configs.espnow.syncTime.value) return; if (const auto result = cpputils::fromString(msg.content); result) @@ -202,7 +202,7 @@ void handle() } else if (msg.type == "BOBBYT") { - if (!receiveTsFromOtherBobbycars || !settings.espnow.syncTimeWithOthers) + if (!receiveTsFromOtherBobbycars || !configs.espnow.syncTimeWithOthers.value) return; if (const auto result = cpputils::fromString(msg.content); result) diff --git a/main/modes/defaultmode.cpp b/main/modes/defaultmode.cpp index 98f5048..7d86e81 100644 --- a/main/modes/defaultmode.cpp +++ b/main/modes/defaultmode.cpp @@ -55,7 +55,7 @@ void DefaultMode::update() float pwm; - if (settings.handbremse.enable && handbremse::stateWish == handbremse::StateWish::brake) + if (configs.handbremse.enable.value && handbremse::stateWish == handbremse::StateWish::brake) { using namespace handbremse; @@ -68,7 +68,7 @@ void DefaultMode::update() } } - if (settings.handbremse.enable && settings.handbremse.automatic && !handbremse::angezogen) + if (configs.handbremse.enable.value && configs.handbremse.automatic.value && !handbremse::angezogen) { using namespace handbremse; const auto speed = abs(avgSpeedKmh); @@ -84,7 +84,7 @@ void DefaultMode::update() standStillFirstDetected = std::nullopt; if (standStillFirstDetected && lastAutoRelease) - if (espchrono::ago(*standStillFirstDetected) > 100ms * settings.handbremse.triggerTimeout && espchrono::ago(*lastAutoRelease) > 5s ) + if (espchrono::ago(*standStillFirstDetected) > 100ms * configs.handbremse.triggerTimeout.value && espchrono::ago(*lastAutoRelease) > 5s ) { angezogen = true; } @@ -148,7 +148,7 @@ void DefaultMode::update() for (bobbycar::protocol::serial::MotorState &motor : motors()) { motor.ctrlTyp = bobbycar::protocol::ControlType::FieldOrientedControl; - switch (settings.handbremse.mode) + switch (configs.handbremse.mode.value) { case HandbremseMode::MOSFETS_OFF: motor.ctrlMod = bobbycar::protocol::ControlMode::Torque; diff --git a/main/newsettings.h b/main/newsettings.h index 8197939..cf70ea1 100644 --- a/main/newsettings.h +++ b/main/newsettings.h @@ -24,6 +24,7 @@ #include "ledstrip.h" #include "unifiedmodelmode.h" #include "displays/lockscreen.h" +#include "handbremse.h" using namespace espconfig; @@ -272,6 +273,24 @@ public: }; } lockscreen; + struct { + ConfigWrapper totalCentimeters {0, DoReset, {}, "totalCentimeter" }; + } savedStatistics; + + struct { + ConfigWrapper mode {HandbremseMode::MOSFETS_OFF, DoReset, {}, "handBremsM" }; + ConfigWrapper triggerTimeout {10, DoReset, {}, "handBremsT" }; + ConfigWrapper automatic {false, DoReset, {}, "handBremsA" }; + ConfigWrapper enable {false, DoReset, {}, "handBremsE" }; + ConfigWrapper visualize {false, DoReset, {}, "handBremsV" }; + } handbremse; + + struct { + ConfigWrapper syncTime {false, DoReset, {}, "espnowSyncT" }; + ConfigWrapper syncTimeWithOthers {false, DoReset, {}, "espnowSyncTWO" }; + ConfigWrapper syncBlink {false, DoReset, {}, "espnowSyncBl" }; + } espnow; + struct { ConfigWrapper bleEnabled {true, DoReset, {}, "bleEnabled" }; } bleSettings; @@ -526,7 +545,19 @@ public: x(lockscreen.pin[0].digit) \ x(lockscreen.pin[1].digit) \ x(lockscreen.pin[2].digit) \ - x(lockscreen.pin[3].digit) + x(lockscreen.pin[3].digit) \ + \ + x(savedStatistics.totalCentimeters) \ + \ + x(handbremse.mode) \ + x(handbremse.triggerTimeout) \ + x(handbremse.automatic) \ + x(handbremse.enable) \ + x(handbremse.visualize) \ + \ + x(espnow.syncTime) \ + x(espnow.syncTimeWithOthers) \ + x(espnow.syncBlink) //x(bleSettings.bleEnabled) template diff --git a/main/presets.h b/main/presets.h index 4e0ad42..a2f03a0 100644 --- a/main/presets.h +++ b/main/presets.h @@ -122,14 +122,6 @@ constexpr Settings::MotortestMode defaultMotortestMode { .maxPwm = 400 }; -constexpr Settings::Handbremse defaultHandbremse { - .mode = HandbremseMode::MOSFETS_OFF, - .triggerTimeout = 10, - .automatic = false, - .enable = false, - .visualize = true, -}; - constexpr Settings::Hybrid defaultHybrid { .hybridMode = UnifiedModelMode::FocTorque, .enable = false, @@ -137,23 +129,6 @@ constexpr Settings::Hybrid defaultHybrid { .deactivationLimit = 950, }; -constexpr Settings::SavedStatistics defaultSavedStatistics { - .totalCentimeters = 0, -}; - -#ifdef FEATURE_ESPNOW -constexpr Settings::ESPNOW defaultEspNowSettings { -#ifndef FEATURE_NTP - .syncTime = true, - .syncTimeWithOthers = true, -#else - .syncTime = false, - .syncTimeWithOthers = false, -#endif - .syncBlink = false -}; -#endif - constexpr Settings defaultSettings { .limits = defaultLimits, .controllerHardware = defaultControllerHardware, @@ -161,11 +136,6 @@ constexpr Settings defaultSettings { .tempomatMode = defaultTempomatMode, .larsmMode = defaultLarsmMode, .motortestMode = defaultMotortestMode, - .hybrid = defaultHybrid, - .savedStatistics = defaultSavedStatistics, - .handbremse = defaultHandbremse, -#ifdef FEATURE_ESPNOW - .espnow = defaultEspNowSettings, -#endif + .hybrid = defaultHybrid }; } // namespace presets diff --git a/main/settings.h b/main/settings.h index 1dca710..0b588f0 100644 --- a/main/settings.h +++ b/main/settings.h @@ -18,8 +18,6 @@ // local includes #include "bobbycar-common.h" #include "unifiedmodelmode.h" -#include "handbremse.h" -#include "ledstrip.h" #include "modes/larsmmode.h" struct Settings @@ -78,25 +76,6 @@ struct Settings int16_t deactivationLimit; } hybrid; - struct SavedStatistics { - uint32_t totalCentimeters; - } savedStatistics; - - struct Handbremse { - HandbremseMode mode; - uint16_t triggerTimeout; - bool automatic; - bool enable; - bool visualize; - } handbremse; -#ifdef FEATURE_ESPNOW - struct ESPNOW { - bool syncTime; - bool syncTimeWithOthers; - bool syncBlink; - } espnow; -#endif - template void executeForEveryCommonSetting(T &&callable); @@ -111,22 +90,6 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("hybridEn", hybrid.enable); callable("hybridAcL", hybrid.activationLimit); callable("hybridDeacL", hybrid.deactivationLimit); - - callable("totalCentimeter", savedStatistics.totalCentimeters); - - callable("handBremsE", handbremse.enable); - callable("handBremsA", handbremse.automatic); - callable("handBremsM", handbremse.mode); - callable("handBremsT", handbremse.triggerTimeout); - callable("handBremsV", handbremse.visualize); - -#ifdef FEATURE_ESPNOW - callable("espnowSyncT", espnow.syncTime); - callable("espnowSyncTWO", espnow.syncTimeWithOthers); -#ifdef FEATURE_LEDSTRIP - callable("espnowSyncBl", espnow.syncBlink); -#endif -#endif } template diff --git a/main/settingspersister.cpp b/main/settingspersister.cpp index 335b70a..abfef15 100644 --- a/main/settingspersister.cpp +++ b/main/settingspersister.cpp @@ -188,24 +188,6 @@ template<> struct nvsGetterHelper { static esp_err_t nvs_get(n *out_value = UnifiedModelMode(tempValue); return err; }}; -template<> struct nvsGetterHelper { static esp_err_t nvs_get(nvs_handle handle, const char* key, HandbremseMode* out_value) -{ - uint8_t tempValue; - esp_err_t err = nvs_get_u8(handle, key, &tempValue); - if (err == ESP_OK) - *out_value = HandbremseMode(tempValue); - return err; -}}; -#if defined(FEATURE_LEDSTRIP) && defined(FEATURE_OTA) -template<> struct nvsGetterHelper { static esp_err_t nvs_get(nvs_handle handle, const char* key, OtaAnimationModes* out_value) -{ - uint8_t tempValue; - esp_err_t err = nvs_get_u8(handle, key, &tempValue); - if (err == ESP_OK) - *out_value = OtaAnimationModes(tempValue); - return err; -}}; -#endif template<> struct nvsGetterHelper { static esp_err_t nvs_get(nvs_handle handle, const char* key, wifi_mode_t* out_value) { uint8_t tempValue; @@ -361,16 +343,6 @@ template<> struct nvsSetterHelper { static esp_err_t nvs_set(n { return nvs_set_u8(handle, key, uint8_t(value)); }}; -template<> struct nvsSetterHelper { static esp_err_t nvs_set(nvs_handle handle, const char* key, HandbremseMode value) -{ - return nvs_set_u8(handle, key, uint8_t(value)); -}}; -#if defined(FEATURE_LEDSTRIP) && defined(FEATURE_OTA) -template<> struct nvsSetterHelper { static esp_err_t nvs_set(nvs_handle handle, const char* key, OtaAnimationModes value) -{ - return nvs_set_u8(handle, key, uint8_t(value)); -}}; -#endif template<> struct nvsSetterHelper { static esp_err_t nvs_set(nvs_handle handle, const char* key, wifi_mode_t value) { return nvs_set_u8(handle, key, uint8_t(value));