Skip to content

Commit

Permalink
Done in principle
Browse files Browse the repository at this point in the history
  • Loading branch information
Aircoookie committed Nov 6, 2020
1 parent d9050dd commit 3ff23ad
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 519 deletions.
4 changes: 2 additions & 2 deletions wled00/alexa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void onAlexaChange(EspalexaDevice* dev)
bri = briLast;
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
}
} else applyMacro(macroAlexaOn);
} else applyPreset(macroAlexaOn);
} else if (m == EspalexaDeviceProperty::off)
{
if (!macroAlexaOff)
Expand All @@ -57,7 +57,7 @@ void onAlexaChange(EspalexaDevice* dev)
bri = 0;
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
}
} else applyMacro(macroAlexaOff);
} else applyPreset(macroAlexaOff);
} else if (m == EspalexaDeviceProperty::bri)
{
bri = espalexaDevice->getValue();
Expand Down
6 changes: 3 additions & 3 deletions wled00/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void shortPressAction()
toggleOnOff();
colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
} else {
applyMacro(macroButton);
applyPreset(macroButton);
}
}

Expand Down Expand Up @@ -41,7 +41,7 @@ void handleButton()
{
if (!buttonLongPressed)
{
if (macroLongPress) {applyMacro(macroLongPress);}
if (macroLongPress) {applyPreset(macroLongPress);}
else _setRandomColor(false,true);

buttonLongPressed = true;
Expand All @@ -62,7 +62,7 @@ void handleButton()
else if (!buttonLongPressed) { //short press
if (macroDoublePress)
{
if (doublePress) applyMacro(macroDoublePress);
if (doublePress) applyPreset(macroDoublePress);
else buttonWaitTime = millis();
} else shortPressAction();
}
Expand Down
56 changes: 47 additions & 9 deletions wled00/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void deserializeConfig() {
bool fromeep = false;
bool success = deserializeConfigSec();
if (!success) { //if file does not exist, try reading from EEPROM
loadSettingsFromEEPROM();
deEEPSettings();
fromeep = true;
}

Expand All @@ -26,7 +26,7 @@ void deserializeConfig() {

success = readObjectFromFile("/cfg.json", nullptr, &doc);
if (!success) { //if file does not exist, try reading from EEPROM
if (!fromeep) deEEP();
if (!fromeep) deEEPSettings();
return;
}

Expand Down Expand Up @@ -298,17 +298,15 @@ void deserializeConfig() {
getStringFromJson(otaPass, pwd, 33); //normally not present due to security
}

DEBUG_PRINTLN(F("Reading settings from /wsec.json..."));

success = readObjectFromFile("/wsec.json", nullptr, &doc);
if (!success) { //if file does not exist, try reading from EEPROM
loadSettingsFromEEPROM();
}
//DMX missing!
}

void serializeConfig() {
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));

DynamicJsonDocument doc(JSON_BUFFER_SIZE);

//{ //scope this to reduce stack size
JsonArray rev = doc.createNestedArray("rev");
rev.add(1); //major settings revision
rev.add(0); //minor settings revision
Expand Down Expand Up @@ -564,14 +562,22 @@ void serializeConfig() {
ota["lock-wifi"] = wifiLock;
ota["pskl"] = strlen(otaPass);
ota["aota"] = aOtaEnabled;
//}

serializeJson(doc, Serial);
File f = WLED_FS.open("/cfg.json", "w");
if (f) serializeJson(doc, f);
f.close();
}

//settings in /wsec.json, not accessible via webserver, for passwords and tokens
bool deserializeConfigSec() {
DEBUG_PRINTLN(F("Reading settings from /wsec.json..."));

DynamicJsonDocument doc(JSON_BUFFER_SIZE);

bool success = readObjectFromFile("/wsec.json", nullptr, &doc);
if (!success) return false;

JsonObject nw_ins_0 = doc["nw"]["ins"][0];
getStringFromJson(clientPass, nw_ins_0["psk"], 65);

Expand All @@ -595,8 +601,40 @@ bool deserializeConfigSec() {
CJSON(otaLock, ota["lock"]);
CJSON(wifiLock, ota["lock-wifi"]);
CJSON(aOtaEnabled, ota["aota"]);

return true;
}

void serializeConfigSec() {
DEBUG_PRINTLN(F("Writing settings to /wsec.json..."));

DynamicJsonDocument doc(JSON_BUFFER_SIZE);

JsonObject nw = doc.createNestedObject("nw");

JsonArray nw_ins = nw.createNestedArray("ins");

JsonObject nw_ins_0 = nw_ins.createNestedObject();
nw_ins_0["psk"] = clientPass;

JsonObject ap = doc.createNestedObject("ap");
ap["psk"] = apPass;

JsonObject interfaces = doc.createNestedObject("if");
JsonObject if_blynk = interfaces.createNestedObject("blynk");
if_blynk["token"] = blynkApiKey;
JsonObject if_mqtt = interfaces.createNestedObject("mqtt");
if_mqtt["psk"] = mqttPass;
JsonObject if_hue = interfaces.createNestedObject("hue");
if_hue["key"] = hueApiKey;

JsonObject ota = doc.createNestedObject("ota");
ota["pwd"] = otaPass;
ota["lock"] = otaLock;
ota["lock-wifi"] = wifiLock;
ota["aota"] = aOtaEnabled;

File f = WLED_FS.open("/wsec.json", "w");
if (f) serializeJson(doc, f);
f.close();
}
2 changes: 0 additions & 2 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@
#define NL_MODE_COLORFADE 2 //Fade to target brightness and secondary color gradually
#define NL_MODE_SUN 3 //Sunrise/sunset. Target brightness is set immediately, then Sunrise effect is started. Max 60 min.

//EEPROM size
#define EEPSIZE 2560 //Maximum is 4096

#define NTP_PACKET_SIZE 48

Expand Down
17 changes: 5 additions & 12 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ void setCronixie();
void _overlayCronixie();
void _drawOverlayCronixie();

//presets.cpp
bool applyPreset(byte index);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, JsonObject saveobj = JsonObject());
void deletePreset(byte index);

//set.cpp
void _setRandomColor(bool _sec,bool fromButton=false);
bool isAsterisksOnly(const char* str, byte maxLen);
Expand Down Expand Up @@ -201,19 +206,7 @@ void userConnected();
void userLoop();

//wled_eeprom.cpp
void commit();
void clearEEPROM();
void writeStringToEEPROM(uint16_t pos, char* str, uint16_t len);
void readStringFromEEPROM(uint16_t pos, char* str, uint16_t len);
void saveSettingsToEEPROM();
void loadSettingsFromEEPROM();
void savedToPresets();
bool applyPreset(byte index);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, JsonObject saveobj = JsonObject());
void deletePreset(byte index);
void loadMacro(byte index, char* m);
void applyMacro(byte index);
void saveMacro(byte index, const String& mc, bool persist = true); //only commit on single save, not in settings
void deEEP();
void deEEPSettings();

Expand Down
2 changes: 1 addition & 1 deletion wled00/hue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void handleHue()
colorUpdated(NOTIFIER_CALL_MODE_HUE); hueReceived = false;
if (hueStoreAllowed && hueNewKey)
{
saveSettingsToEEPROM(); //save api key
serializeConfigSec(); //save api key
hueStoreAllowed = false;
hueNewKey = false;
}
Expand Down
2 changes: 1 addition & 1 deletion wled00/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool decodeIRCustom(uint32_t code)
{
//just examples, feel free to modify or remove
case IRCUSTOM_ONOFF : toggleOnOff(); break;
case IRCUSTOM_MACRO1 : applyMacro(1); break;
case IRCUSTOM_MACRO1 : applyPreset(1); break;

default: return false;
}
Expand Down
2 changes: 1 addition & 1 deletion wled00/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void handleNightlight()
}
updateBlynk();
if (macroNl > 0)
applyMacro(macroNl);
applyPreset(macroNl);
nightlightActiveOld = false;
}
} else if (nightlightActiveOld) //early de-init
Expand Down
72 changes: 72 additions & 0 deletions wled00/presets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "wled.h"

