-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMillisTaskManager.cpp
337 lines (291 loc) · 5.86 KB
/
MillisTaskManager.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
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
#include "MillisTaskManager.h"
#ifndef NULL
#define NULL 0
#endif
#define TASK_NEW(task) \
do \
{ \
task = new Task_t; \
} while (0)
#define TASK_DEL(task) \
do \
{ \
delete task; \
} while (0)
/**
* @brief initialization task list
* @param priorityEnable: Set whether to enable priority
* @retval None
*/
MillisTaskManager::MillisTaskManager(bool priorityEnable)
{
PriorityEnable = priorityEnable;
Head = NULL;
Tail = NULL;
}
/**
* @brief scheduler destructor, release task list memory
* @param none
* @retval None
*/
MillisTaskManager::~MillisTaskManager()
{
Task_t *now = Head; // Move to the head of the list.
while (true)
{
if (now == NULL)
break;
Task_t *now_del = now;
now = now->Next;
TASK_DEL(now_del);
}
}
/**
* @brief Add a task to the task list and set the interval execution time
* @param func: task function pointer
* @param timeMs: cycle time setting (milliseconds)
* @param state: task switch
* @retval task node address
*/
MillisTaskManager::Task_t *MillisTaskManager::Register(TaskFunction_t func, uint32_t timeMs, bool state)
{
Task_t *task = Find(func);
if (task != NULL)
{
task->Time = timeMs;
task->State = state;
task->FirstExecut = true;
return task;
}
TASK_NEW(task);
if (task == NULL)
{
return NULL;
}
task->Function = func;
task->Time = timeMs;
task->State = state;
task->FirstExecut = true;
task->TimePrev = 0;
task->TimeCost = 0;
task->TimeError = 0;
task->Next = NULL;
if (Head == NULL)
{
Head = task;
}
else
{
Tail->Next = task;
}
Tail = task;
return task;
}
/**
* @brief find the task, return the task node
* @param func: task function pointer
* @retval task node address
*/
MillisTaskManager::Task_t *MillisTaskManager::Find(TaskFunction_t func)
{
Task_t *now = Head;
Task_t *task = NULL;
while (true)
{
if (now == NULL)
break;
if (now->Function == func)
{
task = now;
break;
}
now = now->Next;
}
return task;
}
/**
* @brief Get the previous node of the current node
* @param task: current task node address
* @retval previous task node address
*/
MillisTaskManager::Task_t *MillisTaskManager::GetPrev(Task_t *task)
{
Task_t *now = Head;
Task_t *prev = NULL;
Task_t *retval = NULL;
while (true)
{
if (now == NULL)
{
break;
}
if (now == task)
{
retval = prev;
break;
}
prev = now;
now = now->Next;
}
return retval;
}
/**
* @brief logout task (use with caution, thread-unsafe)
* @param func: task function pointer
* @retval true: success; false: failure
*/
bool MillisTaskManager::Logout(TaskFunction_t func)
{
Task_t *task = Find(func);
if (task == NULL)
return false;
Task_t *prev = GetPrev(task);
Task_t *next = task->Next;
if (prev == NULL && next != NULL)
{
Head = next;
}
else if (prev != NULL && next == NULL)
{
prev->Next = NULL;
}
else if (prev != NULL && next != NULL)
{
prev->Next = next;
}
TASK_DEL(task);
return true;
}
/**
* @brief task state control
* @param func: task function pointer
* @param state: task state
* @retval true: success; false: failure
*/
bool MillisTaskManager::SetState(TaskFunction_t func, bool state)
{
Task_t *task = Find(func);
if (task == NULL)
return false;
task->State = state;
return true;
}
/**
* @brief task execution cycle setting
* @param func: task function pointer
* @param timeMs: task execution cycle
* @retval true: success; false: failure
*/
bool MillisTaskManager::SetIntervalTime(TaskFunction_t func, uint32_t timeMs)
{
Task_t *task = Find(func);
if (task == NULL)
return false;
task->Time = timeMs;
return true;
}
/**
* @brief reset task execution time
* @param func: task function pointer
* @param timeMs: reset time
* @retval true: success; false: failure
*/
bool MillisTaskManager::ReSetTaskTime(TaskFunction_t func, uint32_t timeMs)
{
Task_t *task = Find(func);
if (task == NULL)
return false;
task->TimePrev = timeMs;
return true;
}
#if (MTM_USE_CPU_USAGE == 1)
#include "Arduino.h"
static uint32_t UserFuncLoopUs = 0;
/**
* @brief Get CPU usage
* @param none
* @retval CPU usage, 0~100%
*/
float MillisTaskManager::GetCPU_Usage()
{
static uint32_t MtmStartUs;
float usage = (float)UserFuncLoopUs / (micros() - MtmStartUs) * 100.0f;
if (usage > 100.0f)
usage = 100.0f;
MtmStartUs = micros();
UserFuncLoopUs = 0;
return usage;
}
#endif
/**
* @brief time difference judgment
* @param nowTick: current time
* @param prevTick: previous time
* @retval time difference
*/
uint32_t MillisTaskManager::GetTickElaps(uint32_t nowTick, uint32_t prevTick)
{
uint32_t actTime = nowTick;
if (actTime >= prevTick)
{
prevTick = actTime - prevTick;
}
else
{
prevTick = /*UINT32_MAX*/ 0xFFFFFFFF - prevTick + 1;
prevTick += actTime;
}
return prevTick;
}
/**
* @brief Get the time spent on a single task (us)
* @param func: task function pointer
* @retval task single time consumption (us)
*/
uint32_t MillisTaskManager::GetTimeCost(TaskFunction_t func)
{
Task_t *task = Find(func);
if (task == NULL)
return 0;
return task->TimeCost;
}
/**
* @brief scheduler (kernel)
* @param tick: provide a system clock variable accurate to milliseconds
* @retval None
*/
void MillisTaskManager::Running(uint32_t tick)
{
Task_t *now = Head;
while (true)
{
if (now == NULL)
{
break;
}
if (now->Function != NULL && now->State)
{
uint32_t elapsTime = GetTickElaps(tick, now->TimePrev);
if ((elapsTime >= now->Time) || (now->FirstExecut == true))
{
now->FirstExecut = false;
now->TimeError = elapsTime - now->Time;
now->TimePrev = tick;
#if (MTM_USE_CPU_USAGE == 1)
uint32_t start = micros();
now->Function();
uint32_t timeCost = micros() - start;
now->TimeCost = timeCost;
UserFuncLoopUs += timeCost;
#else
now->Function();
#endif
if (PriorityEnable)
{
break;
}
}
}
now = now->Next;
}
}