2021-02-09 17:15:43 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "wled.h"
|
|
|
|
|
|
|
|
// v2 Usermod to automatically save settings
|
2021-05-07 12:41:39 +02:00
|
|
|
// to configurable preset after a change to any of
|
2021-02-09 17:15:43 +01:00
|
|
|
//
|
|
|
|
// * brightness
|
|
|
|
// * effect speed
|
|
|
|
// * effect intensity
|
|
|
|
// * mode (effect)
|
|
|
|
// * palette
|
|
|
|
//
|
2021-05-07 12:41:39 +02:00
|
|
|
// but it will wait for configurable number of seconds, a "settle"
|
2021-02-09 17:15:43 +01:00
|
|
|
// period in case there are other changes (any change will
|
|
|
|
// extend the "settle" window).
|
|
|
|
//
|
2021-05-07 12:41:39 +02:00
|
|
|
// It can be configured to load auto saved preset at startup,
|
|
|
|
// during the first `loop()`.
|
2021-02-09 17:15:43 +01:00
|
|
|
//
|
|
|
|
// AutoSaveUsermod is standalone, but if FourLineDisplayUsermod
|
|
|
|
// is installed, it will notify the user of the saved changes.
|
2021-05-07 12:41:39 +02:00
|
|
|
|
|
|
|
// format: "~ MM-DD HH:MM:SS ~"
|
2021-02-09 17:15:43 +01:00
|
|
|
#define PRESET_NAME_BUFFER_SIZE 25
|
|
|
|
|
|
|
|
class AutoSaveUsermod : public Usermod {
|
|
|
|
|
2021-05-07 12:41:39 +02:00
|
|
|
private:
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
bool firstLoop = true;
|
2021-05-07 12:41:39 +02:00
|
|
|
bool initDone = false;
|
|
|
|
bool enabled = true;
|
|
|
|
|
|
|
|
// configurable parameters
|
|
|
|
unsigned long autoSaveAfterSec = 15; // 15s by default
|
|
|
|
uint8_t autoSavePreset = 250; // last possible preset
|
|
|
|
bool applyAutoSaveOnBoot = false; // do we load auto-saved preset on boot?
|
|
|
|
|
|
|
|
// If we've detected the need to auto save, this will be non zero.
|
|
|
|
unsigned long autoSaveAfter = 0;
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
uint8_t knownBrightness = 0;
|
|
|
|
uint8_t knownEffectSpeed = 0;
|
|
|
|
uint8_t knownEffectIntensity = 0;
|
|
|
|
uint8_t knownMode = 0;
|
|
|
|
uint8_t knownPalette = 0;
|
|
|
|
|
2021-05-07 12:41:39 +02:00
|
|
|
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
2021-02-09 17:15:43 +01:00
|
|
|
FourLineDisplayUsermod* display;
|
2021-05-07 12:41:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// strings to reduce flash memory usage (used more than twice)
|
|
|
|
static const char _name[];
|
|
|
|
static const char _autoSaveEnabled[];
|
|
|
|
static const char _autoSaveAfterSec[];
|
|
|
|
static const char _autoSavePreset[];
|
|
|
|
static const char _autoSaveApplyOnBoot[];
|
|
|
|
|
|
|
|
void inline saveSettings() {
|
|
|
|
char presetNameBuffer[PRESET_NAME_BUFFER_SIZE];
|
|
|
|
updateLocalTime();
|
|
|
|
sprintf_P(presetNameBuffer,
|
|
|
|
PSTR("~ %02d-%02d %02d:%02d:%02d ~"),
|
|
|
|
month(localTime), day(localTime),
|
|
|
|
hour(localTime), minute(localTime), second(localTime));
|
|
|
|
savePreset(autoSavePreset, true, presetNameBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inline displayOverlay() {
|
|
|
|
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
|
|
|
if (display != nullptr) {
|
|
|
|
display->wakeDisplay();
|
|
|
|
display->overlay("Settings", "Auto Saved", 1500);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
public:
|
2021-05-07 12:41:39 +02:00
|
|
|
|
2021-02-09 17:15:43 +01:00
|
|
|
// gets called once at boot. Do all initialization that doesn't depend on
|
|
|
|
// network here
|
|
|
|
void setup() {
|
2021-05-07 12:41:39 +02:00
|
|
|
#ifdef USERMOD_FOUR_LINE_DISPLAY
|
|
|
|
// This Usermod has enhanced funcionality if
|
|
|
|
// FourLineDisplayUsermod is available.
|
|
|
|
display = (FourLineDisplayUsermod*) usermods.lookup(USERMOD_ID_FOUR_LINE_DISP);
|
|
|
|
#endif
|
|
|
|
initDone = true;
|
2021-02-09 17:15:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// gets called every time WiFi is (re-)connected. Initialize own network
|
|
|
|
// interfaces here
|
|
|
|
void connected() {}
|
|
|
|
|
2021-05-07 12:41:39 +02:00
|
|
|
/*
|
2021-02-09 17:15:43 +01:00
|
|
|
* Da loop.
|
|
|
|
*/
|
|
|
|
void loop() {
|
2021-05-07 12:41:39 +02:00
|
|
|
if (!autoSaveAfterSec || !enabled) return; // setting 0 as autosave seconds disables autosave
|
|
|
|
|
2021-02-09 17:15:43 +01:00
|
|
|
unsigned long now = millis();
|
|
|
|
uint8_t currentMode = strip.getMode();
|
|
|
|
uint8_t currentPalette = strip.getSegment(0).palette;
|
|
|
|
if (firstLoop) {
|
|
|
|
firstLoop = false;
|
2021-05-07 12:41:39 +02:00
|
|
|
if (applyAutoSaveOnBoot) applyPreset(autoSavePreset);
|
2021-02-09 17:15:43 +01:00
|
|
|
knownBrightness = bri;
|
|
|
|
knownEffectSpeed = effectSpeed;
|
|
|
|
knownEffectIntensity = effectIntensity;
|
|
|
|
knownMode = currentMode;
|
|
|
|
knownPalette = currentPalette;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:41:39 +02:00
|
|
|
unsigned long wouldAutoSaveAfter = now + autoSaveAfterSec*1000;
|
2021-02-09 17:15:43 +01:00
|
|
|
if (knownBrightness != bri) {
|
|
|
|
knownBrightness = bri;
|
|
|
|
autoSaveAfter = wouldAutoSaveAfter;
|
|
|
|
} else if (knownEffectSpeed != effectSpeed) {
|
|
|
|
knownEffectSpeed = effectSpeed;
|
|
|
|
autoSaveAfter = wouldAutoSaveAfter;
|
|
|
|
} else if (knownEffectIntensity != effectIntensity) {
|
|
|
|
knownEffectIntensity = effectIntensity;
|
|
|
|
autoSaveAfter = wouldAutoSaveAfter;
|
|
|
|
} else if (knownMode != currentMode) {
|
|
|
|
knownMode = currentMode;
|
|
|
|
autoSaveAfter = wouldAutoSaveAfter;
|
|
|
|
} else if (knownPalette != currentPalette) {
|
|
|
|
knownPalette = currentPalette;
|
|
|
|
autoSaveAfter = wouldAutoSaveAfter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoSaveAfter && now > autoSaveAfter) {
|
|
|
|
autoSaveAfter = 0;
|
|
|
|
// Time to auto save. You may have some flickry?
|
|
|
|
saveSettings();
|
|
|
|
displayOverlay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:41:39 +02:00
|
|
|
/*
|
|
|
|
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
|
|
|
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
|
|
|
|
* Below it is shown how this could be used for e.g. a light sensor
|
|
|
|
*/
|
|
|
|
//void addToJsonInfo(JsonObject& root) {
|
|
|
|
//JsonObject user = root["u"];
|
|
|
|
//if (user.isNull()) user = root.createNestedObject("u");
|
|
|
|
//JsonArray data = user.createNestedArray(F("Autosave"));
|
|
|
|
//data.add(F("Loaded."));
|
|
|
|
//}
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
|
|
|
|
* Values in the state object may be modified by connected clients
|
|
|
|
*/
|
2021-05-07 12:41:39 +02:00
|
|
|
//void addToJsonState(JsonObject& root) {
|
|
|
|
//}
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
|
|
|
|
* Values in the state object may be modified by connected clients
|
|
|
|
*/
|
2021-05-07 12:41:39 +02:00
|
|
|
//void readFromJsonState(JsonObject& root) {
|
|
|
|
// if (!initDone) return; // prevent crash on boot applyPreset()
|
|
|
|
//}
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object.
|
|
|
|
* It will be called by WLED when settings are actually saved (for example, LED settings are saved)
|
|
|
|
* If you want to force saving the current state, use serializeConfig() in your loop().
|
|
|
|
*
|
|
|
|
* CAUTION: serializeConfig() will initiate a filesystem write operation.
|
|
|
|
* It might cause the LEDs to stutter and will cause flash wear if called too often.
|
|
|
|
* Use it sparingly and always in the loop, never in network callbacks!
|
|
|
|
*
|
|
|
|
* addToConfig() will also not yet add your setting to one of the settings pages automatically.
|
|
|
|
* To make that work you still have to add the setting to the HTML, xml.cpp and set.cpp manually.
|
|
|
|
*
|
|
|
|
* I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings!
|
|
|
|
*/
|
|
|
|
void addToConfig(JsonObject& root) {
|
2021-05-07 12:41:39 +02:00
|
|
|
// 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;
|
|
|
|
DEBUG_PRINTLN(F("Autosave config saved."));
|
2021-02-09 17:15:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* readFromConfig() can be used to read back the custom settings you added with addToConfig().
|
|
|
|
* This is called by WLED when settings are loaded (currently this only happens once immediately after boot)
|
|
|
|
*
|
|
|
|
* readFromConfig() is called BEFORE setup(). This means you can use your persistent values in setup() (e.g. pin assignments, buffer sizes),
|
|
|
|
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
|
|
|
|
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
|
|
|
|
*/
|
|
|
|
void readFromConfig(JsonObject& root) {
|
2021-05-07 12:41:39 +02:00
|
|
|
// we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}}
|
|
|
|
JsonObject top = root[FPSTR(_name)];
|
|
|
|
if (top.isNull()) {
|
|
|
|
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top[FPSTR(_autoSaveEnabled)].is<bool>()) {
|
|
|
|
// reading from cfg.json
|
|
|
|
enabled = top[FPSTR(_autoSaveEnabled)].as<bool>();
|
|
|
|
} else {
|
|
|
|
// reading from POST message
|
|
|
|
String str = top[FPSTR(_autoSaveEnabled)]; // checkbox -> off or on
|
|
|
|
enabled = (bool)(str!="off"); // off is guaranteed to be present
|
|
|
|
}
|
|
|
|
autoSaveAfterSec = min(3600,max(10,top[FPSTR(_autoSaveAfterSec)].as<int>()));
|
|
|
|
autoSavePreset = min(250,max(100,top[FPSTR(_autoSavePreset)].as<int>()));
|
|
|
|
if (top[FPSTR(_autoSaveApplyOnBoot)].is<bool>()) {
|
|
|
|
// reading from cfg.json
|
|
|
|
applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)].as<bool>();
|
|
|
|
} else {
|
|
|
|
// reading from POST message
|
|
|
|
String str = top[FPSTR(_autoSaveApplyOnBoot)]; // checkbox -> off or on
|
|
|
|
applyAutoSaveOnBoot = (bool)(str!="off"); // off is guaranteed to be present
|
|
|
|
}
|
|
|
|
DEBUG_PRINTLN(F("Autosave config (re)loaded."));
|
|
|
|
}
|
2021-02-09 17:15:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
|
|
|
|
* This could be used in the future for the system to determine whether your usermod is installed.
|
|
|
|
*/
|
|
|
|
uint16_t getId() {
|
|
|
|
return USERMOD_ID_AUTO_SAVE;
|
|
|
|
}
|
2021-05-07 12:41:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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";
|