Skip to content

Commit

Permalink
Use Variable in varEvents function
Browse files Browse the repository at this point in the history
SysModModel
- add EventArguments
- rename funType to eventType
- rename VarFun to VarEvent
- rename varFunctions to varEvents
- move setLabel, setCommment, setOptions, getOptions, clearOptions, findOptionsText and findOptionsTextRec to Variable class
- add Variable::value()
- ui->setComment -> variable.setComment
- ui->setOptions -> variable.setOptions
- var["value"] -> variable.value()

SysModUI
- initVariable: use EventArguments, use Variable instead of JsonObject
  • Loading branch information
ewowi committed Nov 7, 2024
1 parent 64bcfb5 commit b492f6e
Show file tree
Hide file tree
Showing 17 changed files with 447 additions and 422 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ lib_deps =
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24110513 ; Date and time (GMT!), update at every commit!!
-D VERSION=24110711 ; Date and time (GMT!), update at every commit!!
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
-mtext-section-literals ;otherwise [UserModLive::setup()]+0xa17): dangerous relocation: l32r: literal target out of range (try using text-section-literals)
Expand Down
10 changes: 5 additions & 5 deletions src/App/AppModDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AppModDemo: public SysModule {

parentVar = ui->initAppMod(parentVar, name, 1100);

JsonObject currentVar = ui->initCheckBox(parentVar, "on", true, false, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
JsonObject currentVar = ui->initCheckBox(parentVar, "on", true, false, [](EventArguments) { switch (eventType) { //varFun
case onChange:
//implement on/off behaviour
return true;
Expand All @@ -34,7 +34,7 @@ class AppModDemo: public SysModule {
currentVar["dash"] = true;

//logarithmic slider (10)
currentVar = ui->initSlider(parentVar, "brightness", 10, 0, 255, false, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
currentVar = ui->initSlider(parentVar, "brightness", 10, 0, 255, false, [](EventArguments) { switch (eventType) { //varFun
case onChange: {
return true; }
default: return false;
Expand All @@ -44,13 +44,13 @@ class AppModDemo: public SysModule {

ui->initText(parentVar, "textField", "text");

ui->initPin(parentVar, "blinkPin", &blinkPin, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initPin(parentVar, "blinkPin", &blinkPin, false, [this](EventArguments) { switch (eventType) { //varFun
case onUI:
ui->setComment(var, "🚧 tbd: reserved and allocated pins");
variable.setComment("🚧 tbd: reserved and allocated pins");
return true;
case onChange: {
//deallocate old value...
uint8_t oldValue = var["oldValue"];
uint8_t oldValue = variable.var["oldValue"];
ppf("blinkPin onChange %d %d\n", oldValue, blinkPin);
if (oldValue != UINT8_MAX)
pinsM->deallocatePin(oldValue, "Blink");
Expand Down
16 changes: 8 additions & 8 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ void SysModFiles::setup() {
SysModule::setup();
parentVar = ui->initSysMod(parentVar, name, 2101);

JsonObject tableVar = ui->initTable(parentVar, "files", nullptr, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
JsonObject tableVar = ui->initTable(parentVar, "files", nullptr, false, [this](EventArguments) { switch (eventType) {
case onUI:
ui->setComment(var, "List of files");
variable.setComment("List of files");
return true;
case onDelete:
if (rowNr != UINT8_MAX && rowNr < fileNames.size()) {
const char * fileName = fileNames[rowNr].s;
// ppf("files onDelete %s[%d] = %s %s\n", Variable(var).id(), rowNr, Variable(var).valueString().c_str(), fileName);
// ppf("files onDelete %s[%d] = %s %s\n", variable.id(), rowNr, variable.valueString().c_str(), fileName);
this->removeFiles(fileName, false);

#ifdef STARBASE_USERMOD_LIVE
Expand All @@ -58,12 +58,12 @@ void SysModFiles::setup() {

ui->initNumber(tableVar, "size", &fileSizes, 0, UINT16_MAX, true);

// ui->initURL(tableVar, "flLink", nullptr, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
// ui->initURL(tableVar, "flLink", nullptr, true, [this](EventArguments) { switch (eventType) {
// case onSetValue:
// for (size_t rowNr = 0; rowNr < fileList.size(); rowNr++) {
// char urlString[32] = "file/";
// strlcat(urlString, fileList[rowNr].name, sizeof(urlString));
// mdl->setValue(var, JsonString(urlString, JsonString::Copied), rowNr);
// mdl->setValue(variable.var, JsonString(urlString, JsonString::Copied), rowNr);
// }
// return true;
// default: return false;
Expand All @@ -76,10 +76,10 @@ void SysModFiles::setup() {

ui->initFileUpload(parentVar, "upload");//, nullptr, UINT16_MAX, false);

ui->initProgress(parentVar, "totalSize", 0, 0, files->totalBytes(), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initProgress(parentVar, "totalSize", 0, 0, files->totalBytes(), true, [](EventArguments) { switch (eventType) {
case onChange:
var["max"] = files->totalBytes(); //makes sense?
web->addResponse(var, "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
variable.var["max"] = files->totalBytes(); //makes sense?
web->addResponse(variable.var, "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
return true;
default: return false;
}});
Expand Down
66 changes: 33 additions & 33 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,99 +116,99 @@ class SysModInstances:public SysModule {

JsonObject tableVar = ui->initTable(parentVar, "instances", nullptr, true);

ui->initText(tableVar, "name", nullptr, 32, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initText(tableVar, "name", nullptr, 32, false, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, JsonString(instances[rowNrL].name, JsonString::Copied), rowNrL);
mdl->setValue(variable.var, JsonString(instances[rowNrL].name, JsonString::Copied), rowNrL);
return true;
// comment this out for the time being as causes corrupted instance names
// case onChange:
// strlcpy(instances[rowNr].name, mdl->getValue(var, rowNr), sizeof(instances[rowNr].name));
// sendMessageUDP(instances[rowNr].ip, "name", mdl->getValue(var, rowNr));
// strlcpy(instances[rowNr].name, mdl->getValue(variable.var, rowNr), sizeof(instances[rowNr].name));
// sendMessageUDP(instances[rowNr].ip, "name", mdl->getValue(variable.var, rowNr));
// return true;
default: return false;
}});

ui->initURL(tableVar, "show", nullptr, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initURL(tableVar, "show", nullptr, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++) {
char urlString[32] = "http://";
strlcat(urlString, instances[rowNrL].ip.toString().c_str(), sizeof(urlString));
mdl->setValue(var, JsonString(urlString, JsonString::Copied), rowNrL);
mdl->setValue(variable.var, JsonString(urlString, JsonString::Copied), rowNrL);
}
return true;
default: return false;
}});

ui->initNumber(tableVar, "link", UINT16_MAX, 0, UINT16_MAX, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "link", UINT16_MAX, 0, UINT16_MAX, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, calcGroup(instances[rowNrL].name), rowNrL);
mdl->setValue(variable.var, calcGroup(instances[rowNrL].name), rowNrL);
return true;
default: return false;
}});

ui->initText(tableVar, "IP", nullptr, 16, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initText(tableVar, "IP", nullptr, 16, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, JsonString(instances[rowNrL].ip.toString().c_str(), JsonString::Copied), rowNrL);
mdl->setValue(variable.var, JsonString(instances[rowNrL].ip.toString().c_str(), JsonString::Copied), rowNrL);
return true;
default: return false;
}});

ui->initText(tableVar, "type", nullptr, 16, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initText(tableVar, "type", nullptr, 16, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++) {
byte type = instances[rowNrL].sysData.type;
mdl->setValue(var, (type==0)?"WLED":(type==1)?"StarBase":(type==2)?"StarLight":(type==3)?"StarLedsLive":"StarFork", rowNrL);
mdl->setValue(variable.var, (type==0)?"WLED":(type==1)?"StarBase":(type==2)?"StarLight":(type==3)?"StarLedsLive":"StarFork", rowNrL);
}
return true;
default: return false;
}});

ui->initNumber(tableVar, "version", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "version", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].version, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].version, rowNrL);
return true;
default: return false;
}});

ui->initNumber(tableVar, "uptime", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "uptime", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].sysData.uptime, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].sysData.uptime, rowNrL);
return true;
default: return false;
}});
ui->initNumber(tableVar, "now", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "now", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].sysData.now / 1000, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].sysData.now / 1000, rowNrL);
return true;
default: return false;
}});

ui->initNumber(tableVar, "timestamp", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "timestamp", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].sysData.timeSource, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].sysData.timeSource, rowNrL);
return true;
default: return false;
}});

