kopia lustrzana https://github.com/meshtastic/firmware
add locks to PeriodicTask
rodzic
0cbcb7a9bd
commit
90ecdf229e
|
@ -1,19 +1,21 @@
|
|||
#include "PeriodicTask.h"
|
||||
#include "Periodic.h"
|
||||
|
||||
PeriodicTask::PeriodicTask(uint32_t initialPeriod) : period(initialPeriod)
|
||||
{
|
||||
}
|
||||
PeriodicTask::PeriodicTask(uint32_t initialPeriod) : period(initialPeriod) {}
|
||||
|
||||
/// call this from loop
|
||||
void PeriodicTask::loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
if (period && (now - lastMsec) >= period)
|
||||
{
|
||||
lastMsec = now;
|
||||
doTask();
|
||||
meshtastic::LockGuard lg(&lock);
|
||||
uint32_t now = millis();
|
||||
if (!period || (now - lastMsec) < period) {
|
||||
return;
|
||||
}
|
||||
lastMsec = now;
|
||||
}
|
||||
// Release the lock in case the task wants to change the period.
|
||||
doTask();
|
||||
}
|
||||
|
||||
void Periodic::doTask()
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "configuration.h"
|
||||
#include <cstdint>
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
/**
|
||||
* A base class for tasks that want their doTask() method invoked periodically
|
||||
|
@ -15,9 +16,10 @@ class PeriodicTask
|
|||
uint32_t lastMsec = 0;
|
||||
uint32_t period = 1; // call soon after creation
|
||||
|
||||
public:
|
||||
uint32_t periodMsec;
|
||||
// Protects the above variables.
|
||||
meshtastic::Lock lock;
|
||||
|
||||
public:
|
||||
virtual ~PeriodicTask() {}
|
||||
|
||||
PeriodicTask(uint32_t initialPeriod = 1);
|
||||
|
@ -26,8 +28,12 @@ public:
|
|||
virtual void loop();
|
||||
|
||||
/// Set a new period in msecs (can be called from doTask or elsewhere and the scheduler will cope)
|
||||
void setPeriod(uint32_t p) { period = p; }
|
||||
void setPeriod(uint32_t p)
|
||||
{
|
||||
meshtastic::LockGuard lg(&lock);
|
||||
period = p;
|
||||
}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
virtual void doTask() = 0;
|
||||
};
|
||||
|
|
Ładowanie…
Reference in New Issue