Skip to content

Commit

Permalink
A number of changes, controls bugfix, stacksize 16K (for LiveScripts)
Browse files Browse the repository at this point in the history
pio.ini
- latest ESPLiveScript

main.cpp
- SET_LOOP_TASK_STACK_SIZE 16K

SysModModel
- Variable::Value: check rowNr
- setValue: refactor
- preDetails: if oldValue (not boot): delete all details
- postDetails: rowNr check near var

UserModLive
- enabled by default
- Show stack size
  • Loading branch information
ewowi committed Dec 18, 2024
1 parent 1ed10cb commit 69b3586
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 61 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ build_flags =
-D STARBASE_USERMOD_LIVE
-D EXTPRINTF=ppf ;redirect Live Script prints to StarBase print
lib_deps =
https://github.com/ewowi/ESPLiveScript.git#c6463f6 ; v3.1 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates
https://github.com/ewowi/ESPLiveScript.git#7bcd5bf ; v3.1 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates

[STARBASE]
build_flags =
Expand Down Expand Up @@ -288,7 +288,7 @@ build_flags =
${env.build_flags}
-D CONFIG_IDF_TARGET_ESP32S3=1
-D STARBASE_LOLIN_WIFI_FIX ; shouldn't be necessary, but otherwise WiFi issues on my board
-D STARBASE_BOOT_BUTTON_PIN=0 ; boot pin on the esp32 board, check 48?
-D STARBASE_BOOT_BUTTON_PIN=0 ; boot pin on the esp32 board
lib_deps =
${env.lib_deps}

Expand Down
15 changes: 11 additions & 4 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@
}

void Variable::preDetails() {
for (JsonObject varChild: children()) { //for all controls
varChild.remove("o");
print->printJson("preDetails pre", var);
if (var["oldValue"].isNull()) //no previous effect: during boot
for (JsonObject varChild: children()) { //for all controls
varChild.remove("o");
}
else {
var["n"].to<JsonArray>(); //delete old values
}

ppf("preDetails %s.%s post ", pid(), id());
print->printVar(var);
ppf("\n");
Expand Down Expand Up @@ -113,7 +119,6 @@
ppf("remove allnulls %s\n", childVariable.id());
children().remove(childVarIt);
}
web->getResponseObject()["details"]["rowNr"] = rowNr;
}
else
print->printJson("dev array but not rowNr", var);
Expand All @@ -129,10 +134,12 @@
}
} //if new added
ppf("postDetails %s.%s post ", pid(), id());
print->printVar(var);
print->printJson(" Var", var);
ppf("\n");

//post update details
if (rowNr != UINT8_MAX)
web->getResponseObject()["details"]["rowNr"] = rowNr;
web->getResponseObject()["details"]["var"] = var;
}

