7 Task::Task(TaskCallback func,
unsigned long interval,
bool enabled,
int iterations,
const char* name,
bool runImmediately)
8 : mProcWithTask(func), mProcVoid(nullptr), mWithTaskPtr(true)
14 mIterations=iterations;
16 mRunImmediately=runImmediately;
21 mLastStartTime=millis();
25 Task::Task(VoidCallback func,
unsigned long interval,
bool enabled,
int iterations,
const char* name,
bool runImmediately)
26 : mProcWithTask(nullptr), mProcVoid(func), mWithTaskPtr(false)
32 mIterations=iterations;
34 mRunImmediately=runImmediately;
39 mLastStartTime=millis();
44 if(mIterationCount == 0) {
57 if(mIterations <= 0) {
60 if(mIterationCount >=mIterations-1) {
80 Serial.printf(
"%s %d showInit: Name: %s, interval: %s, enabled:%d, iterations:%ld %x\n",
81 __FILE__,__LINE__,mName,res,mEnabled,mIterations,
this);
89 int minutes = milliseconds / 60000;
90 int seconds = (milliseconds % 60000) / 1000;
91 int ms = (milliseconds % 1000);
94 sprintf(res,
"%02d:%02d.%03d", minutes, seconds, ms);
96 Serial.printf(
"%s %d %s\n",__FILE__,__LINE__,res);
110 Serial.printf(
"%s %d Task %s, Enabled? %d, Diff %s, Interval %s, RI %d\n",__FILE__,__LINE__,this->
getName().c_str(),
isEnabled(),sDiff.c_str(),sInt.c_str(),
getRunImmediately());
111 if(passedInterval >0) {
112 Serial.printf(
"For float interval passed in %f, mInterval became %ld\n",passedInterval,mInterval);
115 sprintf(buf,
"Task %s, Enabled? %d, Diff %s, Interval %s, RunIm %d\n",this->
getName().c_str(),
isEnabled(),sDiff.c_str(),sInt.c_str(),
getRunImmediately());
130 mLastStartTime=millis();
132 Serial.printf(
"Enabling %s\n",mName.c_str());
144 Serial.printf(
"Restarting %s\n",mName.c_str());
165 mRunImmediately = newImmediately;
170 mIterations = newIterations;
175 mInterval = newInterval;
180 return mRunImmediately;
190 return mLastStartTime;
195 return mIterationCount;
202 if ((mRunImmediately && mIterationCount == 0) || (millis() - mLastStartTime) > mInterval) {
203 mRunImmediately =
false;
209 Serial.printf(
"%s %d Task %s, Enabled? %d, Diff %s, Interval %s, RI %d\n",__FILE__,__LINE__,
getName().c_str(),
isEnabled(),sDiff.c_str(),sInt.c_str(),
getRunImmediately());
211 if (mWithTaskPtr && mProcWithTask) {
213 }
else if (!mWithTaskPtr && mProcVoid) {
219 Serial.printf(
"%s %d %s in runit iteration count is %ld, max is %ld\n",__FILE__,__LINE__,
getName().c_str(),mIterationCount,mIterations);
221 if(mIterationCount>=mIterations && mIterations != 0) {
224 mLastStartTime=millis();
242 this->mSchedEnabled=
true;
247 Serial.printf(
"add called for task, %s %x\n",task->
getName().c_str(),task);
254 this->mSchedEnabled=
true;
257 this->mSchedEnabled=
false;
260 return this->mSchedEnabled;
267 if(this->mSchedEnabled) {
271 for (
auto it = tTasks.
begin(); it != tTasks.
end(); ++it) {
273 Task* currentTask = *it;
276 Serial.printf(
"%s %d task=%x i=%d millis()=%ld Found name=%s, Is enabled? %d\n",__FILE__,__LINE__,currentTask,i++,millis(),currentTask->
getName().c_str(),currentTask->
isEnabled());
280 currentTask->runIt();
285 Serial.println(
"Not enabled");
bool mEnabled
saved enable flag
unsigned long mIterations
saved run iterations count
bool mRunImmediately
saved run immediately flag
unsigned long mInterval
This member holds the original interval value.
const SimpleList< Task * > & getTasks() const
bool isEnabled()
return true if the scheduler is enabled
unsigned long getSize()
return the number of tasks in the run queue
void enable()
enable the scheduler
void run()
called perodically to check if a task should be scheduled
void disable()
disable the scheduler
void push_back(const T &value)
Add a new element to the end of the list.
size_t get_size() const
Get the number of elements in the list.
iterator begin() const
Get an iterator pointing to the beginning of the list.
iterator end() const
Get an iterator pointing to the end of the list.
Represents a schedulable task.
bool getRunImmediately(void) const
return the run immediately flag
unsigned long getInterval(void) const
return the task interval
void setInterval(unsigned long newInterval)
function to set a new interval
void setIterations(unsigned long newIterations)
function to set a new iterations value
void showInit()
display stuff
String getName() const
return string containing name of task
bool isLastIteration()
return true if this is the last iteration
Task(TaskCallback func, unsigned long interval=5000, bool enabled=false, int iterations=0, const char *name="Unk", bool runImmediately=false)
Constructs a new Task.
static String formatMS(unsigned long milliseconds)
return a string with a formatted time
void setName(String)
give the task a new name
bool isEnabled() const
return true if task is enabled
unsigned long getIterationCount() const
return the iteration count, that is the number of iterations that the task has been run
unsigned long getLastStartTime(void) const
return the last start time flag
void setImmediately(bool)
function to set the run immediately flag
String showTaskInfo() const
function that displays task info
bool isFirstIteration()
return true if this is the first iteration
void setCallback(const TaskCallback &callback)
Sets a new callback function for the task.
void restart()
restart the task with the original parameters, Enable is not restored
void disable()
disable the task
void enable()
enable the task