Skip to content

Commit

Permalink
LedFix generator! Preview2D shows led fixture as they are in reality!
Browse files Browse the repository at this point in the history
General: back to StarMod again 

app.js: preview2D: led position aware (wip)

index.htm: Three.js (3D) max-age !

AppModLeds: 
- ledFix: process lmJson values (WIP)
- lf generator consolidated into one object

SysModFiles:
- add deleteLedFixes temporary (move from model)
- dirToJson: add filter
- add removeFiles
  • Loading branch information
ewoudwijma committed Jul 20, 2023
1 parent 3bfd8e2 commit b3f70b0
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 313 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LedFix
# StarMod

Headstart for building ESP32 applications: printing, file management, persistent data, Wifi, Web, UI and system management works out of the box.

Expand Down
39 changes: 26 additions & 13 deletions data/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,39 @@ function preview2D(node, leds) {
let ctx = node.getContext('2d');
let mW = leds[0]; // matrix width
let mH = leds[1]; // matrix height
let pPL = Math.min(node.width / mW, node.height / mH); // pixels per LED (width of circle)
let pPL = Math.min(node.width / 16, node.height / 16); // pixels per LED (width of circle)
let pPL2 = Math.min(node.width / 120, node.height / 120); // pixels per LED (width of circle)
let lOf = Math.floor((node.width - pPL*mW)/2); //left offeset (to center matrix)
let i = 4;
ctx.clearRect(0, 0, node.width, node.height);
if (jsonValues.pview) { // && jsonValues.pview.map
if (jsonValues.pview.map) {
console.log(jsonValues.pview);
if (jsonValues.pview) { // && jsonValues.pview.leds
if (jsonValues.pview.leds) {
// console.log(jsonValues.pview.leds);
for (var led of jsonValues.pview.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]})`;
ctx.beginPath();
ctx.arc(led[0]*pPL2 + node.width/2, led[1]*pPL2+node.height/2, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
}
i+=3;
}
}
else
else {
console.log("preview2D unknown json", jsonValues.pview);
jsonValues.pview = null;
jsonValues.pview = null;
}
}
for (y=0.5;y<mH;y++) for (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]})`;
ctx.beginPath();
ctx.arc(x*pPL+lOf, y*pPL, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
else {
for (y=0.5;y<mH;y++) for (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]})`;
ctx.beginPath();
ctx.arc(x*pPL+lOf, y*pPL, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
}
i+=3;
}
i+=3;
}
}

Expand Down
7 changes: 4 additions & 3 deletions data/index.htm
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>LedFix by MoonModules</title>
<title>StarMod by MoonModules</title>
<meta charset="utf-8">
<link rel="stylesheet" href="index.css">
<script src="app.js"></script>
<script src="index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- https://www.foxy.io/blog/faster-pageloads-effectively-using-http-caching-cache-busting-and-a-cdn/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js" max-age=86400></script>
</head>
<body onload="onLoad()">
<h1>LedFix by MoonModules</h1>
<h1>StarMod by MoonModules</h1>
<div class="container">
<div id="column0" class = "column">
</div>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "LedFix",
"name": "StarMod",
"version": "0.0.1",
"description": "Tools for LedFix",
"description": "Tools for StarMod",
"main": "tools/cdata.js",
"directories": {
"lib": "lib",
Expand Down
101 changes: 82 additions & 19 deletions src/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,29 @@ class AppModLeds:public Module {
buffer[3] = max(nrOfLeds * web->ws->count()/200, 16U); //interval in ms * 10, not too fast
});

ui->initDropdown(parentObject, "ledfix", 0, [](JsonObject object) { //uiFun
ui->initDropdown(parentObject, "ledFix", 0, [](JsonObject object) { //uiFun
web->addResponse(object, "label", "LedFix");
JsonArray lov = web->addResponseArray(object, "lov");
files->dirToJson2(lov);
files->dirToJson2(lov, "lf"); //only files containing lf
}, [](JsonObject object) { //chFun
print->print("%s Change %s to %d\n", "initDropdown chFun", object["id"].as<const char *>(), object["value"].as<int>());
StaticJsonDocument<2048> lmJson;
StaticJsonDocument<4096> lmJson;
char temp[30] = "/"; //add root prefix
strcat(temp, files->seqNrToName(object["value"].as<int>()));
files->readObjectFromFile(temp, &lmJson);

//change the setup based on the json file
nrOfLeds = lmJson["leds"].size();
mdl->setValue("nrOfLeds", nrOfLeds);
width = nrOfLeds;
height = 1;
depth = 1;

//send the json to pview...
JsonVariant responseVariant = (strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) != 0?web->responseDoc0:web->responseDoc1)->as<JsonVariant>();
(strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) != 0?web->responseDoc0:web->responseDoc1)->clear();
responseVariant["pview"]["json"] = lmJson;
print->printJson("ledfix", responseVariant);
print->printJson("ledFix", responseVariant);
web->sendDataWs(responseVariant);
print->print("ledfix send ws done\n");
});
Expand Down Expand Up @@ -329,25 +337,80 @@ class AppModLeds:public Module {
print->print("Set data pin to %d\n", object["value"].as<int>());
});

ui->initButton(parentObject, "createR35LedFix", "createR35LedFix", nullptr, [](JsonObject object) {
StaticJsonDocument<2048> lmJson;
ui->initDropdown(parentObject, "ledFixGen", 3, [](JsonObject object) { //uiFun
web->addResponse(object, "label", "Ledfix generator");
JsonArray lov = web->addResponseArray(object, "lov");
lov.add("R24"); //0
lov.add("R35"); //1
lov.add("M88"); //2
}, [](JsonObject object) { //chFun
StaticJsonDocument<4096> lmJson; //2048 is not even enough for 64 leds!!!!
JsonObject lmObject = lmJson.to<JsonObject>();
lmObject["name"] = "R35";
lmObject["scale"] = "mm";
lmObject["map"] = lmJson.createNestedArray();
size_t fix = object["value"];

const char * name = "TT";
uint16_t nrOfLeds = 30; //default
uint16_t diameter = 100; //in mm
for (int i=0; i<35; i++) {
float radians = i*360/35 * (M_PI / 180);
int16_t x = sinf(radians) * diameter;
int16_t y = cosf(radians) * diameter;
print->print("createRingLedMap %d %d %d\n", i, x, y);
JsonArray array2 = lmObject["map"].createNestedArray();
array2.add(x);
array2.add(y);

switch (fix) {
case 0: //R24
name = "R24";
lmObject["name"] = name;
nrOfLeds = 24;
case 1: //R35
diameter = 100; //in mm

if (fix == 1) {
name = "R35";
lmObject["name"] = name;
nrOfLeds = 35;
}

lmObject["scale"] = "mm";
lmObject["size"] = diameter;
lmObject["pin"] = 16;
lmObject["leds"] = lmJson.createNestedArray();

for (int i=0; i<nrOfLeds; i++) {
float radians = i*360/nrOfLeds * (M_PI / 180);
int16_t x = sinf(radians) * diameter/2;
int16_t y = cosf(radians) * diameter/2;
// print->print("createR24LedFix %d %d %d\n", i, x, y);
JsonArray array2 = lmObject["leds"].createNestedArray();
array2.add(x);
array2.add(y);
}
break;
case 2: //M88
name = "M88";
diameter = 80; //in mm
nrOfLeds = 64;


lmObject["name"] = name;
lmObject["scale"] = "mm";
lmObject["size"] = diameter;
lmObject["pin"] = 16;
lmObject["leds"] = lmJson.createNestedArray();

for (int y = 0; y<8; y++)
for (int x = 0; x<8 ; x++) {
// print->print("createM88LedFix %d %d %d\n", i, x, y);
JsonArray array2 = lmObject["leds"].createNestedArray();
array2.add(x*10);
array2.add(y*10);
}
break;
}
files->writeObjectToFile("/lfR35.json", &lmJson);
print->printJson("ring", lmJson);

char fileName[30] = "/lf";
strcat(fileName, name);
strcat(fileName, ".json");
files->writeObjectToFile(fileName, &lmJson);
print->printJson(fileName, lmJson);

//reload ledfix dropdown
ui->processUiFun("ledFix");
});

effects.push_back(new RainbowEffect);
Expand Down
60 changes: 44 additions & 16 deletions src/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ void SysModFiles::setup() {
web->addResponse(object, "label", "Total FS size");
});

ui->initButton(parentObject, "deleteLedFixes", "deleteLedFixes", [](JsonObject object) { //uiFun
web->addResponse(object, "comment", "temporary");
}, [](JsonObject object) {
print->print("delete deleteLedFixes json\n");
files->removeFiles("lm1");
files->removeFiles("lf");
});

web->addUpload("/upload");

}
Expand All @@ -66,6 +74,7 @@ void SysModFiles::loop(){

bool SysModFiles::remove(const char * path) {
filesChanged = true;
print->print("File remove %s\n", path);
return LittleFS.remove(path);
}

Expand All @@ -82,56 +91,55 @@ File SysModFiles::open(const char * path, const char * mode, const bool create)
}

// https://techtutorialsx.com/2019/02/24/esp32-arduino-listing-files-in-a-spiffs-file-system-specific-path/
void SysModFiles::dirToJson(JsonArray array) {
void SysModFiles::dirToJson(JsonArray array, const char * filter) {
File root = LittleFS.open("/");

File file = root.openNextFile();

while (file) {

JsonArray row = array.createNestedArray();
row.add((char *)file.name()); //create a copy!
row.add(file.size());

Serial.printf("FILE: %s %d\n", file.name(), file.size());
if (filter == nullptr || strstr(file.name(), filter) != nullptr) {
JsonArray row = array.createNestedArray();
row.add((char *)file.name()); //create a copy!
row.add(file.size());
Serial.printf("FILE: %s %d\n", file.name(), file.size());
}

file.close();

file = root.openNextFile();
}

root.close();
}

void SysModFiles::dirToJson2(JsonArray array) {
void SysModFiles::dirToJson2(JsonArray array, const char * filter) {
File root = LittleFS.open("/");

File file = root.openNextFile();

while (file) {

// array.add(print->fFormat("hoi %s", "file.name()"));
array.add((char *)file.name());

Serial.printf("FILE: %s %d\n", file.name(), file.size());
if (filter == nullptr || strstr(file.name(), filter) != nullptr) {
array.add((char *)file.name());
Serial.printf("FILE: %s %d\n", file.name(), file.size());
}

file.close();

file = root.openNextFile();
}

root.close();
}

void SysModFiles::filesChange() {
filesChanged = true;
}

char * SysModFiles::seqNrToName(size_t seqNr) {
File root = LittleFS.open("/");

File file = root.openNextFile();

size_t counter = 0;
while (file) {

if (counter == seqNr) {
Serial.printf("FILE: %s %d\n", file.name(), file.size());
// file.close();
Expand All @@ -143,6 +151,7 @@ char * SysModFiles::seqNrToName(size_t seqNr) {
file = root.openNextFile();
counter++;
}

root.close();
return nullptr;
}
Expand Down Expand Up @@ -182,3 +191,22 @@ bool SysModFiles::writeObjectToFile(const char* path, JsonDocument* dest) {
}
}

void SysModFiles::removeFiles(const char * filter) {
File root = LittleFS.open("/");
File file = root.openNextFile();

while (file) {
if (filter == nullptr || strstr(file.name(), filter) != nullptr) {
char fileName[30] = "/";
strcat(fileName, file.name());
file.close(); //close otherwise not removeable
remove(fileName);
}
else
file.close();

file = root.openNextFile();
}

root.close();
}
6 changes: 4 additions & 2 deletions src/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class SysModFiles:public Module {
void filesChange();

//get the file names and size in an array of arrays
static void dirToJson(JsonArray array);
static void dirToJson(JsonArray array, const char * filter = nullptr);

//get the file names in an array
static void dirToJson2(JsonArray array);
static void dirToJson2(JsonArray array, const char * filter = nullptr);

//get back the name of a file based on the sequence
static char * seqNrToName(size_t seqNr);
Expand All @@ -40,6 +40,8 @@ class SysModFiles:public Module {
//name is copied from WLED but better to call it readJsonFrom file
bool writeObjectToFile(const char* path, JsonDocument* dest);

void removeFiles(const char * filter = nullptr);

private:
static bool filesChanged;

Expand Down
5 changes: 0 additions & 5 deletions src/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ void SysModModel::setup() {
files->remove("/model.json");
});

ui->initButton(parentObject, "deleteLedMap", "DeleteLedMap", nullptr, [](JsonObject object) {
print->print("delete ledmap json\n");
files->remove("/ledmap1.json");
});

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

Expand Down
Loading

0 comments on commit b3f70b0

Please sign in to comment.