Expand Down
76 changes: 27 additions & 49 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ class Variable {
const char *id() const {return var["id"];}
const char *type() const {return var["type"];}

JsonVariant value() const {return var["value"];}
JsonVariant value(uint8_t rowNr) const {return var["value"][rowNr];}
JsonVariant value(uint8_t rowNr = UINT8_MAX) const {return (rowNr==UINT8_MAX)?var["value"].as<JsonVariant>(): var["value"][rowNr].as<JsonVariant>();}

String valueString(uint8_t rowNr = UINT8_MAX);

Expand Down Expand Up @@ -289,62 +288,41 @@ class Variable {
void setValueJV(JsonVariant value, uint8_t rowNr = UINT8_MAX);

template <typename Type>
void setValue(Type value, uint8_t rowNr = UINT8_MAX) {
bool changed = false;

if (rowNr == UINT8_MAX) { //normal situation
if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
if (!var["value"].isNull() && !readOnly()) var["oldValue"] = var["value"];
var["value"] = value;
//trick to remove null values
if (var["value"].isNull() || var["value"].as<uint16_t>() == UINT16_MAX) {
var.remove("value");
// ppf("dev setValue value removed %s %s\n", id(), var["oldValue"].as<String>().c_str());
}
else {
//only print if ! read only
// if (!readOnly())
// ppf("setValue changed %s.%s %s -> %s\n", pid(), id(), var["oldValue"].as<String>().c_str(), valueString().c_str());
// else
// ppf("setValue changed %s %s\n", id(), var["value"].as<String>().c_str());
JsonVariant value = var["value"];
web->addResponse(var, "value", value);
changed = true;
}
}
}
else {
//if we deal with multiple rows, value should be an array, if not we create one
void setValue(Type newValue, uint8_t rowNr = UINT8_MAX) {

if (var["value"].isNull() || !var["value"].is<JsonArray>()) {
// ppf("setValue var %s[%d] value %s not array, creating\n", id(), rowNr, var["value"].as<String>().c_str());
var["value"].to<JsonArray>();
}
if (value(rowNr).isNull() || value(rowNr).as<Type>() != newValue) { //new or changed

if (var["value"].is<JsonArray>()) {
JsonArray valueArray = valArray();
//set the right value in the array (if array did not contain values yet, all values before rownr are set to false)
changed = true; //rowNr >= size
if (!value().isNull() && !readOnly()) var["oldValue"] = value(); //save oldValue

if (rowNr < valueArray.size())
changed = valueArray[rowNr].isNull() || valueArray[rowNr].as<Type>() != value;
//save newValue, cleanup null values
if (rowNr == UINT8_MAX) {
var["value"] = newValue;

if (changed) {
if (value().isNull() || value().as<uint16_t>() == UINT16_MAX) {
ppf("setValue value removed %s.%s %s->%s\n", pid(), id(), valueString().c_str(), var["oldValue"].as<String>().c_str());
var.remove("value");
}

// if (rowNr >= valueArray.size())
// ppf("notSame %d %d\n", rowNr, valueArray.size());
valueArray[rowNr] = value; //if valueArray[<rowNr] not exists it will be created
// ppf(" assigned %d %d %s\n", rowNr, valueArray.size(), valueArray[rowNr].as<String>().c_str());
JsonVariant value = var["value"];
web->addResponse(var, "value", value); //send the whole array to UI as response is in format value:<value> !!
} else {
var["value"][rowNr] = newValue;

//cleanup Array
size_t size = value().size();
while (size > 0 && (value(size-1).isNull() || value(size-1).as<uint16_t>() == UINT16_MAX)) {
ppf("setValue value removed %s.%s[%d] %s->%s\n", pid(), id(), size - 1, valueString().c_str(), var["oldValue"].as<String>().c_str());
var["value"].remove(size-1);
size = value().size();
}
if (size == 0) {
ppf("setValue value array removed %s.%s[] %s->%s\n", pid(), id(), valueString().c_str(), var["oldValue"].as<String>().c_str());
var.remove("value");
}
}
else {
ppf("setValue %s.%s could not create value array\n", pid(), id());
}

web->addResponse(var, "value", value());
triggerEvent(onChange, rowNr);
}

if (changed) triggerEvent(onChange, rowNr);
}

//Set value with argument list
Expand Down
2 changes: 2 additions & 0 deletions src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ SysModSystem::SysModSystem() :SysModule("System") {};

void SysModSystem::setup() {
SysModule::setup();

ppf("Stack %d of %d B (async %d of %d B) %d\n", sysTools_get_arduino_maxStackUsage(), getArduinoLoopTaskStackSize(), sysTools_get_webserver_maxStackUsage(), CONFIG_ASYNC_TCP_STACK_SIZE, uxTaskGetStackHighWaterMark(xTaskGetCurrentTaskHandle()));

const Variable parentVar = ui->initSysMod(Variable(), name, 2000);
parentVar.var["s"] = true; //setup
Expand Down
3 changes: 1 addition & 2 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,14 @@ void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filte

bool SysModWeb::captivePortal(WebRequest *request)
{
ppf("captivePortal %d %d\n", net->localIP()[3], request->client()->localIP()[3]);

if (ON_STA_FILTER(request)) return false; //only serve captive in AP mode
String hostH;
if (!request->hasHeader("Host")) return false;
hostH = request->getHeader("Host")->value();

if (!isIp(hostH) && hostH.indexOf(mdns->cmDNS) < 0) { //&& hostH.indexOf("wled.me") < 0
ppf("Captive portal\n");
ppf("captivePortal %d %d\n", net->localIP()[3], request->client()->localIP()[3]);
WebResponse *response = request->beginResponse(302);
response->addHeader(F("Location"), F("http://4.3.2.1"));
request->send(response);
Expand Down
3 changes: 2 additions & 1 deletion src/User/UserModLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ static float _time(float j) {
}

ppf("Before parsing of %s\n", fileName);
ppf("%s:%d f:%d / t:%d (l:%d) B [%d %d]\n", __FUNCTION__, __LINE__, ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap(), esp_get_free_heap_size(), esp_get_free_internal_heap_size());
ppf("Heap %s:%d f:%d / t:%d (l:%d) B [%d %d]\n", __FUNCTION__, __LINE__, ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap(), esp_get_free_heap_size(), esp_get_free_internal_heap_size());
ppf("Stack %d of %d B (async %d of %d B) %d\n", sys->sysTools_get_arduino_maxStackUsage(), getArduinoLoopTaskStackSize(), sys->sysTools_get_webserver_maxStackUsage(), CONFIG_ASYNC_TCP_STACK_SIZE, uxTaskGetStackHighWaterMark(xTaskGetCurrentTaskHandle()));

Executable executable = parser.parseScript(&scScript);
executable.name = string(fileName);
Expand Down
4 changes: 1 addition & 3 deletions src/User/UserModLive.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class UserModLive: public SysModule {
char fileName[32] = ""; //running sc file
std::string scScript; //externals etc generated (would prefer String for esp32...)

UserModLive() :SysModule("LiveScripts") {
isEnabled = false; //need to enable after fresh setup
};
UserModLive() :SysModule("LiveScripts") {};

void setup() override;

Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ AppModDemo *appModDemo;
UserModLive *liveM;
#endif

SET_LOOP_TASK_STACK_SIZE(16 * 1024); // 16KB

//setup all modules
void setup() {

mdls = new SysModules();

print = new SysModPrint();
Expand Down

0 comments on commit 69b3586

Please sign in to comment.