mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-22 18:52:30 +00:00
unify periodic timers
This commit is contained in:
@@ -1,32 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "PeriodicTask.h"
|
||||
|
||||
/**
|
||||
* Periodically invoke a callback.
|
||||
*
|
||||
* FIXME: currently just syntatic sugar for polling in loop (you must call .loop), but eventually
|
||||
* generalize with the freertos scheduler so we can save lots of power by having everything either in
|
||||
* something like this or triggered off of an irq.
|
||||
* This just provides C style callback conventions rather than a virtual function - FIXME, remove?
|
||||
*/
|
||||
class Periodic
|
||||
class Periodic : public PeriodicTask
|
||||
{
|
||||
uint32_t lastMsec = 0;
|
||||
uint32_t period = 1; // call soon after creation
|
||||
uint32_t (*callback)();
|
||||
|
||||
public:
|
||||
// callback returns the period for the next callback invocation (or 0 if we should no longer be called)
|
||||
Periodic(uint32_t (*_callback)()) : callback(_callback) {}
|
||||
|
||||
// for the time being this must be called from loop
|
||||
void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
if (period && (now - lastMsec) >= period)
|
||||
{
|
||||
lastMsec = now;
|
||||
period = (*callback)();
|
||||
}
|
||||
}
|
||||
protected:
|
||||
|
||||
uint32_t doTask();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user