Skip to content

Commit

Permalink
Table column changes generic: del files, enable mods, stage changes work
Browse files Browse the repository at this point in the history
chFun:  add rowNr parameter for table columns

SysModFiles:
- delete file using chFun.rowNr

SysModModel
- remove setValueArray (use chFun.rowNr instead)
- setValueX call setChFunAndWs with rowNr

SysModUI
- split ucFunctions in uFunctions and cFunctions.
- split UCFun in UFun and CFun
- CFun add rowNr parameter
- setChFunAndWs: add rowNr

SysModules:
- mdlEnabled: use chFun.rowNr

UserModInstances:
- stage vars: use chFun.rowNr instead of setValueArray
  • Loading branch information
ewowi committed Nov 11, 2023
1 parent edddda7 commit 5e89fe6
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 134 deletions.
4 changes: 2 additions & 2 deletions src/App/AppModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ class AppModFixtureGen:public SysModule {
select.add("3DCube"); //9
select.add("3DGlobe WIP"); //10
select.add("3DGeodesicDome WIP"); //11
}, [this](JsonObject var) { //chFun
}, [this](JsonObject var, uint8_t) { //chFun
fixtureGenChFun(var);
}); //fixtureGen

Expand All @@ -533,7 +533,7 @@ class AppModFixtureGen:public SysModule {
});

ui->initButton(parentVar, "generate", nullptr, false, [](JsonObject var) { //uiFun
}, [this](JsonObject var) {
}, [this](JsonObject var, uint8_t) { //chFun
generateChFun(var);
});

Expand Down
14 changes: 7 additions & 7 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class AppModLeds:public SysModule {

currentVar = ui->initCheckBox(parentVar, "on", true, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "On/Off");
}, [](JsonObject var) { //chFun
}, [](JsonObject var, uint8_t) { //chFun
ui->valChangedForInstancesTemp = true;
});
currentVar["stage"] = true;

