TaskSched
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
Task Class Reference

Represents a schedulable task. More...

#include <TaskSched.h>

Public Types

typedef void(* TaskCallback) (Task *)
 
typedef void(* VoidCallback) ()
 

Public Member Functions

 Task (TaskCallback func, unsigned long interval=5000, bool enabled=false, int iterations=0, const char *name="Unk", bool runImmediately=false)
 Constructs a new Task. More...
 
 Task (VoidCallback func, unsigned long interval=5000, bool enabled=false, int iterations=0, const char *name="Unk", bool runImmediately=false)
 
bool isFirstIteration ()
 return true if this is the first iteration More...
 
bool isLastIteration ()
 return true if this is the last iteration More...
 
bool fRunImmediately ()
 return true if the run immediately flag is set More...
 
void restart ()
 restart the task with the original parameters, Enable is not restored More...
 
void enable ()
 enable the task More...
 
void disable ()
 disable the task More...
 
bool isEnabled () const
 return true if task is enabled More...
 
void setCallback (const TaskCallback &callback)
 Sets a new callback function for the task. More...
 
void setCallback (const VoidCallback &callback)
 Sets a new callback function for the task. More...
 
void setName (String)
 give the task a new name More...
 
void showInit ()
 display stuff More...
 
String getName () const
 return string containing name of task More...
 
unsigned long getIterationCount () const
 return the iteration count, that is the number of iterations that the task has been run More...
 
void setInterval (unsigned long newInterval)
 function to set a new interval More...
 
void setIterations (unsigned long newIterations)
 function to set a new iterations value More...
 
void setImmediately (bool)
 function to set the run immediately flag More...
 
String showTaskInfo () const
 function that displays task info More...
 
unsigned long getInterval (void) const
 return the task interval More...
 
bool getRunImmediately (void) const
 return the run immediately flag More...
 
unsigned long getLastStartTime (void) const
 return the last start time flag More...
 

Static Public Member Functions

static String formatMS (unsigned long milliseconds)
 return a string with a formatted time More...
 

Friends

class Sched
 

Detailed Description

Represents a schedulable task.

Define a new Task.

Parameters
funcFunction to be called at regular intervals.
intervalTime between calls to func, specified in milliseconds or seconds. If interval is a floating-point value, it's interpreted as seconds; otherwise, it's treated as milliseconds.
enabledFlag to indicate whether the task should start in an enabled state (true) or disabled state (false).
iterationsNumber of times the task will be executed. Set to 0 for infinite iterations.
nameA descriptive string name for the task.
runImmediatelyBoolean flag determining whether to run the callback immediately (true) or wait for the interval to expire (false).
Task* t = new Task(turnLedOn, 1000,true,20,"OnOff",true);
Task t1(turnLedOn, 1000,false,100,"On1",false);
Task t2(turnLedOn, 2000,false,2000,"On2",false);
Output:
450 Task On2, Enabled? 0, Diff 00:15.735, Interval 00:02.000, RI 1
450 Task On1, Enabled? 0, Diff 00:15.735, Interval 00:00.100, RI 0
450 Task On, Enabled? 1, Diff 00:01.721, Interval 00:01.000, RI 0
Represents a schedulable task.
Definition: TaskSched.h:80
Task(TaskCallback func, unsigned long interval=5000, bool enabled=false, int iterations=0, const char *name="Unk", bool runImmediately=false)
Constructs a new Task.
Definition: TaskSched.cpp:7

The Task class encapsulates a function to be called at regular intervals, along with parameters controlling its execution.

Definition at line 80 of file TaskSched.h.

Member Typedef Documentation

◆ TaskCallback

typedef void(* Task::TaskCallback) (Task *)

Definition at line 92 of file TaskSched.h.

◆ VoidCallback

typedef void(* Task::VoidCallback) ()

Definition at line 93 of file TaskSched.h.

Constructor & Destructor Documentation

◆ Task() [1/2]

Task::Task ( TaskCallback  func,
unsigned long  interval = 5000,
bool  enabled = false,
int  iterations = 0,
const char *  name = "Unk",
bool  runImmediately = false 
)

Constructs a new Task.

Template Parameters
TType of the interval (can be integral or floating-point)
Parameters
funcFunction to be called at regular intervals
intervalTime between calls to func (in milliseconds if integral, seconds if floating-point)
enabledFlag to indicate whether the task should start in an enabled state
iterationsNumber of times the task will be executed (0 for infinite)
nameA descriptive name for the task
runImmediatelyWhether to run the callback immediately or wait for the interval
Returns
The first form returns a pointer to the task, the second does not.

Definition at line 7 of file TaskSched.cpp.

