Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Update the comment to reference the JSON configuration field.

The comment references AUTOSAVE_IGNORE_PRESETS=false, which suggests a compile-time define, but the actual implementation uses a JSON configuration field autoSaveIgnorePresets. This inconsistency could confuse users trying to configure this option.

Apply this diff to correct the documentation:

-// 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 default it will not save the state if an unmodified preset
+// is selected (to not duplicate it). You can change this behavior
+// by setting "autoSaveIgnorePresets": false in the usermod configuration.
🤖 Prompt for AI Agents
In usermods/usermod_v2_auto_save/usermod_v2_auto_save.cpp around lines 19 to 21,
the comment incorrectly references a compile-time define
"AUTOSAVE_IGNORE_PRESETS=false" while the implementation reads the JSON
configuration field "autoSaveIgnorePresets"; update the comment to mention the
correct JSON config key (autoSaveIgnorePresets) and show the expected JSON usage
(e.g., "set autoSaveIgnorePresets to false in the config JSON") so users are
guided to change the runtime configuration rather than a non-existent
compile-time define.

//
// AutoSaveUsermod is standalone, but if FourLineDisplayUsermod
// is installed, it will notify the user of the saved changes.

Expand Down Expand Up @@ -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
Comment thread
mtin marked this conversation as resolved.
Outdated

// If we've detected the need to auto save, this will be non zero.
unsigned long autoSaveAfter = 0;

Expand All @@ -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];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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."));
}

Expand All @@ -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."));

Expand All @@ -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);