Skip to content

Commit

Permalink
Stability up, preview loads at page refresh and json to file exclusions
Browse files Browse the repository at this point in the history
AppModLeds: 
- ledfix sends file to pview fixed
- add Fixtures enum
- FastLED.addLeds moved (solved crash?!?)
- changeDimensions removed

SysModFiles:
- writeObjectToFile commented out and replaced by:
- LazyJsonRDWS add file write in constructor
- add LazyJsonRDWS.addExclusion for elements not to be written (uiFun and chFun in our case (solved crash?!?)
- add LazyJsonRDWS.writeJsonVariantToFile

SysModModel: use LazyJsonRDWS to write model.json

SysModUI
- change UserLoop by ObjectLoop
- change uiFunctions by ucFunctions
- USFun by UCFun
  • Loading branch information
ewoudwijma committed Jul 23, 2023
1 parent d095fc9 commit 38ecaf3
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 290 deletions.
6 changes: 6 additions & 0 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function generateHTML(parentNode, json) {

newNode = cE("canvas");
newNode.id = json.id;
newNode.addEventListener('click', (event) => {console.log(json.type + " click", event);bigCanvas(event.target, true);});
}
else { //input
newNode = cE("p");
Expand Down Expand Up @@ -361,6 +362,11 @@ function setDropdown(element) {
requestJson(command);
}

function bigCanvas(element, doCreate) {
console.log("bigCanvas", element, doCreate);
}


function initCap(s) {
if (typeof s !== 'string') return '';
// https://www.freecodecamp.org/news/how-to-capitalize-words-in-javascript/
Expand Down
104 changes: 51 additions & 53 deletions src/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,15 @@ class AppModLeds:public Module {
files->dirToJson2(lov, "lf"); //only files containing lf, alphabetically

// ui needs to load the file also initially
// char fileName[30] = "";
// if (files->seqNrToName(fileName, object["value"].as<int>())) {
// web->addResponse("pview", "file", fileName);
// }
char fileName[30] = "";
if (files->seqNrToName(fileName, object["value"])) {
web->addResponse("pview", "file", fileName);
}
}, [](JsonObject object) { //chFun
print->print("%s Change %s to %d\n", "initDropdown chFun", object["id"].as<const char *>(), object["value"].as<int>());

char fileName[30] = "";
if (files->seqNrToName(fileName, object["value"].as<int>())) {
if (files->seqNrToName(fileName, object["value"])) {
LazyJsonRDWS ljrdws(fileName); //open fileName for deserialize

//what to deserialize
Expand All @@ -299,29 +299,36 @@ class AppModLeds:public Module {
(strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) != 0?web->responseDoc0:web->responseDoc1)->clear();
web->addResponse("pview", "file", fileName);
web->sendDataWs(responseVariant);
print->printJson("ledfix send ws done", responseVariant); //during server startup this is not send to a client, so client refresh should also trigger this
print->printJson("ledfix chFun send ws done", responseVariant); //during server startup this is not send to a client, so client refresh should also trigger this
} // if deserialize
} //if fileName
});

ui->initDisplay(parentObject, "width", nullptr, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Max %dK", 32);
}, [](JsonObject object) { //chFun
width = object["value"];
});

ui->initDisplay(parentObject, "height", nullptr, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Max %dK", 32);
}, [](JsonObject object) { //chFun
height = object["value"];
});

ui->initDisplay(parentObject, "depth", nullptr, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Max %dK", 32);
}, [](JsonObject object) { //chFun
depth = object["value"];
});

ui->initDisplay(parentObject, "nrOfLeds", nullptr, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Max %d (FastLed max %d)", NUM_LEDS_preview, NUM_LEDS_Fastled);
});
// mdl->setValueI("nrOfLeds", nrOfLeds); //set here as changeDimensions already called for width/height/depth

// if (!leds)
// leds = (CRGB*)calloc(nrOfLeds, sizeof(CRGB));
// else
// leds = (CRGB*)reallocarray(leds, nrOfLeds, sizeof(CRGB));
// if (leds) free(leds);
// leds = (CRGB*)malloc(nrOfLeds * sizeof(CRGB));
// leds = (CRGB*)reallocarray

