From 8067f1f70c9c773b60b4b4ccf376488052209fa0 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:49:09 +0200 Subject: [PATCH] bus.setPicelColor optimization: move colorBalanceFromKelvin into busmanager.cpp colorBalanceFromKelvin() is only called from inside bus_manager.cpp, so we can help the compiler optimize by making it a local (static) fuction --- wled00/bus_manager.cpp | 18 ++++++++++++++++++ wled00/colors.cpp | 3 +++ wled00/fcn_declare.h | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index e4e70507..73072e97 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -79,6 +79,24 @@ uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, byte #include "wled.h" #endif +// WLEDMM moved here (from colors.cpp) for better optimization +static inline uint32_t __attribute__((hot)) colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb) // WLEDMM: IRAM_ATTR removed, inline for speed +{ + //remember so that slow colorKtoRGB() doesn't have to run for every setPixelColor() + static byte correctionRGB[4] = {255,255,255,0}; // default to neutral + static uint16_t lastKelvin = 0; + if (lastKelvin != kelvin) { + colorKtoRGB(kelvin, correctionRGB); // convert Kelvin to RGB (slow) + lastKelvin = kelvin; + } + byte rgbw[4]; + rgbw[0] = ((uint_fast16_t) correctionRGB[0] * R(rgb)) /255; // correct R //WLEDMM changed to fast type + rgbw[1] = ((uint_fast16_t) correctionRGB[1] * G(rgb)) /255; // correct G + rgbw[2] = ((uint_fast16_t) correctionRGB[2] * B(rgb)) /255; // correct B + rgbw[3] = W(rgb); + return RGBW32(rgbw[0],rgbw[1],rgbw[2],rgbw[3]); +} + void ColorOrderMap::add(uint16_t start, uint16_t len, uint8_t colorOrder) { if (_count >= WLED_MAX_COLOR_ORDER_MAPPINGS) { diff --git a/wled00/colors.cpp b/wled00/colors.cpp index 37241959..8e72d46b 100644 --- a/wled00/colors.cpp +++ b/wled00/colors.cpp @@ -310,6 +310,8 @@ static float maxf (float v, float w) // WLEDMM better use standard library fmax // adjust RGB values based on color temperature in K (range [2800-10200]) (https://en.wikipedia.org/wiki/Color_balance) // called from bus manager when color correction is enabled! +#if 0 +// WLEDMM moved into bus_manager.cpp for better optimization uint32_t __attribute__((hot)) IRAM_ATTR_YN colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb) // WLEDMM: IRAM_ATTR_YN { //remember so that slow colorKtoRGB() doesn't have to run for every setPixelColor() @@ -324,6 +326,7 @@ uint32_t __attribute__((hot)) IRAM_ATTR_YN colorBalanceFromKelvin(uint16_t kelvi rgbw[3] = W(rgb); return RGBW32(rgbw[0],rgbw[1],rgbw[2],rgbw[3]); } +#endif //approximates a Kelvin color temperature from an RGB color. //this does no check for the "whiteness" of the color, diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 1a9247a2..188c10ee 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -66,7 +66,7 @@ void colorXYtoRGB(float x, float y, byte* rgb); // only defined if huesync disab void colorRGBtoXY(byte* rgb, float* xy); // only defined if huesync disabled TODO void colorFromDecOrHexString(byte* rgb, char* in); bool colorFromHexString(byte* rgb, const char* in); -uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb); +//uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb); // WLEDMM function moved into bus_manager.cpp for better optimization uint16_t __attribute__((const)) approximateKelvinFromRGB(uint32_t rgb); // WLEDMM: added attribute const void setRandomColor(byte* rgb); uint8_t gamma8_cal(uint8_t b, float gamma);