Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
index.js
- createHTML: don't show value -99 vars (WIP)
- changeHTML remove node paramater, find all nodes using variable.id
- receiveData: var and rowNr as object elements
- changeHTML: value: if array and rowNr, pick from array

AppEffects
- setPalette: valueFun: set value in array (WIP)
- setEffect: add rowNr to responseObject instead of updating var

AppFixture
- refactor set leds size (WIP)
- mappingTable add: check if within boundaries

AppLeds
- set/getPixelColors: optimizations and debug messages (temp)
  • Loading branch information
ewowi committed Feb 2, 2024
1 parent 61b115c commit fc7338a
Show file tree
Hide file tree
Showing 9 changed files with 1,462 additions and 1,411 deletions.
86 changes: 44 additions & 42 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ function createHTML(json, parentNode = null, rowNr = -1) {
else { // json is variable
let variable = json;

if (Array.isArray(variable.value) && rowNr != -1)
if (variable.value[rowNr] == -99) return;

//if root (type module) add the html to one of the mdlColumns
if (parentNode == null) {
parentNode = gId("mdlColumn" + mdlColumnNr);
Expand Down Expand Up @@ -418,11 +421,11 @@ function createHTML(json, parentNode = null, rowNr = -1) {
//don't call uiFun on table rows (the table header calls uiFun and propagate this to table row columns in changeHTML when needed - e.g. select)
if (variable.uiFun == null || variable.uiFun == -2) { //request processed
variable.chk = "gen2";
changeHTML(variable, varNode, variable, rowNr); // set the variable with its own changed values
changeHTML(variable, variable, rowNr); // set the variable with its own changed values
}
else { //uiFun
if (variable.value)
changeHTML(variable, varNode, {"value":variable.value, "chk":"gen1"}, rowNr); //set only the value
changeHTML(variable, {"value":variable.value, "chk":"gen1"}, rowNr); //set only the value

//call ui Functionality, if defined (to set label, comment, select etc)
if (variable.uiFun >= 0) { //>=0 as element in var
Expand Down Expand Up @@ -498,11 +501,11 @@ function receiveData(json) {
console.log("receiveData no action", key, value);
}
else if (key == "details") {
let variable = value;
let rowNr = variable["rowNr"]!=null?variable["rowNr"]:-1;
let variable = value.var;
let rowNr = value.rowNr == null?-1:value.rowNr;
let nodeId = variable.id + ((rowNr != -1)?"#" + rowNr:"");
//if var object with .n, create .n (e.g. see setEffect and fixtureGenChFun, tbd: )
console.log("receiveData details", key, variable, nodeId);
console.log("receiveData details", key, variable, nodeId, rowNr);
if (gId(nodeId + "_n")) gId(nodeId + "_n").remove(); //remove old ndiv

//create new ndiv
Expand Down Expand Up @@ -548,7 +551,7 @@ function receiveData(json) {
} else if (keyFound) { //colNr 1..n
rowFound = true;
// console.log("receiveData updRow, existing row", tableVar, tableNode, colVar, colNode, rowNr);
changeHTML(colVar, colNode, {"value":colValue, "chk":"updRow"}, rowNr);
changeHTML(colVar, {"value":colValue, "chk":"updRow"}, rowNr);
}
}
else
Expand All @@ -565,18 +568,14 @@ function receiveData(json) {
}
} //tableId
}
else { //{variable:{label:value}}
else { //{variable:{label:value:options:comment:}}
let variable = findVar(key);

if (variable) {
variable.uiFun = -2; // request processed

if (gId(key)) { //update the variable and in case of a table the tableheader
value.chk = "uiFun";
changeHTML(variable, gId(key), value);
}
else
console.log("receiveData id not found in dom", key, value);
value.chk = "uiFun";
changeHTML(variable, value); //changeHTML will find the rownumbers if needed
}
else
console.log("receiveData key is no variable", key, value);
Expand All @@ -588,10 +587,25 @@ function receiveData(json) {
} //receiveData

//do something with an existing (variable) node, key is an existing node, json is what to do with it
function changeHTML(variable, node, commandJson, rowNr = -1) {
function changeHTML(variable, commandJson, rowNr = -1) {

let node = null;

if (!node)
console.log("changeHTML no node !", variable, node, commandJson, rowNr);
if (rowNr != -1) node = gId(variable.id + "#" + rowNr);
else node = gId(variable.id);

if (!node) {
//we should find all nodes, it's a bit if a trick just checking for node0 (what if deleted): tbd: improve
let rowNodes = document.querySelectorAll(`${variable.type}[id*="${variable.id}#"]`); //find nodes from the right class with id + #nr
for (let subNode of rowNodes) {
let rowNr = parseInt(subNode.id.substring(variable.id.length + 1));
console.log("changeHTML found row nodes !", variable, subNode, commandJson, rowNr);
changeHTML(variable, commandJson, rowNr); //recursive call of all nodes
}
if (rowNodes.length == 0)
console.log("dev changeHTML no node !", variable, node, commandJson, rowNr);
return;
}

let nodeType = node.nodeName.toLocaleLowerCase();
let isPartOfTableRow = (rowNr != -1);
Expand Down Expand Up @@ -682,7 +696,7 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {

//if no new value, set the old one
if (commandJson.value == null)
changeHTML(variable, node, {"value":variable.value, "chk":"options"}, rowNr); //(re)set the select value
changeHTML(variable, {"value":variable.value, "chk":"options"}, rowNr); //(re)set the select value
// else
// console.log("changeHTML value will be set in value", variable, node, commandJson, rowNr);

Expand All @@ -706,8 +720,7 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
genTableRowHTML(variable, node, newRowNr);
let colNr = 0;
for (let columnVar of variable.n) {
let varId = columnVar.id + "#" + newRowNr;
changeHTML(columnVar, gId(varId), {"value": row[colNr], "chk":"table"}, newRowNr);
changeHTML(columnVar, {"value": row[colNr], "chk":"table"}, newRowNr);
colNr++;
}

Expand All @@ -728,7 +741,7 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
let trNodes = tableNode.querySelector('tbody').querySelectorAll("tr");
let tableVar = findVar(tableNode.id); //tbd: table in table
let valueLength = Array.isArray(commandJson.value)?commandJson.value.length:1; //tbd: use table nr of rows (not saved yet)
// console.log("changeHTML th column", node.id, (rowNr==-1)?JSON.stringify(commandJson.value):commandJson.value[rowNr], commandJson.chk, rowNr);
// console.log("changeHTML th column", node.id, (rowNr == -1)?JSON.stringify(commandJson.value):commandJson.value[rowNr], commandJson.chk, rowNr);

let max = Math.max(valueLength, trNodes.length);
for (let newRowNr = 0; newRowNr<max;newRowNr++) {
Expand All @@ -743,43 +756,29 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
genTableRowHTML(tableVar, tableNode, newRowNr); //this will set the whole row and its (default) values as stored in the model
}
else {
//find the new table cell and change it's value
let cellNode = gId(node.id + "#" + newRowNr);
if (cellNode) {
// console.log("changeHTML th cellNode found", cellNode.id, newRowNr);
if (newRowNr < valueLength)
changeHTML(variable, cellNode, {"value":newValue, "chk":"column"}, newRowNr);
else
changeHTML(variable, cellNode, {"value":null, "chk":"column"}, newRowNr); //new row cell has no value
}
if (newRowNr < valueLength)
changeHTML(variable, {"value":newValue, "chk":"column"}, newRowNr);
else
console.log("changeHTML th cellNode not found", node, node.id + "#" + newRowNr);
changeHTML(variable, {"value":null, "chk":"column"}, newRowNr); //new row cell has no value
}
// newRowNr++;
}

flushUIFunCommands(); //make sure uiFuns of new elements are called

}
else if (node.parentNode.parentNode.nodeName.toLocaleLowerCase() == "td" && Array.isArray(commandJson.value)) { //table column, called for each column cell!!!
// console.log("changeHTML value array", node.parentNode.parentNode.nodeName.toLocaleLowerCase(), node.id, (rowNr==-1)?JSON.stringify(commandJson.value):commandJson.value[rowNr], commandJson.chk, rowNr);
// console.log("changeHTML value array", node.parentNode.parentNode.nodeName.toLocaleLowerCase(), node.id, (rowNr == -1)?JSON.stringify(commandJson.value):commandJson.value[rowNr], commandJson.chk, rowNr);

if (rowNr == -1) {
console.log("changeHTML value array should not happen when no rowNr", variable, node, commandJson, rowNr);
let newRowNr = 0;
for (let val of commandJson.value) {
let nodeId = node.id + "#" + newRowNr; //tbd: not variable id? using node.id var#x#y possible for nested tables?
if (gId(nodeId)) {
// console.log("changeHTML value array recursive", variable, node.id, gId(nodeId), val);
changeHTML(variable, gId(nodeId), {"value":val, "chk":"Array1"}, newRowNr); //recursive set value for variable in row
}
else
console.log("changeHTML node not found", nodeId, node, commandJson);
newRowNr++;
changeHTML(variable, {"value":val, "chk":"Array1"}, newRowNr); //recursive set value for variable in row
newRowNr++;
}
}
else {
changeHTML(variable, node, {"value":commandJson.value[rowNr], "chk":"Array2"}, rowNr); //recursive set value for variable in row
changeHTML(variable, {"value":commandJson.value[rowNr], "chk":"Array2"}, rowNr); //recursive set value for variable in row
}
// node.checked = commandJson.value;
}
Expand Down Expand Up @@ -843,7 +842,10 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
console.log(" value coord3D value not object[x,y,z]", commandJson.value);
}
else {//inputs or select
node.value = commandJson.value;
if (Array.isArray(commandJson.value) && rowNr != -1)
node.value = commandJson.value[rowNr];
else
node.value = commandJson.value;
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
Expand Down
18 changes: 8 additions & 10 deletions src/App/AppEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Effect {
case 7: palette = HeatColors_p; break;
default: palette = PartyColors_p; break;
}
}, nullptr, 2, [this](JsonObject var, uint8_t rowNr) { //valueFun
mdl->setValue(var, 4, rowNr); //4 is default
});
currentVar["stage"] = true;
}
Expand Down Expand Up @@ -387,7 +389,7 @@ class DistortionWaves2D: public Effect {

void loop(Leds &leds) {

uint8_t speed = mdl->getValue("speed").as<int>()/32;
uint8_t speed = mdl->getValue("Speed").as<int>()/32;
uint8_t scale = mdl->getValue("scale").as<int>()/32;

uint8_t w = 2;
Expand Down Expand Up @@ -429,7 +431,7 @@ class DistortionWaves2D: public Effect {
}
}
bool controls(JsonObject parentVar, uint8_t rowNr) {
ui->initSlider(parentVar, "speed", 128);
ui->initSlider(parentVar, "Speed", 128);
ui->initSlider(parentVar, "scale", 128);
return true;
}
Expand All @@ -452,7 +454,7 @@ class Octopus2D: public Effect {

const uint8_t mapp = 180 / max(leds.size.x,leds.size.y);

uint8_t speed = mdl->getValue("speed");
uint8_t speed = mdl->getValue("Speed");
uint8_t offsetX = mdl->getValue("Offset X");
uint8_t offsetY = mdl->getValue("Offset Y");
uint8_t legs = mdl->getValue("Legs");
Expand Down Expand Up @@ -501,7 +503,7 @@ class Octopus2D: public Effect {
}
bool controls(JsonObject parentVar, uint8_t rowNr) {
addPalette(parentVar, rowNr);
ui->initSlider(parentVar, "speed", 128, 1, 255); //start with speed 1
ui->initSlider(parentVar, "Speed", 128, 1, 255); //start with speed 1
ui->initSlider(parentVar, "Offset X", 128);
ui->initSlider(parentVar, "Offset Y", 128);
ui->initSlider(parentVar, "Legs", 4, 1, 8);
Expand Down Expand Up @@ -988,11 +990,6 @@ class Effects {

leds.fx = mdl->getValue(var, rowNr);

if (rowNr != UINT8_MAX)
var["rowNr"] = rowNr; //store the rownNr of the updated value to send back to ui
else
var.remove("rowNr");

USER_PRINTF("setEffect %d\n", leds.fx);

if (leds.fx < effects.size()) {
Expand Down Expand Up @@ -1063,7 +1060,8 @@ class Effects {
responseDoc->clear(); //needed for deserializeJson?
JsonObject responseObject = responseDoc->to<JsonObject>();

responseObject["details"] = var;
responseObject["details"]["var"] = var;
responseObject["details"]["rowNr"] = rowNr;

print->printJson("var", responseObject);
web->sendDataWs(responseObject); //always send, also when no children, to remove them from ui
Expand Down
60 changes: 38 additions & 22 deletions src/App/AppFixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@
//search: ^(?=.*\bfor\b)(?=.*\b:\b).*$
for (std::vector<Leds>::iterator leds=ledsList.begin(); leds!=ledsList.end(); ++leds) {
if (pixel >= leds->startPos && pixel <=leds->endPos) {

float scale = 1;
switch (leds->effectDimension) {
case _1D:
leds->size.x = leds->mappingTable.size() + 1;
leds->size.y = 1;
leds->size.z = 1;
break;
case _2D:
leds->size.x = abs(leds->endPos.x - leds->startPos.x + 1);
leds->size.y = abs(leds->endPos.y - leds->startPos.y + 1);
leds->size.z = 1;

//scaling (check rounding errors)
//1024 crash in makebuffer...
if (leds->size.x * leds->size.y > 256)
scale = (sqrt((float)256.0 / (leds->size.x * leds->size.y))); //avoid high virtual resolutions
leds->size.x *= scale;
leds->size.y *= scale;
break;
}

// if (leds->fx == 11) { //lines2D
// // USER_PRINTF(" XXX %d %d %d %d, %d, %d", leds->projectionNr, leds->effectDimension, fixtureDimension, pixel.x, pixel.y, pixel.z);
// USER_PRINTF(" %d: %d,%d,%d", indexP, pixel.x, pixel.y, pixel.z);
Expand Down Expand Up @@ -102,23 +124,12 @@
indexV = distance(pixel, leds->startPos);
}
else if (leds->effectDimension == _2D) {
leds->size.z = 1; //no 3D
if (fixtureDimension == _1D)
indexV = pixel.x;
else if (fixtureDimension == _2D) {
leds->size.x = abs(leds->endPos.x - leds->startPos.x + 1);
leds->size.y = abs(leds->endPos.y - leds->startPos.y + 1);
leds->size.z = 1;

Coord3D newPixel = pixel - leds->startPos;

//scaling (check rounding errors)
//1024 crash in makebuffer...
float scale = 1;
if (leds->size.x * leds->size.y > 256)
scale = (sqrt((float)256.0 / (leds->size.x * leds->size.y))); //avoid high virtual resolutions
leds->size.x *= scale;
leds->size.y *= scale;
newPixel.x = (newPixel.x+1) * scale - 1;
newPixel.y = (newPixel.y+1) * scale - 1;
newPixel.z = 0;
Expand All @@ -129,7 +140,6 @@
else if (fixtureDimension == _3D) {
leds->size.x = size.x + size.y;
leds->size.y = size.z;
leds->size.z = 1;
indexV = leds->XY(pixel.x + pixel.y + 1, pixel.z);
// USER_PRINTF("2D to 3D indexV %d %d\n", indexV, size.x);
}
Expand All @@ -144,7 +154,6 @@
break;
case p_Fun: //first attempt for distance from Circle 2D
if (leds->effectDimension == _2D) {
leds->size.z = 1; //no 3D
if (fixtureDimension == _2D) {

float xNew = sin(pixel.x * TWO_PI / (float)(size.x-1)) * size.x;
Expand All @@ -154,13 +163,8 @@
yNew = round(((size.y-1.0-pixel.y)/(size.y-1.0) * yNew + size.y) / 2.0);

USER_PRINTF(" %d,%d->%f,%f->%f,%f", pixel.x, pixel.y, sin(pixel.x * TWO_PI / (float)(size.x-1)), cos(pixel.x * TWO_PI / (float)(size.x-1)), xNew, yNew);
Coord3D newPixel = {xNew, yNew, 0};

leds->size.x = size.x;
leds->size.y = size.y;
leds->size.z = 1;

indexV = leds->XYZ(newPixel);
indexV = leds->XYZ({(uint16_t)xNew, (uint16_t)yNew, 0});
// USER_PRINTF("2D to 2D indexV %f %d %d x %d %d x %d\n", scale, indexV, x, y, size.x, size.y);
}
}
Expand Down Expand Up @@ -213,7 +217,7 @@
if (indexV != UINT16_MAX) { //can be nulled by inverse mapping
//add physical tables if not present
if (indexV >= NUM_LEDS_Max) {
USER_PRINTF("mapping add physMap %d>=%d (%d) too big %d\n", indexV, NUM_LEDS_Max, leds->mappingTable.size(), UINT16_MAX);
USER_PRINTF("dev mapping add physMap %d>=%d (%d) too big %d\n", indexV, NUM_LEDS_Max, leds->mappingTable.size(), UINT16_MAX);
}
else {
//create new physMaps if needed
Expand All @@ -225,7 +229,7 @@
}
}
//indexV is within the square
leds->mappingTable[indexV].push_back(indexP); //add the current led in the right physMap
if (indexP < NUM_LEDS_Max) leds->mappingTable[indexV].push_back(indexP); //add the current led in the right physMap
}
}
// USER_PRINTF("mapping b:%d t:%d V:%d\n", indexV, indexP, leds->mappingTable.size());
Expand Down Expand Up @@ -279,6 +283,12 @@
}

if (leds->projectionNr > p_Random) {

// for (int i = leds->mappingTable.size(); i<leds->size.x * leds->size.y * leds->size.z;i++) {
// USER_PRINTF("mapping add extra physMap %d %d\n", i, leds->mappingTable.size());
// std::vector<uint16_t> physMap;
// leds->mappingTable.push_back(physMap);
// }
leds->nrOfLeds = leds->mappingTable.size();

// uint16_t x=0; //indexV
Expand All @@ -293,13 +303,19 @@
// // }
// USER_PRINTF("\n");
// }
// else
// USER_PRINTF("ledV %d no mapping\n", x);

// x++;
// }
}

USER_PRINTF("projectAndMap V:%dx%dx%d V:%dx%dx%d and P:%d P:%d\n", leds->size.x, leds->size.y, leds->size.z, size.x, size.y, size.z, leds->nrOfLeds, nrOfLeds);
USER_PRINTF("projectAndMap V:%dx%dx%d V:%dx%dx%d and V:%d P:%d\n", leds->size.x, leds->size.y, leds->size.z, size.x, size.y, size.z, leds->nrOfLeds, nrOfLeds);
mdl->setValue("fxSize", leds->size, rowNr);
mdl->setValue("fxCount", leds->nrOfLeds, rowNr);

USER_PRINTF("Size of Leds is %d + %d\n", sizeof(Leds), leds->mappingTable.size()); //44

rowNr++;
}

Expand Down
Loading

0 comments on commit fc7338a

Please sign in to comment.