From 7d77b23eb6a3383e4950f2f06ac7524b6a8102da Mon Sep 17 00:00:00 2001 From: Gareth Coleman Date: Thu, 18 Apr 2024 22:00:33 +0100 Subject: [PATCH] support for generic 4 pin CC and CA RGB LEDS --- src/AmbientLightingThread.h | 48 ++++++++++++++---- src/main.cpp | 2 +- src/modules/ExternalNotificationModule.cpp | 59 ++++++++++++++++++++++ 3 files changed, 99 insertions(+), 10 deletions(-) diff --git a/src/AmbientLightingThread.h b/src/AmbientLightingThread.h index fd3c66cda..1425d3266 100644 --- a/src/AmbientLightingThread.h +++ b/src/AmbientLightingThread.h @@ -25,8 +25,8 @@ class AmbientLightingThread : public concurrency::OSThread // moduleConfig.ambient_lighting.green = (myNodeInfo.my_node_num & 0x00FF00) >> 8; // moduleConfig.ambient_lighting.blue = myNodeInfo.my_node_num & 0x0000FF; - _type = type; #ifdef HAS_NCP5623 + _type = type; if (_type == ScanI2C::DeviceType::NONE) { LOG_DEBUG("AmbientLightingThread disabling due to no RGB leds found on I2C bus\n"); disable(); @@ -51,22 +51,39 @@ class AmbientLightingThread : public concurrency::OSThread } LOG_DEBUG("AmbientLightingThread initializing\n"); setLighting(); +#endif +#ifdef RGBLED_RED + if (!moduleConfig.ambient_lighting.led_state) { + LOG_DEBUG("AmbientLightingThread disabling due to moduleConfig.ambient_lighting.led_state OFF\n"); + disable(); + return; + } + LOG_DEBUG("AmbientLightingThread initializing\n"); + pinMode(RGBLED_RED, output); + pinMode(RGBLED_GREEN, output); + pinMode(RGBLED_BLUE, output); + setLighting(); #endif } protected: int32_t runOnce() override { -#if defined(HAS_NCP5623) || defined(UNPHONE) - if ((_type == ScanI2C::NCP5623 || _type == ScanI2C::RGBLED_CA) && moduleConfig.ambient_lighting.led_state) { +#ifdef HAS_NCP5623 + if (_type == ScanI2C::NCP5623 && moduleConfig.ambient_lighting.led_state) { setLighting(); return 30000; // 30 seconds to reset from any animations that may have been running from Ext. Notification - } else { - return disable(); } -#else - return disable(); #endif +#ifdef UNPHONE + setLighting(); + return 30000; // 30 seconds to reset from any animations that may have been running from Ext. Notification +#endif +#ifdef RGBLED_RED + setLighting(); + return 30000; // 30 seconds to reset from any animations that may have been running from Ext. Notification +#endif + return disable(); } private: @@ -79,14 +96,27 @@ class AmbientLightingThread : public concurrency::OSThread rgb.setRed(moduleConfig.ambient_lighting.red); rgb.setGreen(moduleConfig.ambient_lighting.green); rgb.setBlue(moduleConfig.ambient_lighting.blue); - LOG_DEBUG("Initializing Ambient lighting w/ current=%d, red=%d, green=%d, blue=%d\n", + LOG_DEBUG("Initializing NCP5623 Ambient lighting w/ current=%d, red=%d, green=%d, blue=%d\n", moduleConfig.ambient_lighting.current, moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); #endif #ifdef UNPHONE unphone.rgb(moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); - LOG_DEBUG("Initializing Ambient lighting w/ red=%d, green=%d, blue=%d\n", moduleConfig.ambient_lighting.red, + LOG_DEBUG("Initializing unPhone Ambient lighting w/ red=%d, green=%d, blue=%d\n", moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); +#endif +#ifdef RGBLED_CA + analogWrite(RGBLED_RED, 255 - moduleConfig.ambient_lighting.red); + analogWrite(RGBLED_GREEN, 255 - moduleConfig.ambient_lighting.green); + analogWrite(RGBLED_BLUE, 255 - moduleConfig.ambient_lighting.blue); + LOG_DEBUG("Initializing Ambient lighting RGB Common Anode w/ red=%d, green=%d, blue=%d\n", + moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); +#elifdef RGBLED_RED + analogWrite(RGBLED_RED, moduleConfig.ambient_lighting.red); + analogWrite(RGBLED_GREEN, moduleConfig.ambient_lighting.green); + analogWrite(RGBLED_BLUE, moduleConfig.ambient_lighting.blue); + LOG_DEBUG("Initializing Ambient lighting RGB Common Cathode w/ red=%d, green=%d, blue=%d\n", + moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); #endif } }; diff --git a/src/main.cpp b/src/main.cpp index b1a15634f..d8640bb59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -608,7 +608,7 @@ void setup() #endif #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) - if (rgb_found.type != ScanI2C::DeviceType::NONE) { + if (rgb_found.type != ScanI2C::DeviceType::NONE || UNPHONE) { ambientLightingThread = new AmbientLightingThread(rgb_found.type); } #endif diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index d7997b849..ee3b73efd 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -48,6 +48,16 @@ const uint8_t duration = 15; uint8_t counter = 0; #endif +#ifdef RGBLED_RED +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 +bool ascending = true; +#endif + #ifndef PIN_BUZZER #define PIN_BUZZER false #endif @@ -84,6 +94,7 @@ int32_t ExternalNotificationModule::runOnce() if (!moduleConfig.external_notification.enabled) { return INT32_MAX; // we don't need this thread here... } else { + bool isPlaying = rtttl::isPlaying(); #ifdef HAS_I2S isPlaying = rtttl::isPlaying() || audioThread->isPlaying(); @@ -159,6 +170,30 @@ int32_t ExternalNotificationModule::runOnce() } #endif +#ifdef RGBLED_RED + 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 + analogWrite(RGBLED_RED, red); + analogWrite(RGBLED_GREEN, green); + analogWrite(RGBLED_BLUE, blue); + 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; + } + } +#endif + #ifdef T_WATCH_S3 drv.go(); #endif @@ -229,6 +264,15 @@ void ExternalNotificationModule::setExternalOn(uint8_t index) #ifdef UNPHONE unphone.rgb(red, green, blue); #endif +#ifdef RGBLED_CA + analogWrite(RGBLED_RED, 255 - red); + analogWrite(RGBLED_GREEN, 255 - green); + analogWrite(RGBLED_BLUE, 255 - blue); +#elifdef RGBLED_RED + analogWrite(RGBLED_RED, red); + analogWrite(RGBLED_GREEN, green); + analogWrite(RGBLED_BLUE, blue); +#endif #ifdef T_WATCH_S3 drv.go(); #endif @@ -271,6 +315,21 @@ void ExternalNotificationModule::setExternalOff(uint8_t index) blue = 0; unphone.rgb(red, green, blue); #endif +#ifdef RGBLED_CA + red = 0; + green = 0; + blue = 0; + analogWrite(RGBLED_RED, 255 - red); + analogWrite(RGBLED_GREEN, 255 - green); + analogWrite(RGBLED_BLUE, 255 - blue); +#elifdef RGBLED_RED + red = 0; + green = 0; + blue = 0; + analogWrite(RGBLED_RED, red); + analogWrite(RGBLED_GREEN, green); + analogWrite(RGBLED_BLUE, blue); +#endif #ifdef T_WATCH_S3 drv.stop(); #endif