forked from cproape/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk_timer_samples.c
165 lines (137 loc) · 3.84 KB
/
tk_timer_samples.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
/**
* 说明:
* 创建4个定时器
* 定时器1和2为静态创建,通过user_data配置定时器名称,共用一个超时回调函数。
* 定时器3和4为动态创建,单独有自己的超时回调函数
* 到达20 tick 脱离timer2和删除timer4,只有timer1和3工作
* 为了演示方便,main函数主循环1s tick加1,并循环运行tk_timer_loop_handler函数。
*
* Change Logs:
* Date Author Notes
* 2020-01-29 zhangran the first version
* 2023-04-17 shadow3d change comment format
*/
#include <windows.h>
#include <stdio.h>
#include "toolkit.h"
uint32_t tick = 0;
/* 定义获取系统tick回调函数 */
uint32_t get_sys_tick(void)
{
return tick;
}
/* 定时器句柄 */
struct tk_timer timer1;
struct tk_timer timer2;
struct tk_timer *timer3 = NULL;
struct tk_timer *timer4 = NULL;
/* 定时器1和2共用的超时回调函数 */
void timer_timeout_callback(struct tk_timer *timer)
{
if (timer == &timer1)
printf("timeout_callback: [%s] timeout:%ld\n", (const char*)timer->user_data, get_sys_tick());
else if (timer == &timer2)
printf("timeout_callback: [%s] timeout:%ld\n", (const char*)timer->user_data, get_sys_tick());
}
/* 定时器3超时回调函数 */
void timer3_timeout_callback(struct tk_timer *timer)
{
printf("timeout_callback: timer3 timeout:%ld\n", get_sys_tick());
}
/* 定时器4超时回调函数 */
void timer4_timeout_callback(struct tk_timer *timer)
{
printf("timeout_callback: timer4 timeout:%ld\n", get_sys_tick());
}
int main(int argc, char *argv[])
{
/* 初始化软件定时器功能,并配置tick获取回调函数*/
tk_timer_func_init(get_sys_tick);
/* 静态创建定时器1和2 */
tk_timer_init(&timer1, timer_timeout_callback);
tk_timer_init(&timer2, timer_timeout_callback);
timer1.user_data = "timer1";
timer2.user_data = "timer2";
/* 动态创建定时器3 */
timer3 = tk_timer_create(timer3_timeout_callback);
if (timer3 == NULL)
{
printf("timer3 create error!\n");
return 0;
}
/* 动态创建定时器4 */
timer4 = tk_timer_create(timer4_timeout_callback);
if (timer4 == NULL)
{
printf("timer4 create error!\n");
return 0;
}
/* 启动定时器1,循环模式,5tick时长 */
tk_timer_start(&timer1, TIMER_MODE_LOOP, 5);
/* 启动定时器2,循环模式,3tick时长 */
tk_timer_start(&timer2, TIMER_MODE_LOOP, 3);
/* 启动定时器3,循环模式,5tick时长 */
tk_timer_start(timer3, TIMER_MODE_LOOP, 5);
/* 启动定时器4,循环模式,3tick时长 */
tk_timer_start(timer4, TIMER_MODE_LOOP, 3);
while (1)
{
/* 定时器处理程序 */
tk_timer_loop_handler();
printf("tick:%ld\n", get_sys_tick());
/* 到达20 tick 脱离timer2和删除timer4 */
if (tick == 20)
{
tk_timer_detach(&timer2);
tk_timer_delete(timer4);
printf("timer2 detach timer4 delete.\n");
}
tick++;
Sleep(1000);
}
return 0;
}
#if 0
/* 单片机 伪代码 */
#include <stdio.h>
#include "toolkit.h"
struct tk_timer timer1;
struct tk_timer timer2;
/* stm32 滴答定时器中断,假设1ms中断一次 */
void SysTick_Handler(void)
{
tick++;
}
/* 定义获取系统tick回调函数 */
uint32_t get_sys_tick(void)
{
return tick;
}
/* 定时器1超时回调函数 */
void timer1_timeout_cb(struct tk_timer *timer)
{
printf("timeout_callback: timer1 timeout.\n");
}
/* 定时器2超时回调函数 */
void timer2_timeout_cb(struct tk_timer *timer)
{
printf("timeout_callback: timer2 timeout.\n");
}
int main(void)
{
bsp_init();
/* 初始化软件定时器功能,并配置tick获取回调函数*/
tk_timer_func_init(get_sys_tick);
/* 静态创建定时器1和2 */
tk_timer_init(&timer1, (timeout_callback*)timer1_timeout_cb);
tk_timer_init(&timer2, (timeout_callback*)timer2_timeout_cb);
/* 启动定时器1,循环模式,5秒时长 */
tk_timer_start(&timer1, TIMER_MODE_LOOP, 5000);
/* 启动定时器2,循环模式,3秒时长 */
tk_timer_start(&timer2, TIMER_MODE_LOOP, 1000);
while (1)
{
tk_timer_loop_handler();
}
}
#endif