From 90ecdf229e168f834746401cdd9e712b0ae17e42 Mon Sep 17 00:00:00 2001 From: Girts Folkmanis Date: Sun, 15 Mar 2020 19:27:42 -0700 Subject: [PATCH] add locks to PeriodicTask --- src/PeriodicTask.cpp | 16 +++++++++------- src/PeriodicTask.h | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/PeriodicTask.cpp b/src/PeriodicTask.cpp index b9410c69..99115faf 100644 --- a/src/PeriodicTask.cpp +++ b/src/PeriodicTask.cpp @@ -1,23 +1,25 @@ #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) { + meshtastic::LockGuard lg(&lock); + uint32_t now = millis(); + if (!period || (now - lastMsec) < period) { + return; + } lastMsec = now; - doTask(); } + // Release the lock in case the task wants to change the period. + doTask(); } void Periodic::doTask() { uint32_t p = callback(); setPeriod(p); -} \ No newline at end of file +} diff --git a/src/PeriodicTask.h b/src/PeriodicTask.h index 59659e2d..f4a35a2c 100644 --- a/src/PeriodicTask.h +++ b/src/PeriodicTask.h @@ -1,11 +1,12 @@ #pragma once -#include -#include "configuration.h" +#include + +#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; };