Always repaint NPB buffer on brightness change

Fix bus re-init causing full brightness (every show() now attempts to set the brightness, bus will ignore this if it stays the same)
This commit is contained in:
cschwinne 2023-07-19 17:25:25 +02:00
parent 2fce15db94
commit 8ccf349458
3 changed files with 24 additions and 25 deletions

View File

@ -1231,7 +1231,7 @@ void WS2812FX::show(void) {
#endif #endif
uint8_t newBri = estimateCurrentAndLimitBri(); uint8_t newBri = estimateCurrentAndLimitBri();
if (newBri < _brightness) busses.setBrightness(newBri, true); // "repaint" all pixels busses.setBrightness(newBri); // "repaints" all pixels if brightness changed
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
sumCurrent += micros() - microsStart; sumCurrent += micros() - microsStart;
@ -1245,7 +1245,7 @@ void WS2812FX::show(void) {
// restore bus brightness to its original value // restore bus brightness to its original value
// this is done right after show, so this is only OK if LED updates are completed before show() returns // this is done right after show, so this is only OK if LED updates are completed before show() returns
// or async show has a separate buffer (ESP32 RMT and I2S are ok) // or async show has a separate buffer (ESP32 RMT and I2S are ok)
if (newBri < _brightness) busses.setBrightness(_brightness, true); if (newBri < _brightness) busses.setBrightness(_brightness);
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
sumMicros += micros() - microsStart; sumMicros += micros() - microsStart;
@ -1328,15 +1328,10 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) {
seg.freeze = false; seg.freeze = false;
} }
} }
if (direct) { // setting brightness with NeoPixelBusLg has no effect on already painted pixels,
// setting brightness with NeoPixelBusLg has no effect on already painted pixels, // so we need to force an update to existing buffer
// so we need to force an update to existing buffer busses.setBrightness(b);
// that would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() if (!direct) {
busses.setBrightness(b, true);
} else {
// setting brightness with NeoPixelBusLg has no effect on already painted pixels,
// so we need to redraw whole canvas for the change of brightness to take effect
busses.setBrightness(b);
unsigned long t = millis(); unsigned long t = millis();
if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) trigger(); //apply brightness change immediately if no refresh soon if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) trigger(); //apply brightness change immediately if no refresh soon
} }

View File

@ -157,7 +157,7 @@ bool BusDigital::canShow() {
return PolyBus::canShow(_busPtr, _iType); return PolyBus::canShow(_busPtr, _iType);
} }
void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) { void BusDigital::setBrightness(uint8_t b) {
if (_bri == b) return; if (_bri == b) return;
//Fix for turning off onboard LED breaking bus //Fix for turning off onboard LED breaking bus
#ifdef LED_BUILTIN #ifdef LED_BUILTIN
@ -168,14 +168,18 @@ void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) {
uint8_t prevBri = _bri; uint8_t prevBri = _bri;
Bus::setBrightness(b); Bus::setBrightness(b);
PolyBus::setBrightness(_busPtr, _iType, b); PolyBus::setBrightness(_busPtr, _iType, b);
if (!_buffering && updateNPBBuffer) {
uint16_t hwLen = _len; if (_buffering) return;
if (_type == TYPE_WS2812_1CH_X3) hwLen = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus
for (uint_fast16_t i = 0; i < hwLen; i++) { // must update/repaint every LED in the NeoPixelBus buffer to the new brightness
// use 0 as color order, actual order does not matter here as we just update the channel values as-is // the only case where repainting is unnecessary is when all pixels are set after the brightness change but before the next show
uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, 0),prevBri); // (which we can't rely on)
PolyBus::setPixelColor(_busPtr, _iType, i, c, 0); uint16_t hwLen = _len;
} if (_type == TYPE_WS2812_1CH_X3) hwLen = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus
for (uint_fast16_t i = 0; i < hwLen; i++) {
// use 0 as color order, actual order does not matter here as we just update the channel values as-is
uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, 0),prevBri);
PolyBus::setPixelColor(_busPtr, _iType, i, c, 0);
} }
} }
@ -583,9 +587,9 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) {
} }
} }
void BusManager::setBrightness(uint8_t b, bool updateBuffer) { void BusManager::setBrightness(uint8_t b) {
for (uint8_t i = 0; i < numBusses; i++) { for (uint8_t i = 0; i < numBusses; i++) {
busses[i]->setBrightness(b, updateBuffer); busses[i]->setBrightness(b);
} }
} }

View File

@ -124,7 +124,7 @@ class Bus {
virtual void setStatusPixel(uint32_t c) {} virtual void setStatusPixel(uint32_t c) {}
virtual void setPixelColor(uint16_t pix, uint32_t c) = 0; virtual void setPixelColor(uint16_t pix, uint32_t c) = 0;
virtual uint32_t getPixelColor(uint16_t pix) { return 0; } virtual uint32_t getPixelColor(uint16_t pix) { return 0; }
virtual void setBrightness(uint8_t b, bool updateBuffer = false) { _bri = b; }; virtual void setBrightness(uint8_t b) { _bri = b; };
virtual void cleanup() = 0; virtual void cleanup() = 0;
virtual uint8_t getPins(uint8_t* pinArray) { return 0; } virtual uint8_t getPins(uint8_t* pinArray) { return 0; }
virtual uint16_t getLength() { return _len; } virtual uint16_t getLength() { return _len; }
@ -202,7 +202,7 @@ class BusDigital : public Bus {
void show(); void show();
bool canShow(); bool canShow();
void setBrightness(uint8_t b, bool updateBuffer = false); void setBrightness(uint8_t b);
void setStatusPixel(uint32_t c); void setStatusPixel(uint32_t c);
void setPixelColor(uint16_t pix, uint32_t c); void setPixelColor(uint16_t pix, uint32_t c);
void setColorOrder(uint8_t colorOrder); void setColorOrder(uint8_t colorOrder);
@ -317,7 +317,7 @@ class BusManager {
bool canAllShow(); bool canAllShow();
void setStatusPixel(uint32_t c); void setStatusPixel(uint32_t c);
void setPixelColor(uint16_t pix, uint32_t c); void setPixelColor(uint16_t pix, uint32_t c);
void setBrightness(uint8_t b, bool updateBuffer = false); void setBrightness(uint8_t b);
void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); void setSegmentCCT(int16_t cct, bool allowWBCorrection = false);
uint32_t getPixelColor(uint16_t pix); uint32_t getPixelColor(uint16_t pix);