From 975d443ccbb725b326c158573b63d2face104b8b Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Fri, 15 Nov 2024 19:48:32 -0500 Subject: [PATCH 1/5] Remove unnecessary nesting --- src/modules/ExternalNotificationModule.cpp | 167 ++++++++++----------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index 3ec6ff6902..eb05d064c3 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -82,120 +82,119 @@ int32_t ExternalNotificationModule::runOnce() { if (!moduleConfig.external_notification.enabled) { return INT32_MAX; // we don't need this thread here... - } else { + } - bool isPlaying = rtttl::isPlaying(); + bool isPlaying = rtttl::isPlaying(); #ifdef HAS_I2S - isPlaying = rtttl::isPlaying() || audioThread->isPlaying(); -#endif - if ((nagCycleCutoff < millis()) && !isPlaying) { - // let the song finish if we reach timeout - nagCycleCutoff = UINT32_MAX; - LOG_INFO("Turning off external notification: "); - for (int i = 0; i < 3; i++) { - setExternalState(i, false); - externalTurnedOn[i] = 0; - LOG_INFO("%d ", i); - } - LOG_INFO(""); + isPlaying = rtttl::isPlaying() || audioThread->isPlaying(); +#endif + if ((nagCycleCutoff < millis()) && !isPlaying) { + // let the song finish if we reach timeout + nagCycleCutoff = UINT32_MAX; + LOG_INFO("Turning off external notification: "); + for (int i = 0; i < 3; i++) { + setExternalState(i, false); + externalTurnedOn[i] = 0; + LOG_INFO("%d ", i); + } + LOG_INFO(""); #ifdef HAS_I2S - // GPIO0 is used as mclk for I2S audio and set to OUTPUT by the sound library - // T-Deck uses GPIO0 as trackball button, so restore the mode + // GPIO0 is used as mclk for I2S audio and set to OUTPUT by the sound library + // T-Deck uses GPIO0 as trackball button, so restore the mode #if defined(T_DECK) || (defined(BUTTON_PIN) && BUTTON_PIN == 0) - pinMode(0, INPUT); + pinMode(0, INPUT); #endif #endif - isNagging = false; - return INT32_MAX; // save cycles till we're needed again - } + isNagging = false; + return INT32_MAX; // save cycles till we're needed again + } - // If the output is turned on, turn it back off after the given period of time. - if (isNagging) { - if (externalTurnedOn[0] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms - : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < - millis()) { - setExternalState(0, !getExternal(0)); - } - if (externalTurnedOn[1] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms - : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < - millis()) { - setExternalState(1, !getExternal(1)); - } - if (externalTurnedOn[2] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms - : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < - millis()) { + // If the output is turned on, turn it back off after the given period of time. + if (isNagging) { + if (externalTurnedOn[0] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms + : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < + millis()) { + setExternalState(0, !getExternal(0)); + } + if (externalTurnedOn[1] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms + : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < + millis()) { + setExternalState(1, !getExternal(1)); + } + if (externalTurnedOn[2] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms + : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < + millis()) { LOG_DEBUG("EXTERNAL 2 %d compared to %d", externalTurnedOn[2]+moduleConfig.external_notification.output_ms, millis()); - setExternalState(2, !getExternal(2)); - } + setExternalState(2, !getExternal(2)); + } #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE) - red = (colorState & 4) ? brightnessValues[brightnessIndex] : 0; // Red enabled on colorState = 4,5,6,7 - green = (colorState & 2) ? brightnessValues[brightnessIndex] : 0; // Green enabled on colorState = 2,3,6,7 - blue = (colorState & 1) ? (brightnessValues[brightnessIndex] * 1.5) : 0; // Blue enabled on colorState = 1,3,5,7 + red = (colorState & 4) ? brightnessValues[brightnessIndex] : 0; // Red enabled on colorState = 4,5,6,7 + green = (colorState & 2) ? brightnessValues[brightnessIndex] : 0; // Green enabled on colorState = 2,3,6,7 + blue = (colorState & 1) ? (brightnessValues[brightnessIndex] * 1.5) : 0; // Blue enabled on colorState = 1,3,5,7 #ifdef HAS_NCP5623 - if (rgb_found.type == ScanI2C::NCP5623) { - rgb.setColor(red, green, blue); - } + if (rgb_found.type == ScanI2C::NCP5623) { + rgb.setColor(red, green, blue); + } #endif #ifdef RGBLED_CA - analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic - analogWrite(RGBLED_GREEN, 255 - green); - analogWrite(RGBLED_BLUE, 255 - blue); + analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic + analogWrite(RGBLED_GREEN, 255 - green); + analogWrite(RGBLED_BLUE, 255 - blue); #elif defined(RGBLED_RED) - analogWrite(RGBLED_RED, red); - analogWrite(RGBLED_GREEN, green); - analogWrite(RGBLED_BLUE, blue); + analogWrite(RGBLED_RED, red); + analogWrite(RGBLED_GREEN, green); + analogWrite(RGBLED_BLUE, blue); #endif #ifdef HAS_NEOPIXEL - pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT); - pixels.show(); + pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT); + pixels.show(); #endif #ifdef UNPHONE - unphone.rgb(red, green, blue); + unphone.rgb(red, green, blue); #endif - if (ascending) { // fade in - brightnessIndex++; - if (brightnessIndex == (sizeof(brightnessValues) - 1)) { - ascending = false; - } - } else { - brightnessIndex--; // fade out + if (ascending) { // fade in + brightnessIndex++; + if (brightnessIndex == (sizeof(brightnessValues) - 1)) { + ascending = false; } - if (brightnessIndex == 0) { - ascending = true; - colorState++; // next color - if (colorState > 7) { - colorState = 1; - } + } else { + brightnessIndex--; // fade out + } + if (brightnessIndex == 0) { + ascending = true; + colorState++; // next color + if (colorState > 7) { + colorState = 1; } + } #endif #ifdef T_WATCH_S3 - drv.go(); + drv.go(); #endif - } + } - // Play RTTTL over i2s audio interface if enabled as buzzer + // Play RTTTL over i2s audio interface if enabled as buzzer #ifdef HAS_I2S - if (moduleConfig.external_notification.use_i2s_as_buzzer) { - if (audioThread->isPlaying()) { - // Continue playing - } else if (isNagging && (nagCycleCutoff >= millis())) { - audioThread->beginRttl(rtttlConfig.ringtone, strlen_P(rtttlConfig.ringtone)); - } + if (moduleConfig.external_notification.use_i2s_as_buzzer) { + if (audioThread->isPlaying()) { + // Continue playing + } else if (isNagging && (nagCycleCutoff >= millis())) { + audioThread->beginRttl(rtttlConfig.ringtone, strlen_P(rtttlConfig.ringtone)); } + } #endif - // now let the PWM buzzer play - if (moduleConfig.external_notification.use_pwm && config.device.buzzer_gpio) { - if (rtttl::isPlaying()) { - rtttl::play(); - } else if (isNagging && (nagCycleCutoff >= millis())) { - // start the song again if we have time left - rtttl::begin(config.device.buzzer_gpio, rtttlConfig.ringtone); - } + // now let the PWM buzzer play + if (moduleConfig.external_notification.use_pwm && config.device.buzzer_gpio) { + if (rtttl::isPlaying()) { + rtttl::play(); + } else if (isNagging && (nagCycleCutoff >= millis())) { + // start the song again if we have time left + rtttl::begin(config.device.buzzer_gpio, rtttlConfig.ringtone); } - - return EXT_NOTIFICATION_DEFAULT_THREAD_MS; } + + return EXT_NOTIFICATION_DEFAULT_THREAD_MS; } bool ExternalNotificationModule::wantPacket(const meshtastic_MeshPacket *p) From 426e804324e291f30c07257454c2976cf1fef1b6 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Sun, 17 Nov 2024 11:10:09 -0500 Subject: [PATCH 2/5] Centralize ExternalNotificationModule LED handling This PR attemtps to take a small step towards addressing issue 5194 by de-duplicating LED handling code within ExternalNotificationModule. I also refactored the color state code around an alpha channel approach. --- src/modules/ExternalNotificationModule.cpp | 122 ++++++++++----------- src/modules/ExternalNotificationModule.h | 7 +- 2 files changed, 63 insertions(+), 66 deletions(-) diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index eb05d064c3..6f37d55358 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -38,12 +38,16 @@ extern unPhone unphone; #endif #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE) +#define HAS_LED +#endif + +#ifdef HAS_LED uint8_t red = 0; uint8_t green = 0; uint8_t blue = 0; uint8_t colorState = 1; -uint8_t brightnessIndex = 0; -uint8_t brightnessValues[] = {0, 10, 20, 30, 50, 90, 160, 170}; // blue gets multiplied by 1.5 +uint8_t alphaIndex = 0; +uint8_t alphaValues[] = {0, 15, 30, 45, 75, 135, 240, 255}; bool ascending = true; #endif @@ -68,6 +72,22 @@ bool ascending = true; #define ASCII_BELL 0x07 +struct ExternalNotificationModule::RGB { + constexpr RGB(uint8_t red, uint8_t green,uint8_t blue) + : red{red}, green{green}, blue{blue} + {} + + #define SCALE_ALPHA(COLOR, ALPHA) (uint8_t)((((uint16_t)COLOR) * ALPHA) / (uint16_t)255) + constexpr RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) + : red{SCALE_ALPHA(red, alpha)}, green{SCALE_ALPHA(green, alpha)}, blue{SCALE_ALPHA(blue, alpha)} + {} + #undef SCALE_ALPHA + + uint8_t red = 0; + uint8_t green = 0; + uint8_t blue = 0; +}; + meshtastic_RTTTLConfig rtttlConfig; ExternalNotificationModule *externalNotificationModule; @@ -127,46 +147,11 @@ int32_t ExternalNotificationModule::runOnce() LOG_DEBUG("EXTERNAL 2 %d compared to %d", externalTurnedOn[2]+moduleConfig.external_notification.output_ms, millis()); setExternalState(2, !getExternal(2)); } -#if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE) - red = (colorState & 4) ? brightnessValues[brightnessIndex] : 0; // Red enabled on colorState = 4,5,6,7 - green = (colorState & 2) ? brightnessValues[brightnessIndex] : 0; // Green enabled on colorState = 2,3,6,7 - blue = (colorState & 1) ? (brightnessValues[brightnessIndex] * 1.5) : 0; // Blue enabled on colorState = 1,3,5,7 -#ifdef HAS_NCP5623 - if (rgb_found.type == ScanI2C::NCP5623) { - rgb.setColor(red, green, blue); - } -#endif -#ifdef RGBLED_CA - analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic - analogWrite(RGBLED_GREEN, 255 - green); - analogWrite(RGBLED_BLUE, 255 - blue); -#elif defined(RGBLED_RED) - analogWrite(RGBLED_RED, red); - analogWrite(RGBLED_GREEN, green); - analogWrite(RGBLED_BLUE, blue); -#endif -#ifdef HAS_NEOPIXEL - pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT); - pixels.show(); -#endif -#ifdef UNPHONE - unphone.rgb(red, green, blue); -#endif - if (ascending) { // fade in - brightnessIndex++; - if (brightnessIndex == (sizeof(brightnessValues) - 1)) { - ascending = false; - } - } else { - brightnessIndex--; // fade out - } - if (brightnessIndex == 0) { - ascending = true; - colorState++; // next color - if (colorState > 7) { - colorState = 1; - } - } +#ifdef HAS_LED + red = (colorState & 4) ? 170 : 0; // Red enabled on colorState = 4,5,6,7 + green = (colorState & 2) ? 170 : 0; // Green enabled on colorState = 2,3,6,7 + blue = (colorState & 1) ? 255 : 0; // Blue enabled on colorState = 1,3,5,7 + setLEDs({red, green, blue, alphaValues[alphaIndex]}); #endif #ifdef T_WATCH_S3 @@ -231,35 +216,15 @@ void ExternalNotificationModule::setExternalState(uint8_t index, bool on) break; } -#if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE) +#ifdef HAS_LED if (!on) { red = 0; green = 0; blue = 0; } + setLEDs({red, green, blue, 255}); #endif -#ifdef HAS_NCP5623 - if (rgb_found.type == ScanI2C::NCP5623) { - rgb.setColor(red, green, blue); - } -#endif -#ifdef RGBLED_CA - analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic - analogWrite(RGBLED_GREEN, 255 - green); - analogWrite(RGBLED_BLUE, 255 - blue); -#elif defined(RGBLED_RED) - analogWrite(RGBLED_RED, red); - analogWrite(RGBLED_GREEN, green); - analogWrite(RGBLED_BLUE, blue); -#endif -#ifdef HAS_NEOPIXEL - pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT); - pixels.show(); -#endif -#ifdef UNPHONE - unphone.rgb(red, green, blue); -#endif #ifdef T_WATCH_S3 if (on) { drv.go(); @@ -289,6 +254,35 @@ void ExternalNotificationModule::stopNow() #endif } +void ExternalNotificationModule::setLEDs(const RGB& rgb) +{ +#ifdef HAS_LEDS + // LOG_DEBUG("setLEDs red=%d, green=%d, blue=%d", rgb.red, rgb.green, rgb.blue); + +#ifdef HAS_NCP5623 + if (rgb_found.type == ScanI2C::NCP5623) { + rgb.setColor(rgb.red, rgb.green, rgb.blue); + } +#endif +#ifdef RGBLED_CA + analogWrite(RGBLED_RED, 255 - rgb.red); // CA type needs reverse logic + analogWrite(RGBLED_GREEN, 255 - rgb.green); + analogWrite(RGBLED_BLUE, 255 - rgb.blue); +#elif defined(RGBLED_RED) + analogWrite(RGBLED_RED, rgb.red); + analogWrite(RGBLED_GREEN, rgb.green); + analogWrite(RGBLED_BLUE, rgb.blue); +#endif +#ifdef HAS_NEOPIXEL + pixels.fill(pixels.Color(rgb.red, rgb.green, rgb.blue), 0, NEOPIXEL_COUNT); + pixels.show(); +#endif +#ifdef UNPHONE + unphone.rgb(rgb.red, rgb.green, rgb.blue); +#endif +#endif +} + ExternalNotificationModule::ExternalNotificationModule() : SinglePortModule("ExternalNotificationModule", meshtastic_PortNum_TEXT_MESSAGE_APP), concurrency::OSThread("ExternalNotification") diff --git a/src/modules/ExternalNotificationModule.h b/src/modules/ExternalNotificationModule.h index 841ca6de9a..a9b396779e 100644 --- a/src/modules/ExternalNotificationModule.h +++ b/src/modules/ExternalNotificationModule.h @@ -33,8 +33,6 @@ class ExternalNotificationModule : public SinglePortModule, private concurrency: ExternalNotificationModule(); uint32_t nagCycleCutoff = 1; - - void setExternalState(uint8_t index = 0, bool on = false); bool getExternal(uint8_t index = 0); void setMute(bool mute) { isMuted = mute; } @@ -46,6 +44,11 @@ class ExternalNotificationModule : public SinglePortModule, private concurrency: void handleSetRingtone(const char *from_msg); protected: + void setExternalState(uint8_t index = 0, bool on = false); + + struct RGB; + void setLEDs(const RGB& rgba); + /** Called to handle a particular incoming message @return ProcessMessage::STOP if you've guaranteed you've handled this message and no other handlers should be considered for it From c566a9f46559b81a92e150aa94d752024fefe694 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Sun, 17 Nov 2024 11:10:09 -0500 Subject: [PATCH 3/5] Centralize ExternalNotificationModule LED handling This PR attemtps to take a small step towards addressing issue 5194 by de-duplicating LED handling code within ExternalNotificationModule. I also refactored the color state code around an alpha channel approach. --- src/modules/ExternalNotificationModule.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index 6f37d55358..d08badcc8d 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -256,7 +256,8 @@ void ExternalNotificationModule::stopNow() void ExternalNotificationModule::setLEDs(const RGB& rgb) { -#ifdef HAS_LEDS + +#ifdef HAS_LED // LOG_DEBUG("setLEDs red=%d, green=%d, blue=%d", rgb.red, rgb.green, rgb.blue); #ifdef HAS_NCP5623 From 9ab22c04cd25dd734bf76651bbda1a3f1396aefa Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Mon, 25 Nov 2024 06:57:52 -0600 Subject: [PATCH 4/5] Trunk fmt --- src/modules/ExternalNotificationModule.cpp | 26 +++++++++++----------- src/modules/ExternalNotificationModule.h | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index d08badcc8d..b0af8ec23e 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -73,15 +73,14 @@ bool ascending = true; #define ASCII_BELL 0x07 struct ExternalNotificationModule::RGB { - constexpr RGB(uint8_t red, uint8_t green,uint8_t blue) - : red{red}, green{green}, blue{blue} - {} - - #define SCALE_ALPHA(COLOR, ALPHA) (uint8_t)((((uint16_t)COLOR) * ALPHA) / (uint16_t)255) + constexpr RGB(uint8_t red, uint8_t green, uint8_t blue) : red{red}, green{green}, blue{blue} {} + +#define SCALE_ALPHA(COLOR, ALPHA) (uint8_t)((((uint16_t)COLOR) * ALPHA) / (uint16_t)255) constexpr RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) - : red{SCALE_ALPHA(red, alpha)}, green{SCALE_ALPHA(green, alpha)}, blue{SCALE_ALPHA(blue, alpha)} - {} - #undef SCALE_ALPHA + : red{SCALE_ALPHA(red, alpha)}, green{SCALE_ALPHA(green, alpha)}, blue{SCALE_ALPHA(blue, alpha)} + { + } +#undef SCALE_ALPHA uint8_t red = 0; uint8_t green = 0; @@ -144,13 +143,14 @@ int32_t ExternalNotificationModule::runOnce() if (externalTurnedOn[2] + (moduleConfig.external_notification.output_ms ? moduleConfig.external_notification.output_ms : EXT_NOTIFICATION_MODULE_OUTPUT_MS) < millis()) { - LOG_DEBUG("EXTERNAL 2 %d compared to %d", externalTurnedOn[2]+moduleConfig.external_notification.output_ms, millis()); + LOG_DEBUG("EXTERNAL 2 %d compared to %d", externalTurnedOn[2] + moduleConfig.external_notification.output_ms, + millis()); setExternalState(2, !getExternal(2)); } #ifdef HAS_LED - red = (colorState & 4) ? 170 : 0; // Red enabled on colorState = 4,5,6,7 - green = (colorState & 2) ? 170 : 0; // Green enabled on colorState = 2,3,6,7 - blue = (colorState & 1) ? 255 : 0; // Blue enabled on colorState = 1,3,5,7 + red = (colorState & 4) ? 170 : 0; // Red enabled on colorState = 4,5,6,7 + green = (colorState & 2) ? 170 : 0; // Green enabled on colorState = 2,3,6,7 + blue = (colorState & 1) ? 255 : 0; // Blue enabled on colorState = 1,3,5,7 setLEDs({red, green, blue, alphaValues[alphaIndex]}); #endif @@ -254,7 +254,7 @@ void ExternalNotificationModule::stopNow() #endif } -void ExternalNotificationModule::setLEDs(const RGB& rgb) +void ExternalNotificationModule::setLEDs(const RGB &rgb) { #ifdef HAS_LED diff --git a/src/modules/ExternalNotificationModule.h b/src/modules/ExternalNotificationModule.h index a9b396779e..1427c26827 100644 --- a/src/modules/ExternalNotificationModule.h +++ b/src/modules/ExternalNotificationModule.h @@ -47,7 +47,7 @@ class ExternalNotificationModule : public SinglePortModule, private concurrency: void setExternalState(uint8_t index = 0, bool on = false); struct RGB; - void setLEDs(const RGB& rgba); + void setLEDs(const RGB &rgba); /** Called to handle a particular incoming message @return ProcessMessage::STOP if you've guaranteed you've handled this message and no other handlers should be considered for From 309af0af132f9848a02af41a69619196dba12463 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Fri, 6 Dec 2024 22:05:42 -0500 Subject: [PATCH 5/5] Fix compilation error due to `rgb` name conflict. --- protobufs | 2 +- src/modules/ExternalNotificationModule.cpp | 35 +++++++++++++++------- src/modules/ExternalNotificationModule.h | 4 +-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/protobufs b/protobufs index c952f8a4c1..af2fea10fe 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit c952f8a4c1c30f724743ee322dd3ec3ec2f934c4 +Subproject commit af2fea10fe2eba5857fb8e27975bbcea9c10af8e diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index b0af8ec23e..868f31363d 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -72,6 +72,7 @@ bool ascending = true; #define ASCII_BELL 0x07 +<<<<<<< HEAD struct ExternalNotificationModule::RGB { constexpr RGB(uint8_t red, uint8_t green, uint8_t blue) : red{red}, green{green}, blue{blue} {} @@ -81,6 +82,18 @@ struct ExternalNotificationModule::RGB { { } #undef SCALE_ALPHA +======= +struct ExternalNotificationModule::Color { + constexpr Color(uint8_t red, uint8_t green,uint8_t blue) + : red{red}, green{green}, blue{blue} + {} + + #define SCALE_ALPHA(COLOR, ALPHA) (uint8_t)((((uint16_t)COLOR) * ALPHA) / (uint16_t)255) + constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) + : red{SCALE_ALPHA(red, alpha)}, green{SCALE_ALPHA(green, alpha)}, blue{SCALE_ALPHA(blue, alpha)} + {} + #undef SCALE_ALPHA +>>>>>>> 79093de8 (Fix compilation error due to `rgb` name conflict.) uint8_t red = 0; uint8_t green = 0; @@ -254,32 +267,32 @@ void ExternalNotificationModule::stopNow() #endif } -void ExternalNotificationModule::setLEDs(const RGB &rgb) +void ExternalNotificationModule::setLEDs(const Color& color) { #ifdef HAS_LED - // LOG_DEBUG("setLEDs red=%d, green=%d, blue=%d", rgb.red, rgb.green, rgb.blue); + // LOG_DEBUG("setLEDs red=%d, green=%d, blue=%d", color.red, color.green, color.blue); #ifdef HAS_NCP5623 if (rgb_found.type == ScanI2C::NCP5623) { - rgb.setColor(rgb.red, rgb.green, rgb.blue); + rgb.setColor(color.red, color.green, color.blue); } #endif #ifdef RGBLED_CA - analogWrite(RGBLED_RED, 255 - rgb.red); // CA type needs reverse logic - analogWrite(RGBLED_GREEN, 255 - rgb.green); - analogWrite(RGBLED_BLUE, 255 - rgb.blue); + analogWrite(RGBLED_RED, 255 - color.red); // CA type needs reverse logic + analogWrite(RGBLED_GREEN, 255 - color.green); + analogWrite(RGBLED_BLUE, 255 - color.blue); #elif defined(RGBLED_RED) - analogWrite(RGBLED_RED, rgb.red); - analogWrite(RGBLED_GREEN, rgb.green); - analogWrite(RGBLED_BLUE, rgb.blue); + analogWrite(RGBLED_RED, color.red); + analogWrite(RGBLED_GREEN, color.green); + analogWrite(RGBLED_BLUE, color.blue); #endif #ifdef HAS_NEOPIXEL - pixels.fill(pixels.Color(rgb.red, rgb.green, rgb.blue), 0, NEOPIXEL_COUNT); + pixels.fill(pixels.Color(color.red, color.green, color.blue), 0, NEOPIXEL_COUNT); pixels.show(); #endif #ifdef UNPHONE - unphone.rgb(rgb.red, rgb.green, rgb.blue); + unphone.rgb(color.red, color.green, color.blue); #endif #endif } diff --git a/src/modules/ExternalNotificationModule.h b/src/modules/ExternalNotificationModule.h index 1427c26827..b1b69c85fd 100644 --- a/src/modules/ExternalNotificationModule.h +++ b/src/modules/ExternalNotificationModule.h @@ -46,8 +46,8 @@ class ExternalNotificationModule : public SinglePortModule, private concurrency: protected: void setExternalState(uint8_t index = 0, bool on = false); - struct RGB; - void setLEDs(const RGB &rgba); + struct Color; + void setLEDs(const Color& color); /** Called to handle a particular incoming message @return ProcessMessage::STOP if you've guaranteed you've handled this message and no other handlers should be considered for