Skip to content

Commit f1e2439

Browse files
committed
Slight IR JSON simplefication
Check for missing file No duplicate cmd object
1 parent a839809 commit f1e2439

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

wled00/const.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
#define ERR_FS_BEGIN 10 // Could not init filesystem (no partition?)
216216
#define ERR_FS_QUOTA 11 // The FS is full or the maximum file size is reached
217217
#define ERR_FS_PLOAD 12 // It was attempted to load a preset that does not exist
218+
#define ERR_FS_IRLOAD 13 // It was attempted to load an IR JSON cmd, but the "ir.json" file does not exist
218219
#define ERR_FS_GENERAL 19 // A general unspecified filesystem error occured
219220
#define ERR_OVERTEMP 30 // An attached temperature sensor has measured above threshold temperature (not implemented)
220221
#define ERR_OVERCURRENT 31 // An attached current sensor has measured a current above the threshold (not implemented)

wled00/ir.cpp

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -568,60 +568,62 @@ void decodeIRJson(uint32_t code)
568568
char objKey[10];
569569
const char* cmd;
570570
String cmdStr;
571-
byte irError;
572571
DynamicJsonDocument irDoc(JSON_BUFFER_SIZE);
573572
JsonObject fdo;
574573
JsonObject jsonCmdObj;
575574

576575
sprintf(objKey, "\"0x%X\":", code);
577576

578-
irError = readObjectFromFile("/ir.json", objKey, &irDoc) ? ERR_NONE : ERR_FS_PLOAD;
577+
readObjectFromFile("/ir.json", objKey, &irDoc);
579578
fdo = irDoc.as<JsonObject>();
580579
lastValidCode = 0;
581-
if (!irError)
580+
if (fdo.isNull()) {
581+
//the received code does not exist
582+
if (!WLED_FS.exists("/ir.json")) errorFlag = ERR_FS_IRLOAD; //warn if IR file itself doesn't exist
583+
return;
584+
}
585+
586+
jsonCmdObj = fdo["cmd"];
587+
cmdStr = String(jsonCmdObj);
588+
589+
if (!cmdStr.isEmpty())
582590
{
583-
cmd = fdo["cmd"];
584-
cmdStr = String(cmd);
585-
jsonCmdObj = fdo["cmd"];
586-
if (!cmdStr.isEmpty())
587-
{
588-
if (cmdStr.startsWith("!")) {
589-
// call limited set of C functions
590-
if (cmdStr.startsWith(F("!incBri"))) {
591-
lastValidCode = code;
592-
incBrightness();
593-
} else if (cmdStr.startsWith(F("!decBri"))) {
594-
lastValidCode = code;
595-
decBrightness();
596-
} else if (cmdStr.startsWith(F("!presetF"))) { //!presetFallback
597-
uint8_t p1 = fdo["PL"] ? fdo["PL"] : 1;
598-
uint8_t p2 = fdo["FX"] ? fdo["FX"] : random8(MODE_COUNT);
599-
uint8_t p3 = fdo["FP"] ? fdo["FP"] : 0;
600-
presetFallback(p1, p2, p3);
601-
}
602-
} else {
603-
// HTTP API command
604-
if (cmdStr.indexOf("~") || fdo["rpt"])
605-
{
606-
// repeatable action
607-
lastValidCode = code;
608-
}
609-
if (effectCurrent == 0 && cmdStr.indexOf("FP=") > -1) {
610-
// setting palette but it wont show because effect is solid
611-
effectCurrent = FX_MODE_GRADIENT;
612-
}
613-
if (!cmdStr.startsWith("win&")) {
614-
cmdStr = "win&" + cmdStr;
615-
}
616-
handleSet(nullptr, cmdStr, false);
617-
}
618-
} else if (!jsonCmdObj.isNull()) {
619-
// command is JSON object
620-
//allow applyPreset() to reuse JSON buffer, or it would alloc. a second buffer and run out of mem.
621-
fileDoc = &irDoc;
622-
deserializeState(jsonCmdObj, CALL_MODE_BUTTON);
623-
fileDoc = nullptr;
624-
}
591+
if (cmdStr.startsWith("!")) {
592+
// call limited set of C functions
593+
if (cmdStr.startsWith(F("!incBri"))) {
594+
lastValidCode = code;
595+
incBrightness();
596+
} else if (cmdStr.startsWith(F("!decBri"))) {
597+
lastValidCode = code;
598+
decBrightness();
599+
} else if (cmdStr.startsWith(F("!presetF"))) { //!presetFallback
600+
uint8_t p1 = fdo["PL"] ? fdo["PL"] : 1;
601+
uint8_t p2 = fdo["FX"] ? fdo["FX"] : random8(MODE_COUNT);
602+
uint8_t p3 = fdo["FP"] ? fdo["FP"] : 0;
603+
presetFallback(p1, p2, p3);
604+
}
605+
} else {
606+
// HTTP API command
607+
if (cmdStr.indexOf("~") || fdo["rpt"])
608+
{
609+
// repeatable action
610+
lastValidCode = code;
611+
}
612+
if (effectCurrent == 0 && cmdStr.indexOf("FP=") > -1) {
613+
// setting palette but it wont show because effect is solid
614+
effectCurrent = FX_MODE_GRADIENT;
615+
}
616+
if (!cmdStr.startsWith("win&")) {
617+
cmdStr = "win&" + cmdStr;
618+
}
619+
handleSet(nullptr, cmdStr, false);
620+
}
621+
} else if (!jsonCmdObj.isNull()) {
622+
// command is JSON object
623+
//allow applyPreset() to reuse JSON buffer, or it would alloc. a second buffer and run out of mem.
624+
fileDoc = &irDoc;
625+
deserializeState(jsonCmdObj, CALL_MODE_BUTTON);
626+
fileDoc = nullptr;
625627
}
626628
}
627629

0 commit comments

Comments
 (0)