From fe6fe38460b3e019af586e640025ee29b636b275 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sun, 5 Jan 2025 19:28:23 +0100 Subject: [PATCH] Make sure that dimmers will start once ZCD module detects electricity after startup --- src/yasolr_zcd.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/yasolr_zcd.cpp b/src/yasolr_zcd.cpp index 751af12a..6a8730e3 100644 --- a/src/yasolr_zcd.cpp +++ b/src/yasolr_zcd.cpp @@ -23,20 +23,26 @@ void yasolr_init_zcd() { if (!pulseAnalyzer->isEnabled()) return; - zcdTask = new Mycila::Task("ZCD", Mycila::TaskType::ONCE, [](void* params) { + zcdTask = new Mycila::Task("ZCD", [](void* params) { // check if ZCD is online (connected to the grid) // this is required for dimmers to work if (!pulseAnalyzer->isOnline()) { - logger.debug(TAG, "No electricity detected by ZCD module"); - zcdTask->resume(2 * Mycila::TaskDuration::SECONDS); // retry in 2 seconds + logger.warn(TAG, "No electricity detected by ZCD module"); return; } // => ZCD switch turned on + Pulse Analyzer online if (!Thyristor::getSemiPeriod() || (output1 && !output1->isDimmerEnabled()) || (output2 && !output2->isDimmerEnabled())) { float frequency = yasolr_frequency(); - uint16_t semiPeriod = 1000000 / 2 / frequency; + + if (!frequency) { + logger.warn(TAG, "No electricity detected by ZCD module"); + return; + } + + const uint16_t semiPeriod = 1000000 / 2 / frequency; logger.info(TAG, "Detected grid frequency: %.2f Hz", frequency); + zcdTask->setEnabled(false); if (!Thyristor::getSemiPeriod()) { logger.info(TAG, "Starting Thyristor with semi-period: %" PRIu16 " us", semiPeriod); @@ -64,10 +70,13 @@ void yasolr_init_zcd() { // refresh dashboard when electricity is back dashboardInitTask.resume(); + + } else { + zcdTask->setEnabled(false); } }); + zcdTask->setInterval(2 * Mycila::TaskDuration::SECONDS); zcdTask->setManager(coreTaskManager); - zcdTask->resume(); } }