Files
firmware/src/PeriodicTask.h

37 lines
839 B
C
Raw Normal View History

2020-02-06 07:39:21 -08:00
#pragma once
#include <Arduino.h>
#include "configuration.h"
class PeriodicTask
{
/// we use prevMsec rather than nextMsec because it is easier to handle the uint32 rollover in that case, also changes in periodMsec take effect immediately
uint32_t prevMsec;
public:
uint32_t periodMsec;
virtual ~PeriodicTask() {}
PeriodicTask(uint32_t period) : periodMsec(period)
{
prevMsec = millis();
}
/// call this from loop
virtual void loop()
{
uint32_t now = millis();
if (now > (prevMsec + periodMsec))
{
// FIXME, this lets period slightly drift based on scheduling - not sure if that is always good
prevMsec = now;
2020-02-06 08:18:20 -08:00
// DEBUG_MSG("Calling periodic task\n");
2020-02-06 07:39:21 -08:00
doTask();
}
}
virtual void doTask() = 0;
};