-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskScheduler.cpp
304 lines (230 loc) · 8.18 KB
/
TaskScheduler.cpp
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
#include "TaskScheduler.h"
// Public Functions
// ============================================================================
TaskScheduler::TaskScheduler()
{
//zero out the task array, and all the struct-type member variables
memset(arrayOfDelayedTasks, 0, sizeof(arrayOfDelayedTasks));
memset(&nextScheduledTask, 0, sizeof(nextScheduledTask));
memset(&arrayOfEventTasks, 0, sizeof(arrayOfEventTasks));
idOfNextScheduledTask = 0;
eventFlags = 0;
shouldExitWhenPossible = false;
for(uint8_t i = 0; i < MAX_NUM_DELAYED_TASKS; i++)
{
removeScheduledTask(i);
}
}//end constructor
taskToken_t TaskScheduler::scheduleDelayedTask(pTaskFunc newtask, void* newClientData,
uint32_t delayAmount)
{
taskToken_t arrPosition = findFreeSpotInDelayedTaskArray();
if( arrPosition == 0 )
{
//no more spots in the task array, or we got passed in a null pointer
return 0;
}
else if(newtask == 0)
{
return 0;
}
arrayOfDelayedTasks[arrPosition].pTask = newtask;
arrayOfDelayedTasks[arrPosition].clientData = newClientData;
arrayOfDelayedTasks[arrPosition].scheduledTime = millis() + delayAmount;
//Figure out which task is scheduled next
sync();
return arrPosition;
}//end scheduleDelayedTask
bool TaskScheduler::removeScheduledTask( taskToken_t taskId )
{
bool wasDeleteSuccess = false;
if( taskId < MAX_NUM_DELAYED_TASKS )
{
if(arrayOfDelayedTasks[taskId].pTask != NULL)
{
//delete the task
arrayOfDelayedTasks[taskId].pTask = 0;
arrayOfDelayedTasks[taskId].clientData = 0;
arrayOfDelayedTasks[taskId].scheduledTime = 0;
sync();
wasDeleteSuccess = true;
}
}
return wasDeleteSuccess;
}//end removeScheduledTask
//This function reschedule a task to another time
// Also updates the task handler function and client data
void TaskScheduler::rescheduleDelayedTask (taskToken_t taskId , pTaskFunc newtask, void* newClientData, uint32_t delayAmount)
{
arrayOfDelayedTasks[taskId].pTask = newtask;
arrayOfDelayedTasks[taskId].clientData = newClientData;
arrayOfDelayedTasks[taskId].scheduledTime = millis() + delayAmount;
sync();
}//end rescheduleDelayedTask
void TaskScheduler::doEventLoop()
{
shouldExitWhenPossible = false;
sync();
while(shouldExitWhenPossible == false)
{
uint32_t currentTime = millis();
// Check if it is time to handle the next delayed task.
if( currentTime >= nextScheduledTask.scheduledTime)
{
//Delete the task from the array.
// Do this before calling the task, because the task
// may wish to add itself back on the task list.
arrayOfDelayedTasks[idOfNextScheduledTask].pTask = 0;
arrayOfDelayedTasks[idOfNextScheduledTask].clientData = 0;
arrayOfDelayedTasks[idOfNextScheduledTask].scheduledTime = 0;
//call the task
(nextScheduledTask.pTask)(nextScheduledTask.clientData);
sync();
}
else
{
//it's not yet time to do the next task,
//do nothing
}
// Check if any event was triggered and needs to be handled
if( eventFlags > 0)
{
// At least one event is set
handleAllTriggeredEvents();
}
else
{
//No events have happened
//Do nothing
}
}//end while
}//end doEventLoop
void TaskScheduler::exit()
{
shouldExitWhenPossible = true;
}//end exit
eventId_t TaskScheduler::createEventTrigger(pTaskFunc eventHandler)
{
uint8_t arrIndex = 0;
// Find the next free spot in the array. Returns zero is no free spot is found.
arrIndex = findFreeSpotInEventTaskArray();
if( arrIndex != 0 )
{
// Free spot was found, save the event Handler function.
arrayOfEventTasks[arrIndex].pTask = eventHandler;
}
else
{
//no free spot found. Return 0.
}
return arrIndex;
}//end createEventTrigger
void TaskScheduler::triggerEvent(eventId_t eventId, void* clientData)
{
// Make sure this event has a handler
if(arrayOfEventTasks[eventId].pTask != NULL)
{
//set the flag for this event to indicate it has happened and should be handled
eventFlags = eventFlags | (1 << eventId);
//copy over the client data
arrayOfEventTasks[eventId].clientData = clientData;
//Serial.println("Event flag set for the trigger event"); delay(20);
}
else
{
//Serial.println("pTask is null! Event not set"); delay(20);
// Trying to trigger an event that does not exist.
// Do nothing.
}
}//end triggerEvent
void TaskScheduler::deleteEventTrigger(eventId_t eventId)
{
//clear out the handler from the even task array
arrayOfEventTasks[eventId].pTask = NULL;
//Clear the task flag in case this event is trigger and waiting to be handled
eventFlags &= ~(1 << eventId);
}//end deleteEventTrigger
// Private Functions
// ============================================================================
taskToken_t TaskScheduler::findFreeSpotInDelayedTaskArray()
{
taskToken_t positionToReturn = 0;
// Position 0 is unused. It indicates an error.
for( uint8_t i = 1; i < MAX_NUM_DELAYED_TASKS; i++)
{
if(arrayOfDelayedTasks[i].pTask == NULL)
{
//found an empty position
positionToReturn = i;
break;
}
else
{
//Serial.print("Position "); //debug
//Serial.print(i); //debug
//Serial.print(" is occupied because pTask = "); //debug
//Serial.println((uint16_t)(arrayOfDelayedTasks[i].pTask)); //debug
//this array position is occupied
}
}
return positionToReturn;
}//end findFreeSpotInDelayedTaskArray
taskToken_t TaskScheduler::findFreeSpotInEventTaskArray()
{
taskToken_t positionToReturn = 0;
// Position 0 is unused. It indicates an error.
for( uint8_t i = 1; i < MAX_NUM_EVENT_TASKS; i++)
{
if(arrayOfEventTasks[i].pTask == NULL)
{
//found an empty position
positionToReturn = i;
break;
}
else
{
//this array position is occupied
}
}
return positionToReturn;
}//findFreeSpotInEventTaskArray
void TaskScheduler::handleAllTriggeredEvents()
{
uint32_t mask = 0;
//todo: should be MAX_NUM_EVENT_TASKS + 1
for( uint8_t i = 1; i < MAX_NUM_EVENT_TASKS; i++)
{
mask = (1 << i);
// If this flag is set
if( (eventFlags & mask) > 0)
{
//clear the flag
eventFlags &= ~(mask);
//call the handler function
(arrayOfEventTasks[i].pTask)(arrayOfEventTasks[i].clientData);
}
else
{
// This flag is not set. Go on to the next one.
}
}
}//end handleAllTriggeredEvents
void TaskScheduler::sync()
{
//find the task with the earliest scheduled time
uint32_t earliestTime = -1; // the largest value
// Position 0 is unused. It indicates an error.
for( taskToken_t i = 1; i < MAX_NUM_DELAYED_TASKS; i++)
{
if( (arrayOfDelayedTasks[i].pTask != NULL) && //this array position is occupied
(arrayOfDelayedTasks[i].scheduledTime < earliestTime) ) //and the time is earlier
{
earliestTime = arrayOfDelayedTasks[i].scheduledTime;
idOfNextScheduledTask = i;
nextScheduledTask.pTask = arrayOfDelayedTasks[i].pTask;
nextScheduledTask.clientData = arrayOfDelayedTasks[i].clientData;
nextScheduledTask.scheduledTime = arrayOfDelayedTasks[i].scheduledTime;
}
}
//TODO: handle the case when no delayed tasks are scheduled
}//end sync