Skip to content

Commit

Permalink
Migrate SunPosition to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent a7c9c2d commit 12031ed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions include/SunPosition.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <TaskSchedulerDeclarations.h>
#include <atomic>
#include <sunset.h>

class SunPositionClass {
public:
SunPositionClass();
void init();
void loop();
void init(Scheduler* scheduler);

bool isDayPeriod();
bool isSunsetAvailable();
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/SunPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void setup()

// Initialize SunPosition
MessageOutput.print("Initialize SunPosition... ");
SunPosition.init();
SunPosition.init(&scheduler);
MessageOutput.println("done");

// Initialize MqTT
Expand Down Expand Up @@ -171,8 +171,6 @@ void loop()
yield();
Display.loop();
yield();
SunPosition.loop();
yield();
MessageOutput.loop();
yield();
LedSingle.loop();
Expand Down

0 comments on commit 12031ed

Please sign in to comment.