Fix for smaller number of pixeld than matrix size.

This commit is contained in:
Blaz Kristan 2023-03-11 15:03:28 +01:00
parent 763b64cc57
commit 7c186e4fcc
3 changed files with 21 additions and 12 deletions

View File

@ -718,7 +718,6 @@ class WS2812FX { // 96 bytes
finalizeInit(), finalizeInit(),
service(void), service(void),
setMode(uint8_t segid, uint8_t m), setMode(uint8_t segid, uint8_t m),
setColor(uint8_t slot, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0),
setColor(uint8_t slot, uint32_t c), setColor(uint8_t slot, uint32_t c),
setCCT(uint16_t k), setCCT(uint16_t k),
setBrightness(uint8_t b, bool direct = false), setBrightness(uint8_t b, bool direct = false),
@ -735,7 +734,8 @@ class WS2812FX { // 96 bytes
show(void), show(void),
setTargetFps(uint8_t fps); setTargetFps(uint8_t fps);
void fill(uint32_t c) { for (int i = 0; i < _length; i++) setPixelColor(i, c); } // fill whole strip with color (inline) void setColor(uint8_t slot, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0) { setColor(slot, RGBW32(r,g,b,w)); }
void fill(uint32_t c) { for (int i = 0; i < getLengthTotal(); i++) setPixelColor(i, c); } // fill whole strip with color (inline)
void addEffect(uint8_t id, mode_ptr mode_fn, const char *mode_name); // add effect to the list; defined in FX.cpp void addEffect(uint8_t id, mode_ptr mode_fn, const char *mode_name); // add effect to the list; defined in FX.cpp
void setupEffectData(void); // add default effects to the list; defined in FX.cpp void setupEffectData(void); // add default effects to the list; defined in FX.cpp
@ -783,17 +783,17 @@ class WS2812FX { // 96 bytes
ablMilliampsMax, ablMilliampsMax,
currentMilliamps, currentMilliamps,
getLengthPhysical(void), getLengthPhysical(void),
getLengthTotal(void), // will include virtual/nonexistent pixels in matrix
getFps(); getFps();
inline uint16_t getFrameTime(void) { return _frametime; } inline uint16_t getFrameTime(void) { return _frametime; }
inline uint16_t getMinShowDelay(void) { return MIN_SHOW_DELAY; } inline uint16_t getMinShowDelay(void) { return MIN_SHOW_DELAY; }
inline uint16_t getLengthTotal(void) { return _length; } inline uint16_t getLength(void) { return _length; } // 2D matrix may have less pixels than W*H
inline uint16_t getTransition(void) { return _transitionDur; } inline uint16_t getTransition(void) { return _transitionDur; }
uint32_t uint32_t
now, now,
timebase, timebase,
currentColor(uint32_t colorNew, uint8_t tNr),
getPixelColor(uint16_t); getPixelColor(uint16_t);
inline uint32_t getLastShow(void) { return _lastShow; } inline uint32_t getLastShow(void) { return _lastShow; }

View File

@ -161,12 +161,11 @@ 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 * Segment::maxWidth + x; uint16_t index = y * Segment::maxWidth + x;
if (index >= customMappingSize) return;
#else #else
uint16_t index = x; uint16_t index = x;
if (index >= _length) return;
#endif #endif
if (index < customMappingSize) index = customMappingTable[index]; if (index < customMappingSize) index = customMappingTable[index];
if (index >= _length) return;
busses.setPixelColor(index, col); busses.setPixelColor(index, col);
} }
@ -174,12 +173,11 @@ void /*IRAM_ATTR*/ WS2812FX::setPixelColorXY(int x, int y, uint32_t col)
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 * Segment::maxWidth + x); uint16_t index = (y * Segment::maxWidth + x);
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;
if (index >= _length) return 0;
#endif #endif
if (index < customMappingSize) index = customMappingTable[index]; if (index < customMappingSize) index = customMappingTable[index];
if (index >= _length) return 0;
return busses.getPixelColor(index); return busses.getPixelColor(index);
} }

View File

