From 1e921bcbdba7895a054775aa18ecaf3fe894ee6c Mon Sep 17 00:00:00 2001 From: Martin Fritzsche Date: Tue, 9 Dec 2025 18:21:22 +0100 Subject: [PATCH 1/4] Add option to save unmodified presets to autosave usermod --- .../usermod_v2_auto_save.cpp | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp index 1b97ea94da..3f817216ff 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp @@ -16,6 +16,10 @@ // It can be configured to load auto saved preset at startup, // during the first `loop()`. // +// By default it will not save the state if an unmodified preset +// is selected (to not duplicate it). You can change this behaviour +// by setting AUTOSAVE_IGNORE_PRESETS=false +// // AutoSaveUsermod is standalone, but if FourLineDisplayUsermod // is installed, it will notify the user of the saved changes. @@ -49,6 +53,12 @@ class AutoSaveUsermod : public Usermod { bool applyAutoSaveOnBoot = false; // do we load auto-saved preset on boot? #endif + #ifdef AUTOSAVE_IGNORE_PRESETS + bool autoSaveIgnorePresets = AUTOSAVE_IGNORE_PRESETS; + #else + bool autoSaveIgnorePresets = true; // ignore by default to not duplicate presets + #endif + // If we've detected the need to auto save, this will be non zero. unsigned long autoSaveAfter = 0; @@ -68,6 +78,7 @@ class AutoSaveUsermod : public Usermod { static const char _autoSaveAfterSec[]; static const char _autoSavePreset[]; static const char _autoSaveApplyOnBoot[]; + static const char _autoSaveIgnorePresets[]; void inline saveSettings() { char presetNameBuffer[PRESET_NAME_BUFFER_SIZE]; @@ -122,7 +133,7 @@ class AutoSaveUsermod : public Usermod { void loop() { static unsigned long lastRun = 0; unsigned long now = millis(); - if (!autoSaveAfterSec || !enabled || currentPreset>0 || (strip.isUpdating() && now - lastRun < 240)) return; // setting 0 as autosave seconds disables autosave + if (!autoSaveAfterSec || !enabled || (autoSaveIgnorePresets && currentPreset>0) || (strip.isUpdating() && now - lastRun < 240)) return; // setting 0 as autosave seconds disables autosave uint8_t currentMode = strip.getMainSegment().mode; uint8_t currentPalette = strip.getMainSegment().palette; @@ -219,10 +230,11 @@ class AutoSaveUsermod : public Usermod { void addToConfig(JsonObject& root) { // we add JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}} JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname - top[FPSTR(_autoSaveEnabled)] = enabled; - top[FPSTR(_autoSaveAfterSec)] = autoSaveAfterSec; // usermodparam - top[FPSTR(_autoSavePreset)] = autoSavePreset; // usermodparam - top[FPSTR(_autoSaveApplyOnBoot)] = applyAutoSaveOnBoot; + top[FPSTR(_autoSaveEnabled)] = enabled; + top[FPSTR(_autoSaveAfterSec)] = autoSaveAfterSec; // usermodparam + top[FPSTR(_autoSavePreset)] = autoSavePreset; // usermodparam + top[FPSTR(_autoSaveApplyOnBoot)] = applyAutoSaveOnBoot; + top[FPSTR(_autoSaveIgnorePresets)] = autoSaveIgnorePresets; DEBUG_PRINTLN(F("Autosave config saved.")); } @@ -245,12 +257,13 @@ class AutoSaveUsermod : public Usermod { return false; } - enabled = top[FPSTR(_autoSaveEnabled)] | enabled; - autoSaveAfterSec = top[FPSTR(_autoSaveAfterSec)] | autoSaveAfterSec; - autoSaveAfterSec = (uint16_t) min(3600,max(10,(int)autoSaveAfterSec)); // bounds checking - autoSavePreset = top[FPSTR(_autoSavePreset)] | autoSavePreset; - autoSavePreset = (uint8_t) min(250,max(100,(int)autoSavePreset)); // bounds checking - applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)] | applyAutoSaveOnBoot; + enabled = top[FPSTR(_autoSaveEnabled)] | enabled; + autoSaveAfterSec = top[FPSTR(_autoSaveAfterSec)] | autoSaveAfterSec; + autoSaveAfterSec = (uint16_t) min(3600,max(10,(int)autoSaveAfterSec)); // bounds checking + autoSavePreset = top[FPSTR(_autoSavePreset)] | autoSavePreset; + autoSavePreset = (uint8_t) min(250,max(100,(int)autoSavePreset)); // bounds checking + applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)] | applyAutoSaveOnBoot; + autoSaveIgnorePresets = top[FPSTR(_autoSaveIgnorePresets)] | autoSaveIgnorePresets; DEBUG_PRINT(FPSTR(_name)); DEBUG_PRINTLN(F(" config (re)loaded.")); @@ -268,11 +281,12 @@ class AutoSaveUsermod : public Usermod { }; // strings to reduce flash memory usage (used more than twice) -const char AutoSaveUsermod::_name[] PROGMEM = "Autosave"; -const char AutoSaveUsermod::_autoSaveEnabled[] PROGMEM = "enabled"; -const char AutoSaveUsermod::_autoSaveAfterSec[] PROGMEM = "autoSaveAfterSec"; -const char AutoSaveUsermod::_autoSavePreset[] PROGMEM = "autoSavePreset"; -const char AutoSaveUsermod::_autoSaveApplyOnBoot[] PROGMEM = "autoSaveApplyOnBoot"; +const char AutoSaveUsermod::_name[] PROGMEM = "Autosave"; +const char AutoSaveUsermod::_autoSaveEnabled[] PROGMEM = "enabled"; +const char AutoSaveUsermod::_autoSaveAfterSec[] PROGMEM = "autoSaveAfterSec"; +const char AutoSaveUsermod::_autoSavePreset[] PROGMEM = "autoSavePreset"; +const char AutoSaveUsermod::_autoSaveApplyOnBoot[] PROGMEM = "autoSaveApplyOnBoot"; +const char AutoSaveUsermod::_autoSaveIgnorePresets[] PROGMEM = "autoSaveIgnorePresets"; static AutoSaveUsermod autosave; REGISTER_USERMOD(autosave); From beca598d8a26655545852cd6df03bcd05e4a3338 Mon Sep 17 00:00:00 2001 From: Martin Fritzsche Date: Wed, 10 Dec 2025 12:52:38 +0100 Subject: [PATCH 2/4] Fix lastRun never being assigned --- usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp index 3f817216ff..0865b308a7 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp @@ -134,6 +134,7 @@ class AutoSaveUsermod : public Usermod { static unsigned long lastRun = 0; unsigned long now = millis(); if (!autoSaveAfterSec || !enabled || (autoSaveIgnorePresets && currentPreset>0) || (strip.isUpdating() && now - lastRun < 240)) return; // setting 0 as autosave seconds disables autosave + lastRun = now; uint8_t currentMode = strip.getMainSegment().mode; uint8_t currentPalette = strip.getMainSegment().palette; From 4752d75c5af08130b8eccb46c1dc7d1946fc3625 Mon Sep 17 00:00:00 2001 From: Martin Fritzsche Date: Wed, 10 Dec 2025 12:54:38 +0100 Subject: [PATCH 3/4] Remove setting configuration option via #defines, edit code locally instead --- usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp index 0865b308a7..0b91fa617b 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp @@ -53,11 +53,7 @@ class AutoSaveUsermod : public Usermod { bool applyAutoSaveOnBoot = false; // do we load auto-saved preset on boot? #endif - #ifdef AUTOSAVE_IGNORE_PRESETS - bool autoSaveIgnorePresets = AUTOSAVE_IGNORE_PRESETS; - #else bool autoSaveIgnorePresets = true; // ignore by default to not duplicate presets - #endif // If we've detected the need to auto save, this will be non zero. unsigned long autoSaveAfter = 0; From cb70453c45594d7457d2c82fcfef6dfd9e322faa Mon Sep 17 00:00:00 2001 From: Martin Fritzsche Date: Wed, 10 Dec 2025 13:02:39 +0100 Subject: [PATCH 4/4] Update hints in comment for removed #define variable --- usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp index 0b91fa617b..fe508b1f32 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp @@ -18,7 +18,7 @@ // // By default it will not save the state if an unmodified preset // is selected (to not duplicate it). You can change this behaviour -// by setting AUTOSAVE_IGNORE_PRESETS=false +// by setting autoSaveIgnorePresets=false // // AutoSaveUsermod is standalone, but if FourLineDisplayUsermod // is installed, it will notify the user of the saved changes.