Skip to content

Commit

Permalink
Newui: add sysInfo and Canvas type -> show board on canvas
Browse files Browse the repository at this point in the history
index.js
- add sysInfo
- fetchModelForLiveServer: send sysInfo
- fetchModelForLiveServer: send board data
- receiveData: process sysInfo

modules.js
- add PinType enum
- previewBoard in modules class
- add CanvasVariable

webbundle: also check data folder
  • Loading branch information
ewoudwijma committed Aug 13, 2024
1 parent 8fabcba commit 94107f9
Show file tree
Hide file tree
Showing 6 changed files with 2,088 additions and 1,881 deletions.
7 changes: 4 additions & 3 deletions data/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ function appName() {
}

function userFun(buffer) {
if (buffer[0]==1 && jsonValues.pview) {
let pviewNode = gId("pview");

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

if (buffer[0]==1) {
return true;
}

return false;
}
54 changes: 46 additions & 8 deletions data/newui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Controller {

//kept vars public as other classes uses them
ws = null
sysInfo = {}

modules = null
theme = null
mainNav = null
Expand All @@ -29,8 +31,6 @@ class Controller {

this.mainNav = new MainNav(this.modules.model);
this.mainNav.createHTML();


}

async fetchModelForLiveServer() {
Expand All @@ -47,10 +47,42 @@ class Controller {
this.modules.addModule(moduleJson)
}

// this.generateData(); //every 1 second
var intervalId = window.setInterval(function(){
controller.modules.generateData()
}, 1000);
//send sysInfo
let json = {}
json.sysInfo = {};
json.sysInfo.board = "esp32"
json.sysInfo.nrOfPins = 40
json.sysInfo.pinTypes = []
for (let pinNr = 0; pinNr < json.sysInfo.nrOfPins; pinNr++)
json.sysInfo.pinTypes[pinNr] = Math.round(Math.random() * 3)

this.receiveData(json);

// every 1 second
window.setInterval(function(){
controller.modules.generateData()
}, 1000);

// every 10th second send binarydata
window.setInterval(function(){
let buffer = [0,1,2,3,4]
buffer[0] = Math.round(Math.random())

// console.log(buffer)
// let buffer = new Uint8Array([0,1,2,3,4,5,6,7,8]);
if (buffer[0] == 0) {
for (let pinNr = 0; pinNr < controller.sysInfo.nrOfPins; pinNr++)
buffer[pinNr+5] = Math.round(Math.random() * 256)
let pviewNode = document.getElementById("board");
// console.log(buffer, pviewNode);
if (pviewNode)
controller.modules.previewBoard(pviewNode, buffer);
}
else {
userFun(buffer);
}

}, 100);

}

Expand All @@ -67,7 +99,7 @@ class Controller {
let pviewNode = document.getElementById("board");
// console.log(buffer, pviewNode);
if (pviewNode)
previewBoard(pviewNode, buffer);
this.modules.previewBoard(pviewNode, buffer);
}
else
userFun(buffer);
Expand Down Expand Up @@ -141,7 +173,10 @@ class Controller {
let variableClass = varJsonToClass(variable);
variableClass.receiveData(value)
} // if variable

else if (key == "sysInfo") { //update the row of a table
// ppf("receiveData", key, value.board);
this.sysInfo = value;
}
} //for key
}
}
Expand All @@ -160,6 +195,9 @@ window.controller = new Controller()

// Utility functions

const UINT8_MAX = 255;
const UINT16_MAX = 256*256-1;

function initCap(s) {
if (typeof s !== 'string') return '';
// https://www.freecodecamp.org/news/how-to-capitalize-words-in-javascript/
Expand Down
137 changes: 128 additions & 9 deletions data/newui/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
// @license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// @license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]

const pinTypeIO = 0;
const pinTypeReadOnly = 1;
const pinTypeReserved = 2;
const pinTypeSpi = 3;
const pinTypeInvalid = UINT8_MAX;

class Modules {

model = [] //model.json (as send by the server), used by FindVar
Expand Down Expand Up @@ -87,26 +93,127 @@ class Modules {
if (variable.id == id) //found variable
return variable; //this stops the walkThrough
})

}

