Skip to content

Commit

Permalink
Create Fixture class and move fixture/physical stuff here (From AppLeds)
Browse files Browse the repository at this point in the history
AppFixture
- New!!
- move NUM_LEDS_MAX, ledsP, nrOfLedsP, fixtureNr and size here
- declare Leds class pointer

AppLeds
- use fixture variables
- remove fixture variables
- declare fixture class pointer
- move Fixture stuff to AppModFixture

Rename AppModPreview to AppModFixture
- move fixture, size, count, fps to here
AppModLeds
- Constructor: cross link Leds and Fixture class variables
- use fixture variables
  • Loading branch information
ewowi committed Jan 29, 2024
1 parent 7cb8de2 commit c3c0f17
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 268 deletions.
45 changes: 45 additions & 0 deletions src/App/AppFixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
@title StarMod
@file AppFixture.h
@date 20240114
@repo https://github.com/ewowi/StarMod
@Authors https://github.com/ewowi/StarMod/commits/main
@Copyright (c) 2024 Github StarMod Commit Authors
@license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
@license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]
*/

// #pragma once

// #include "AppLeds.h"

#define NUM_LEDS_Max 4096

class Leds; //forward

class Fixture {

public:

CRGB ledsP[NUM_LEDS_Max];

// CRGB *leds = nullptr;
// if (!leds)
// leds = (CRGB*)calloc(nrOfLeds, sizeof(CRGB));
// else
// leds = (CRGB*)reallocarray(leds, nrOfLeds, sizeof(CRGB));
// if (leds) free(leds);
// leds = (CRGB*)malloc(nrOfLeds * sizeof(CRGB));
// leds = (CRGB*)reallocarray

uint16_t nrOfLeds = 64; //amount of physical leds
uint8_t fixtureNr = -1;
Coord3D size = {8,8,1};

Leds *leds;

Fixture() {
// leds->distance(1,2,3,4,5,6);
}

};
48 changes: 24 additions & 24 deletions src/App/AppLeds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
void Leds::fixtureProjectAndMap() {
char fileName[32] = "";

if (files->seqNrToName(fileName, fixtureNr)) {
if (files->seqNrToName(fileName, fixture->fixtureNr)) {
JsonRDWS jrdws(fileName); //open fileName for deserialize

mappingTableLedCounter = 0;
Expand Down Expand Up @@ -58,17 +58,17 @@ void Leds::fixtureProjectAndMap() {
// startPos.z = 0;
break;
case p_DistanceFromCenter:
// startPos.x = sizeP.x / 2;
// startPos.y = sizeP.y / 2;
// startPos.z = sizeP.z / 2;
// startPos.x = fixture->size.x / 2;
// startPos.y = fixture->size.y / 2;
// startPos.z = fixture->size.z / 2;
break;
}

//what to deserialize
jrdws.lookFor("width", &sizeP.x);
jrdws.lookFor("height", &sizeP.y);
jrdws.lookFor("depth", &sizeP.z);
jrdws.lookFor("nrOfLeds", &nrOfLedsP);
jrdws.lookFor("width", &fixture->size.x);
jrdws.lookFor("height", &fixture->size.y);
jrdws.lookFor("depth", &fixture->size.z);
jrdws.lookFor("nrOfLeds", &fixture->nrOfLeds);
jrdws.lookFor("pin", &currPin);

//lookFor leds array and for each item in array call lambdo to make a projection
Expand All @@ -79,9 +79,9 @@ void Leds::fixtureProjectAndMap() {
// USER_PRINTF("\n");

uint8_t fixtureDimension = 0;
if (sizeP.x>1) fixtureDimension++;
if (sizeP.y>1) fixtureDimension++;
if (sizeP.z>1) fixtureDimension++;
if (fixture->size.x>1) fixtureDimension++;
if (fixture->size.y>1) fixtureDimension++;
if (fixture->size.z>1) fixtureDimension++;

if (uint16CollectList.size()>=1 && fixtureDimension>=1 && fixtureDimension<=3) {

Expand All @@ -93,7 +93,7 @@ void Leds::fixtureProjectAndMap() {

if (x >= startPos.x && x <=endPos.x && y >= startPos.y && y <=endPos.y && z >= startPos.z && z <=endPos.z ) {

// USER_PRINTF("projectionNr p:%d f:%d s:%d, %d-%d-%d %d-%d-%d %d-%d-%d\n", projectionNr, effectDimension, fixtureDimension, x, y, z, uint16CollectList[0], uint16CollectList[1], uint16CollectList[2], sizeP.x, sizeP.y, sizeP.z);
// USER_PRINTF("projectionNr p:%d f:%d s:%d, %d-%d-%d %d-%d-%d %d-%d-%d\n", projectionNr, effectDimension, fixtureDimension, x, y, z, uint16CollectList[0], uint16CollectList[1], uint16CollectList[2], fixture->size.x, fixture->size.y, fixture->size.z);

//calculate the bucket to add to current physical led to
uint16_t bucket = UINT16_MAX;
Expand Down Expand Up @@ -141,8 +141,8 @@ void Leds::fixtureProjectAndMap() {
// USER_PRINTF("2D to 2D bucket %f %d %d x %d %d x %d\n", scale, bucket, x, y, size.x, size.y);
}
else if (fixtureDimension == _3D) {
size.x = sizeP.x + sizeP.y;
size.y = sizeP.z;
size.x = fixture->size.x + fixture->size.y;
size.y = fixture->size.z;
size.z = 1;
bucket = XY(x + y + 1, z);
// USER_PRINTF("2D to 3D bucket %d %d\n", bucket, size.x);
Expand All @@ -167,12 +167,12 @@ void Leds::fixtureProjectAndMap() {
xNew = round(((size.y-1.0-y)/(size.y-1.0) * xNew + size.x) / 2.0);
yNew = round(((size.y-1.0-y)/(size.y-1.0) * yNew + size.y) / 2.0);

USER_PRINTF(" %d,%d->%f,%f->%f,%f", x, y, sin(x * TWO_PI / (float)(sizeP.x-1)), cos(x * TWO_PI / (float)(sizeP.x-1)), xNew, yNew);
USER_PRINTF(" %d,%d->%f,%f->%f,%f", x, y, sin(x * TWO_PI / (float)(fixture->size.x-1)), cos(x * TWO_PI / (float)(fixture->size.x-1)), xNew, yNew);
x = xNew;
y = yNew;

size.x = sizeP.x;
size.y = sizeP.y;
size.x = fixture->size.x;
size.y = fixture->size.y;
size.z = 1;

bucket = XY(x, y);
Expand Down Expand Up @@ -201,7 +201,7 @@ void Leds::fixtureProjectAndMap() {
xNew = round(((size.y-1.0-y)/(size.y-1.0) * xNew + size.x) / 2.0);
yNew = round(((size.y-1.0-y)/(size.y-1.0) * yNew + size.y) / 2.0);

// USER_PRINTF(" %d,%d->%f,%f->%f,%f", x, y, sin(x * TWO_PI / (float)(sizeP.x-1)), cos(x * TWO_PI / (float)(sizeP.x-1)), xNew, yNew);
// USER_PRINTF(" %d,%d->%f,%f->%f,%f", x, y, sin(x * TWO_PI / (float)(size.x-1)), cos(x * TWO_PI / (float)(size.x-1)), xNew, yNew);

float distance = abs(bucket - xNew - yNew * size.x);

Expand Down Expand Up @@ -286,8 +286,8 @@ void Leds::fixtureProjectAndMap() {

if (projectionNr <= p_Random) {
//defaults
size = sizeP;
nrOfLeds = nrOfLedsP;
size = fixture->size;
nrOfLeds = fixture->nrOfLeds;
}

if (projectionNr > p_Random) {
Expand All @@ -308,15 +308,15 @@ void Leds::fixtureProjectAndMap() {
// }
}

USER_PRINTF("fixtureProjectAndMap P:%dx%dx%d V:%dx%dx%d and P:%d V:%d\n", sizeP.x, sizeP.y, sizeP.z, size.x, size.y, size.z, nrOfLedsP, nrOfLeds);
USER_PRINTF("fixtureProjectAndMap P:%dx%dx%d V:%dx%dx%d and P:%d V:%d\n", fixture->size.x, fixture->size.y, fixture->size.z, size.x, size.y, size.z, fixture->nrOfLeds, nrOfLeds);
mdl->setValue("fxSize", size);
mdl->setValue("fxCount", nrOfLeds);

mdl->setValue("fixSize", sizeP);
mdl->setValue("fixCount", nrOfLedsP);
mdl->setValue("fixSize", fixture->size);
mdl->setValue("fixCount", fixture->nrOfLeds);

} // if deserialize
} //if fileName
else
USER_PRINTF("fixtureProjectAndMap: Filename for fixture %d not found\n", fixtureNr);
USER_PRINTF("fixtureProjectAndMap: Filename for fixture %d not found\n", fixture->fixtureNr);
}
32 changes: 9 additions & 23 deletions src/App/AppLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "ArduinoJson.h"
#include "../Sys/SysModModel.h" //for Coord3D

#define NUM_LEDS_Max 4096
#include "AppFixture.h"

enum Projections
{
Expand All @@ -42,22 +42,8 @@ class Leds {

public:

//tbd: move ledsPhysical and nrOfLedsP out of Leds
// CRGB *leds = nullptr;
CRGB ledsPhysical[NUM_LEDS_Max];
// if (!leds)
// leds = (CRGB*)calloc(nrOfLeds, sizeof(CRGB));
// else
// leds = (CRGB*)reallocarray(leds, nrOfLeds, sizeof(CRGB));
// if (leds) free(leds);
// leds = (CRGB*)malloc(nrOfLeds * sizeof(CRGB));
// leds = (CRGB*)reallocarray

uint16_t nrOfLedsP = 64; //amount of physical leds
uint8_t fixtureNr = -1;
Coord3D sizeP = {8,8,1};
Fixture *fixture;


uint16_t nrOfLeds = 64; //amount of virtual leds (calculated by projection)

Coord3D size = {8,8,1};
Expand Down Expand Up @@ -131,22 +117,22 @@ class Leds {
if (indexV >= mappingTable.size()) return;
for (uint16_t indexP:mappingTable[indexV]) {
if (indexP < NUM_LEDS_Max)
ledsPhysical[indexP] = color;
fixture->ledsP[indexP] = color;
}
}
else //no projection
ledsPhysical[projectionNr==p_Random?random(nrOfLedsP):indexV] = color;
fixture->ledsP[projectionNr==p_Random?random(fixture->nrOfLeds):indexV] = color;
}

CRGB getPixelColor(int indexV) {
if (mappingTable.size()) {
if (indexV >= mappingTable.size()) return CRGB::Black;
if (!mappingTable[indexV].size() || mappingTable[indexV][0] > NUM_LEDS_Max) return CRGB::Black;

return ledsPhysical[mappingTable[indexV][0]]; //any would do as they are all the same
return fixture->ledsP[mappingTable[indexV][0]]; //any would do as they are all the same
}
else //no projection
return ledsPhysical[indexV];
return fixture->ledsP[indexV];
}

void addPixelColor(int indexV, CRGB color) {
Expand All @@ -159,7 +145,7 @@ class Leds {
for (index.x = startPos.x; index.x <= endPos.x; index.x++)
for (index.y = startPos.y; index.y <= endPos.y; index.y++)
for (index.z = startPos.z; index.z <= endPos.z; index.z++) {
ledsPhysical[index.x + index.y * sizeP.x + index.z * sizeP.x * sizeP.y].nscale8(255-fadeBy);
fixture->ledsP[index.x + index.y * fixture->size.x + index.z * fixture->size.x * fixture->size.y].nscale8(255-fadeBy);
}
}
void fill_solid(const struct CRGB& color) {
Expand All @@ -168,7 +154,7 @@ class Leds {
for (index.x = startPos.x; index.x <= endPos.x; index.x++)
for (index.y = startPos.y; index.y <= endPos.y; index.y++)
for (index.z = startPos.z; index.z <= endPos.z; index.z++) {
ledsPhysical[index.x + index.y * sizeP.x + index.z * sizeP.x * sizeP.y] = color;
fixture->ledsP[index.x + index.y * fixture->size.x + index.z * fixture->size.x * fixture->size.y] = color;
}
}

Expand All @@ -181,7 +167,7 @@ class Leds {
for (index.x = startPos.x; index.x <= endPos.x; index.x++)
for (index.y = startPos.y; index.y <= endPos.y; index.y++)
for (index.z = startPos.z; index.z <= endPos.z; index.z++) {
ledsPhysical[index.x + index.y * sizeP.x + index.z * sizeP.x * sizeP.y] = hsv;
fixture->ledsP[index.x + index.y * fixture->size.x + index.z * fixture->size.x * fixture->size.y] = hsv;
hsv.hue += deltahue;
}
}
Expand Down
106 changes: 106 additions & 0 deletions src/App/AppModFixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
@title StarMod
@file AppModFixture.h
@date 20240114
@repo https://github.com/ewowi/StarMod
@Authors https://github.com/ewowi/StarMod/commits/main
@Copyright (c) 2024 Github StarMod Commit Authors
@license GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
@license For non GPL-v3 usage, commercial licenses must be purchased. Contact [email protected]
*/

#include "SysModule.h"

class AppModFixture:public SysModule {

public:

AppModFixture() :SysModule("Fixture") {};

void setup() {
SysModule::setup();

parentVar = ui->initAppMod(parentVar, name);
if (parentVar["o"] > -1000) parentVar["o"] = -1000; //set default order. Don't use auto generated order as order can be changed in the ui (WIP)

ui->initCanvas(parentVar, "pview", UINT16_MAX, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Preview");
web->addResponse(var["id"], "comment", "Shows the fixture");
// web->addResponse(var["id"], "comment", "Click to enlarge");
}, nullptr, [this](JsonObject var, uint8_t rowNr) { //loopFun

var["interval"] = max(lds->fixture.nrOfLeds * web->ws->count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms

web->sendDataWs([this](AsyncWebSocketMessageBuffer * wsBuf) {
uint8_t* buffer;

buffer = wsBuf->get();

// send leds preview to clients
for (size_t i = 0; i < lds->fixture.nrOfLeds; i++)
{
buffer[i*3+5] = lds->fixture.ledsP[i].red;
buffer[i*3+5+1] = lds->fixture.ledsP[i].green;
buffer[i*3+5+2] = lds->fixture.ledsP[i].blue;
}
//new values
buffer[0] = 1; //userFun id
// buffer[1] = fixture.nrOfLeds/256;
// buffer[2] = fixture.nrOfLeds%256;
// buffer[4] = max(fixture.nrOfLeds * SysModWeb::ws->count()/200, 16U); //interval in ms * 10, not too fast

}, lds->fixture.nrOfLeds * 3 + 5, true);
});
ui->initSelect(parentVar, "fixture", lds->fixture.fixtureNr, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Fixture to display effect on");
JsonArray select = web->addResponseA(var["id"], "options");
files->dirToJson(select, true, "D"); //only files containing D (1D,2D,3D), alphabetically, only looking for D not very destinctive though

// ui needs to load the file also initially
char fileName[32] = "";
if (files->seqNrToName(fileName, var["value"])) {
web->addResponse("pview", "file", JsonString(fileName, JsonString::Copied));
}
}, [this](JsonObject var, uint8_t) { //chFun

lds->fixture.fixtureNr = var["value"];
lds->doMap = true;

char fileName[32] = "";
if (files->seqNrToName(fileName, lds->fixture.fixtureNr)) {
//send to pview a message to get file filename
JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse("pview", "file", JsonString(fileName, JsonString::Copied));
web->sendDataWs(responseVariant);
print->printJson("fixture chFun send ws done", responseVariant); //during server startup this is not send to a client, so client refresh should also trigger this
}
}); //fixture

ui->initCoord3D(parentVar, "fixSize", lds->fixture.size, 0, UINT16_MAX, true, [this](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Size");
// web->addResponse(var["id"], "value", fixture.size);
});

ui->initNumber(parentVar, "fixCount", lds->fixture.nrOfLeds, 0, UINT16_MAX, true, [this](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Count");
web->addResponseV(var["id"], "comment", "Max %d", NUM_LEDS_Max);
// web->addResponse(var["id"], "value", fixture.nrOfLeds);
});

ui->initNumber(parentVar, "fps", lds->fps, 1, 999, false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Frames per second");
}, [this](JsonObject var, uint8_t) { //chFun
lds->fps = var["value"];
});

ui->initText(parentVar, "realFps", nullptr, 10, true, [this](JsonObject var) { //uiFun
web->addResponseV(var["id"], "comment", "f(%d leds)", lds->fixture.nrOfLeds);
});

}
};

static AppModFixture *fix;
Loading

0 comments on commit c3c0f17

Please sign in to comment.