8  : mProcWithTask(func), mProcVoid(nullptr), mWithTaskPtr(true)
9 {
10  // Initialize other members...
11  //mProc=func;
12  mInterval=interval;
13  mEnabled=enabled;
14  mIterations=iterations;
15  mName=name;
16  mRunImmediately=runImmediately;
17  mOrig.mEnabled=enabled;
18  mOrig.mInterval=mInterval;
19  mOrig.mIterations=iterations;
20  mOrig.mRunImmediately=runImmediately;
21  mLastStartTime=millis();
22  mIterationCount=0;
23 }
bool mEnabled
saved enable flag
Definition: TaskSched.h:31
unsigned long mIterations
saved run iterations count
Definition: TaskSched.h:41
bool mRunImmediately
saved run immediately flag
Definition: TaskSched.h:36
unsigned long mInterval
This member holds the original interval value.
Definition: TaskSched.h:27

References InitialState::mEnabled, InitialState::mInterval, InitialState::mIterations, and InitialState::mRunImmediately.

◆ Task() [2/2]

Task::Task ( VoidCallback  func,
unsigned long  interval = 5000,
bool  enabled = false,
int  iterations = 0,
const char *  name = "Unk",
bool  runImmediately = false 
)

Definition at line 25 of file TaskSched.cpp.

26  : mProcWithTask(nullptr), mProcVoid(func), mWithTaskPtr(false)
27 {
28  // Initialize other members...
29  //mProc=func;
30  mInterval=interval;
31  mEnabled=enabled;
32  mIterations=iterations;
33  mName=name;
34  mRunImmediately=runImmediately;
35  mOrig.mEnabled=enabled;
36  mOrig.mInterval=interval;
37  mOrig.mIterations=iterations;
38  mOrig.mRunImmediately=runImmediately;
39  mLastStartTime=millis();
40  mIterationCount=0;
41 }

References InitialState::mEnabled, InitialState::mInterval, InitialState::mIterations, and InitialState::mRunImmediately.

Member Function Documentation

◆ disable()

void Task::disable ( )

disable the task

Definition at line 66 of file TaskSched.cpp.

66  {
67  mEnabled=false;
68  mIterationCount=0;
69  return;
70 }

◆ enable()

void Task::enable ( )

enable the task

Definition at line 128 of file TaskSched.cpp.

128  {
129  mEnabled=true;
130  mLastStartTime=millis();
131 #ifdef DEBUG
132  Serial.printf("Enabling %s\n",mName.c_str());
133 #endif
134  return;
135 }

◆ formatMS()

String Task::formatMS ( unsigned long  milliseconds)
static

return a string with a formatted time

Returns

Definition at line 87 of file TaskSched.cpp.

87  {
88 
89  int minutes = milliseconds / 60000;
90  int seconds = (milliseconds % 60000) / 1000;
91  int ms = (milliseconds % 1000);
92 
93  char res[20];
94  sprintf(res, "%02d:%02d.%03d", minutes, seconds, ms);
95 #ifdef DEBUG
96  Serial.printf("%s %d %s\n",__FILE__,__LINE__,res);
97 #endif
98 
99  return String(res);
100 }

Referenced by showInit(), and showTaskInfo().

◆ fRunImmediately()

bool Task::fRunImmediately ( )

return true if the run immediately flag is set

Returns

◆ getInterval()

unsigned long Task::getInterval ( void  ) const

return the task interval

Returns

Definition at line 183 of file TaskSched.cpp.

184 {
185  return mInterval;
186 }

Referenced by showTaskInfo().

◆ getIterationCount()

unsigned long Task::getIterationCount ( ) const

return the iteration count, that is the number of iterations that the task has been run

Returns

Definition at line 193 of file TaskSched.cpp.

194 {
195  return mIterationCount;
196 }

◆ getLastStartTime()

unsigned long Task::getLastStartTime ( void  ) const

return the last start time flag

Returns

Definition at line 188 of file TaskSched.cpp.

189 {
190  return mLastStartTime;
191 }

Referenced by showTaskInfo().

◆ getName()

String Task::getName ( ) const

return string containing name of task

Returns

Definition at line 50 of file TaskSched.cpp.

51 {
52  return mName;
53 }

Referenced by Sched::addTask(), Sched::run(), and showTaskInfo().

◆ getRunImmediately()

bool Task::getRunImmediately ( void  ) const

return the run immediately flag

Returns

Definition at line 178 of file TaskSched.cpp.

179 {
180  return mRunImmediately;
181 }

Referenced by showTaskInfo().

◆ isEnabled()

bool Task::isEnabled ( ) const

return true if task is enabled

Returns

Definition at line 124 of file TaskSched.cpp.

124  {
125  return mEnabled;
126 }