/*
* Methods to handle saving and loading presets to/from the filesystem
*/

bool applyPreset(byte index)
{
if (fileDoc) {
errorFlag = readObjectFromFileUsingId("/presets.json", index, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
#ifdef WLED_DEBUG_FS
serializeJson(*fileDoc, Serial);
#endif
deserializeState(fileDoc->as<JsonObject>());
} else {
DEBUGFS_PRINTLN(F("Make read buf"));
DynamicJsonDocument fDoc(JSON_BUFFER_SIZE);
errorFlag = readObjectFromFileUsingId("/presets.json", index, &fDoc) ? ERR_NONE : ERR_FS_PLOAD;
#ifdef WLED_DEBUG_FS
serializeJson(fDoc, Serial);
#endif
deserializeState(fDoc.as<JsonObject>());
}

if (!errorFlag) {
currentPreset = index;
isPreset = true;
return true;
}
return false;
}

void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
{
if (index == 0) return;
bool docAlloc = fileDoc;
JsonObject sObj = saveobj;

if (!docAlloc) {
DEBUGFS_PRINTLN(F("Allocating saving buffer"));
fileDoc = new DynamicJsonDocument(JSON_BUFFER_SIZE);
sObj = fileDoc->to<JsonObject>();
if (pname) sObj["n"] = pname;
} else {
DEBUGFS_PRINTLN(F("Reuse recv buffer"));
sObj.remove(F("psave"));
sObj.remove(F("v"));
}

if (!sObj["o"]) {
DEBUGFS_PRINTLN(F("Save current state"));
serializeState(sObj, true, sObj["ib"], sObj["sb"]);
currentPreset = index;
}
sObj.remove("o");
sObj.remove("ib");
sObj.remove("sb");
sObj.remove(F("error"));
sObj.remove(F("time"));

writeObjectToFileUsingId("/presets.json", index, fileDoc);
if (!docAlloc) delete fileDoc;
presetsModifiedTime = now(); //unix time
updateFSInfo();
}

void deletePreset(byte index) {
StaticJsonDocument<24> empty;
writeObjectToFileUsingId("/presets.json", index, &empty);
presetsModifiedTime = now(); //unix time
updateFSInfo();
}
40 changes: 4 additions & 36 deletions wled00/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,6 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
countdownMin = request->arg(F("CM")).toInt();
countdownSec = request->arg(F("CS")).toInt();

for (int i=1;i<17;i++)
{
String a = "M"+String(i);
if (request->hasArg(a.c_str())) saveMacro(i,request->arg(a),false);
}

macroBoot = request->arg(F("MB")).toInt();
macroAlexaOn = request->arg(F("A0")).toInt();
macroAlexaOff = request->arg(F("A1")).toInt();
macroButton = request->arg(F("MP")).toInt();
Expand Down Expand Up @@ -272,7 +265,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
{
if (request->hasArg(F("RS"))) //complete factory reset
{
clearEEPROM();
WLED_FS.format();
serveMessage(request, 200, F("All Settings erased."), F("Connect to WLED-AP to setup again"),255);
doReboot = true;
}
Expand Down Expand Up @@ -327,7 +320,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
}

#endif
if (subPage != 6 || !doReboot) saveSettingsToEEPROM(); //do not save if factory reset
if (subPage != 6 || !doReboot) serializeConfig(); //do not save if factory reset
if (subPage == 2) {
strip.init(useRGBW,ledCount,skipFirstLed);
}
Expand Down Expand Up @@ -382,31 +375,6 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
DEBUG_PRINT(F("API req: "));
DEBUG_PRINTLN(req);

//write presets and macros saved to flash directly?
bool persistSaves = true;
pos = req.indexOf(F("NP"));
if (pos > 0) {
persistSaves = false;
}

//save macro, requires &MS=<slot>(<macro>) format
pos = req.indexOf(F("&MS="));
if (pos > 0) {
int i = req.substring(pos + 4).toInt();
pos = req.indexOf('(') +1;
if (pos > 0) {
int en = req.indexOf(')');
String mc = req.substring(pos);
if (en > 0) mc = req.substring(pos, en);
saveMacro(i, mc, persistSaves);
}

pos = req.indexOf(F("IN"));
if (pos < 1) XML_response(request);
return true;
//if you save a macro in one request, other commands in that request are ignored due to unwanted behavior otherwise
}

strip.applyToAllSelected = true;

//segment select (sets main segment)
Expand Down Expand Up @@ -591,10 +559,10 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
overlayCurrent = getNumVal(&req, pos);
}

//apply macro
//apply macro (deprecated, added for compatibility with pre-0.11 automations)
pos = req.indexOf(F("&M="));
if (pos > 0) {
applyMacro(getNumVal(&req, pos));
applyPreset(getNumVal(&req, pos) + 16);
}

//toggle send UDP direct notifications
Expand Down
Loading

0 comments on commit 3ff23ad

Please sign in to comment.