Remove "strip" dependency in Segment class

This commit is contained in:
Blaz Kristan 2022-12-16 22:31:07 +01:00
parent 8619e8fc0b
commit 3c5838cafd
6 changed files with 43 additions and 43 deletions

View File

@ -369,9 +369,10 @@ typedef struct Segment {
uint32_t call; // call counter uint32_t call; // call counter
uint16_t aux0; // custom var uint16_t aux0; // custom var
uint16_t aux1; // custom var uint16_t aux1; // custom var
byte* data; byte* data; // effect data pointer
CRGB* leds; CRGB* leds; // local leds[] array (may be a pointer to global)
static CRGB *_globalLeds; static CRGB *_globalLeds; // global leds[] array
static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions)
private: private:
union { union {
@ -657,8 +658,6 @@ class WS2812FX { // 96 bytes
vPanels(1), vPanels(1),
panelH(8), panelH(8),
panelW(8), panelW(8),
matrixWidth(DEFAULT_LED_COUNT),
matrixHeight(1),
matrix{0,0,0,0}, matrix{0,0,0,0},
panel{{0,0,0,0}}, panel{{0,0,0,0}},
#endif #endif
@ -814,9 +813,7 @@ class WS2812FX { // 96 bytes
uint16_t uint16_t
panelH, panelH,
panelW, panelW;
matrixWidth,
matrixHeight;
typedef struct panel_bitfield_t { typedef struct panel_bitfield_t {
bool bottomStart : 1; // starts at bottom? bool bottomStart : 1; // starts at bottom?

View File

@ -29,8 +29,8 @@
// setUpMatrix() - constructs ledmap array from matrix of panels with WxH pixels // setUpMatrix() - constructs ledmap array from matrix of panels with WxH pixels
// this converts physical (possibly irregular) LED arrangement into well defined // this converts physical (possibly irregular) LED arrangement into well defined
// array of logical pixels: fist entry corresponds to left-topmost logical pixel // array of logical pixels: fist entry corresponds to left-topmost logical pixel
// followed by horizontal pixels, when matrixWidth logical pixels are added they // followed by horizontal pixels, when Segment::maxWidth logical pixels are added they
// are followed by next row (down) of matrixWidth pixels (and so forth) // are followed by next row (down) of Segment::maxWidth pixels (and so forth)
// note: matrix may be comprised of multiple panels each with different orientation // note: matrix may be comprised of multiple panels each with different orientation
// but ledmap takes care of that. ledmap is constructed upon initialization // but ledmap takes care of that. ledmap is constructed upon initialization
// so matrix should disable regular ledmap processing // so matrix should disable regular ledmap processing
@ -41,19 +41,20 @@ void WS2812FX::setUpMatrix() {
customMappingTable = nullptr; customMappingTable = nullptr;
customMappingSize = 0; customMappingSize = 0;
// isMatrix is set in cfg.cpp or set.cpp
if (isMatrix) { if (isMatrix) {
matrixWidth = hPanels * panelW; Segment::maxWidth = hPanels * panelW;
matrixHeight = vPanels * panelH; Segment::maxHeight = vPanels * panelH;
// safety check // safety check
if (matrixWidth * matrixHeight > MAX_LEDS) { if (Segment::maxWidth * Segment::maxHeight > MAX_LEDS || Segment::maxWidth == 1 || Segment::maxHeight == 1) {
matrixWidth = _length; Segment::maxWidth = _length;
matrixHeight = 1; Segment::maxHeight = 1;
isMatrix = false; isMatrix = false;
return; return;
} }
customMappingSize = matrixWidth * matrixHeight; customMappingSize = Segment::maxWidth * Segment::maxHeight;
customMappingTable = new uint16_t[customMappingSize]; customMappingTable = new uint16_t[customMappingSize];
if (customMappingTable != nullptr) { if (customMappingTable != nullptr) {
@ -69,7 +70,7 @@ void WS2812FX::setUpMatrix() {
x = (matrix.vertical ? matrix.bottomStart : matrix.rightStart) ? h - i - 1 : i; x = (matrix.vertical ? matrix.bottomStart : matrix.rightStart) ? h - i - 1 : i;
x = matrix.serpentine && j%2 ? h - x - 1 : x; x = matrix.serpentine && j%2 ? h - x - 1 : x;
startL = (matrix.vertical ? y : x) * panelW + (matrix.vertical ? x : y) * matrixWidth * panelH; // logical index (top-left corner) startL = (matrix.vertical ? y : x) * panelW + (matrix.vertical ? x : y) * Segment::maxWidth * panelH; // logical index (top-left corner)
startP = p * panelW * panelH; // physical index (top-left corner) startP = p * panelW * panelH; // physical index (top-left corner)
uint8_t H = panel[h*j + i].vertical ? panelW : panelH; uint8_t H = panel[h*j + i].vertical ? panelW : panelH;
@ -79,7 +80,7 @@ void WS2812FX::setUpMatrix() {
y = (panel[h*j + i].vertical ? panel[h*j + i].rightStart : panel[h*j + i].bottomStart) ? H - l - 1 : l; y = (panel[h*j + i].vertical ? panel[h*j + i].rightStart : panel[h*j + i].bottomStart) ? H - l - 1 : l;
x = (panel[h*j + i].vertical ? panel[h*j + i].bottomStart : panel[h*j + i].rightStart) ? W - k - 1 : k; x = (panel[h*j + i].vertical ? panel[h*j + i].bottomStart : panel[h*j + i].rightStart) ? W - k - 1 : k;
x = (panel[h*j + i].serpentine && l%2) ? (W - x - 1) : x; x = (panel[h*j + i].serpentine && l%2) ? (W - x - 1) : x;
offset = (panel[h*j + i].vertical ? y : x) + (panel[h*j + i].vertical ? x : y) * matrixWidth; offset = (panel[h*j + i].vertical ? y : x) + (panel[h*j + i].vertical ? x : y) * Segment::maxWidth;
customMappingTable[startL + offset] = startP + q; customMappingTable[startL + offset] = startP + q;
} }
} }
@ -88,22 +89,22 @@ void WS2812FX::setUpMatrix() {
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
DEBUG_PRINT(F("Matrix ledmap:")); DEBUG_PRINT(F("Matrix ledmap:"));
for (uint16_t i=0; i<customMappingSize; i++) { for (uint16_t i=0; i<customMappingSize; i++) {
if (!(i%matrixWidth)) DEBUG_PRINTLN(); if (!(i%Segment::maxWidth)) DEBUG_PRINTLN();
DEBUG_PRINTF("%4d,", customMappingTable[i]); DEBUG_PRINTF("%4d,", customMappingTable[i]);
} }
DEBUG_PRINTLN(); DEBUG_PRINTLN();
#endif #endif
} else { } else {
// memory allocation error // memory allocation error
matrixWidth = _length; Segment::maxWidth = _length;
matrixHeight = 1; Segment::maxHeight = 1;
isMatrix = false; isMatrix = false;
return; return;
} }
} else { } else {
// not a matrix set up // not a matrix set up
matrixWidth = _length; Segment::maxWidth = _length;
matrixHeight = 1; Segment::maxHeight = 1;
} }
#endif #endif
} }
@ -113,7 +114,7 @@ void IRAM_ATTR WS2812FX::setPixelColorXY(int x, int y, uint32_t col)
{ {
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (!isMatrix) return; // not a matrix set-up if (!isMatrix) return; // not a matrix set-up
uint16_t index = y * matrixWidth + x; uint16_t index = y * Segment::maxWidth + x;
if (index >= customMappingSize) return; // customMappingSize is always W * H of matrix in 2D setup if (index >= customMappingSize) return; // customMappingSize is always W * H of matrix in 2D setup
#else #else
uint16_t index = x; uint16_t index = x;
@ -126,7 +127,7 @@ void IRAM_ATTR WS2812FX::setPixelColorXY(int x, int y, uint32_t col)
// returns RGBW values of pixel // returns RGBW values of pixel
uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) { uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) {
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
uint16_t index = (y * matrixWidth + x); uint16_t index = (y * Segment::maxWidth + x);
if (index >= customMappingSize) return 0; // customMappingSize is always W * H of matrix in 2D setup if (index >= customMappingSize) return 0; // customMappingSize is always W * H of matrix in 2D setup
#else #else
uint16_t index = x; uint16_t index = x;
@ -151,7 +152,7 @@ uint16_t IRAM_ATTR Segment::XY(uint16_t x, uint16_t y) {
void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col) void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col)
{ {
if (!strip.isMatrix) return; // not a matrix set-up if (Segment::maxHeight==1) return; // not a matrix set-up
if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return; // if pixel would fall out of virtual segment just exit if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return; // if pixel would fall out of virtual segment just exit
if (leds) leds[XY(x,y)] = col; if (leds) leds[XY(x,y)] = col;
@ -198,7 +199,7 @@ void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col)
// anti-aliased version of setPixelColorXY() // anti-aliased version of setPixelColorXY()
void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa) void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa)
{ {
if (!strip.isMatrix) return; // not a matrix set-up if (Segment::maxHeight==1) return; // not a matrix set-up
if (x<0.0f || x>1.0f || y<0.0f || y>1.0f) return; // not normalized if (x<0.0f || x>1.0f || y<0.0f || y>1.0f) return; // not normalized
const uint16_t cols = virtualWidth(); const uint16_t cols = virtualWidth();

View File

@ -75,6 +75,8 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for their data[] uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for their data[]
CRGB *Segment::_globalLeds = nullptr; CRGB *Segment::_globalLeds = nullptr;
uint16_t Segment::maxWidth = DEFAULT_LED_COUNT;
uint16_t Segment::maxHeight = 1;
// copy constructor // copy constructor
Segment::Segment(const Segment &orig) { Segment::Segment(const Segment &orig) {
@ -192,7 +194,7 @@ void Segment::setUpLeds() {
// deallocation happens in resetIfRequired() as it is called when segment changes or in destructor // deallocation happens in resetIfRequired() as it is called when segment changes or in destructor
if (Segment::_globalLeds) if (Segment::_globalLeds)
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
leds = &Segment::_globalLeds[start + startY*strip.matrixWidth]; // TODO: remove this hack leds = &Segment::_globalLeds[start + startY*Segment::maxWidth];
#else #else
leds = &Segment::_globalLeds[start]; leds = &Segment::_globalLeds[start];
#endif #endif
@ -497,7 +499,7 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
if (i >= virtualLength() || i<0) return; // if pixel would fall out of segment just exit if (i >= virtualLength() || i<0) return; // if pixel would fall out of segment just exit
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (is2D()) { // if this does not work use strip.isMatrix if (is2D()) {
uint16_t vH = virtualHeight(); // segment height in logical pixels uint16_t vH = virtualHeight(); // segment height in logical pixels
uint16_t vW = virtualWidth(); uint16_t vW = virtualWidth();
switch (map1D2D) { switch (map1D2D) {
@ -530,7 +532,7 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
break; break;
} }
return; return;
} else if (strip.isMatrix && (width()==1 || height()==1)) { // TODO remove this hack } else if (Segment::maxHeight!=1 && (width()==1 || height()==1)) {
// we have a vertical or horizontal 1D segment (WARNING: virtual...() may be transposed) // we have a vertical or horizontal 1D segment (WARNING: virtual...() may be transposed)
int x = 0, y = 0; int x = 0, y = 0;
if (virtualHeight()>1) y = i; if (virtualHeight()>1) y = i;
@ -618,7 +620,7 @@ uint32_t Segment::getPixelColor(int i)
i &= 0xFFFF; i &= 0xFFFF;
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (is2D()) { // if this does not work use strip.isMatrix if (is2D()) {
uint16_t vH = virtualHeight(); // segment height in logical pixels uint16_t vH = virtualHeight(); // segment height in logical pixels
uint16_t vW = virtualWidth(); uint16_t vW = virtualWidth();
switch (map1D2D) { switch (map1D2D) {
@ -1333,10 +1335,10 @@ void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping,
} }
if (isMatrix) { if (isMatrix) {
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (i1 < matrixWidth) seg.start = i1; if (i1 < Segment::maxWidth) seg.start = i1;
seg.stop = i2 > matrixWidth ? matrixWidth : i2; seg.stop = i2 > Segment::maxWidth ? Segment::maxWidth : i2;
if (startY < matrixHeight) seg.startY = startY; if (startY < Segment::maxHeight) seg.startY = startY;
seg.stopY = stopY > matrixHeight ? matrixHeight : MAX(1,stopY); seg.stopY = stopY > Segment::maxHeight ? Segment::maxHeight : MAX(1,stopY);
#endif #endif
} else { } else {
if (i1 < _length) seg.start = i1; if (i1 < _length) seg.start = i1;
@ -1360,7 +1362,7 @@ void WS2812FX::restartRuntime() {
void WS2812FX::resetSegments() { void WS2812FX::resetSegments() {
_segments.clear(); // destructs all Segment as part of clearing _segments.clear(); // destructs all Segment as part of clearing
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
segment seg = isMatrix ? Segment(0, matrixWidth, 0, matrixHeight) : Segment(0, _length); segment seg = isMatrix ? Segment(0, Segment::maxWidth, 0, Segment::maxHeight) : Segment(0, _length);
#else #else
segment seg = Segment(0, _length); segment seg = Segment(0, _length);
#endif #endif
@ -1376,9 +1378,9 @@ void WS2812FX::makeAutoSegments(bool forceReset) {
else if (getActiveSegmentsNum() == 1) { else if (getActiveSegmentsNum() == 1) {
size_t i = getLastActiveSegmentId(); size_t i = getLastActiveSegmentId();
_segments[i].start = 0; _segments[i].start = 0;
_segments[i].stop = matrixWidth; _segments[i].stop = Segment::maxWidth;
_segments[i].startY = 0; _segments[i].startY = 0;
_segments[i].stopY = matrixHeight; _segments[i].stopY = Segment::maxHeight;
_segments[i].grouping = 1; _segments[i].grouping = 1;
_segments[i].spacing = 0; _segments[i].spacing = 0;
_mainSegment = i; _mainSegment = i;

View File

@ -565,8 +565,8 @@ void serializeInfo(JsonObject root)
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (strip.isMatrix) { if (strip.isMatrix) {
JsonObject matrix = leds.createNestedObject("matrix"); JsonObject matrix = leds.createNestedObject("matrix");
matrix["w"] = strip.matrixWidth; matrix["w"] = Segment::maxWidth;
matrix["h"] = strip.matrixHeight; matrix["h"] = Segment::maxHeight;
} }
#endif #endif

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2212130 #define VERSION 2212160
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG

View File

@ -161,8 +161,8 @@ bool sendLiveLedsWs(uint32_t wsClient)
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
if (strip.isMatrix) { if (strip.isMatrix) {
buffer[1] = 2; //version buffer[1] = 2; //version
buffer[2] = strip.matrixWidth; buffer[2] = Segment::maxWidth;
buffer[3] = strip.matrixHeight; buffer[3] = Segment::maxHeight;
} }
#endif #endif