Referenced by Sched::run(), and showTaskInfo().

◆ isFirstIteration()

bool Task::isFirstIteration ( )

return true if this is the first iteration

Returns

Definition at line 43 of file TaskSched.cpp.

43  {
44  if(mIterationCount == 0) {
45  return true;
46  }
47  return false;
48 }

◆ isLastIteration()

bool Task::isLastIteration ( )

return true if this is the last iteration

Returns

Definition at line 55 of file TaskSched.cpp.

55  {
56 // Serial.printf("count = %ld, Iter = %ld\n",mIterationCount,mIterations);
57  if(mIterations <= 0) {
58  return false;
59  }
60  if(mIterationCount >=mIterations-1) {
61  return true;
62  }
63  return false;
64 }

◆ restart()

void Task::restart ( )

restart the task with the original parameters, Enable is not restored

Definition at line 137 of file TaskSched.cpp.

137  {
138  mInterval=mOrig.mInterval;
139  mIterations=mOrig.mIterations;
140  mEnabled=false;
141  mIterationCount=0;
142  mRunImmediately=mOrig.mRunImmediately;
143 #ifdef DEBUG
144  Serial.printf("Restarting %s\n",mName.c_str());
145 #endif
146  return;
147 }

References InitialState::mInterval, InitialState::mIterations, and InitialState::mRunImmediately.

◆ setCallback() [1/2]

void Task::setCallback ( const TaskCallback callback)

Sets a new callback function for the task.

Parameters
callbackThe new callback function

Definition at line 154 of file TaskSched.cpp.

154  {
155  //mProc=func;
156  mProcWithTask=func;
157 }

◆ setCallback() [2/2]

void Task::setCallback ( const VoidCallback callback)

Sets a new callback function for the task.

Parameters
callbackThe new callback function

Definition at line 149 of file TaskSched.cpp.

149  {
150  //mProc=func;
151  mProcVoid=func;
152 }

◆ setImmediately()

void Task::setImmediately ( bool  newImmediately)

function to set the run immediately flag

Definition at line 163 of file TaskSched.cpp.

164 {
165  mRunImmediately = newImmediately;
166 }

◆ setInterval()

void Task::setInterval ( unsigned long  newInterval)

function to set a new interval

Definition at line 173 of file TaskSched.cpp.

174 {
175  mInterval = newInterval;
176 }

◆ setIterations()

void Task::setIterations ( unsigned long  newIterations)

function to set a new iterations value

Definition at line 168 of file TaskSched.cpp.

169 {
170  mIterations = newIterations;
171 }

◆ setName()

void Task::setName ( String  newName)

give the task a new name

Definition at line 159 of file TaskSched.cpp.

159  {
160  mName=newName;
161 }

◆ showInit()

void Task::showInit ( )

display stuff

Definition at line 72 of file TaskSched.cpp.

72  {
73  if(!doShow) {
74  return;
75  }
76  doShow=false; // only do it once
77 #ifdef DEBUG
78 
79  res = Task::formatMS(mInterval);
80  Serial.printf("%s %d showInit: Name: %s, interval: %s, enabled:%d, iterations:%ld %x\n",
81  __FILE__,__LINE__,mName,res,mEnabled,mIterations,this);
82 #endif
83 
84  return;
85 }
static String formatMS(unsigned long milliseconds)
return a string with a formatted time
Definition: TaskSched.cpp:87

References formatMS().

Referenced by Sched::run().

◆ showTaskInfo()

String Task::showTaskInfo ( ) const

function that displays task info

Returns
String containing info about this task.

Definition at line 102 of file TaskSched.cpp.

103 {
104 
105  char buf[200];
106  unsigned long diff = millis() - getLastStartTime();
107  String sDiff= Task::formatMS(diff);
108  String sInt= Task::formatMS(getInterval());
109 #ifdef DEBUG
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);
113  }
114 #else
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());
116  String ret(buf);
117  return ret;
118 
119 #endif
120 
121  return "";
122 }
bool getRunImmediately(void) const
return the run immediately flag
Definition: TaskSched.cpp:178
unsigned long getInterval(void) const
return the task interval
Definition: TaskSched.cpp:183
String getName() const
return string containing name of task
Definition: TaskSched.cpp:50
bool isEnabled() const
return true if task is enabled
Definition: TaskSched.cpp:124
unsigned long getLastStartTime(void) const
return the last start time flag
Definition: TaskSched.cpp:188

References formatMS(), getInterval(), getLastStartTime(), getName(), getRunImmediately(), and isEnabled().

Friends And Related Function Documentation

◆ Sched

friend class Sched
friend

Definition at line 88 of file TaskSched.h.


The documentation for this class was generated from the following files: