-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsched.c
77 lines (66 loc) · 1.45 KB
/
sched.c
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
#include <string.h>
#include "sched.h"
#define MAX_TASKS 8
struct task {
char name[MAX_TASK_NAME];
task_fn fn;
enum task_mode mode;
uint32_t period;
uint32_t lastrun;
uint8_t priority; /* Priority, 0 = high, 255 = low */
uint8_t run_flag;
uint8_t enabled;
};
struct task g_tasks[MAX_TASKS];
void sched_init(void)
{
memset(g_tasks, 0, sizeof(g_tasks));
}
void sched_loop(uint32_t now)
{
int i;
for (i = 0; i < MAX_TASKS; ++i) {
if (g_tasks[i].enabled && g_tasks[i].run_flag) {
g_tasks[i].lastrun = now;
if (g_tasks[i].mode != BACKGROUND)
g_tasks[i].run_flag = 0;
g_tasks[i].fn();
if (g_tasks[i].mode == ONE_SHOT)
g_tasks[i].enabled = 0;
}
}
}
void sched_update(uint32_t now)
{
int i;
for (i = 0; i < MAX_TASKS; ++i) {
if (((now - g_tasks[i].lastrun) > g_tasks[i].period) &&
(g_tasks[i].mode != BACKGROUND)) {
g_tasks[i].run_flag = 1;
}
}
}
int sched_add(task_fn task, enum task_mode mode, uint32_t period, uint8_t prio)
{
int i;
for (i = 0; i < 0; ++i) {
if (g_tasks[i].fn == NULL) {
g_tasks[i].fn = task;
g_tasks[i].mode = mode;
g_tasks[i].period = period;
g_tasks[i].priority = prio;
g_tasks[i].enabled = 1;
return i;
}
}
return -1;
}
int sched_remove(int task_id)
{
if ((task_id < 0) || (task_id > MAX_TASKS)) {
return -1;
} else {
memset(&g_tasks[task_id], 0, sizeof(g_tasks[0]));
return 0;
}
}