Skip to content

Commit

Permalink
Coord3D improvements, refactors, FastLed gamma by setCorrection
Browse files Browse the repository at this point in the history
General
- remove __PRETTY_FUNCTION__ debug output
- setValueUIOnly for readonly vars which update every second (so not saved in the model, only send to UI)

index.js
- refactor help ?
- set document title to instanceName

AppModLeds
- fxStart, End, Size initCoord3D functions : accept Coord3D value (no need for other value / valuefun inits
- init fxCount and fixCount with leds.size, norOfLeds
- FastLED.addLeds use setCorrection(TypicalLEDStrip) !!!
- brightness not logarithmic due to setCorrection

SysModModel
- add setChFunAndWs (from UI), to avoid cross dependency between model and ui class
- setValue: remove readonly option as they also need to be saved (except see setValueUIOnly)
- setValue: remove values which are null (incl UINT16_MAX)
- add setValueUIOnly, remove setValueP

SysModUI
- move processURL serveJson to Web
- initCoord3D support Coord3D value
- initVarAndUpdate refactor using mdl->setValue (include SysModModel)
- cFunctions public so can be used in mdl->setChFunAndWs

SysModWeb
- processURL serveJson (from SysModUI)
  • Loading branch information
ewowi committed Jan 28, 2024
1 parent 7c1831e commit da760a6
Show file tree
Hide file tree
Showing 28 changed files with 1,812 additions and 1,916 deletions.
1 change: 1 addition & 0 deletions data/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ select {

h1,h2 {
color: var(--h-color);
line-height: 5px;
}

div {
Expand Down
3 changes: 2 additions & 1 deletion data/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
</head>
<body onload="onLoad()">
<div class="module">
<h1>StarMod by MoonModules 💫 <div id="instanceName"></div></h1>
<h1>StarMod by MoonModules 💫</h1>
<h2><div id="instanceName"></div></h2>
<input type="button" value="App" id="vApp" onclick="setView(this)">
<input type="button" value="Stage" id="vStage" onclick="setView(this)">
<input type="button" value="User" id="vUser" onclick="setView(this)">
Expand Down
20 changes: 10 additions & 10 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,17 @@ function generateHTML(json, parentNode = null, rowNr = -1) {
ndivNeeded = false;

varNode = cE("div");

let h2Node = cE("h2");
h2Node.innerText = initCap(variable.id);
varNode.appendChild(h2Node);

let helpNode = cE("input");
helpNode.type = "button";
helpNode.value = "?";
helpNode.addEventListener('click', (event) => {
location.href="https://ewowi.github.io/StarDocs/";// + variable.id;
// location.href="https://starmod.org/" + variable.id;
});

let helpNode = cE("a");
helpNode.innerText = "❓";
let initCapVarType = variable.type=="appmod"?"AppMod":variable.type=="usermod"?"UserMod":"SysMod";
helpNode.setAttribute('href', "https://ewowi.github.io/StarDocs/" + initCapVarType + "/" + initCapVarType + initCap(variable.id));
varNode.appendChild(helpNode);

setupModule(varNode); //enable drag and drop of modules
}
else if (variable.type == "table") {
Expand Down Expand Up @@ -817,8 +815,10 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
node.dispatchEvent(new Event("input")); // triggers addEventListener('input',...). now only used for input type range (slider), needed e.g. for qlc+ input

//'hack' show the serverName on top of the page
if (variable.id == "serverName")
if (variable.id == "serverName") {
gId("instanceName").innerText = commandJson.value;
document.title = commandJson.value;
}
}

//value assignments depending on different situations
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ platform = [email protected] ;using platformio/framework-arduinoespressif32 @ ~3
upload_speed = 1500000
build_flags = ${env.build_flags}
-DCONFIG_IDF_TARGET_ESP32=1

; RAM: [== ] 15.6% (used 51124 bytes from 327680 bytes)
; Flash: [======= ] 68.1% (used 892033 bytes from 1310720 bytes)

Expand All @@ -89,6 +90,7 @@ board = lolin_d32 ;https://github.com/platformio/platform-espressif32/blob/devel
platform = [email protected] ;using platformio/framework-arduinoespressif32 @ ~3.20014.0 / framework-arduinoespressif32 @ 3.20014.231204 (2.0.14)
build_flags = ${env.build_flags}
-DCONFIG_IDF_TARGET_ESP32=1

; RAM: [== ] 15.6% (used 51124 bytes from 327680 bytes)
; Flash: [======= ] 68.1% (used 892033 bytes from 1310720 bytes)

Expand All @@ -102,11 +104,11 @@ build_unflags =
-DARDUINO_USB_CDC_ON_BOOT=1 ;; un-comment if you want to use a "real" serial-to-USB moddule
build_flags = ${env.build_flags}
-DCONFIG_IDF_TARGET_ESP32S2=1
; -DLFS_THREADSAFE ;; enables use of semaphores in LittleFS driver
-DARDUINO_USB_CDC_ON_BOOT=1 -DARDUINO_USB_DFU_ON_BOOT=1 -DARDUINO_USB_MSC_ON_BOOT=0 ;; for debugging over USB
; -DARDUINO_USB_CDC_ON_BOOT=0 -DARDUINO_USB_DFU_ON_BOOT=1 -DARDUINO_USB_MSC_ON_BOOT=0 ;; with serial-to-USB moddule (use in case your board hangs without USB connection)
-DARDUINO_USB_MODE=0 ;; Make sure that the right HardwareSerial driver is picked in arduino-esp32 (mandatory on -S2)
; -D DEBUG=1 -D CORE_DEBUG_LEVEL=1 -D ARDUINOJSON_DEBUG=1 ;; for more debug output

; RAM: [== ] 24.5% (used 80240 bytes from 327680 bytes)
; Flash: [========= ] 92.4% (used 1210894 bytes from 1310720 bytes)
; after WLED_ESP32_4MB_256KB_FS:
Expand Down
2 changes: 0 additions & 2 deletions src/App/AppModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ class AppModFixtureGen:public SysModule {

void setup() {
SysModule::setup();
USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

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

Expand Down Expand Up @@ -538,7 +537,6 @@ class AppModFixtureGen:public SysModule {
generateChFun(var);
});

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

void loop() {
Expand Down
252 changes: 115 additions & 137 deletions src/App/AppModLeds.h

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/App/AppModPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class AppModPreview:public SysModule {

void setup() {
SysModule::setup();
USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

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

Expand Down
6 changes: 1 addition & 5 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
bool SysModFiles::filesChanged = false;

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

if (!LittleFS.begin(true)) { //true: formatOnFail
USER_PRINTF(" An Error has occurred while mounting File system");
USER_PRINTF(" fail\n");
success = false;
}

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
};

//setup filesystem
Expand Down Expand Up @@ -100,7 +96,7 @@ void SysModFiles::loop(){
// }

if (filesChanged) {
mdl->setValueP("drsize", "%d / %d B", usedBytes(), totalBytes());
mdl->setValueUIOnly("drsize", "%d / %d B", usedBytes(), totalBytes());
filesChanged = false;

ui->processUiFun("fileTbl");
Expand Down
60 changes: 49 additions & 11 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "SysModule.h"
#include "SysModFiles.h"
#include "SysJsonRDWS.h"
#include "SysModUI.h"

BasicJsonDocument<RAM_Allocator> * SysModModel::model = nullptr;
JsonObject SysModModel::modelParentVar;
Expand All @@ -22,24 +23,18 @@ SysModModel::SysModModel() :SysModule("Model") {

JsonArray root = model->to<JsonArray>(); //create

USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

USER_PRINTF("Reading model from /model.json... (deserializeConfigFromFS)\n");
if (files->readObjectFromFile("/model.json", model)) {//not part of success...
print->printJson("Read model", *model);
web->sendDataWs(*model);
} else {
root = model->to<JsonArray>(); //re create the model as it is corrupted by readFromFile
}

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

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

USER_PRINT_FUNCTION("%s %s\n", __PRETTY_FUNCTION__, name);

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

ui->initText(parentVar, "mSize", nullptr, 32, true, [](JsonObject var) { //uiFun
Expand All @@ -62,8 +57,6 @@ void SysModModel::setup() {
web->addResponse(var["id"], "label", "Delete obsolete variables");
web->addResponse(var["id"], "comment", "WIP");
});

USER_PRINT_FUNCTION("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

void SysModModel::loop() {
Expand Down Expand Up @@ -99,7 +92,7 @@ void SysModModel::setup() {
}
}
void SysModModel::loop1s() {
setValueV("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
setValueUIOnly("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
}

void SysModModel::cleanUpModel(JsonArray vars, bool oPos, bool ro) {
Expand All @@ -113,7 +106,7 @@ void SysModModel::cleanUpModel(JsonArray vars, bool oPos, bool ro) {
if (oPos) {
if (var["o"].isNull() || var["o"].as<int>() >= 0) { //not set negative in initVar
if (!doShowObsolete) {
USER_PRINTF("remove var %s (o>=0)\n", var["id"].as<const char *>());
USER_PRINTF("cleanUpModel remove var %s (""o"">=0)\n", var["id"].as<const char *>());
vars.remove(varV); //remove the obsolete var (no o or )
}
}
Expand All @@ -122,7 +115,7 @@ void SysModModel::cleanUpModel(JsonArray vars, bool oPos, bool ro) {
}
} else { //!oPos
if (var["o"].isNull() || var["o"].as<int>() < 0) {
USER_PRINTF("remove var %s (o<0)\n", var["id"].as<const char *>());
USER_PRINTF("cleanUpModel remove var %s (""o""<0)\n", var["id"].as<const char *>());
vars.remove(varV); //remove the obsolete var (no o or o is negative - not cleanedUp)
}
}
Expand Down Expand Up @@ -197,5 +190,50 @@ void SysModModel::varToValues(JsonObject var, JsonArray row) {
varToValues(childVar, row.createNestedArray());
}
}
}

//tbd: use template T for value
//run the change function and send response to all? websocket clients
void SysModModel::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 < ui->cFunctions.size()) {
USER_PRINTF("chFun %s", var["id"].as<const char *>());
if (rowNr!=UINT8_MAX)
USER_PRINTF("[%d]", rowNr);
USER_PRINTF(" <- %s\n", var["value"].as<String>().c_str());
ui->cFunctions[funNr](var, rowNr);
}
else
USER_PRINTF("setChFunAndWs function nr %s outside bounds %d >= %d\n", var["id"].as<const char *>(), funNr, ui->cFunctions.size());
}

if (var["stage"])
ui->stageVarChanged = true;

JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

if (value)
web->addResponse(var["id"], "value", JsonString(value, JsonString::Copied));
else {
if (var["value"].is<int>())
web->addResponse(var["id"], "value", var["value"].as<int>());
else if (var["value"].is<bool>())
web->addResponse(var["id"], "value", var["value"].as<bool>());
else if (var["value"].is<const char *>())
web->addResponse(var["id"], "value", var["value"].as<const char *>());
else if (var["value"].is<JsonArray>()) {
// USER_PRINTF("setChFunAndWs %s JsonArray %s\n", var["id"].as<const char *>(), var["value"].as<String>().c_str());
web->addResponse(var["id"], "value", var["value"].as<JsonArray>());
}
else {
USER_PRINTF("setChFunAndWs %s unknown type for %s\n", var["id"].as<const char *>(), var["value"].as<String>().c_str());
// web->addResponse(var["id"], "value", var["value"]);
}
}

web->sendDataWs(responseVariant);
}
53 changes: 29 additions & 24 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#pragma once
#include "SysModule.h"
#include "SysModPrint.h"
#include "SysModUI.h"
#include "SysModWeb.h"

#include "ArduinoJson.h"
Expand Down Expand Up @@ -66,7 +65,7 @@ namespace ArduinoJson {
dst["x"] = src.x;
dst["y"] = src.y;
dst["z"] = src.z;
USER_PRINTF("dest %d,%d,%d -> %s ", src.x, src.y, src.z, dst.as<String>().c_str());
USER_PRINTF("Coord3D toJson %d,%d,%d -> %s\n", src.x, src.y, src.z, dst.as<String>().c_str());
return true;
}

Expand Down Expand Up @@ -137,21 +136,18 @@ class SysModModel:public SysModule {
JsonObject setValue(JsonObject var, Type value, uint8_t rowNr = UINT8_MAX) {
// print->printJson("setValueB", var);
if (rowNr == UINT8_MAX) { //normal situation
if (var["ro"].as<bool>()) {
//read only vars not in the model (tbd: modify setChFunAndWs)
JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse(var["id"], "value", value);

web->sendDataWs(responseVariant);
}
else if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
USER_PRINTF("setValue changed %s[%d] %s->", var["id"].as<const char *>(), rowNr, var["value"].as<String>().c_str()); //old value
if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
JsonString oldValue = JsonString(var["value"], JsonString::Copied);
var["value"] = value;
USER_PRINTF("%s\n", var["value"].as<String>().c_str()); //newvalue
ui->setChFunAndWs(var, rowNr);
//trick to remove null values
if (var["value"].isNull() || var["value"].as<uint32_t>() == UINT16_MAX) {
var.remove("value");
USER_PRINTF("setValue value removed %s %s\n", var["id"].as<const char *>(), oldValue.c_str()); //old value
}
else {
USER_PRINTF("setValue changed %s %s->%s\n", var["id"].as<const char *>(), oldValue.c_str(), var["value"].as<String>().c_str()); //old value
setChFunAndWs(var);
}
}
}
else {
Expand All @@ -172,8 +168,9 @@ class SysModModel:public SysModule {
notSame = valueArray[rowNr].isNull() || valueArray[rowNr].as<Type>() != value;

if (notSame) {
// setValue(var, value, rowNr);
valueArray[rowNr] = value; //if valueArray[rowNr] not exists it will be created
ui->setChFunAndWs(var, rowNr);
setChFunAndWs(var, rowNr);
}
}
else {
Expand All @@ -185,10 +182,6 @@ class SysModModel:public SysModule {
}

//Set value with argument list
// JsonObject setValueV(const char * id, uint8_t rowNr = UINT8_MAX, const char * format, ...) {
// setValueV(id, UINT8_MAX, format);
// }

JsonObject setValueV(const char * id, const char * format, ...) {
va_list args;
va_start(args, format);
Expand All @@ -198,20 +191,29 @@ class SysModModel:public SysModule {

va_end(args);

USER_PRINTF("%s\n", value);

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

JsonObject setValueP(const char * id, const char * format, ...) {
JsonObject setValueUIOnly(const char * id, const char * format, ...) {
va_list args;
va_start(args, format);

char value[128];
vsnprintf(value, sizeof(value)-1, format, args);
USER_PRINTF("%s\n", value);

va_end(args);

return setValue(id, JsonString(value, JsonString::Copied));
JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse(id, "value", JsonString(value, JsonString::Copied));

web->sendDataWs(responseVariant);

return JsonObject();
}

JsonVariant getValue(const char * id, uint8_t rowNr = UINT8_MAX) {
Expand Down Expand Up @@ -245,6 +247,9 @@ class SysModModel:public SysModule {
//recursively add values in a variant
void varToValues(JsonObject var, JsonArray values);

//run the change function and send response to all? websocket clients
static void setChFunAndWs(JsonObject var, uint8_t rowNr = UINT8_MAX, const char * value = nullptr);

private:
bool doShowObsolete = false;
bool cleanUpModelDone = false;
Expand Down
Loading

0 comments on commit da760a6

Please sign in to comment.