From d2705f033d5826f0b1ffeae9dab11e2d9373e7a0 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 28 Jun 2022 23:32:29 +0200 Subject: [PATCH] Enhance API - addPixelColorXY() - fadeToBlackBy() --- wled00/FX.cpp | 2 +- wled00/FX.h | 1 + wled00/FX_2Dfcn.cpp | 7 +++++++ wled00/FX_fcn.cpp | 10 +++++++--- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 4b506756..9d37d714 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5067,7 +5067,7 @@ uint16_t WS2812FX::mode_2DLissajous(void) { // By: Andrew Tuline const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - fadeToBlackBy(nullptr, SEGMENT.intensity); + fadeToBlackBy(SEGMENT.intensity); //fade_out(SEGMENT.intensity); //for (int i=0; i < 4*(cols+rows); i ++) { diff --git a/wled00/FX.h b/wled00/FX.h index 9e177537..0675a297 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -1097,6 +1097,7 @@ class WS2812FX { setPixelColorXY(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0), setPixelColorXY(float x, float y, byte r, byte g, byte b, byte w = 0, bool aa = false), blendPixelColorXY(uint16_t x, uint16_t y, uint32_t color, uint8_t blend), + addPixelColorXY(uint16_t x, uint16_t y, uint32_t color), blur1d(CRGB* leds, fract8 blur_amount), blur1d(uint16_t i, bool vertical, fract8 blur_amount, CRGB* leds=nullptr), // 1D box blur (with weight) blur2d(CRGB* leds, fract8 blur_amount), diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index e0e73f9c..eac97fd1 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -274,6 +274,13 @@ void WS2812FX::blendPixelColorXY(uint16_t x, uint16_t y, uint32_t color, uint8_t setPixelColorXY(x, y, color_blend(getPixelColorXY(x,y), color, blend)); } +/* + * Adds the specified color with the existing pixel color perserving color balance. + */ +void WS2812FX::addPixelColorXY(uint16_t x, uint16_t y, uint32_t color) { + setPixelColorXY(x, y, color_add(getPixelColorXY(x,y), color)); +} + // blurRow: perform a blur on a row of a rectangular matrix void WS2812FX::blurRow(uint16_t row, fract8 blur_amount, CRGB* leds) { const uint16_t cols = SEGMENT.virtualWidth(); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index d235c9d5..43aceb92 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -968,7 +968,7 @@ void WS2812FX::fade_out(uint8_t rate) { int g2 = G(color); int b2 = B(color); - for(uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) { + for (uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) { color = isMatrix ? getPixelColorXY(x, y) : getPixelColor(x); int w1 = W(color); int r1 = R(color); @@ -993,8 +993,12 @@ void WS2812FX::fade_out(uint8_t rate) { // fades all pixels to black using nscale8() void WS2812FX::fadeToBlackBy(uint8_t fadeBy) { - for (uint16_t i = 0; i < SEGLEN; i++) { - setPixelColor(i, col_to_crgb(getPixelColor(i)).nscale8(255-fadeBy)); + const uint16_t cols = isMatrix ? SEGMENT.virtualWidth() : SEGMENT.virtualLength(); + const uint16_t rows = SEGMENT.virtualHeight(); // will be 1 for 1D + + for (uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) { + if (isMatrix) setPixelColorXY(x, y, col_to_crgb(getPixelColorXY(x,y)).nscale8(255-fadeBy)); + else setPixelColor(x, col_to_crgb(getPixelColor(x)).nscale8(255-fadeBy)); } }