ui->initNumber(tableVar, "time", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "time", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].sysData.tokiTime, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].sysData.tokiTime, rowNrL);
return true;
default: return false;
}});

ui->initNumber(tableVar, "ms", UINT16_MAX, 0, (unsigned long)-1, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initNumber(tableVar, "ms", UINT16_MAX, 0, (unsigned long)-1, true, [this](EventArguments) { switch (eventType) {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++)
mdl->setValue(var, instances[rowNrL].sysData.tokiMs, rowNrL);
mdl->setValue(variable.var, instances[rowNrL].sysData.tokiMs, rowNrL);
return true;
default: return false;
}});
Expand All @@ -225,32 +225,32 @@ class SysModInstances:public SysModule {
JsonObject insVar; // = ui->cloneVar(var, columnVarID, [this, var](JsonObject insVar){});

//create a var of the same type. InitVar is not calling onChange which is good in this situation!
insVar = ui->initVar(tableVar, columnVarID, var["type"], false, [this, var](JsonObject insVar, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
insVar = ui->initVar(tableVar, columnVarID, var["type"], false, [this, var](Variable insVariable, uint8_t rowNr, uint8_t eventType) { switch (eventType) { //varEvent
case onSetValue:
//should not trigger onChange
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++) {
// ppf("initVar dash %s[%d]\n", Variable(insVar).id(), rowNrL);
// ppf("initVar dash %s[%d]\n", variable.id(), rowNrL);
//do what setValue is doing except calling onChange
// insVar["value"][rowNrL] = instances[rowNrL].jsonData[Variable(var).id()]; //only int values...
// insVar["value"][rowNrL] = instances[rowNrL].jsonData[variable.id()]; //only int values...

web->addResponse(insVar, "value", instances[rowNrL].jsonData[Variable(var).id()], rowNrL);
web->addResponse(insVariable.var, "value", instances[rowNrL].jsonData[Variable(var).id()], rowNrL);

// mdl->setValue(insVar, instances[rowNrL].jsonData[Variable(var).id()], rowNr);
// mdl->setValue(insVariable.var, instances[rowNrL].jsonData[Variable(var).id()], rowNr);
//send to ws?
}
return true;
case onUI:
// call onUI of the base variable for the new variable
mdl->varFunctions[var["fun"]](insVar, rowNr, onUI);
mdl->varEvents[var["fun"]](insVariable, rowNr, onUI);
return true;
case onChange: {
//do not set this initially!!!
if (rowNr != UINT8_MAX) {
//if this instance update directly, otherwise send over network
if (instances[rowNr].ip == net->localIP()) {
mdl->setValue(var, mdl->getValue(insVar, rowNr).as<uint8_t>()); //this will call sendDataWS (tbd...), do not set for rowNr
mdl->setValue(var, mdl->getValue(insVariable.var, rowNr).as<uint8_t>()); //this will call sendDataWS (tbd...), do not set for rowNr
} else {
sendMessageUDP(instances[rowNr].ip, var, mdl->getValue(insVar, rowNr));
sendMessageUDP(instances[rowNr].ip, var, mdl->getValue(insVariable.var, rowNr));
}
}
// print->printJson(" ", var);
Expand Down Expand Up @@ -603,7 +603,7 @@ class SysModInstances:public SysModule {
instance.jsonData.to<JsonObject>(); //clear

//send dash values
mdl->findVars("dash", true, [&instance](JsonObject var) { //varFun
mdl->findVars("dash", true, [&instance](JsonObject var) { //varEvent
instance.jsonData[Variable(var).id()] = var["value"];
// // print->printJson("setVar", var);
// JsonArray valArray = Variable(var).valArray();
Expand Down
Loading

0 comments on commit b492f6e

Please sign in to comment.