From 25251785fa9ae25239eb9a723dac634dc0c8eceb Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Sat, 2 Aug 2025 17:39:02 +0200 Subject: [PATCH] led flash not working ma vabbeh --- lib/GPIO/LED_Driver.cpp | 20 ++++++++++++-------- lib/GPIO/LED_Driver.h | 2 ++ src/main.cpp | 12 ++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/GPIO/LED_Driver.cpp b/lib/GPIO/LED_Driver.cpp index 9d00023..4a1927d 100644 --- a/lib/GPIO/LED_Driver.cpp +++ b/lib/GPIO/LED_Driver.cpp @@ -31,23 +31,27 @@ namespace drivers void Led::flashHandle(TimerHandle_t th) { Led *led = (Led *)pvTimerGetTimerID(th); - std::lock_guard lock(led->m_ledMutex); rgbLedWrite(led->c_ledPin, led->m_colorDefault.g, led->m_colorDefault.r, led->m_colorDefault.b); // reset color to saved color - LOG_DEBUG("Led Flash timer expired"); - xTimerDelete(th, 0); - led->m_flashTimer = NULL; + return; } void Led::flashColor(const uint16_t tOn, const color_t color) { std::lock_guard lock(m_ledMutex); + rgbLedWrite(c_ledPin, color.g, color.r, color.b); // set color to flash if (m_flashTimer == NULL) { - blinkStop(); - rgbLedWrite(c_ledPin, color.g, color.r, color.b); // set color to flash - m_flashTimer = xTimerCreate("flasher", pdMS_TO_TICKS(tOn), pdFALSE, this, flashHandle); + m_flashTimer = xTimerCreate("flasher", pdMS_TO_TICKS(tOn), pdFALSE, NULL, flashHandle); xTimerStart(m_flashTimer, 0); - LOG_DEBUG("Led Flash timer created"); + LOG_INFO("Led Flash timer created"); + return; + } + xTimerStop(m_flashTimer, 0); + if (!xTimerChangePeriod(m_flashTimer, pdMS_TO_TICKS(tOn), pdMS_TO_TICKS(1)) || !xTimerReset(m_flashTimer, pdMS_TO_TICKS(1))) + { + LOG_ERROR("Led Flash timer failed reset"); + xTimerDelete(m_flashTimer, 0); + m_flashTimer = NULL; } } diff --git a/lib/GPIO/LED_Driver.h b/lib/GPIO/LED_Driver.h index 8be0ef0..fba7dad 100644 --- a/lib/GPIO/LED_Driver.h +++ b/lib/GPIO/LED_Driver.h @@ -59,6 +59,8 @@ namespace drivers TaskHandle_t m_blinkTask; TimerHandle_t m_flashTimer; + bool m_flashing; + std::mutex m_ledMutex; }; diff --git a/src/main.cpp b/src/main.cpp index fdb1d8b..edb4d2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -83,14 +83,14 @@ void loop() MQTTwrapper::MessageCallback onMessage = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message) { - LOG_INFO("onMessage callback [", topic.c_str(),"]"); - devices.led.flashColor(250, devices.led.COLOR_YELLOW); + LOG_INFO("onMessage callback [", topic.c_str(), "]"); + devices.led.setColor(devices.led.COLOR_MAGENTA); }; MQTTwrapper::MessageCallback onPublish = [&devices](const MQTTwrapper::Topic &topic, const MQTTwrapper::Message &message) { - LOG_INFO("onPublish callback [", topic.c_str(),"]"); - devices.led.flashColor(250, devices.led.COLOR_BLUE); + LOG_INFO("onPublish callback [", topic.c_str(), "]"); + devices.led.setColor(devices.led.COLOR_SKYBLUE); }; ///////////// CRONJOB ////////////// @@ -144,8 +144,8 @@ void loop() buzzer.beep(250, NOTE_B); led.setColor(led.COLOR_GREEN); mqtt.subscribe(conf.m_mqttSubscribe["commands"], commandsCallback); - //mqtt.setOnMessageCb(onMessage); - //mqtt.setOnPublishCb(onPublish); + mqtt.setOnMessageCb(onMessage); + mqtt.setOnPublishCb(onPublish); break; } delay(250);