//logarithmic slider (10)
currentVar = ui->initSlider(parentVar, "bri", 10, 0, 255, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Brightness");
}, [](JsonObject var) { //chFun
}, [](JsonObject var, uint8_t) { //chFun
uint8_t bri = var["value"];

uint8_t result = linearToLogarithm(var, bri);
Expand Down Expand Up @@ -98,7 +98,7 @@ class AppModLeds:public SysModule {
for (Effect *effect:effects.effects) {
select.add(effect->name());
}
}, [this](JsonObject var) { //chFun
}, [this](JsonObject var, uint8_t) { //chFun
uint8_t fx = var["value"];
USER_PRINTF("%s Change %s to %d\n", "initSelect chFun", var["id"].as<const char *>(), fx);

Expand All @@ -118,7 +118,7 @@ class AppModLeds:public SysModule {
select.add("RainbowStripeColors");
select.add("PartyColors");
select.add("HeatColors");
}, [](JsonObject var) { //chFun
}, [](JsonObject var, uint8_t) { //chFun
USER_PRINTF("%s Change %s to %d\n", "initSelect chFun", var["id"].as<const char *>(), var["value"].as<int>());
switch (var["value"].as<uint8_t>()) {
case 0: palette = CloudColors_p; break;
Expand Down Expand Up @@ -148,7 +148,7 @@ class AppModLeds:public SysModule {
select.add("Multiply"); //6
select.add("Kaleidoscope"); //7
select.add("Fun"); //8
}, [this](JsonObject var) { //chFun
}, [this](JsonObject var, uint8_t) { //chFun
USER_PRINTF("%s Change %s to %d\n", "initSelect chFun", var["id"].as<const char *>(), var["value"].as<int>());

ledsV.projectionNr = var["value"];
Expand All @@ -167,7 +167,7 @@ class AppModLeds:public SysModule {
if (files->seqNrToName(fileName, var["value"])) {
web->addResponse("pview", "file", fileName);
}
}, [this](JsonObject var) { //chFun
}, [this](JsonObject var, uint8_t) { //chFun
USER_PRINTF("%s Change %s to %d\n", "initSelect chFun", var["id"].as<const char *>(), var["value"].as<int>());

ledsV.fixtureNr = var["value"];
Expand Down Expand Up @@ -201,7 +201,7 @@ class AppModLeds:public SysModule {

ui->initNumber(parentVar, "fps", fps, 1, 999, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Frames per second");
}, [this](JsonObject var) { //chFun
}, [this](JsonObject var, uint8_t) { //chFun
fps = var["value"];
USER_PRINTF("fps changed %d\n", fps);
});
Expand Down
24 changes: 17 additions & 7 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void SysModFiles::setup() {
web->addResponse(var["id"], "comment", "List of files");
JsonArray rows = web->addResponseA(var["id"], "table");
dirToJson(rows);

});
ui->initText(tableVar, "flName", nullptr, 32, true, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Name");
Expand All @@ -53,11 +52,22 @@ void SysModFiles::setup() {
});
ui->initButton(tableVar, "flDel", "", false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Delete"); //table header title
}, [](JsonObject var) { //chFun
print->printJson("flDel chFun", var); //not called yet for buttons...
//instead:
// processJson k:flDel r:6 (⌫ == ⌫ ? 1)
// we want an array for value but : {"id":"flDel","type":"button","ro":false,"o":23,"uiFun":25,"chFun":26,"value":"⌫"}
}, [this, tableVar](JsonObject var, uint8_t rowNr) { //chFun

USER_PRINTF("chFun %s r:%d v:%s", var["id"].as<const char *>(), rowNr, var["value"][rowNr].as<String>());
if (rowNr != uint8Max) {
// call uiFun of tbl to fill responseVariant with files
ui->uFunctions[tableVar["uiFun"]](tableVar);
JsonVariant responseVariant = web->getResponseDoc()->as<JsonVariant>();
JsonArray row = responseVariant["fileTbl"]["table"][rowNr];
const char * fileName = row[0]; //first column
print->printJson("\n", row);
removeFiles(fileName, false);
}
else {
USER_PRINTF(" no rowNr!!");
}
print->printJson(" ", var);
});

ui->initText(parentVar, "drsize", nullptr, 32, true, [](JsonObject var) { //uiFun
Expand All @@ -69,7 +79,7 @@ void SysModFiles::setup() {

ui->initButton(parentVar, "deleteFiles", nullptr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "All but model.json");
}, [this](JsonObject var) {
}, [this](JsonObject var, uint8_t) { //chFun
USER_PRINTF("delete files\n");
removeFiles("model.json", true); //all but model.json
});
Expand Down
60 changes: 15 additions & 45 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,30 @@ void SysModModel::setup() {

parentVar = ui->initModule(parentVar, name);

ui->initText(parentVar, "mSize", nullptr, 32, true, [](JsonObject var) {
ui->initText(parentVar, "mSize", nullptr, 32, true, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Size");
});

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

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

ui->initButton(parentVar, "deleteObsolete", nullptr, false, [](JsonObject var) {
ui->initButton(parentVar, "deleteObsolete", nullptr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Delete obsolete variables");
web->addResponse(var["id"], "comment", "WIP");
}, [](JsonObject var) {
});

ui->initButton(parentVar, "deleteModel", nullptr, false, [](JsonObject var) {
ui->initButton(parentVar, "deleteModel", nullptr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Back to defaults");
}, [](JsonObject var) {
}, [](JsonObject var, uint8_t) { //chFun
USER_PRINTF("delete model json\n");
files->remove("/model.json");
});
Expand Down Expand Up @@ -146,12 +145,12 @@ JsonObject SysModModel::setValueC(const char * id, const char * value, uint8_t r
if (var["value"].isNull() || var["value"] != value) {
// USER_PRINTF("setValue changed %s %s->%s\n", id, var["value"].as<String>().c_str(), value);
if (var["ro"]) { // do not update var["value"]
ui->setChFunAndWs(var, value); //value: bypass var["value"]
ui->setChFunAndWs(var, rowNr, value); //value: bypass var["value"]
//now only used for ro not lossy
USER_PRINTF("setValueC: RO non lossy %s %s\n", id, value);
} else {
var["value"] = (char *)value; //(char *) forces a copy (https://arduinojson.org/v6/api/jsonvariant/subscript/) (otherwise crash!!)
ui->setChFunAndWs(var);
ui->setChFunAndWs(var, rowNr);
}
}
} else {
Expand All @@ -171,7 +170,7 @@ JsonObject SysModModel::setValueI(const char * id, int value, uint8_t rowNr) {
if (var["value"].isNull() || var["value"] != value) {
// USER_PRINTF("setValue changed %s %s->%s\n", id, var["value"].as<String>().c_str(), value);
var["value"] = value;
ui->setChFunAndWs(var);
ui->setChFunAndWs(var, rowNr);
}
}
else {
Expand All @@ -187,7 +186,7 @@ JsonObject SysModModel::setValueI(const char * id, int value, uint8_t rowNr) {
//set the right value in the array (if array did not contain values yet, all values before rownr are set to false)
if (var["value"][rowNr] != value) {
var["value"][rowNr] = value;
ui->setChFunAndWs(var);
ui->setChFunAndWs(var, rowNr);
}
}
else
Expand All @@ -208,7 +207,7 @@ JsonObject SysModModel::setValueB(const char * id, bool value, uint8_t rowNr) {
if (var["value"].isNull() || var["value"] != value) {
USER_PRINTF("setValueB changed %s (%d) %s->%s\n", id, rowNr, var["value"].as<String>().c_str(), value?"true":"false");
var["value"] = value;
ui->setChFunAndWs(var);
ui->setChFunAndWs(var, rowNr);
}
}
else {
Expand All @@ -224,7 +223,7 @@ JsonObject SysModModel::setValueB(const char * id, bool value, uint8_t rowNr) {
//set the right value in the array (if array did not contain values yet, all values before rownr are set to false)
if (var["value"][rowNr] != value) {
var["value"][rowNr] = value;
ui->setChFunAndWs(var);
ui->setChFunAndWs(var, rowNr);
}
}
else
Expand Down Expand Up @@ -338,33 +337,4 @@ void SysModModel::findVars(const char * id, bool value, FindFun fun, JsonArray p
if (!var["n"].isNull())
findVars(id, value, fun, var["n"]);
}
}

void SysModModel::setValueArray(JsonObject var, size_t arraySize, uint8_t *array, ChangeFun fun) {
//if value not array, create array
if (!var["value"].is<JsonArray>()) //comment if forced to recreate enabled array
var.createNestedArray("value");

size_t rowNr = 0;
//if value array not same size as nr of modules
if (var["value"].size() != arraySize) {
for (int i=0; i< arraySize; i++) {
USER_PRINTF(" %s[%d].sva reset %d->%d\n", var["id"].as<const char *>(), i, array[i], var["value"][i].as<uint8_t>());
var["value"][i] = array[i];
}
} else { //read array and set value if different
for (int i=0; i< arraySize; i++) {
// for (uint8_t valueElement:var["value"].as<JsonArray>()) {
// int value = nodes[rowNr].app.getVar(var["id"]);
if (var["value"][i] != array[i]) {
USER_PRINTF(" %s[%d].sva update %d->%d\n", var["id"].as<const char *>(), i, array[i], var["value"][i].as<uint8_t>());
// nodes[rowNr]->app.setVar(var["id"], valueElement);
// modules[rowNr]->enabledChanged();
// var["value"][i] = array[i];
fun(var, i);
}
rowNr++;
}
}

}
}
2 changes: 0 additions & 2 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class SysModModel:public SysModule {
static JsonObject findVar(const char * id, JsonArray parent = JsonArray()); //static for processJson
void findVars(const char * id, bool value, FindFun fun, JsonArray parent = JsonArray());

void setValueArray(JsonObject var, size_t arraySize, uint8_t *array, ChangeFun fun);

private:
bool doWriteModel = false;
bool doShowObsolete = false;
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ void SysModNetwork::setup() {
ui->initPassword(parentVar, "pw", "", 32, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Password");
});
ui->initButton(parentVar, "connect", nullptr, false, [](JsonObject var) {
ui->initButton(parentVar, "connect", nullptr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Force reconnect (you loose current connection)");
}, [this](JsonObject var) {
}, [this](JsonObject var, uint8_t) { //chFun
forceReconnect = true;
});
ui->initText(parentVar, "nwstatus", nullptr, 32, true, [](JsonObject var) { //uiFun
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void SysModSystem::setup() {
});
ui->initText(parentVar, "stack", nullptr, 16, true);

ui->initButton(parentVar, "restart", nullptr, false, nullptr, [](JsonObject var) { //chFun
ui->initButton(parentVar, "restart", nullptr, false, nullptr, [](JsonObject var, uint8_t) { //chFun
SysModWeb::ws->closeAll(1012);
ESP.restart();
});
Expand Down
32 changes: 17 additions & 15 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include "html_ui.h"

//init static variables (https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm)
std::vector<UCFun> SysModUI::ucFunctions;
std::vector<UFun> SysModUI::uFunctions;
std::vector<CFun> SysModUI::cFunctions;
std::vector<VarLoop> SysModUI::loopFunctions;
int SysModUI::varCounter = 1; //start with 1 so it can be negative, see var["o"]
bool SysModUI::varLoopsChanged = false;;
Expand Down Expand Up @@ -81,7 +82,7 @@ void SysModUI::loop() {
SysModWeb::wsSendBytesCounter++;

//send var to notify client data coming is for var (client then knows it is canvas and expects data for it)
setChFunAndWs(varLoop->var, "new");
setChFunAndWs(varLoop->var, uint8Max, "new");

//send leds info in binary data format
//tbd: this can crash on 64*64 matrices...
Expand Down Expand Up @@ -141,7 +142,7 @@ void SysModUI::loop1s() {

}

JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * type, bool readOnly, UCFun uiFun, UCFun chFun, LoopFun loopFun) {
JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * type, bool readOnly, UFun uiFun, CFun chFun, LoopFun loopFun) {
JsonObject var = mdl->findVar(id);

//create new var
Expand Down Expand Up @@ -179,8 +180,8 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty
// if (itr!=ucFunctions.end()) //found
// var["uiFun"] = distance(ucFunctions.begin(), itr); //assign found function
// else { //not found
ucFunctions.push_back(uiFun); //add new function
var["uiFun"] = ucFunctions.size()-1;
uFunctions.push_back(uiFun); //add new function
var["uiFun"] = uFunctions.size()-1;
// }
}

Expand All @@ -191,8 +192,8 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty
// if (itr!=ucFunctions.end()) //found
// var["chFun"] = distance(ucFunctions.begin(), itr); //assign found function
// else { //not found
ucFunctions.push_back(chFun); //add new function
var["chFun"] = ucFunctions.size()-1;
cFunctions.push_back(chFun); //add new function
var["chFun"] = cFunctions.size()-1;
// }
}

Expand All @@ -217,14 +218,15 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty

//tbd: use template T for value
//run the change function and send response to all? websocket clients
void SysModUI::setChFunAndWs(JsonObject var, const char * value) { //value: bypass var["value"]
void SysModUI::setChFunAndWs(JsonObject var, uint8_t rowNr, const char * value) { //value: bypass var["value"]

if (!var["chFun"].isNull()) {//isNull needed here!
size_t funNr = var["chFun"];
if (funNr < ucFunctions.size())
ucFunctions[funNr](var);
if (funNr < cFunctions.size()) {
cFunctions[funNr](var, rowNr);
}
else
USER_PRINTF("setChFunAndWs function nr %s outside bounds %d >= %d\n", var["id"].as<const char *>(), funNr, ucFunctions.size());
USER_PRINTF("setChFunAndWs function nr %s outside bounds %d >= %d\n", var["id"].as<const char *>(), funNr, cFunctions.size());
}

JsonDocument *responseDoc = web->getResponseDoc();
Expand Down Expand Up @@ -281,10 +283,10 @@ const char * SysModUI::processJson(JsonVariant &json) {
//call ui function...
if (!var["uiFun"].isNull()) {//isnull needed here!
size_t funNr = var["uiFun"];
if (funNr < ucFunctions.size())
ucFunctions[funNr](var);
if (funNr < uFunctions.size())
uFunctions[funNr](var);
else
USER_PRINTF("processJson function nr %s outside bounds %d >= %d\n", var["id"].as<const char *>(), funNr, ucFunctions.size());
USER_PRINTF("processJson function nr %s outside bounds %d >= %d\n", var["id"].as<const char *>(), funNr, uFunctions.size());

//if select var, send value back
if (var["type"] == "select")
Expand Down Expand Up @@ -339,7 +341,7 @@ const char * SysModUI::processJson(JsonVariant &json) {
changed = var["value"] != value;

if (var["type"] == "button") //button always
setChFunAndWs(var); //setValue without assignment
setChFunAndWs(var, atoi(rowNr)); //setValue without assignment
else if (changed) {
// USER_PRINTF("processJson %s %s->%s\n", key, var["value"].as<String>().c_str(), value.as<String>().c_str());

Expand Down
Loading

0 comments on commit 5e89fe6

Please sign in to comment.