diff --git a/src/AmbientLightingThread.h b/src/AmbientLightingThread.h new file mode 100644 index 000000000..0dd0fdf4a --- /dev/null +++ b/src/AmbientLightingThread.h @@ -0,0 +1,75 @@ +#include "configuration.h" + +#ifdef HAS_NCP5623 +#include +NCP5623 rgb; +#endif + +namespace concurrency +{ +class AmbientLightingThread : public concurrency::OSThread +{ + public: + AmbientLightingThread(ScanI2C::DeviceType type) : OSThread("AmbientLightingThread") + { + // Uncomment to test module + // moduleConfig.ambient_lighting.led_state = true; + // moduleConfig.ambient_lighting.current = 10; + // // Default to a color based on our node number + // moduleConfig.ambient_lighting.red = (myNodeInfo.my_node_num & 0xFF0000) >> 16; + // moduleConfig.ambient_lighting.green = (myNodeInfo.my_node_num & 0x00FF00) >> 8; + // moduleConfig.ambient_lighting.blue = myNodeInfo.my_node_num & 0x0000FF; + +#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(); + return; + } + 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"); + if (_type == ScanI2C::NCP5623) { + rgb.begin(); + setLighting(); + } +#endif + } + + protected: + int32_t runOnce() override + { +#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 + } + + private: + ScanI2C::DeviceType _type = ScanI2C::DeviceType::NONE; + + void setLighting() + { +#ifdef HAS_NCP5623 + rgb.setCurrent(moduleConfig.ambient_lighting.current); + 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", + moduleConfig.ambient_lighting.current, moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, + moduleConfig.ambient_lighting.blue); +#endif + } +}; + +} // namespace concurrency \ No newline at end of file diff --git a/src/graphics/RAKled.h b/src/graphics/RAKled.h index 2e36b874a..659ea9b72 100644 --- a/src/graphics/RAKled.h +++ b/src/graphics/RAKled.h @@ -1,5 +1,3 @@ -#include "main.h" - #ifdef HAS_NCP5623 #include extern NCP5623 rgb; diff --git a/src/main.cpp b/src/main.cpp index 3ca3b50f3..2ae17225f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,6 +73,7 @@ NRF52Bluetooth *nrf52Bluetooth; #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #include "AccelerometerThread.h" +#include "AmbientLightingThread.h" #endif using namespace concurrency; @@ -169,6 +170,7 @@ static OSThread *buttonThread; uint32_t ButtonThread::longPressTime = 0; #endif static OSThread *accelerometerThread; +static OSThread *ambientLightingThread; SPISettings spiSettings(4000000, MSBFIRST, SPI_MODE0); RadioInterface *rIf = NULL; @@ -409,14 +411,6 @@ void setup() // Only one supported RGB LED currently #ifdef HAS_NCP5623 rgb_found = i2cScanner->find(ScanI2C::DeviceType::NCP5623); - - // Start the RGB LED at 50% - - if (rgb_found.type == ScanI2C::NCP5623) { - rgb.begin(); - rgb.setCurrent(10); - rgb.setColor(128, 128, 128); - } #endif #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) @@ -521,6 +515,12 @@ void setup() } #endif +#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) + if (rgb_found.type != ScanI2C::DeviceType::NONE) { + ambientLightingThread = new AmbientLightingThread(rgb_found.type); + } +#endif + #ifdef T_WATCH_S3 drv.begin(); drv.selectLibrary(1); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 5774eb084..2bbc4b779 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -255,6 +255,13 @@ void NodeDB::installDefaultModuleConfig() moduleConfig.detection_sensor.detection_triggered_high = true; moduleConfig.detection_sensor.minimum_broadcast_secs = 45; + moduleConfig.has_ambient_lighting = true; + moduleConfig.ambient_lighting.current = 10; + // Default to a color based on our node number + moduleConfig.ambient_lighting.red = (myNodeInfo.my_node_num & 0xFF0000) >> 16; + moduleConfig.ambient_lighting.green = (myNodeInfo.my_node_num & 0x00FF00) >> 8; + moduleConfig.ambient_lighting.blue = myNodeInfo.my_node_num & 0x0000FF; + initModuleConfigIntervals(); } diff --git a/src/modules/ExternalNotificationModule.cpp b/src/modules/ExternalNotificationModule.cpp index 20191e706..1e0e8250d 100644 --- a/src/modules/ExternalNotificationModule.cpp +++ b/src/modules/ExternalNotificationModule.cpp @@ -27,7 +27,6 @@ #ifdef HAS_NCP5623 #include -NCP5623 rgb; uint8_t red = 0; uint8_t green = 0; diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 88a396e69..c4061037d 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -228,7 +228,6 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define GPS_TX_PIN PIN_SERIAL1_TX // Define pin to enable GPS toggle (set GPIO to LOW) via user button triple press -#define PIN_GPS_EN 34 // GPS power enable pin // RAK12002 RTC Module #define RV3028_RTC (uint8_t)0b1010010 @@ -276,4 +275,4 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG * Arduino objects - C++ only *----------------------------------------------------------------------------*/ -#endif +#endif \ No newline at end of file