From d41a99ee887cb974622e1ee2271fdb71bf2cf8c7 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Sat, 11 Apr 2026 00:40:33 +0200 Subject: [PATCH] rgb led --- RotaxMonitor/lib/led/led.cpp | 29 ++++++++++++++++++++++ RotaxMonitor/lib/led/led.h | 48 ++++++++++++++++++++++++++++++++++++ RotaxMonitor/platformio.ini | 2 ++ RotaxMonitor/src/main.cpp | 22 ++++++++++++----- 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 RotaxMonitor/lib/led/led.cpp create mode 100644 RotaxMonitor/lib/led/led.h diff --git a/RotaxMonitor/lib/led/led.cpp b/RotaxMonitor/lib/led/led.cpp new file mode 100644 index 0000000..c75f987 --- /dev/null +++ b/RotaxMonitor/lib/led/led.cpp @@ -0,0 +1,29 @@ +#include + +RGBled::RGBled(const uint8_t pin, const uint8_t num) +{ + m_led = Adafruit_NeoPixel(num, pin, NEO_GRB + NEO_KHZ800); + m_led.begin(); + m_led.setPixelColor(0, RED); + m_led.show(); +} + +RGBled::~RGBled() +{ + m_led.clear(); +} + +void RGBled::setStatus(const LedStatus s) +{ + if (m_status == s) return; + m_status = s; + RGB_BUILTIN_LED_COLOR_ORDER + m_led.setPixelColor(0, s); + m_led.setBrightness(16); + m_led.show(); +} + +const RGBled::LedStatus RGBled::getSatus(void) +{ + return m_status; +} \ No newline at end of file diff --git a/RotaxMonitor/lib/led/led.h b/RotaxMonitor/lib/led/led.h new file mode 100644 index 0000000..8d026be --- /dev/null +++ b/RotaxMonitor/lib/led/led.h @@ -0,0 +1,48 @@ +#pragma once + +// System Inlcudes +#include +#include + +#define RED 0x00FF00 +#define GREEN 0xFF0000 +#define BLUE 0x0000FF +#define WHITE 0xFFFFFF +#define YELLOW 0xFFFF00 +#define CYAN 0xFF00FF +#define MAGENTA 0x00FFFF +#define ORANGE 0xA5FF00 +#define PURPLE 0x008080 +#define PINK 0x69FFB4 +#define LIME 0xCD3232 +#define SKY_BLUE 0xCE87EB +#define GOLD 0xD7FF00 +#define TURQUOISE 0xE040D0 +#define INDIGO 0x004B82 +#define GRAY 0x808080 + +class RGBled +{ +public: + enum LedStatus + { + OK = GREEN, + ERROR = RED, + INIT = YELLOW, + DATA_A = CYAN, + DATA_B = MAGENTA, + DATA_ALL = ORANGE, + IDLE = GRAY + }; + +public: + RGBled(const uint8_t pin = 48, const uint8_t num = 1); + ~RGBled(); + + void setStatus(const LedStatus s); + const LedStatus getSatus(void); + +private: + Adafruit_NeoPixel m_led; + LedStatus m_status = LedStatus::IDLE; +}; \ No newline at end of file diff --git a/RotaxMonitor/platformio.ini b/RotaxMonitor/platformio.ini index c5d3aa7..e7fb4f6 100644 --- a/RotaxMonitor/platformio.ini +++ b/RotaxMonitor/platformio.ini @@ -20,6 +20,7 @@ lib_deps = hideakitai/PCA95x5@^0.1.3 me-no-dev/AsyncTCP@^3.3.2 me-no-dev/ESPAsyncWebServer@^3.6.0 + adafruit/Adafruit NeoPixel@^1.15.4 upload_protocol = esptool upload_port = /dev/ttyACM1 upload_speed = 921600 @@ -45,6 +46,7 @@ platform = ${env:esp32-s3-devkitc1-n16r8.platform} framework = ${env:esp32-s3-devkitc1-n16r8.framework} lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps} + adafruit/Adafruit NeoPixel@^1.15.4 upload_protocol = esptool upload_port = /dev/ttyACM1 upload_speed = 921600 diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index a39faa4..c45e175 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -14,6 +14,7 @@ #include #include #include +#include // Defines to enable channel B #define CH_B_ENABLE @@ -76,6 +77,8 @@ void setup() void loop() { // global variables + RGBled led; + led.setStatus(RGBled::LedStatus::INIT); bool running = true; const uint32_t max_queue = 128; const uint32_t filter_k = 10; @@ -219,6 +222,7 @@ void loop() } LOG_DEBUG("Real Time Tasks A & B initialized"); + led.setStatus(RGBled::LedStatus::OK); bool partial_save = false; // flag to indicate if a partial save has been done after a timeout auto last_data = millis(); @@ -238,7 +242,7 @@ void loop() WebPage webPage(80, LittleFS); // Initialize webserver and Websocket //////////////// INNER LOOP ///////////////////// - while (running) + while (running) { auto dataA = pdFALSE; auto dataB = pdFALSE; @@ -264,9 +268,15 @@ void loop() #endif // Update last data if (dataA == pdTRUE || dataB == pdTRUE) - { last_data = millis(); - } + + // Update Led color + if (dataA == pdTRUE && dataB == pdFALSE) + led.setStatus(RGBled::DATA_A); + else if (dataB == pdTRUE && dataA == pdFALSE) + led.setStatus(RGBled::DATA_B); + else + led.setStatus(RGBled::DATA_ALL); if (dataA == pdTRUE) { @@ -314,17 +324,17 @@ void loop() partial_save = true; first_save = true; } - //Serial.printf("[%d] Waiting for data...\r", wait_count++); + // Serial.printf("[%d] Waiting for data...\r", wait_count++); + led.setStatus(RGBled::LedStatus::IDLE); delay(100); } - + if ((millis() - last_info) > 1000) { clearScreen(); Serial.println(); printRunningTasksMod(Serial); last_info = millis(); - } } //////////////// INNER LOOP /////////////////////