@ -551,7 +551,9 @@ uint16_t Segment::virtualLength() const {
void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
{ {
#ifndef WLED_DISABLE_2D
int vStrip = i>>16; // hack to allow running on virtual strips (2D segment columns/rows) int vStrip = i>>16; // hack to allow running on virtual strips (2D segment columns/rows)
#endif
i &= 0xFFFF; i &= 0xFFFF;
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
@ -691,7 +693,9 @@ void Segment::setPixelColor(float i, uint32_t col, bool aa)
uint32_t Segment::getPixelColor(int i) uint32_t Segment::getPixelColor(int i)
{ {
#ifndef WLED_DISABLE_2D
int vStrip = i>>16; int vStrip = i>>16;
#endif
i &= 0xFFFF; i &= 0xFFFF;
#ifndef WLED_DISABLE_2D #ifndef WLED_DISABLE_2D
@ -1049,7 +1053,7 @@ void WS2812FX::finalizeInit(void)
Segment::_globalLeds = nullptr; Segment::_globalLeds = nullptr;
} }
if (useLedsArray) { if (useLedsArray) {
size_t arrSize = sizeof(CRGB) * MAX(_length, Segment::maxWidth*Segment::maxHeight); size_t arrSize = sizeof(CRGB) * getLengthTotal();
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM)
if (psramFound()) if (psramFound())
Segment::_globalLeds = (CRGB*) ps_malloc(arrSize); Segment::_globalLeds = (CRGB*) ps_malloc(arrSize);
@ -1123,15 +1127,15 @@ void WS2812FX::service() {
void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col) void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col)
{ {
if (i >= _length) return;
if (i < customMappingSize) i = customMappingTable[i]; if (i < customMappingSize) i = customMappingTable[i];
if (i >= _length) return;
busses.setPixelColor(i, col); busses.setPixelColor(i, col);
} }
uint32_t WS2812FX::getPixelColor(uint16_t i) uint32_t WS2812FX::getPixelColor(uint16_t i)
{ {
if (i >= _length) return 0;
if (i < customMappingSize) i = customMappingTable[i]; if (i < customMappingSize) i = customMappingTable[i];
if (i >= _length) return 0;
return busses.getPixelColor(i); return busses.getPixelColor(i);
} }
@ -1344,6 +1348,12 @@ uint8_t WS2812FX::getActiveSegmentsNum(void) {
return c; return c;
} }
uint16_t WS2812FX::getLengthTotal(void) {
uint16_t len = Segment::maxWidth * Segment::maxHeight; // will be _length for 1D (see finalizeInit()) but should cover whole matrix for 2D
if (isMatrix && _length > len) len = _length; // for 2D with trailing strip
return len;
}
uint16_t WS2812FX::getLengthPhysical(void) { uint16_t WS2812FX::getLengthPhysical(void) {
uint16_t len = 0; uint16_t len = 0;
for (size_t b = 0; b < busses.getNumBusses(); b++) { for (size_t b = 0; b < busses.getNumBusses(); b++) {
@ -1568,7 +1578,8 @@ void WS2812FX::printSize() {
DEBUG_PRINTF("Modes: %d*%d=%uB\n", sizeof(mode_ptr), _mode.size(), (_mode.capacity()*sizeof(mode_ptr))); DEBUG_PRINTF("Modes: %d*%d=%uB\n", sizeof(mode_ptr), _mode.size(), (_mode.capacity()*sizeof(mode_ptr)));
DEBUG_PRINTF("Data: %d*%d=%uB\n", sizeof(const char *), _modeData.size(), (_modeData.capacity()*sizeof(const char *))); DEBUG_PRINTF("Data: %d*%d=%uB\n", sizeof(const char *), _modeData.size(), (_modeData.capacity()*sizeof(const char *)));
DEBUG_PRINTF("Map: %d*%d=%uB\n", sizeof(uint16_t), (int)customMappingSize, customMappingSize*sizeof(uint16_t)); DEBUG_PRINTF("Map: %d*%d=%uB\n", sizeof(uint16_t), (int)customMappingSize, customMappingSize*sizeof(uint16_t));
if (useLedsArray) DEBUG_PRINTF("Buffer: %d*%d=%uB\n", sizeof(CRGB), (int)_length, _length*sizeof(CRGB)); size = getLengthTotal();
if (useLedsArray) DEBUG_PRINTF("Buffer: %d*%u=%uB\n", sizeof(CRGB), size, size*sizeof(CRGB));
} }
#endif #endif