add locks to PeriodicTask

This commit is contained in:
Girts Folkmanis
2020-03-15 19:27:42 -07:00
parent 0cbcb7a9bd
commit 90ecdf229e
2 changed files with 22 additions and 14 deletions

View File

@@ -1,11 +1,12 @@
#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
*
*
* 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.
@@ -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;
};