Skip to content

Commit

Permalink
Merge remote-tracking branch 'ewowi/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoudwijma committed Apr 11, 2024
2 parents 8aae28b + 84c1771 commit 556a156
Show file tree
Hide file tree
Showing 9 changed files with 1,636 additions and 1,490 deletions.
148 changes: 131 additions & 17 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ let jsonValues = {};
let uiFunCommands = [];
let model = []; //model.json (as send by the server), used by FindVar
let savedView = null;

//C++ equivalents
const UINT8_MAX = 255;
const UINT16_MAX = 256*256-1;

const pinTypeIO = 0;
const pinTypeReadOnly = 1;
const pinTypeReserved = 2;
const pinTypeSpi = 3;
const pinTypeInvalid = UINT8_MAX;
let sysInfo = {};
function getPinType(pinNr) {
if (sysInfo.pinTypes[pinNr] == pinTypeIO) return "🟒";
else if (sysInfo.pinTypes[pinNr] == pinTypeReadOnly) return "🟠";
else if (sysInfo.pinTypes[pinNr] == pinTypeReserved) return "🟣";
else return "πŸ”΄";
}

function gId(c) {return d.getElementById(c);}
function cE(e) { return d.createElement(e); }

Expand Down Expand Up @@ -215,6 +230,15 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
ndivNeeded = false;

varNode = cE("div");
let mdlName = findVar("mdlName");
if (mdlName) {
let index = mdlName.value.indexOf(variable.id); //find this module
if (index != -1) {
let mdlEnabled = findVar("mdlEnabled");
if (mdlEnabled)
varNode.hidden = !mdlEnabled.value[index]; //hidden if not enabled
}
}

let hgroupNode = cE("hgroup");

Expand Down Expand Up @@ -286,6 +310,13 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
else {
varNode = cE("select");
varNode.addEventListener('change', (event) => {console.log("select change", event);sendValue(event.target);});
}