ui->initNumber(parentObject, "fps", fps, [](JsonObject object) { //uiFun
web->addResponse(object["id"], "comment", "Frames per second");
}, [](JsonObject object) { //chFun
Expand All @@ -331,21 +338,25 @@ class AppModLeds:public Module {

ui->initDisplay(parentObject, "realFps");

ui->initDisplay(parentObject, "nrOfLeds", nullptr, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Max %d (FastLed max %d)", NUM_LEDS_preview, NUM_LEDS_Fastled);
}, [](JsonObject object) { //chFun
print->print("nrOfLeds changed %d\n", fps);
changeDimensions();
// fadeToBlackBy( leds, nrOfLeds, 100);
});
// mdl->setValueI("nrOfLeds", nrOfLeds); //set here as changeDimensions already called for width/height/depth

ui->initNumber(parentObject, "dataPin", dataPin, [](JsonObject object) { //uiFun
web->addResponseV(object["id"], "comment", "Not implemented yet (fixed to %d)", DATA_PIN);
}, [](JsonObject object) { //chFun
print->print("Set data pin to %d\n", object["value"].as<int>());
});

enum Fixtures
{
Spiral,
R24,
R35,
M88,
M888,
M888h,
HSC,
Globe
};

ui->initDropdown(parentObject, "ledFixGen", 3, [](JsonObject object) { //uiFun
web->addResponse(object["id"], "label", "Ledfix generator");
JsonArray lov = web->addResponseA(object["id"], "lov");
Expand All @@ -369,35 +380,35 @@ class AppModLeds:public Module {

size_t fix = object["value"];
switch (fix) {
case 0: //Spiral
case Spiral:
name = "Spiral";
nrOfLeds = 64;
break;
case 1: //R24
case R24:
name = "R24";
nrOfLeds = 24;
break;
case 2: //R35
case R35:
name = "R35";
nrOfLeds = 35;
break;
case 3: //M88
case M88:
name = "M88";
nrOfLeds = 64;
break;
case 4: //M888
case M888:
name = "M888";
nrOfLeds = 512;
break;
case 5: //M888h
case M888h:
name = "M888h";
nrOfLeds = 320;
break;
case 6: //HSC
case HSC:
name = "HSC";
nrOfLeds = 2000;
break;
case 7: //Globe
case Globe:
name = "Globe";
nrOfLeds = 512;
break;
Expand All @@ -420,9 +431,9 @@ class AppModLeds:public Module {
f.printf(",\"nrOfLeds\":%d", nrOfLeds);
f.printf(",\"pin\":%d",16);
switch (fix) {
case 0: //Spiral
case 1: //R24
case 2: //R35
case Spiral:
case R24:
case R35:
diameter = 100; //in mm

f.printf(",\"scale\":\"%s\"", "mm");
Expand All @@ -445,7 +456,7 @@ class AppModLeds:public Module {
}
f.printf("]");
break;
case 3: //M88
case M88:
diameter = 8; //in cm

f.printf(",\"scale\":\"%s\"", "cm");
Expand All @@ -470,7 +481,7 @@ class AppModLeds:public Module {
f.printf("]");

break;
case 4: //M888
case M888:
diameter = 8; //in cm

f.printf(",\"scale\":\"%s\"", "cm");
Expand All @@ -493,8 +504,8 @@ class AppModLeds:public Module {
f.printf("]");

break;
case 5: //M888h
case 6: //HSC
case M888h:
case HSC:
if (fix==5) {
diameter = 8; //in cm
width = 8;
Expand Down Expand Up @@ -540,7 +551,7 @@ class AppModLeds:public Module {
f.printf("]");

break;
case 7: //Globe
case Globe:
diameter = 100; //in mm

f.printf(",\"scale\":\"%s\"", "mm");
Expand Down Expand Up @@ -574,7 +585,7 @@ class AppModLeds:public Module {

//reload ledfix dropdown
ui->processUiFun("ledFix");
});
}); //ledFixGen

effects.push_back(new RainbowEffect);
effects.push_back(new RainbowWithGlitterEffect);
Expand All @@ -586,6 +597,9 @@ class AppModLeds:public Module {
effects.push_back(new Ripples3DEffect);
effects.push_back(new SphereMove3DEffect);

// FastLED.addLeds<NEOPIXEL, 6>(leds, 1);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS_Fastled);

print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

Expand Down Expand Up @@ -613,22 +627,6 @@ class AppModLeds:public Module {
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
}

static void changeDimensions() { //static because of lambda functions
// nrOfLeds = min(width * height * depth, NUM_LEDS_preview); //not highter then 4K leds
// mdl->setValueI("nrOfLeds", nrOfLeds);
print->print("changeDimensions %d x %d x %d and %d\n", width, height, depth, nrOfLeds);
// if (!leds)
// leds = (CRGB*)calloc(nrOfLeds, sizeof(CRGB));
// else
// leds = (CRGB*)reallocarray(leds, nrOfLeds, sizeof(CRGB));
// if (leds) free(leds);
// leds = (CRGB*)malloc(nrOfLeds * sizeof(CRGB));
// leds = (CRGB*)reallocarray
// FastLED.addLeds<NEOPIXEL, 6>(leds, 1);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS_Fastled);
}

};

static AppModLeds *lds;
27 changes: 14 additions & 13 deletions src/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,20 @@ bool SysModFiles::readObjectFromFile(const char* path, JsonDocument* dest) {
}
}

bool SysModFiles::writeObjectToFile(const char* path, JsonDocument* dest) {
File f = open(path, "w");
if (f) {
print->println(F(" success"));
serializeJson(*dest, f);
f.close();
filesChange();
return true;
} else {
print->println(F(" fail"));
return false;
}
}
//candidate for deletion as taken over by LazyJsonRDWS
// bool SysModFiles::writeObjectToFile(const char* path, JsonDocument* dest) {
// File f = open(path, "w");
// if (f) {
// print->println(F(" success"));
// serializeJson(*dest, f);
// f.close();
// filesChange();
// return true;
// } else {
// print->println(F(" fail"));
// return false;
// }
// }

void SysModFiles::removeFiles(const char * filter, bool reverse) {
File root = LittleFS.open("/");
Expand Down
65 changes: 62 additions & 3 deletions src/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class SysModFiles:public Module {

//write json into file
//name is copied from WLED but better to call it readJsonFrom file
bool writeObjectToFile(const char* path, JsonDocument* dest);
//candidate for deletion as taken over by LazyJsonRDWS
// bool writeObjectToFile(const char* path, JsonDocument* dest);

//remove files meeting filter condition, if no filter, all, if reverse then all but filter
void removeFiles(const char * filter = nullptr, bool reverse = false);
Expand All @@ -61,15 +62,28 @@ class LazyJsonRDWS {
public:
bool foundAll = false;

LazyJsonRDWS(const char * path) {
f = files->open(path, "r");
LazyJsonRDWS(const char * path, const char * mode = "r") {
print->print("LazyJsonRDWS constructing %s %s\n", path, mode);
f = files->open(path, mode);
if (!f)
print->print("LazyJsonRDWS open %s for %s failed", path, mode);
}

~LazyJsonRDWS() {
print->print("LazyJsonRDWS destructing\n");
f.close();
}

void addExclusion(const char * key) {
charList.push_back((char *)key);
}

//serializeJson
void writeJsonDocToFile(JsonDocument* dest) {
writeJsonVariantToFile(dest->as<JsonVariant>());
files->filesChange();
}

//look for uint8 object
void lookFor(const char * id, uint8_t * value) {
// const char *p = (const char*)&value; //pointer trick
Expand Down Expand Up @@ -212,4 +226,49 @@ class LazyJsonRDWS {
print->print("Hooray, LazyJsonRDWS found all what we were looking for, no further search needed\n");
}

//writeJsonVariantToFile calls itself recursively until whole json document has been parsed
void writeJsonVariantToFile(JsonVariant variant) {
if (variant.is<JsonObject>()) {
f.printf("{");
char sep[3] = "";
for (JsonPair pair: variant.as<JsonObject>()) {
bool found = false;
for (char *el:charList) {
if (strcmp(el, pair.key().c_str())==0) {
found = true;
break;
}
}
// std::vector<char *>::iterator itr = find(charList.begin(), charList.end(), pair.key().c_str());
if (!found) { //not found
f.printf("%s\"%s\":", sep, pair.key().c_str());
strcpy(sep,",");
writeJsonVariantToFile(pair.value());
}
}
f.printf("}");
}
else if (variant.is<JsonArray>()) {
f.printf("[");
char sep[3] = "";
for (JsonVariant variant2: variant.as<JsonArray>()) {
f.print(sep);
strcpy(sep,",");
writeJsonVariantToFile(variant2);
}
f.printf("]");
}
else if (variant.is<const char *>()) {
f.printf("\"%s\"", variant.as<const char *>());
}
else if (variant.is<int>()) {
f.printf("%d", variant.as<int>());
}
else if (variant.is<bool>()) {
f.printf("%s", variant.as<bool>()?"true":"false");
}
else
print->print("%s not supported", variant.as<String>());
}

};
Loading

0 comments on commit 38ecaf3

Please sign in to comment.