Skip to content

Commit

Permalink
set/getValue to Variable. ESPLiveScript 1.2.0
Browse files Browse the repository at this point in the history
pio.ini
- add STARBASE_USERMOD_MIDI (not active yet)
- update LiveScript t0 main/1.2.0
- rename esp32_wrover to esp-wrover-kit

main.cpp
- add STARBASE_USERMOD_MIDI (not active yet)

SysModModel
- setValueJV, setValueF, getValue, setValue -> Variable
- mdl->setValue(variable.var, -> variable.setValue(
- mdl->getValue(variable.var, -> variable.getValue(
  • Loading branch information
ewowi committed Nov 8, 2024
1 parent b492f6e commit 14b8b74
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 174 deletions.
15 changes: 12 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ build_flags =
lib_deps =
ElectronicCats/MPU6050 @ 1.3.0

[STARBASE_USERMOD_MIDI]
build_flags =
-D STARBASE_USERMOD_MIDI
lib_deps =
; https://github.com/marcel-licence/esp32_usb_midi
; https://github.com/felis/USB_Host_Shield_2.0

;asmParser © https://github.com/hpwit/ASMParser
[STARBASE_USERMOD_LIVE]
build_flags =
-D STARBASE_USERMOD_LIVE
-D EXTPRINTF=ppf
lib_deps =
; https://github.com/hpwit/ESPLiveScript.git#17b4bb7 ;v2.9.3 @ 20241030 15:39
https://github.com/ewowi/ESPLiveScript.git#v2.9.3 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates
https://github.com/ewowi/ESPLiveScript.git#main ;1.2.0. ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates

[STARBASE]
build_flags =
Expand All @@ -88,6 +95,7 @@ build_flags =
-D STARBASE_ETHERNET ; +41.876 bytes (2.2%)
${STARBASE_USERMOD_E131.build_flags} ;+11.416 bytes 0.6%
${STARBASE_USERMOD_MPU6050.build_flags} ;+35.308 bytes 1.8%
; ${STARBASE_USERMOD_MIDI.build_flags} ;+5%...
; ${STARBASE_USERMOD_HA.build_flags}
${STARBASE_USERMOD_LIVE.build_flags} ;+222.204 bytes 11.7%
lib_deps =
Expand All @@ -97,6 +105,7 @@ lib_deps =
;optional:
${STARBASE_USERMOD_E131.lib_deps}
${STARBASE_USERMOD_MPU6050.lib_deps}
; ${STARBASE_USERMOD_MIDI.lib_deps}
; ${STARBASE_USERMOD_HA.lib_deps}
${STARBASE_USERMOD_LIVE.lib_deps}

Expand Down Expand Up @@ -148,7 +157,7 @@ lib_deps =
; https://github.com/platformio/platform-espressif32/issues/1360
; https://community.platformio.org/t/support-esp32-wrover-module/17717
; note: flasghing to new board goes wrong, try first without ICVD then with and without etc until it works (witchcraft)
[env:esp32_wrover]
[env:esp-wrover-kit]
board = esp-wrover-kit ; esp-wrover-kit ;https://github.com/platformio/platform-espressif32/blob/develop/boards/esp-wrover-kit.json
; recommended to pin to a platform version, see https://github.com/platformio/platform-espressif32/releases
platform = [email protected] ;using platformio/framework-arduinoespressif32 @ ~3.20014.0 / framework-arduinoespressif32 @ 3.20014.231204 (2.0.14)
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void SysModFiles::setup() {
// for (size_t rowNr = 0; rowNr < fileList.size(); rowNr++) {
// char urlString[32] = "file/";
// strlcat(urlString, fileList[rowNr].name, sizeof(urlString));
// mdl->setValue(variable.var, JsonString(urlString, JsonString::Copied), rowNr);
// variable.setValue(JsonString(urlString, JsonString::Copied), rowNr);
// }
// return true;
// default: return false;
Expand Down
34 changes: 17 additions & 17 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class SysModInstances:public SysModule {
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(variable.var, JsonString(instances[rowNrL].name, JsonString::Copied), rowNrL);
variable.setValue(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(variable.var, rowNr), sizeof(instances[rowNr].name));
// sendMessageUDP(instances[rowNr].ip, "name", mdl->getValue(variable.var, rowNr));
// strlcpy(instances[rowNr].name, variable.getValue(rowNr), sizeof(instances[rowNr].name));
// sendMessageUDP(instances[rowNr].ip, "name", variable.getValue(rowNr));
// return true;
default: return false;
}});
Expand All @@ -134,7 +134,7 @@ class SysModInstances:public SysModule {
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(variable.var, JsonString(urlString, JsonString::Copied), rowNrL);
variable.setValue(JsonString(urlString, JsonString::Copied), rowNrL);
}
return true;
default: return false;
Expand All @@ -143,15 +143,15 @@ class SysModInstances:public SysModule {
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(variable.var, calcGroup(instances[rowNrL].name), rowNrL);
variable.setValue(calcGroup(instances[rowNrL].name), rowNrL);
return true;
default: return false;
}});

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(variable.var, JsonString(instances[rowNrL].ip.toString().c_str(), JsonString::Copied), rowNrL);
variable.setValue(JsonString(instances[rowNrL].ip.toString().c_str(), JsonString::Copied), rowNrL);
return true;
default: return false;
}});
Expand All @@ -160,7 +160,7 @@ class SysModInstances:public SysModule {
case onSetValue:
for (size_t rowNrL = 0; rowNrL < instances.size() && (rowNr == UINT8_MAX || rowNrL == rowNr); rowNrL++) {
byte type = instances[rowNrL].sysData.type;
mdl->setValue(variable.var, (type==0)?"WLED":(type==1)?"StarBase":(type==2)?"StarLight":(type==3)?"StarLedsLive":"StarFork", rowNrL);
variable.setValue((type==0)?"WLED":(type==1)?"StarBase":(type==2)?"StarLight":(type==3)?"StarLedsLive":"StarFork", rowNrL);
}
return true;
default: return false;
Expand All @@ -169,46 +169,46 @@ class SysModInstances:public SysModule {
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(variable.var, instances[rowNrL].version, rowNrL);
variable.setValue(instances[rowNrL].version, rowNrL);
return true;
default: return false;
}});

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(variable.var, instances[rowNrL].sysData.uptime, rowNrL);
variable.setValue(instances[rowNrL].sysData.uptime, rowNrL);
return true;
default: return false;
}});
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(variable.var, instances[rowNrL].sysData.now / 1000, rowNrL);
variable.setValue(instances[rowNrL].sysData.now / 1000, rowNrL);
return true;
default: return false;
}});

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(variable.var, instances[rowNrL].sysData.timeSource, rowNrL);
variable.setValue(instances[rowNrL].sysData.timeSource, rowNrL);
return true;
default: return false;
}});

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(variable.var, instances[rowNrL].sysData.tokiTime, rowNrL);
variable.setValue(instances[rowNrL].sysData.tokiTime, rowNrL);
return true;
default: return false;
}});

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(variable.var, instances[rowNrL].sysData.tokiMs, rowNrL);
variable.setValue(instances[rowNrL].sysData.tokiMs, rowNrL);
return true;
default: return false;
}});
Expand Down Expand Up @@ -248,9 +248,9 @@ class SysModInstances:public SysModule {
if (rowNr != UINT8_MAX) {
//if this instance update directly, otherwise send over network
if (instances[rowNr].ip == net->localIP()) {
mdl->setValue(var, mdl->getValue(insVariable.var, rowNr).as<uint8_t>()); //this will call sendDataWS (tbd...), do not set for rowNr
Variable(var).setValue(insVariable.getValue(rowNr).as<uint8_t>()); //this will call sendDataWS (tbd...), do not set for rowNr
} else {
sendMessageUDP(instances[rowNr].ip, var, mdl->getValue(insVariable.var, rowNr));
sendMessageUDP(instances[rowNr].ip, var, insVariable.getValue(rowNr));
}
}
// print->printJson(" ", var);
Expand Down Expand Up @@ -499,7 +499,7 @@ class SysModInstances:public SysModule {
if (!message["id"].isNull() && !message["value"].isNull()) {
ppf("handleNotifications i:%d json message %.*s l:%d\n", instanceUDP.remoteIP()[3], packetSize, buffer, packetSize);

mdl->setValueJV(mdl->findVar(message["pid"].as<const char *>(), message["id"].as<const char *>()), message["value"]);
Variable(mdl->findVar(message["pid"].as<const char *>(), message["id"].as<const char *>())).setValueJV(message["value"]);
}
}
}
Expand Down Expand Up @@ -774,7 +774,7 @@ class SysModInstances:public SysModule {
id = strtok(NULL, "."); //the rest after .
}