if (variable.type == "pin") {
variable.options = [];
for (let index = 0; index < 40; index++) {
variable.options.push(index + " " + getPinType(index));
}
}

} else if (variable.type == "canvas") {
Expand Down Expand Up @@ -478,6 +509,9 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
if (variable.value)
changeHTML(variable, {"value":variable.value, "chk":"gen1"}, rowNr); //set only the value

if (variable.options) // eg for pin type
changeHTML(variable, {"options":variable.options, "chk":"gen1"}, rowNr); //set only the options

//call ui Functionality, if defined (to set label, comment, select etc)
if (variable.fun >= 0) { //>=0 as element in var
uiFunCommands.push(variable.id);
Expand Down Expand Up @@ -555,9 +589,6 @@ function receiveData(json) {
if (key == "uiFun") {
console.log("receiveData no action", key, value); //should not happen anymore
}
else if (key == "aiButton") {
console.log("receiveData", key, value);
}
else if (key == "view") {
console.log("receiveData", key, value);
changeHTMLView(value);
Expand Down Expand Up @@ -644,6 +675,9 @@ function receiveData(json) {
colNr++;
}

} else if (key == "sysInfo") { //update the row of a table
console.log("receiveData", key, value);
sysInfo = value;
} else { //{variable:{label:value:options:comment:}}

let variable = findVar(key);
Expand Down Expand Up @@ -837,8 +871,9 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
newValue = commandJson.value[newRowNr];
//hide/show disabled/enabled modules
if (variable.id == "mdlEnabled") {
let mdlNode = gId(findVar("mdlName").value[newRowNr]);
// console.log("mdlEnabled", variable, node, newValue, newRowNr, mdlNode);
let nameVar = findVar("mdlName");
let mdlNode = gId(nameVar.value[newRowNr]);
// console.log("mdlEnabled", variable, node, newValue, newRowNr, nameVar, mdlNode);
if (mdlNode) {
if (mdlNode.hidden && newValue) mdlNode.hidden = false;
if (!mdlNode.hidden && !newValue) mdlNode.hidden = true;
Expand Down Expand Up @@ -1482,13 +1517,13 @@ function saveModel(node) {

function setView(node) {
var command = {};
command["view"] = node.id;
command.view = node.id;
requestJson(command);
}

function setTheme(node) {
var command = {};
command["theme"] = node.value;
command.theme = node.value;
requestJson(command);
}

Expand All @@ -1498,21 +1533,100 @@ function getTheme() {
}

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

let ctx = canvasNode.getContext('2d');
//assuming 20 pins
let mW = 10; // matrix width
let mH = 2; // matrix height
let pPL = Math.min(canvasNode.width / mW, canvasNode.height / mH); // pixels per LED (width of circle)
let lOf = Math.floor((canvasNode.width - pPL*mW)/2); //left offeset (to center matrix)
let i = 5;
let mW = sysInfo.nrOfPins<=40?2:4; // nr of pin columns
let mH = 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);
for (let y=0.5;y<mH;y++) for (let x=0.5; x<mW; x++) {
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]})`;

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(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 = 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();
ctx.arc(x*pPL+lOf, y*pPL, pPL*0.4, 0, 2 * Math.PI);
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++;
}
i+=3;
}
}
37 changes: 0 additions & 37 deletions src/App/LedModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,43 +793,6 @@ class LedModFixtureGen:public SysModule {
ui->initPin(parentVar, "fixPin", 2, false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun: {
ui->setLabel(var, "Pin");

//tbd: move logic to pinMgr and create initPin
JsonArray options = ui->setOptions(var);

for (forUnsigned8 pinNr = 0; pinNr < NUM_DIGITAL_PINS; pinNr++) {
char text[32];
itoa(pinNr, text, 10);
if (digitalPinIsValid(pinNr)) {

#if defined(CONFIG_IDF_TARGET_ESP32S2)
if ((pinNr > 18 && pinNr < 21) || (pinNr > 21 && pinNr < 33)) strcat(text, " 🟣"); else
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
if ((pinNr > 18 && pinNr < 21) || (pinNr > 21 && pinNr < 33)) strcat(text, " 🟣"); else
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
if ((pinNr > 11 && pinNr < 18) || (pinNr > 17 && pinNr < 20)) strcat(text, " 🟣"); else
#elif defined(ESP32)
if (pinNr > 5 && pinNr < 12) strcat(text, " 🟣"); else
#else //???
#endif

if (!digitalPinCanOutput(pinNr))
strcat(text, " 🟠"); //read only
else
strcat(text, " 🟒"); //io

//results in crashes
// if (digitalPinToRtcPin(pinNr)) strcat(text, " 🟒"); else strcat(text, " πŸ”΄"); //error: 'RTC_GPIO_IS_VALID_GPIO' was not declared in this scope
// if (digitalPinToDacChannel(pinNr)) strcat(text, " 🟒"); else strcat(text, " πŸ”΄"); //error: 'DAC_CHANNEL_1_GPIO_NUM' was not declared in this scope

//not so relevant
// if (digitalPinToAnalogChannel(pinNr)) strcat(text, " 🟣");
// if (digitalPinToTouchChannel(pinNr)) strcat(text, " 🟀");
}
else
strcat(text, " πŸ”΄"); //not valid
options.add(JsonString(text, JsonString::Copied));
}
return true; }
case f_ChangeFun: {
//set remaining rows to same pin
Expand Down
10 changes: 6 additions & 4 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,18 @@ class SysModModel:public SysModule {

if (rowNr == UINT8_MAX) { //normal situation
if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
JsonString oldValue = JsonString(var["value"], JsonString::Copied);
if (!var["value"].isNull() && !varRO(var)) var["oldValue"] = var["value"];
var["value"] = value;
//trick to remove null values
if (var["value"].isNull() || var["value"].as<unsigned16>() == UINT16_MAX) {
var.remove("value");
if (oldValue.size()>0)
USER_PRINTF("dev setValue value removed %s %s\n", varID(var), oldValue.c_str()); //old value
USER_PRINTF("dev setValue value removed %s %s\n", varID(var), var["oldValue"].as<String>().c_str());
}
else {
USER_PRINTF("setValue changed %s %s -> %s\n", varID(var), oldValue.c_str(), var["value"].as<String>().c_str()); //old value
if (varRO(var))
USER_PRINTF("setValue changed %s %s\n", varID(var), var["value"].as<String>().c_str());
else
USER_PRINTF("setValue changed %s %s -> %s\n", varID(var), var["oldValue"].as<String>().c_str(), var["value"].as<String>().c_str());
web->addResponse(var["id"], "value", var["value"]);
changed = true;
}
Expand Down
Loading

0 comments on commit 556a156

Please sign in to comment.