From 12031ed09ec65b949151feb7494537f67b79710b Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Mon, 20 Nov 2023 20:57:14 +0100 Subject: [PATCH] Migrate SunPosition to TaskScheduler --- include/SunPosition.h | 7 +++++-- src/SunPosition.cpp | 6 +++++- src/main.cpp | 4 +--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/SunPosition.h b/include/SunPosition.h index 1e1508d33..81414c979 100644 --- a/include/SunPosition.h +++ b/include/SunPosition.h @@ -1,14 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-or-later #pragma once +#include #include #include class SunPositionClass { public: SunPositionClass(); - void init(); - void loop(); + void init(Scheduler* scheduler); bool isDayPeriod(); bool isSunsetAvailable(); @@ -17,10 +17,13 @@ class SunPositionClass { void setDoRecalc(bool doRecalc); private: + void loop(); void updateSunData(); bool checkRecalcDayChanged(); bool getSunTime(struct tm* info, uint32_t offset); + Task _loopTask; + bool _isSunsetAvailable = true; uint32_t _sunriseMinutes = 0; uint32_t _sunsetMinutes = 0; diff --git a/src/SunPosition.cpp b/src/SunPosition.cpp index 48e308204..5e10d9828 100644 --- a/src/SunPosition.cpp +++ b/src/SunPosition.cpp @@ -13,8 +13,12 @@ SunPositionClass::SunPositionClass() { } -void SunPositionClass::init() +void SunPositionClass::init(Scheduler* scheduler) { + scheduler->addTask(_loopTask); + _loopTask.setCallback(std::bind(&SunPositionClass::loop, this)); + _loopTask.setIterations(TASK_FOREVER); + _loopTask.enable(); } void SunPositionClass::loop() diff --git a/src/main.cpp b/src/main.cpp index 2976cbab3..6385b37bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,7 +93,7 @@ void setup() // Initialize SunPosition MessageOutput.print("Initialize SunPosition... "); - SunPosition.init(); + SunPosition.init(&scheduler); MessageOutput.println("done"); // Initialize MqTT @@ -171,8 +171,6 @@ void loop() yield(); Display.loop(); yield(); - SunPosition.loop(); - yield(); MessageOutput.loop(); yield(); LedSingle.loop();