TaskSched
TaskSched.cpp
Go to the documentation of this file.
1 //#define DEBUG 1
2 #include "TaskSched.h"
3 #ifdef DEBUG
4 //#include "esp_debug_helpers.h"
5 #endif
6 
7 Task::Task(TaskCallback func, unsigned long interval, bool enabled, int iterations, const char* name, bool runImmediately)
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 }
24 
25 Task::Task(VoidCallback func, unsigned long interval, bool enabled, int iterations, const char* name, bool runImmediately)
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 }
42 
44  if(mIterationCount == 0) {
45  return true;
46  }
47  return false;
48 }
49 
50 String Task::getName() const
51 {
52  return mName;
53 }
54 
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 }
65 
66 void Task::disable() {
67  mEnabled=false;
68  mIterationCount=0;
69  return;
70 }
71 
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 }
86 
87 String Task::formatMS(unsigned long milliseconds){
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 }
101 
102 String Task::showTaskInfo() const
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 }
123 
124 bool Task::isEnabled() const {
125  return mEnabled;
126 }
127 
128 void Task::enable() {
129  mEnabled=true;
130  mLastStartTime=millis();
131 #ifdef DEBUG
132  Serial.printf("Enabling %s\n",mName.c_str());
133 #endif
134  return;
135 }
136 
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 }
148 
149 void Task::setCallback(const VoidCallback & func) {
150  //mProc=func;
151  mProcVoid=func;
152 }
153 
154 void Task::setCallback(const TaskCallback & func) {
155  //mProc=func;
156  mProcWithTask=func;
157 }
158 
159 void Task::setName(String newName) {
160  mName=newName;
161 }
162 
163 void Task::setImmediately(bool newImmediately)
164 {
165  mRunImmediately = newImmediately;
166 }
167 
168 void Task::setIterations(unsigned long newIterations)
169 {
170  mIterations = newIterations;
171 }
172 
173 void Task::setInterval(unsigned long newInterval)
174 {
175  mInterval = newInterval;
176 }
177 
178 bool Task::getRunImmediately(void) const
179 {
180  return mRunImmediately;
181 }
182 
183 unsigned long Task::getInterval(void) const
184 {
185  return mInterval;
186 }
187 
188 unsigned long Task::getLastStartTime(void) const
189 {
190  return mLastStartTime;
191 }
192 
193 unsigned long Task::getIterationCount() const
194 {
195  return mIterationCount;
196 }
197 void Task::runIt() {
198  if (!isEnabled()) {
199  return;
200  }
201 
202  if ((mRunImmediately && mIterationCount == 0) || (millis() - mLastStartTime) > mInterval) {
203  mRunImmediately = false;
204 
205  unsigned long diff = millis() - getLastStartTime();
206  String sDiff= Task::formatMS(diff);
207  String sInt= Task::formatMS(getInterval());
208 #ifdef DEBUG
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());
210 #endif
211  if (mWithTaskPtr && mProcWithTask) {
212  mProcWithTask(this);
213  } else if (!mWithTaskPtr && mProcVoid) {
214  mProcVoid();
215  }
216 
217  ++(mIterationCount);
218 #ifdef DEBUG
219  Serial.printf("%s %d %s in runit iteration count is %ld, max is %ld\n",__FILE__,__LINE__,getName().c_str(),mIterationCount,mIterations);
220 #endif
221  if(mIterationCount>=mIterations && mIterations != 0) {
222  disable();
223  } else {
224  mLastStartTime=millis();
225  }
226 #ifdef DEBUG
227  // Serial.printf("%s in runit count is %ld\n",getName(),mIterationCount);
228 #endif
229  }
230 
231 }
232 
233 unsigned long Sched::getSize()
234 {
235  return tTasks.get_size();
236 }
237 
239 }
240 
241 void Sched::begin() {
242  this->mSchedEnabled=true;
243 }
244 void Sched::addTask(Task *task)
245 {
246 #ifdef DEBUG
247  Serial.printf("add called for task, %s %x\n",task->getName().c_str(),task);
248 #endif
249  tTasks.push_back(task);
250  return;
251 };
252 
254  this->mSchedEnabled=true;
255 }
257  this->mSchedEnabled=false;
258 }
260  return this->mSchedEnabled;
261 }
262 
263 int ckCnt=0;
264 
266 {
267  if(this->mSchedEnabled) {
268 #ifdef DEBUG
269  // Serial.println("Looking for tasks");
270 #endif
271  for (auto it = tTasks.begin(); it != tTasks.end(); ++it) {
272  // for (it = tTasks.begin(); it != tTasks.end(); ++it){
273  Task* currentTask = *it;
274 
275 #ifdef DEBUGR
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());
277  currentTask->showInit();
278 #endif
279  if(currentTask->isEnabled()) {
280  currentTask->runIt();
281  }
282  }
283  } else {
284 #ifdef DEBUG
285  Serial.println("Not enabled");
286 #endif
287  }
288 }
289 
291 {
292  return tTasks;
293 }
294 
int ckCnt
Definition: TaskSched.cpp:263
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
const SimpleList< Task * > & getTasks() const
Definition: TaskSched.cpp:290
bool isEnabled()
return true if the scheduler is enabled
Definition: TaskSched.cpp:259
unsigned long getSize()
return the number of tasks in the run queue
Definition: TaskSched.cpp:233
void enable()
enable the scheduler
Definition: TaskSched.cpp:253
void begin()
Definition: TaskSched.cpp:241
void run()
called perodically to check if a task should be scheduled
Definition: TaskSched.cpp:265
void addTask(Task *task)
Definition: TaskSched.cpp:244
void disable()
disable the scheduler
Definition: TaskSched.cpp:256
void push_back(const T &value)
Add a new element to the end of the list.
Definition: SimpleList.h:51
size_t get_size() const
Get the number of elements in the list.
Definition: SimpleList.h:116
iterator begin() const
Get an iterator pointing to the beginning of the list.
Definition: SimpleList.h:273
iterator end() const
Get an iterator pointing to the end of the list.
Definition: SimpleList.h:280
Represents a schedulable task.
Definition: TaskSched.h:80
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
void setInterval(unsigned long newInterval)
function to set a new interval
Definition: TaskSched.cpp:173
void setIterations(unsigned long newIterations)
function to set a new iterations value
Definition: TaskSched.cpp:168
void showInit()
display stuff
Definition: TaskSched.cpp:72
String getName() const
return string containing name of task
Definition: TaskSched.cpp:50
bool isLastIteration()
return true if this is the last iteration
Definition: TaskSched.cpp:55
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
static String formatMS(unsigned long milliseconds)
return a string with a formatted time
Definition: TaskSched.cpp:87
void setName(String)
give the task a new name
Definition: TaskSched.cpp:159
bool isEnabled() const
return true if task is enabled
Definition: TaskSched.cpp:124
unsigned long getIterationCount() const
return the iteration count, that is the number of iterations that the task has been run
Definition: TaskSched.cpp:193
unsigned long getLastStartTime(void) const
return the last start time flag
Definition: TaskSched.cpp:188
void setImmediately(bool)
function to set the run immediately flag
Definition: TaskSched.cpp:163
String showTaskInfo() const
function that displays task info
Definition: TaskSched.cpp:102
bool isFirstIteration()
return true if this is the first iteration
Definition: TaskSched.cpp:43
void setCallback(const TaskCallback &callback)
Sets a new callback function for the task.
Definition: TaskSched.cpp:154
void restart()
restart the task with the original parameters, Enable is not restored
Definition: TaskSched.cpp:137
void disable()
disable the task
Definition: TaskSched.cpp:66
void enable()
enable the task
Definition: TaskSched.cpp:128