-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSched.h
More file actions
358 lines (338 loc) · 10.8 KB
/
TaskSched.h
File metadata and controls
358 lines (338 loc) · 10.8 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef TASKSCHED_H
#define TASKSCHED_H
//#define DEBUGA 1
#include "Arduino.h"
//#include <list>
#define TASK_SECOND 1000
#define TASK_MINUTE 60*TASK_SECOND
#include <SimpleList.h>
class Task;
// was typedef std::function<void(Task *)> voidFuncTypeWith;
/**
* @brief This class holds the initial state of a task.
*
* It's kinda like the DTO in the Java world.
*/
class InitialState {
public:
/**
* @brief This member holds the original interval value.
*/
unsigned long mInterval;
/**
* @brief saved enable flag
*/
bool mEnabled;
/**
* @brief saved run immediately flag
* @return
*/
bool mRunImmediately;
/**
* @brief saved run iterations count
* @return
*/
unsigned long mIterations;
};
using savedInitial = InitialState;
/** Define a new Task.
* @param func:
* Function to be called at regular intervals.
* @param interval:
* Time 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.
* @param enabled:
* Flag to indicate whether the task should start in an enabled state (true) or disabled state (false).
* @param iterations:
* Number of times the task will be executed. Set to 0 for infinite iterations.
* @param name:
* A descriptive string name for the task.
* @param runImmediately:
* Boolean flag determining whether to run the callback immediately (true) or wait for the interval to expire (false).
*
*/
/**
@code
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
@endcode
*/
/**
* @brief Represents a schedulable task.
*
* The Task class encapsulates a function to be called at regular intervals,
* along with parameters controlling its execution.
*/
class Task {
private:
/**
* @brief function used by the schedule to run this task, shouldn't be called by user
*/
void runIt();
// make Sched a friend so it can call a private method, runIt
friend class Sched;
public:
// Function pointer types
typedef void (*TaskCallback)(Task*);
typedef void (*VoidCallback)();
/**
* @brief Constructs a new Task.
*
* @tparam T Type of the interval (can be integral or floating-point)
* @param func Function to be called at regular intervals
* @param interval Time between calls to func (in milliseconds if integral, seconds if floating-point)
* @param enabled Flag to indicate whether the task should start in an enabled state
* @param iterations Number of times the task will be executed (0 for infinite)
* @param name A descriptive name for the task
* @param runImmediately Whether to run the callback immediately or wait for the interval
* @return The first form returns a pointer to the task, the second does not.
*/
Task(TaskCallback func, unsigned long interval = 5000, bool enabled = false,
int iterations = 0, const char* name = "Unk", bool runImmediately = false);
// Constructor for callback without Task pointer
Task(VoidCallback func, unsigned long interval = 5000, bool enabled = false,
int iterations = 0, const char* name = "Unk", bool runImmediately = false);
private:
/**
* @brief Contains a pointer to the function that will be called when the task is run.
* @return It returns a pointer to the Task object.
*/
TaskCallback mProcWithTask;
/**
* @brief Contains a pointer to the function that will be called when the task is run.
*/
VoidCallback mProcVoid;
/**
* @brief Contains a pointer to the function that will be called when the task is run.
*/
bool mWithTaskPtr;
/**
* @brief This stores the interval that was passed at object creation.
*/
unsigned long mInterval;
/**
* @brief This stores the enable status of the Task. It was set at object creation.
*/
bool mEnabled;
/**
* @brief This is the number of iterations that the Task object will run.
*/
unsigned long mIterations;
/**
* @brief Name of the Task set at creation.
*/
String mName;
/**
* @brief This is a flag that tells the scheduler to initiall run the Task immediately. Otherwise it will run after
* the interval expires.
*/
bool mRunImmediately;
/**
* @brief This is where the iteration counter is stored.
*/
unsigned long mIterationCount;
/**
* @brief This is a flag used internally to limit the showInit method runs only once.
*/
bool doShow=true;
/**
* @brief
*/
unsigned long mLastStartTime;
/**
* @brief
*/
savedInitial mOrig;
/**
* @brief return true if this is the first iteration
* @return
*/
public:
bool isFirstIteration();
/**
* @brief return true if this is the last iteration
* @return
*/
bool isLastIteration();
/**
* @brief return true if the run immediately flag is set
* @return
*/
bool fRunImmediately();
/**
* @brief restart the task with the original parameters, Enable is not restored
*/
void restart();
/**
* @brief enable the task
*/
void enable();
/**
* @brief disable the task
*/
void disable();
/**
* @brief return true if task is enabled
* @return
*/
bool isEnabled() const;
/**
* @brief Sets a new callback function for the task.
* @param callback The new callback function
*/
void setCallback(const TaskCallback &callback);
/**
* @brief Sets a new callback function for the task.
* @param callback The new callback function
*/
void setCallback(const VoidCallback &callback);
/**
* @brief give the task a new name
*/
void setName(String);
/**
* @brief display stuff
*/
void showInit();
/**
* @brief return string containing name of task
* @return
*/
String getName() const;
/**
* @brief return the iteration count, that is the number of iterations that the task has been run
* @return
*/
unsigned long getIterationCount() const;
/**
* @brief function to set a new interval
*/
void setInterval(unsigned long newInterval);
/**
* @brief function to set a new iterations value
*/
void setIterations(unsigned long newIterations);
/**
* @brief function to set the run immediately flag
*/
void setImmediately(bool);
/**
* @brief function that displays task info
*
* @return String containing info about this task.
*/
String showTaskInfo() const;
/**
* @brief return the task interval
* @return
*/
unsigned long getInterval(void) const;
/**
* @brief return the run immediately flag
* @return
*/
bool getRunImmediately(void) const;
/**
* @brief return the last start time flag
* @return
*/
unsigned long getLastStartTime(void) const;
/**
* @brief return a string with a formatted time
* @return
*/
static String formatMS(unsigned long milliseconds);
};
/**
@code
Task* t = new Task(turnLedOn, 1000,true,20,"OnOff",true);
Task t1(turnLedOn, 100,false,20,"On1",false);
Task t2(turnLedOn, 2.0,false,20,"On2",false);
@endcode
*/
/**
* @brief This class schedules the tasks.
*
*/
class Sched {
public:
/**
* @brief return the number of tasks in the run queue
* @return
*/
unsigned long getSize();
/** Constructor
*/
Sched();
/** used to start the scheduling. A call to begin will also enable the scheduler.
```
Sched scheduler;
scheduler.begin();
```
*/
/**
* @brief
*/
void begin();
/**
```
Example:
Task* t = new Task(turnLedOn, 1000,true,20,"OnOff",true);
Task t1(dummy, 100, false, 20, "On1", * false);
Task t2(dummy, 2.0, false, 20, "On2", * false);
scheduler.addTask(t);
scheduler.addTask(&t1);
scheduler.addTask(&t2);
```
*/
/**
* @brief
*/
void addTask(Task *task);
/**
* @brief enable the scheduler
*/
void enable();
/**
* @brief disable the scheduler
*/
void disable();
/**
* @brief return true if the scheduler is enabled
* @return
*/
bool isEnabled();
/** brief */
const SimpleList<Task *>& getTasks() const;
/**
* @brief called perodically to check if a task should be scheduled
*/
void run();
private:
//SimpleList<Task*> tTasks;
/**
* @brief
* @return
*/
SimpleList<Task *> tTasks;
/**
* @brief
* @return
*/
bool mSchedEnabled;
};
///home/jwl/sketchbook/libraries/TaskSched/TaskSched.h 127 Task Rand_4720, Diff 0:09:054, Interval 0:04:720, RI 0
/* @param func Function to be called.
* @param interval Time between calls to func in milliseconds.
* If intval is passed as a floating point number, e. g. 4.5 then it is treated as if it were a number of seconds otherwise the interval is treated as if it were a number of millisconds
* @param enabled Flag to be set if the task should start in an enabled state.
* @param iterations Number of iterations the task will be run. Set to zero for infinited iterations.
* @param name A String value of a name of the task.
* @param runImmediately A boolean flag that directs the scheduler to run the callback immediately rather than wait for the interval to expire. False says to wait for the interval to run the task.
*/
#endif