previewBoard(canvasNode, buffer) {
let boardColor;
if (controller.sysInfo.board == "esp32s2") boardColor = "purple";
else if (controller.sysInfo.board == "esp32s3") boardColor = "blue";
else boardColor = "green";
// console.log("previewBoard", buffer, controller.sysInfo)

let ctx = canvasNode.getContext('2d');
//assuming 20 pins
let mW = controller.sysInfo.nrOfPins<=40?2:4; // nr of pin columns
let mH = controller.sysInfo.nrOfPins / mW; // pins per column
let pPL = Math.min(canvasNode.width / mW, canvasNode.height / (mH+2)); // pixels per LED (width of circle)
let bW = pPL*10;
let bH = mH * pPL;
let lOf = Math.floor((canvasNode.width - bW) / 2); //left offset (to center matrix)
// let i = 5;

let pos = {};

ctx.clearRect(0, 0, canvasNode.width, canvasNode.height);

pos.x = lOf; pos.y = pPL;
//board
ctx.beginPath();
ctx.fillStyle = boardColor;
ctx.fillRect(pos.x, pos.y, bW, bH);

//wifi
ctx.fillStyle = "darkBlue";
if (mW == 2)
ctx.fillRect(pos.x + 1.5*pPL, 0, pPL * 7, pPL * 3);
else
ctx.fillRect(pos.x + 2.5*pPL, 0, pPL * 5, pPL * 3);

//cpu
ctx.fillStyle = "gray";
if (mW == 2)
ctx.fillRect(pos.x + 1.5*pPL, pos.y + 3*pPL, pPL * 7, pPL * 7);
else
ctx.fillRect(pos.x + 2.5*pPL, pos.y + 3*pPL, pPL * 5, pPL * 5);


//esp32 text
ctx.beginPath();
ctx.font = pPL *1.5 + "px serif";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText(controller.sysInfo.board, pos.x + 5*pPL, pos.y + 6 * pPL);

//chip
if (mW == 2) { //no space if 4 columns
ctx.fillStyle = "black";
ctx.fillRect(pos.x + 6 * pPL, pos.y + 12*pPL, pPL * 2, pPL * 5);
}

//usb
ctx.fillStyle = "grey";
ctx.fillRect(pos.x + 3.5 * pPL, bH - pPL, pPL * 3, pPL * 3);

let index = 0;
for (let x = 0; x < mW; x++) {
for (let y = 0; y < mH; y++) {
let pinType = controller.sysInfo.pinTypes[index];
let pinColor = [];
switch (pinType) {
case pinTypeIO:
pinColor = [78,173,50]; // green
break;
case pinTypeReadOnly:
pinColor = [218,139,49];//"orange";
break;
case pinTypeReserved:
pinColor = [156,50,246];//"purple";
break;
default:
pinColor = [192,48,38];//"red";
}
ctx.fillStyle = `rgba(${pinColor[0]},${pinColor[1]},${pinColor[2]},${buffer[index + 5]})`;
pos.y = (y+0.5)*pPL + pPL;
ctx.beginPath();
if ((x == 0 && mW == 2) || (x <= 1 && mW == 4))
pos.x = (x+0.5)*pPL + lOf;
else if (mW == 2)
pos.x = (x+0.5)*pPL + lOf + 8 * pPL;
else
pos.x = (x+0.5)*pPL + lOf + 6 * pPL;
ctx.arc(pos.x, pos.y, pPL * 0.4, 0, 2 * Math.PI);
ctx.fill();

ctx.beginPath();
ctx.font = pPL*0.5 + "px serif";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText(index, pos.x, pos.y + pPL / 4);

index++;
}
}
} //previewBoard

} //class Modules

//see this.otherMethodInThisClass, should be moved in Modules class
//creates the right class based on variable.type
function varJsonToClass(variable) {
switch (variable.type) {
case "button":
return new ButtonVariable(variable);
case "progress":
return new ProgressVariable(variable);
case "text":
return new TextVariable(variable);
case "select":
return new SelectVariable(variable);
case "checkbox":
return new CheckboxVariable(variable);
default:
case "button":
return new ButtonVariable(variable);
case "select":
return new SelectVariable(variable);
case "progress":
return new ProgressVariable(variable);
case "canvas":
return new CanvasVariable(variable);
default:
return new Variable(variable);
}
}
Expand Down Expand Up @@ -215,4 +322,16 @@ class ProgressVariable extends Variable {
return super.createHTML(`<progress max="${this.variable.max}" id="${this.variable.id}" class="progress"></progress>`)
}

} //class ProgressVariable
} //class ProgressVariable

class CanvasVariable extends Variable {

createHTML() { //base
return super.createHTML(`<canvas id=${this.variable.id} class="${this.variable.type}" value="${this.variable.value}"></canvas>`);
}

generateData() {
super.generateData(`"value":"${Math.random().toString(36).slice(2)}"`)
}

} //class CanvasVariable
Loading

0 comments on commit 94107f9

Please sign in to comment.