Skip to content

Commit

Permalink
Lambda [this] for Files, Model and Modules, add loop1s and loop10s
Browse files Browse the repository at this point in the history
index.js:
- generateHTML: select: add isPartOfTable
- small changes

AppModLeds: use loop1s & private vars

Module.h: replace secondMillis by loop1s and loop10s

SysModFiles
- remove static for filesChanged and dirToJson

SysModModel
- remove static for doWriteModel and doShowObsolete
- use loop1s

SysModModules
- remove static for modules
- add oneSecondMillis and tenSecondMillis
- loop: implement loop1s and loop10s

SysModNetwork: use loop1s

SysModSystem: use loop1s and loop10s

SysModUI: use loop1s

SysModWeb: use loop1s

UserModInstances: use loop10s
  • Loading branch information
ewowi committed Oct 30, 2023
1 parent 92a8296 commit 602369d
Show file tree
Hide file tree
Showing 24 changed files with 1,277 additions and 1,263 deletions.
115 changes: 69 additions & 46 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ function generateHTML(parentNode, json, rowNr = -1) {
let labelNode = cE("label");
labelNode.innerText = initCap(json.id);

let isPartOfTable = (rowNr != -1);

if (json.type == "module") {
ndivNeeded = false;
newNode = cE("div");
Expand Down Expand Up @@ -170,45 +172,67 @@ function generateHTML(parentNode, json, rowNr = -1) {
}
else { //primitive types

//table header
//table header //no newNode created
if (parentNode.nodeName.toLocaleLowerCase() == "table") { //table add the id in the header
let tdNode = cE("th");
tdNode.id = json.id;
tdNode.innerText = initCap(json.id); //label uiFun response can change it
parentNode.firstChild.firstChild.appendChild(tdNode); //<thead><tr>
let thNode = cE("th");
thNode.id = json.id;
thNode.innerText = initCap(json.id); //label uiFun response can change it
parentNode.firstChild.firstChild.appendChild(thNode); //<thead><tr>
} else {

if (json.type == "select") {
if (json.ro) { //e.g. for reset/restart reason: do not show a select but only show the selected option
//if part of a table, use the saved list of options, otheriwise create select and uiFun will get the options

//newNode has no id here ...

if (!isPartOfTable) {
newNode = cE("p");
newNode.appendChild(labelNode);
let spanNode = cE("span");
spanNode.id = json.id;
if (json.value) spanNode.innerText = json.value;
newNode.appendChild(spanNode);
if (json.type != "button") newNode.appendChild(labelNode); //add label
}

let valueNode;
if (json.ro) { //e.g. for reset/restart reason: do not show a select but only show the selected option
valueNode = cE("span");
if (json.value) valueNode.innerText = json.value;
}
else {
//<p> with <label><select> (<comment> in processUpdate)
newNode = cE("p");
newNode.appendChild(labelNode);

let selectNode = cE("select");
selectNode.id = json.id;
selectNode.addEventListener('change', (event) => {console.log("select change", event);setSelect(event.target);});
valueNode = cE("select");
valueNode.addEventListener('change', (event) => {console.log("select change", event);setSelect(event.target);});

if (isPartOfTable) {
console.log("genHTML select table", parentNode, json, rowNr, gId(json.id), gId(json.id).innerText, gId(json.id).innerHTML);
valueNode.innerHTML = gId(json.id).innerHTML; //gId(json.id) is the <th> where uiFun assigned to option values to
// let index = 0;
// // for (var value of json.select) {
// for (var i=0; i<20;i++) {
// let optNode = cE("option");
// optNode.value = index;
// optNode.text = i;
// valueNode.appendChild(optNode);
// index++;
// }
}
}

newNode.appendChild(selectNode);
//(default) value will be set in processUpdate
if (!isPartOfTable) {
valueNode.id = json.id;
newNode.appendChild(valueNode);
} else {
valueNode.id = json.id + "#" + rowNr;
newNode = valueNode;
}
}
else if (json.type == "canvas") {
//<p><label><span><canvas>
var pNode = cE("p");
let pNode = cE("p");
pNode.appendChild(labelNode);

let spanNode = cE("span");
spanNode.innerText= "🔍";
// spanNode.addEventListener('click', (event) => {toggleModal(newNode);});
pNode.appendChild(spanNode);
//3 lines of code to only add 🔍
let valueNode = cE("span");
valueNode.innerText= "🔍";
pNode.appendChild(valueNode);

parentNode.appendChild(pNode);

Expand All @@ -217,7 +241,7 @@ function generateHTML(parentNode, json, rowNr = -1) {
newNode.addEventListener('dblclick', (event) => {toggleModal(event.target);});
}
else if (json.type == "textarea") {
pNode = cE("p");
let pNode = cE("p");
pNode.appendChild(labelNode);
parentNode.appendChild(pNode);

Expand All @@ -232,36 +256,32 @@ function generateHTML(parentNode, json, rowNr = -1) {
}
else if (json.type == "url") {
//tbd: th and table row outside this if
if (rowNr == -1) {
if (!isPartOfTable) {
newNode = cE("p");
newNode.appendChild(labelNode);
//add label
if (json.type != "button") newNode.appendChild(labelNode);
if (json.type != "button") newNode.appendChild(labelNode); //add label
}

let valueNode = cE("a");
valueNode.setAttribute('href', json.value);
// valueNode.setAttribute('target', "_blank"); //does not work well on mobile
valueNode.innerText = json.value;

if (rowNr == -1) {
if (!isPartOfTable) {
valueNode.id = json.id;
newNode.appendChild(valueNode);
} else {
valueNode.id = json.id + "#" + rowNr;
newNode = valueNode;
}
} else { //input
if (rowNr == -1) {
if (!isPartOfTable) {
newNode = cE("p");
newNode.appendChild(labelNode);
//add label
if (json.type != "button") newNode.appendChild(labelNode);
}
if (json.type != "button") newNode.appendChild(labelNode); //add label
}

let rangeValueNode = null;
let buttonSaveNode = null;
let buttonCancelNode = null;
// let buttonSaveNode = null;
// let buttonCancelNode = null;

let valueNode;

Expand All @@ -287,7 +307,9 @@ function generateHTML(parentNode, json, rowNr = -1) {
if (json.value) valueNode.value = json.value;
//numerical ui value changes while draging the slider (oninput)
valueNode.addEventListener('input', (event) => {
gId(json.id + "_rv").innerText = json.log?linearToLogarithm(json, event.target.value):event.target.value;
if (gId(json.id + "_rv")) {
gId(json.id + "_rv").innerText = json.log?linearToLogarithm(json, event.target.value):event.target.value;
}
});
//server value changes after draging the slider (onchange)
valueNode.addEventListener('change', (event) => {
Expand Down Expand Up @@ -327,17 +349,17 @@ function generateHTML(parentNode, json, rowNr = -1) {
}
} //not checkbox or button or range

if (rowNr == -1) {
if (!isPartOfTable) {
valueNode.id = json.id;
newNode.appendChild(valueNode);
} else {
valueNode.id = json.id + "#" + rowNr;
newNode = valueNode;
}

if (rangeValueNode) newNode.appendChild(rangeValueNode);
if (buttonSaveNode) newNode.appendChild(buttonSaveNode);
if (buttonCancelNode) newNode.appendChild(buttonCancelNode);
if (rangeValueNode) newNode.appendChild(rangeValueNode); //_rv value of range / sliders
// if (buttonSaveNode) newNode.appendChild(buttonSaveNode);
// if (buttonCancelNode) newNode.appendChild(buttonCancelNode);
} //input type

//disable drag of parent screenBox
Expand All @@ -349,7 +371,7 @@ function generateHTML(parentNode, json, rowNr = -1) {
if (newNode) parentNode.appendChild(newNode); //add new node to parent

//don't call uiFun on rowNrs (for the moment)
if (rowNr == -1) {
if (!isPartOfTable) {
//call ui Functionality, if defined (to set label, comment, select etc)
if (json.uiFun >= 0) { //>=0 as element in var
uiFunCommands.push(json.id);
Expand All @@ -358,7 +380,7 @@ function generateHTML(parentNode, json, rowNr = -1) {
}
}

if (json.n) {
if (json.n) { //multple details
//add a div with _n extension and details have this as parent
if (ndivNeeded) {
let divNode = cE("div");
Expand Down Expand Up @@ -506,15 +528,16 @@ function processVarNode(node, key, json) {

//call generateHTML to create the variable in the UI
// console.log("table cell generateHTML", tdNode, variable, variable.n, colNr, rowNr);
let newNode = generateHTML(tdNode, variable.n[colNr], rowNr); //no <p><label>
let varx = variable.n[colNr];
let newNode = generateHTML(tdNode, varx, rowNr); //no <p><label>
if (newNode) {
//very strange: gId(newNode.id) is not working here. Delay before it is in the dom??? (workaround create processVarNode function)
let updateJson;
if (variable.n[colNr].type == "checkbox" || variable.n[colNr].type == "number")
if (varx.type == "checkbox" || varx.type == "number" || varx.type == "select")
updateJson = `{"value":${columnRow}}`;
else
updateJson = `{"value":"${columnRow}"}`
// console.log("tablecolumn", rowNr, colNr, newNode, variable.n[colNr], updateJson, JSON.parse(updateJson), gId(newNode.id));
// console.log("tablecolumn", rowNr, colNr, newNode, varx, updateJson, JSON.parse(updateJson), gId(newNode.id));
//call processVarNode to give the variable a value
processVarNode(newNode, newNode.id, JSON.parse(updateJson));
}
Expand Down
18 changes: 9 additions & 9 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ inline uint16_t getRGBWsize(uint16_t nleds){
class AppModLeds:public Module {

public:
unsigned long frameMillis = 0;
unsigned long frameCounter = 0;
bool newFrame = false; //for other modules (DDP)

uint16_t fps = 60;
Expand Down Expand Up @@ -237,13 +235,6 @@ class AppModLeds:public Module {
newFrame = false;
}

//update ui
if (millis() - secondMillis >= 1000) {
secondMillis = millis();
mdl->setValueLossy("realFps", "%lu /s", frameCounter);
frameCounter = 0;
}

//update projection
if (millis() - lastMappingMillis >= 1000 && doMap) { //not more then once per second (for E131)
lastMappingMillis = millis();
Expand Down Expand Up @@ -328,6 +319,15 @@ class AppModLeds:public Module {
}
} //loop

void loop1s() {
mdl->setValueLossy("realFps", "%lu /s", frameCounter);
frameCounter = 0;
}

private:
unsigned long frameMillis = 0;
unsigned long frameCounter = 0;

};

static AppModLeds *lds;
3 changes: 2 additions & 1 deletion src/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Module {
const char * name;
bool success;
bool isEnabled;
unsigned long secondMillis = 0; // Feels like it should be private, bit doesn't compile if set as such

JsonObject parentVar;

Expand All @@ -32,6 +31,8 @@ class Module {
virtual void setup() {}

virtual void loop() {}
virtual void loop1s() {}
virtual void loop10s() {}

virtual void connectedChanged() {onOffChanged();}
virtual void enabledChanged() {onOffChanged();}
Expand Down
8 changes: 3 additions & 5 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

// #include <FS.h>

bool SysModFiles::filesChanged = false;

SysModFiles::SysModFiles() :Module("Files") {
USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

Expand All @@ -35,7 +33,7 @@ void SysModFiles::setup() {
Module::setup();
parentVar = ui->initModule(parentVar, name);

JsonObject tableVar = ui->initTable(parentVar, "fileTbl", nullptr, false, [](JsonObject var) { //uiFun
JsonObject tableVar = ui->initTable(parentVar, "fileTbl", nullptr, false, [this](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Files");
web->addResponse(var["id"], "comment", "List of files");
JsonArray rows = web->addResponseA(var["id"], "table");
Expand Down Expand Up @@ -69,9 +67,9 @@ void SysModFiles::setup() {

ui->initButton(parentVar, "deleteFiles", nullptr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "All but model.json");
}, [](JsonObject var) {
}, [this](JsonObject var) {
USER_PRINTF("delete files\n");
files->removeFiles("model.json", true); //all but model.json
removeFiles("model.json", true); //all but model.json
});

// ui->initURL(parentVar, "urlTest", "file/3DCube202005.json", true);
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SysModFiles:public Module {
void filesChange();

//get the file names and size in an array
static void dirToJson(JsonArray array, bool nameOnly = false, const char * filter = nullptr);
void dirToJson(JsonArray array, bool nameOnly = false, const char * filter = nullptr);

//get back the name of a file based on the sequence
bool seqNrToName(char * fileName, size_t seqNr);
Expand All @@ -50,7 +50,7 @@ class SysModFiles:public Module {
bool readFile(const char * path);

private:
static bool filesChanged;
bool filesChanged = false;

};

Expand Down
14 changes: 5 additions & 9 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "SysModFiles.h"
#include "SysJsonRDWS.h"

bool SysModModel::doWriteModel = false;
bool SysModModel::doShowObsolete = false;
DynamicJsonDocument * SysModModel::model = nullptr;

SysModModel::SysModModel() :Module("Model") {
Expand Down Expand Up @@ -51,13 +49,13 @@ void SysModModel::setup() {

ui->initButton(parentVar, "saveModel", nullptr, false, [](JsonObject var) {
web->addResponse(var["id"], "comment", "Write to model.json (manual save only currently)");
}, [](JsonObject var) {
}, [this](JsonObject var) {
doWriteModel = true;
});

ui->initCheckBox(parentVar, "showObsolete", false, false, [](JsonObject var) {
web->addResponse(var["id"], "comment", "Show in UI (refresh)");
}, [](JsonObject var) {
}, [this](JsonObject var) {
doShowObsolete = var["value"];
});

Expand Down Expand Up @@ -99,18 +97,16 @@ void SysModModel::setup() {
doWriteModel = false;
}

if (millis() - secondMillis >= 1000) {
secondMillis = millis();
setValueLossy("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
}

if (model->memoryUsage() / model->capacity() > 0.95) {
print->printJDocInfo("model", *model);
size_t memBefore = model->memoryUsage();
model->garbageCollect();
print->printJDocInfo("garbageCollect", *model);
}
}
void SysModModel::loop1s() {
setValueLossy("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
}

void SysModModel::cleanUpModel(JsonArray vars) {
for (JsonArray::iterator varV=vars.begin(); varV!=vars.end(); ++varV) {
Expand Down
Loading

0 comments on commit 602369d

Please sign in to comment.