From 03ea9dc9263f0e3b296f16a2d7d0136139b22a9a Mon Sep 17 00:00:00 2001 From: EnricoGuccii Date: Sat, 30 May 2026 22:13:37 +0200 Subject: rgb driver fix --- lib/rgb/rgb.cpp | 37 +++++++++++++++++++++---------------- lib/rgb/rgb.h | 4 +++- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/rgb/rgb.cpp b/lib/rgb/rgb.cpp index 6dc3834..6c53be2 100644 --- a/lib/rgb/rgb.cpp +++ b/lib/rgb/rgb.cpp @@ -10,14 +10,7 @@ #define PWM_FREQ 5000 #define PWM_RES 10 -int RGB::scaleColor(uint8_t val) -{ - return map(val, 0, 255, 0, (1 << PWM_RES) - 1); -} -RGB::RGB() - : currentR(0), currentG(0), currentB(0) -{ -} +RGB::RGB() : currentR(0), currentG(0), currentB(0), brightness(255) {} void RGB::init() { @@ -31,7 +24,19 @@ void RGB::init() ledcAttachPin(BLUE_PIN, BLUE_CHANNEL); } -void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor = true) +void RGB::setBrightness(uint8_t br) +{ + brightness = br; + setColorRGB(currentR, currentG, currentB, true); +} + +int RGB::scaleColor(uint8_t val) +{ + uint32_t dimmed = (val * brightness) / 255; + return map(dimmed, 0, 255, 0, (1 << PWM_RES) - 1); +} + +void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor) { ledcWrite(RED_CHANNEL, scaleColor(red)); ledcWrite(GREEN_CHANNEL, scaleColor(green)); @@ -58,7 +63,7 @@ void RGB::fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration) uint8_t newB = startB + ((blue - startB) * i) / duration; setColorRGB(newR, newG, newB, false); - delay(1); + vTaskDelay(1 / portTICK_PERIOD_MS); } setColorRGB(red, green, blue); @@ -69,9 +74,9 @@ void RGB::blink(int cycles, int interval) for (int i = 0; i < cycles; i++) { setColorRGB(0, 0, 0, false); - delay(interval); + vTaskDelay(interval / portTICK_PERIOD_MS); setColorRGB(currentR, currentG, currentB, false); - delay(interval); + vTaskDelay(interval / portTICK_PERIOD_MS); } } @@ -90,14 +95,14 @@ void RGB::breathe(int cycles, int period) { float scale = (float)i / steps; setColorRGB(baseR * scale, baseG * scale, baseB * scale, false); - delay(stepDelay); + vTaskDelay(stepDelay / portTICK_PERIOD_MS); } for (int i = steps; i >= 0; --i) { float scale = (float)i / steps; setColorRGB(baseR * scale, baseG * scale, baseB * scale, false); - delay(stepDelay); + vTaskDelay(stepDelay / portTICK_PERIOD_MS); } } @@ -106,8 +111,8 @@ void RGB::breathe(int cycles, int period) void RGB::rainbowCycle(int duration, float speed) { - // speed (0.1 – slow, 1.0 – normal, 2.0 – fast) const int steps = 256; + int stepDelay = duration / steps; for (int i = 0; i < steps; i++) { @@ -117,7 +122,7 @@ void RGB::rainbowCycle(int duration, float speed) int blue = sin(angle + 4) * 127 + 128; setColorRGB(red, green, blue, false); - delay(duration / steps); + vTaskDelay(stepDelay / portTICK_PERIOD_MS); } setColorRGB(currentR, currentG, currentB, false); diff --git a/lib/rgb/rgb.h b/lib/rgb/rgb.h index 2c153f1..fa1cbd8 100644 --- a/lib/rgb/rgb.h +++ b/lib/rgb/rgb.h @@ -9,10 +9,12 @@ public: uint8_t currentR; uint8_t currentG; uint8_t currentB; + uint8_t brightness; RGB(); void init(); - void setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor); + void setBrightness(uint8_t br); + void setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor = true); void fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration); void blink(int cycles, int interval); void breathe(int cycles, int period); -- cgit v1.2.3