Skip to content

Commit

Permalink
Refactor canvas buffering, delRow, addRow using table chFun
Browse files Browse the repository at this point in the history
App.js
- rename leds to buffer (match with c code)
- buffer[0] instead of userFunId

index.js
- remove userFunId
- rename insRow to addRow
- add coord3D type
- receiveData: updRow: add row if not found (WIP)
- changeHTML: isArray only for td
- changeHTML: canvas: no value setting anymore

AppModLeds and SysModPins:
- preview: use buffer[0] to store id

SysModFiles:
- move delRow to table chFun and use standard delete trigger
- remove flDel (delete)

SysModel
- rename Coordinate to Coord3D and move from AppLedsV, rename in FixtureGen

SysModUI:
- use buffer[0] for id, don't send id using setChFunAndWs anymore
- addRow and delRow: extract id and rowNr from value
  • Loading branch information
ewowi committed Jan 18, 2024
1 parent 57d0066 commit ac91887
Show file tree
Hide file tree
Showing 10 changed files with 1,473 additions and 1,400 deletions.
53 changes: 26 additions & 27 deletions data/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
// @license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// @license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]

function userFun(userFunId, data) {
if (userFunId == "pview" && jsonValues.pview) {
let leds = new Uint8Array(data);
function userFun(data) {
let buffer = new Uint8Array(data);
if (buffer[0]==1 && jsonValues.pview) {
let pviewNode = gId("pview");

//replace the canvas: in case we switch from 2D to 3D as they cannot be reused between them
Expand All @@ -29,27 +29,26 @@ function userFun(userFunId, data) {
pviewNode.addEventListener('dblclick', (event) => {toggleModal(event.target);});
}

// console.log("userFun", leds);
// console.log("userFun", buffer);

// if (jsonValues.pview.depth == 1)
// preview2D(pviewNode, leds);
// preview2D(pviewNode, buffer);
// else
preview3D(pviewNode, leds);
preview3D(pviewNode, buffer);

return true;
}
else if (userFunId == "board") {
let leds = new Uint8Array(data);
else if (buffer[0]==0) {
let pviewNode = gId("board");
// console.log(leds, pviewNode);
previewBoard(pviewNode, leds);
// console.log(buffer, pviewNode);
previewBoard(pviewNode, buffer);
}
return false;
}

function preview2D(node, leds) {
function preview2D(node, buffer) {
let ctx = node.getContext('2d');
let i = 4;
let i = 5;
let factor = 10;//fixed value: from mm to cm
ctx.clearRect(0, 0, node.width, node.height);
if (jsonValues.pview) {
Expand All @@ -58,10 +57,10 @@ function preview2D(node, leds) {
if (jsonValues.pview.outputs) {
// console.log("preview2D jsonValues", jsonValues.pview);
for (var output of jsonValues.pview.outputs) {
if (output.leds) {
for (var led of output.leds) {
if (leds[i] + leds[i+1] + leds[i+2] > 20) { //do not show nearly blacks
ctx.fillStyle = `rgb(${leds[i]},${leds[i+1]},${leds[i+2]})`;
if (output.buffer) {
for (var led of output.buffer) {
if (buffer[i] + buffer[i+1] + buffer[i+2] > 20) { //do not show nearly blacks
ctx.fillStyle = `rgb(${buffer[i]},${buffer[i+1]},${buffer[i+2]})`;
ctx.beginPath();
ctx.arc(led[0]*pPL/factor + lOf, led[1]*pPL/factor, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
Expand Down Expand Up @@ -92,11 +91,11 @@ let intersect = null;
let mousePointer = null;

//https://stackoverflow.com/questions/8426822/rotate-camera-in-three-js-with-mouse
function preview3D(node, leds) {
function preview3D(node, buffer) {
//3D vars
// let mW = leds[0];
// let mH = leds[1];
// let mD = leds[2];
// let mW = buffer[0];
// let mH = buffer[1];
// let mD = buffer[2];
import ('three').then((THREE) => {

function onMouseMove( event ) {
Expand Down Expand Up @@ -246,17 +245,17 @@ function preview3D(node, leds) {
if (renderer.width != gId("pview").width || renderer.height != gId("pview").height)
renderer.setSize( gId("pview").width, gId("pview").height);
//light up the cube
let firstLed = 4;
let firstLed = 5;
var i = 1;
if (jsonValues.pview.outputs) {
// console.log("preview3D jsonValues", jsonValues.pview);
for (var output of jsonValues.pview.outputs) {
if (output.leds) {
for (var led of output.leds) {
if (i < scene.children.length) {
scene.children[i].visible = leds[i*3 + firstLed] + leds[i*3 + firstLed + 1] + leds[i*3 + firstLed+2] > 10; //do not show blacks
scene.children[i].visible = buffer[i*3 + firstLed] + buffer[i*3 + firstLed + 1] + buffer[i*3 + firstLed+2] > 10; //do not show blacks
if (scene.children[i].visible)
scene.children[i].material.color = new THREE.Color(`${leds[i*3 + firstLed]/255}`, `${leds[i*3 + firstLed + 1]/255}`, `${leds[i*3 + firstLed + 2]/255}`);
scene.children[i].material.color = new THREE.Color(`${buffer[i*3 + firstLed]/255}`, `${buffer[i*3 + firstLed + 1]/255}`, `${buffer[i*3 + firstLed + 2]/255}`);
}
i++;
}
Expand Down Expand Up @@ -307,18 +306,18 @@ function preview3D(node, leds) {
}); //import Three
} //preview3D

function previewBoard(node, leds) {
function previewBoard(node, buffer) {
let ctx = node.getContext('2d');
//assuming 20 pins
let mW = 10; // matrix width
let mH = 2; // matrix height
let pPL = Math.min(node.width / mW, node.height / mH); // pixels per LED (width of circle)
let lOf = Math.floor((node.width - pPL*mW)/2); //left offeset (to center matrix)
let i = 4;
let i = 5;
ctx.clearRect(0, 0, node.width, node.height);
for (let y=0.5;y<mH;y++) for (let x=0.5; x<mW; x++) {
if (leds[i] + leds[i+1] + leds[i+2] > 20) { //do not show nearly blacks
ctx.fillStyle = `rgb(${leds[i]},${leds[i+1]},${leds[i+2]})`;
if (buffer[i] + buffer[i+1] + buffer[i+2] > 20) { //do not show nearly blacks
ctx.fillStyle = `rgb(${buffer[i]},${buffer[i+1]},${buffer[i+2]})`;
ctx.beginPath();
ctx.arc(x*pPL+lOf, y*pPL, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
Expand Down
84 changes: 61 additions & 23 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ let ws = null;

let mdlColumnNr = 0;
let nrOfMdlColumns = 4;
let userFunId = "";
let jsonValues = {};
let uiFunCommands = [];
let model = []; //model.json (as send by the server), used by FindVar
Expand Down Expand Up @@ -44,10 +43,10 @@ function makeWS() {
ws.binaryType = "arraybuffer";
ws.onmessage = (e)=>{
if (e.data instanceof ArrayBuffer) { // preview packet
if (userFun(userFunId, e.data))
userFunId = "";
userFun(e.data);
}
else {
// console.log("onmessage", e.data);
clearTimeout(jsonTimeout);
jsonTimeout = null;
gId('connind').style.backgroundColor = "var(--c-l)";
Expand Down Expand Up @@ -114,6 +113,9 @@ function makeWS() {
console.log("WS open", e);
reqsLegal = true;
}
ws.onerror = (e)=>{
console.log("WS error", e);
}
}

function linearToLogarithm(json, value) {
Expand Down Expand Up @@ -148,7 +150,7 @@ function genTableRowHTML(json, parentNode = null, rowNr = -1) {
for (let columnVar of variable.n) {
let tdNode = cE("td");
trNode.appendChild(tdNode);
generateHTML(columnVar, tdNode, rowNr);
generateHTML(columnVar, tdNode, rowNr); //will also do the values
}
if (!variable.ro) {
let tdNode = cE("td");
Expand All @@ -160,7 +162,7 @@ function genTableRowHTML(json, parentNode = null, rowNr = -1) {
console.log("Table -", event.target);

var command = {};
command["delRow"] = {"id": variable.id, "row":rowNr};
command["delRow"] = {"id": variable.id, "rowNr":rowNr};
requestJson(command);

});
Expand Down Expand Up @@ -261,12 +263,12 @@ function generateHTML(json, parentNode = null, rowNr = -1) {
let tbodyNode = tableNode.querySelector("tbody");
console.log("Table +", divNode, variable, tableNode);

let newRowNr = tbodyNode.querySelectorAll("tr").length; //new rowNr
let newRowNr = tbodyNode.querySelectorAll("tr").length;

genTableRowHTML(variable, tableNode, newRowNr);

var command = {};
command["insRow"] = {"id": variable.id, "row":newRowNr};
command["addRow"] = {"id": variable.id, "rowNr":newRowNr};
requestJson(command);
});
divNode.appendChild(buttonNode);
Expand All @@ -279,7 +281,6 @@ function generateHTML(json, parentNode = null, rowNr = -1) {

varNode = cE("th");
varNode.innerText = initCap(variable.id); //label uiFun response can change it
parentNode.querySelector('thead').querySelector("tr").appendChild(varNode); //<thead><tr> (containing th)

} else if (variable.type == "select") {

Expand Down Expand Up @@ -349,6 +350,20 @@ function generateHTML(json, parentNode = null, rowNr = -1) {
});
rangeValueNode = cE("span");
rangeValueNode.id = rvNode; //rangeValue
} else if (variable.type == "coord3D") {
varNode = cE("div");
let xNode = cE("input");
xNode.type = "number";
xNode.min = variable.min?variable.min:0; //if not specified then unsigned value (min=0)
if (variable.max) xNode.max = variable.max;
xNode.placeholder = "x";
let yNode = xNode.cloneNode();
yNode.placeholder = "y";
let zNode = xNode.cloneNode();
zNode.placeholder = "z";
varNode.appendChild(xNode);
varNode.appendChild(yNode);
varNode.appendChild(zNode);
} else {
//input types: text, search, tel, url, email, and password.

Expand Down Expand Up @@ -377,13 +392,13 @@ function generateHTML(json, parentNode = null, rowNr = -1) {
} //if variable type

if (parentNodeType == "table") { //table headers don't have a divNode (why not...)
varNode.id = variable.id;
divNode = varNode;
// divNode = varNode;
parentNode.querySelector('thead').querySelector("tr").appendChild(varNode); //<thead><tr> (containing th)
} else {
varNode.id = variable.id + (isPartOfTableRow?"#" + rowNr:"");
divNode.appendChild(varNode); //add to <div>
divNode.appendChild(varNode);
parentNode.appendChild(divNode);
}
varNode.id = variable.id + (isPartOfTableRow?"#" + rowNr:"");
varNode.className = variable.type;

if (rangeValueNode) divNode.appendChild(rangeValueNode); //_rv value of range / sliders
Expand Down Expand Up @@ -447,7 +462,7 @@ function receiveData(json) {
// console.log("receiveData", json);

if (Object.keys(json)) {
for (var key of Object.keys(json)) {
for (let key of Object.keys(json)) {
let value = json[key];

//tbd: for each node of a variable (rowNr)
Expand Down Expand Up @@ -486,34 +501,38 @@ function receiveData(json) {
flushUIFunCommands(); //make sure uiFuns of new elements are called
}
else if (key == "updRow") { //update the row of a table
for (var tableId of Object.keys(value)) { //currently only one table
for (let tableId of Object.keys(value)) { //currently only one table
let tableRows = value[tableId];
console.log("receiveData updRow", key, tableId, tableRows);
let tableNode = gId(tableId);
let tableVar = findVar(tableId);
// console.log("updRow main", tableId, tableRows, tableNode, tableVar);

for (var nodeRowNr = 1, rowNode; rowNode = tableNode.rows[nodeRowNr]; nodeRowNr++) { //<table> rows starting with header row
let rowNr = nodeRowNr - 1;
let rowFound = false;
let rowNr = -1;
for (let nodeRowNr = 1, rowNode; rowNode = tableNode.rows[nodeRowNr]; nodeRowNr++) { //<table> rows starting with header row
rowNr = nodeRowNr - 1;
// console.log(" noderow", rowNr, rowNode);

if (Array.isArray(tableRows)) {
for (let tableRow of tableRows) {
// console.log(" tablerow", tableId, tableRow);

//loop over all column vars
//like genTableRowHTML but here only values, id's do exist already
let colNr = 0;
let found = false;
let keyFound = false; //reset each iteration
for (let colVar of tableVar.n) {
let colNode = gId(colVar.id + "#" + rowNr);
if (colNode) {
let colValue = tableRow[colNr];
// console.log(" ", colVar, colNode, colValue);

if (colNr == 0) { //check on the value of the first table column: tbd: check other columns?
found = colNode.innerText == colValue;
//innerText is assuming span like node. tbd: others
} else if (found) { //columns 1..n
keyFound = colNode.innerText == colValue; //innerText is assuming span like node. tbd: others
} 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);
}
}
Expand All @@ -523,6 +542,11 @@ function receiveData(json) {
}
}
}
} //for each row
if (!rowFound) {
// this is a new rode, add it
console.log("receiveData updRow, new row", tableVar, tableNode, rowNr);
// genTableRowHTML(tableVar, tableNode, rowNr+1);
}
} //tableId
}
Expand Down Expand Up @@ -710,8 +734,8 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
flushUIFunCommands(); //make sure uiFuns of new elements are called

}
else if (Array.isArray(commandJson.value)) { //table column, called for each column cell!!!
// console.log("changeHTML value array", node.id, (rowNr==-1)?JSON.stringify(commandJson.value):commandJson.value[rowNr], commandJson.chk, rowNr);
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);

if (rowNr == -1) {
console.log("changeHTML value array should not happen when no rowNr", variable, node, commandJson, rowNr);
Expand Down Expand Up @@ -757,14 +781,28 @@ function changeHTML(variable, node, commandJson, rowNr = -1) {
node.setAttribute('href', commandJson.value);
}
else if (node.className == "canvas")
userFunId = node.id; //prepare for websocket data
console.log("not called anymore");
else if (node.className == "checkbox") {
node.checked = commandJson.value;
node.indeterminate = (commandJson.value == null); //set the false if it has a non null value
}
else if (node.className == "button") {
if (commandJson.value) node.value = commandJson.value; //else the id / label is used as button label
}
else if (node.className == "coord3D") {
console.log("chHTML value coord3D", node, commandJson.value, rowNr);

if (commandJson.value && Object.keys(commandJson.value)) { //tbd: support arrays (now only objects)
let index = 0;
for (let key of Object.keys(commandJson.value)) {
let childNode = node.childNodes[index++];
childNode.value = commandJson.value[key];
childNode.dispatchEvent(new Event("input")); // triggers addEventListener('input',...). now only used for input type range (slider), needed e.g. for qlc+ input
}
}
else
console.log(" value coord3D value not object[x,y,z]", commandJson.value);
}
else {//inputs or select
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
Expand Down
6 changes: 0 additions & 6 deletions src/App/AppLedsV.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ enum Projections
count
};

struct Coordinate {
uint16_t x;
uint16_t y;
uint16_t z;
};

class LedsV {

public:
Expand Down
6 changes: 3 additions & 3 deletions src/App/AppModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class GenFix {
closePin();
}

void plane3DFindNextPoint(Coordinate *point, Coordinate first, Coordinate last, uint8_t axis, bool clockWise) {
void plane3DFindNextPoint(Coord3D *point, Coord3D first, Coord3D last, uint8_t axis, bool clockWise) {
if (axis == 0) {
if (point->x != last.x) {
point->x += point->x<=last.x?1:-1;
Expand Down Expand Up @@ -366,10 +366,10 @@ class GenFix {
// USER_PRINTF("plane3DFindNextPoint %d %d %d %d \n", axis, point->x, point->y, point->z);
}

void plane3D (Coordinate first, Coordinate last, bool clockWise) {
void plane3D (Coord3D first, Coord3D last, bool clockWise) {
openPin();
bool cont = true;
Coordinate point = first;
Coord3D point = first;

write3D(point.x*10, point.y*10, point.z*10);

Expand Down
Loading

0 comments on commit ac91887

Please sign in to comment.