mdl->setValueJV(mdl->findVar(pid, id), pair.value());
Variable(mdl->findVar(pid, id)).setValueJV(pair.value());
}
instance.jsonData = newData; // deepcopy: https://github.com/bblanchon/ArduinoJson/issues/1023
// ppf("updateInstance json ip:%d", instance.ip[3]);
Expand Down
48 changes: 48 additions & 0 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,54 @@
return false;
}

JsonObject Variable::setValueJV(JsonVariant value, uint8_t rowNr) {
if (value.is<JsonArray>()) {
uint8_t rowNr = 0;
// ppf(" %s is Array\n", value.as<String>().c_str);
JsonObject var;
for (JsonVariant el: value.as<JsonArray>()) {
var = setValueJV(el, rowNr++);
}
return var;
}
else if (value.is<const char *>())
return setValue(JsonString(value, JsonString::Copied), rowNr);
else if (value.is<Coord3D>()) //otherwise it will be treated as JsonObject and toJson / fromJson will not be triggered!!!
return setValue(value.as<Coord3D>(), rowNr);
else
return setValue(value, rowNr);
}

//Set value with argument list
JsonObject Variable::setValueF(const char * format, ...) {
va_list args;
va_start(args, format);

char value[128];
vsnprintf(value, sizeof(value)-1, format, args);

va_end(args);

return setValue(JsonString(value, JsonString::Copied));
}

JsonVariant Variable::getValue(uint8_t rowNr) {
if (var["value"].is<JsonArray>()) {
JsonArray valueArray = valArray();
if (rowNr == UINT8_MAX) rowNr = mdl->getValueRowNr;
if (rowNr != UINT8_MAX && rowNr < valueArray.size())
return valueArray[rowNr];
else if (valueArray.size())
return valueArray[0]; //return the first element
else {
ppf("dev getValue no array or rownr wrong %s.%s %s %d\n", pid(), id(), valueString().c_str(), rowNr);
return JsonVariant(); // return null
}
}
else
return var["value"];
}

SysModModel::SysModModel() :SysModule("Model") {
model = new JsonDocument(&allocator);

Expand Down
Loading

0 comments on commit 14b8b74

Please sign in to comment.