-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeriodicTask.cpp
46 lines (37 loc) · 1.21 KB
/
PeriodicTask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "PeriodicTask.h"
#include <Arduino.h>
void PeriodicTask::doInit()
{
setNextExpiration(mTimeInterval);
}
void PeriodicTask::setTimeInterval(unsigned long timeInterval)
{
mTimeInterval = timeInterval;
setNextExpiration(mTimeInterval);
}
void PeriodicTask::enableTask(bool runNow)
{
mEnabled = true;
// set expiration time to something current, not zero - otherwise, during the latter half of micros, the task will not run (long)micros() is negative)
setNextExpiration(mTimeInterval, runNow);
}
void PeriodicTask::run()
{
if (!mEnabled) return; // disabled, return as to not take any more processing time
// handles rollover, see - http://arduino.cc/playground/Code/TimingRollover
if (((long)(currentTime() - mExpirationTime)) > 0) // time to execute task
{
// doTask may change our time interval, so we do it first
doTask();
setNextExpiration(mTimeInterval); // set time for next task
}
// else wait till next time
}
unsigned long PeriodicTask::currentTime() const
{
return mUseMillis? millis() : micros();
}
void PeriodicTask::setNextExpiration(unsigned long timeInterval, bool runImmediately)
{
mExpirationTime = (runImmediately)? currentTime() : currentTime() + timeInterval;
}