GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing averageguy.github.io — TaskSched: TaskSched.cpp Source File
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
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
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
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
103 {
104
105 char buf[200];
107 String sDiff= Task::formatMS(diff);
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
125 return mEnabled;
126 }
127
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
150 //mProc=func;
151 mProcVoid=func;
152 }
153
155 //mProc=func;
156 mProcWithTask=func;
157 }
158
160 mName=newName;
161 }
162
164 {
165 mRunImmediately = newImmediately;
166 }
167
169 {
170 mIterations = newIterations;
171 }
172
174 {
175 mInterval = newInterval;
176 }
177
179 {
180 return mRunImmediately;
181 }
182
184 {
185 return mInterval;
186 }
187
189 {
190 return mLastStartTime;
191 }
192
194 {
195 return mIterationCount;
196 }
197 void Task::runIt() {
199 return;
200 }
201
202 if ((mRunImmediately && mIterationCount == 0) || (millis() - mLastStartTime) > mInterval) {
203 mRunImmediately = false;
204
206 String sDiff= Task::formatMS(diff);
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
234 {
236 }
237
238 Sched::Sched() {
239 }
240
242 this->mSchedEnabled=true;
243 }
245 {
246 #ifdef DEBUG
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
264
266 {
267 if(this->mSchedEnabled) {
268 #ifdef DEBUG
269 // Serial.println("Looking for tasks");
270 #endif
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
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
iterator begin() const Get an iterator pointing to the beginning of the list. Definition: SimpleList.h:273 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 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 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 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 Generated by | |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |