forked from xiaoliang314/libatask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
169 lines (138 loc) · 4.98 KB
/
main.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
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
/*
* Copyright (C) 2018 xiaoliang<[email protected]>.
*/
#include "../lib/atask.h"
#include <stdio.h>
/* Asynchronous function 1 */
/* 异步函数1 */
static void async_func1(task_t *task, event_t *ev, time_ms_t interval, time_ms_t timeout)
{
/* Defines the bpd pointer required for the coroutine (asynchronous function) */
/* 定义协程(异步函数)所需的bpd指针 */
uint8_t *bpd = TASK_BPD(task);
/* Define asynchronous variables for coroutines (asynchronous functions) */
/* 定义协程(异步函数)的异步变量 */
struct vars
{
/* Define a timer */
/* 定义一个定时器 */
timer_event_t timer;
/* Total execution time */
/* 总执行时间 */
time_ms_t total;
/* Execution time*/
/* 已执行时间*/
time_ms_t current;
/* execution interval */
/* 执行间隔 */
time_ms_t interval;
/* binds the address of the asynchronous variable to the stack space of the current task */
/* 将异步变量的地址绑定到当前任务的栈空间中 */
} *vars = task_asyn_vars_get(task, sizeof(*vars));
/* Coroutine start */
/* 开始协程 */
bpd_begin(2);
/* can be used to initialize other events directly using the event in the task.
* Guarantee that the current coroutine can be restored after these events are returned */
/* 可直接使用task中的event对其他事件进行初始化,
* 保证这些事件返回之后可恢复当前协程 */
timer_init_inherit(&vars->timer, &task->event);
/* Save the parameters to the asynchronous variable before yield, so that it can still be used after yield */
/* 在yield之前先将参数保存到异步变量中,使之yield之后仍能使用 */
vars->total = timeout;
vars->interval = interval;
vars->current = 0;
for (vars->current = 0;
vars->current < vars->total;
vars->current += vars->interval * 2)
{
/* delay interval ms */
/* 延时interval ms */
el_timer_start_ms(&vars->timer, vars->interval);
bpd_yield(1);
printf("LED ON\n");
/* delay interval ms */
/* 延时interval ms */
el_timer_start_ms(&vars->timer, vars->interval);
bpd_yield(2);
printf("LED OFF\n");
}
/* Coroutine end */
/* 结束协程 */
bpd_end();
/* Coroutine (asynchronous function) returns to the asynchronous caller */
/* 协程(异步函数)返回到异步调用者 */
task_asyn_return(task);
}
/* Asynchronous function 2 */
/* 异步函数2 */
static void async_func2(task_t *task, event_t *ev)
{
uint8_t *bpd = TASK_BPD(task);
bpd_begin(2);
while (1)
{
/* Asynchronous call asynchronous function 1 Parameters: 1000,8000 */
/* 异步调用异步函数1 参数:1000,8000*/
task_bpd_asyn_call(1, task, async_func1, 1000, 8000);
/* Asynchronous call asynchronous function 2 Parameters: 2000,10000*/
/* 异步调用异步函数1 参数:2000,10000*/
task_bpd_asyn_call(2, task, async_func1, 2000, 10000);
}
bpd_end();
}
/* Task end callback */
/* 任务结束回调 */
static void on_task_end(void *ctx, event_t *ev)
{
printf("%s\n", (const char *)ctx);
}
/* Separate timer callback */
/* 单独的定时器回调 */
static void on_timer(void *ctx, event_t *ev)
{
printf("%s\n", (const char *)ctx);
el_timer_start_ms((timer_event_t*)ev, 1000);
}
int main()
{
event_t task_end_ev;
timer_event_t timer;
/*
* Define some task with a stack size of 128 bytes
* and an event priority of LOWER_GROUP_PRIORITY
*/
/*
* 定义几个任务,栈大小128字节,事件优先级为LOWER_GROUP_PRIORITY
*/
TASK_DEFINE(task1, 128, LOWER_GROUP_PRIORITY);
TASK_DEFINE(task2, 128, LOWER_GROUP_PRIORITY);
TASK_DEFINE(task3, 128, LOWER_GROUP_PRIORITY);
/* Start asynchronous function (task) 2 */
/* 启动异步函数(任务)2 */
task_start(&task1, async_func2);
/* Start multiple asynchronous functions (tasks) 2 */
/* 开启多个异步函数(任务)2 */
task_start(&task2, async_func2);
/* Initialize an event */
/* 初始化一个事件 */
event_init(&task_end_ev, on_task_end, "Async function 1 end!\n", LOWER_GROUP_PRIORITY);
/* Set to task3 to end the notification event */
/* 设置为task3结束通知事件 */
task_end_wait(&task3, &task_end_ev);
/* Start asynchronous function with parameters (task) 1 */
/* 带参数启动异步函数(任务)1 */
task_start(&task3, async_func1, 5000, 15000);
/* Initialize timer */
/* 初始化定时器 */
timer_init(&timer, on_timer, "This is a timer event!\n", LOWER_GROUP_PRIORITY);
/* Start a separate timer event */
/* 启动单独的定时器事件 */
el_timer_start_ms(&timer, 1500);
/* Running the atask kernel */
/* 运行atask内核 */
while (1)
{
el_schedule();
}
}