├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── README.pdf ├── include ├── toolkit.h └── toolkit_cfg.h ├── samples ├── tk_event_samples.c ├── tk_queue_samples.c └── tk_timer_samples.c └── src ├── tk_event.c ├── tk_queue.c └── tk_timer.c /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MicroPython.executeButton": [ 3 | { 4 | "text": "▶", 5 | "tooltip": "运行", 6 | "alignment": "left", 7 | "command": "extension.executeFile", 8 | "priority": 3.5 9 | } 10 | ], 11 | "MicroPython.syncButton": [ 12 | { 13 | "text": "$(sync)", 14 | "tooltip": "同步", 15 | "alignment": "left", 16 | "command": "extension.execute", 17 | "priority": 4 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Cproape (911830982@qq.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ToolKit 2 | 3 | ![version](https://img.shields.io/badge/version-V1.0.6-brightgreen.svg) ![date](https://img.shields.io/badge/date-2020.12.09-brightgreen.svg) 4 | 5 | ## 1、介绍 6 | 7 | [ToolKit](https://github.com/cproape)是一套应用于嵌入式系统的通用工具包,可灵活应用到有无RTOS的程序中,采用C语言面向对象的思路实现各个功能,尽可能最大化的复用代码,目前为止工具包包含:**循环队列、软件定时器、事件集**。 8 | 9 | - **Queue** 循环队列 10 | 1. 支持动态、静态方式进行队列的创建与删除。 11 | 2. 可独立配置缓冲区大小。 12 | 3. 支持数据**最新保持**功能,当配置此模式并且缓冲区已满,若有新的数据存入,将会移除最早数据,并保持缓冲区已满。 13 | - **Timer** 软件定时器 14 | 1. 支持动态、静态方式进行定时器的创建与删除。 15 | 2. 支持**循环**、**单次**模式。 16 | 3. 可配置有无超时回调函数。 17 | 4. 可配置定时器工作在**周期**或**间隔**模式。 18 | 5. 使用双向链表,超时统一管理,不会因为增加定时器而增加超时判断代码。 19 | - **Event** 事件集 20 | 1. 支持动态、静态方式进行事件集的创建与删除。 21 | 2. 每个事件最大支持**32**个标志位。 22 | 3. 事件的触发可配置为**“标志与”**和**“标志或”**。 23 | 24 | ## 2 、文件目录 25 | 26 | ``` 27 | toolkit 28 | ├── include // 包含文件目录 29 | | ├── toolkit.h // toolkit头文件 30 | | └── toolkit_cfg.h // toolkit配置文件 31 | ├── src // toolkit源码目录 32 | | ├── tk_queue.c // 循环队列源码 33 | | ├── tk_timer.c // 软件定时器源码 34 | | └── tk_event.c // 事件集源码 35 | ├── samples // 例子 36 | | ├── tk_queue_samples.c // 循环队列使用例程源码 37 | | ├── tk_timer_samples.c // 软件定时器使用例程源码 38 | | └── tk_event_samples.c // 事件集使用例程源码 39 | └── README.md // 说明文档 40 | ``` 41 | 42 | ## 3 、函数定义 43 | 44 | ### 3.1 配置文件 45 | 46 | **** 47 | 48 | - **ToolKit配置项** 49 | 50 | | 宏定义 | 描述 | 51 | | -------------------- | ------------------------- | 52 | | TOOLKIT_USING_ASSERT | ToolKit使用断言功能 | 53 | | TOOLKIT_USING_QUEUE | ToolKit使用循环队列功能 | 54 | | TOOLKIT_USING_TIMER | ToolKit使用软件定时器功能 | 55 | | TOOLKIT_USING_EVENT | ToolKit使用事件集功能 | 56 | 57 | - **Queue 循环队列配置项** 58 | 59 | | 宏定义 | 描述 | 60 | | --------------------- | -------------------------------- | 61 | | TK_QUEUE_USING_CREATE | Queue 循环队列使用动态创建和删除 | 62 | 63 | - **Timer 软件定时器配置项** 64 | 65 | | 宏定义 | 描述 | 66 | | ------------------------------- | ---------------------------------- | 67 | | TK_TIMER_USING_CREATE | Timer 软件定时器使用动态创建和删除 | 68 | | TK_TIMER_USING_INTERVAL | Timer 软件定时器使用间隔模式 | 69 | | TK_TIMER_USING_TIMEOUT_CALLBACK | Timer 软件定时器使用超时回调函数 | 70 | 71 | - **Event 事件集配置项** 72 | 73 | | 宏定义 | 描述 | 74 | | --------------------- | ------------------------------ | 75 | | TK_EVENT_USING_CREATE | Event 事件集使用动态创建和删除 | 76 | 77 | > **说明**:当配置**TOOLKIT_USING_ASSERT**后,所有功能都将会启动参数检查。 78 | 79 | 80 | 81 | ### 3.2 Queue 循环队列API函数 82 | 83 | **** 84 | 85 | > 以下为详细API说明及简要示例程序,综合demo可查看[tk_queue_samples.c](./samples/tk_queue_samples.c)示例。 86 | 87 | #### 3.2.1 动态创建队列 88 | 89 | > **注意**:当配置**TOOLKIT_USING_QUEUE**后,才能使用此函数。此函数需要用到**malloc**。 90 | 91 | ```c 92 | struct tk_queue *tk_queue_create(uint16_t queue_size, uint16_t max_queues, bool keep_fresh); 93 | ``` 94 | 95 | | 参数 | 描述 | 96 | | ---------- | ------------------------------------------------------------ | 97 | | queue_size | 缓存区大小(单位字节) | 98 | | max_queues | 最大队列个数 | 99 | | keep_fresh | 是否为保持最新模式,**true**:保持最新;**false**:默认(存满不能再存) | 100 | | 返回值 | 创建的队列对象(**NULL**为创建失败) | 101 | 102 | **队列创建示例:** 103 | 104 | ```c 105 | int main(int argc, char *argv[]) 106 | { 107 | /* 动态方式创建一个循环队"queue",缓冲区大小50字节,不保持最新 */ 108 | struct tk_queue *queue = tk_queue_create(50, 1, false); 109 | if( queue == NULL){ 110 | printf("队列创建失败!\n"); 111 | } 112 | /* ... */ 113 | /* You can add your code under here. */ 114 | return 0; 115 | } 116 | ``` 117 | 118 | #### **3.2.2** 动态删除队列 119 | 120 | > **注意**:当配置**TOOLKIT_USING_QUEUE**后,才能使用此函数。此函数需要用到**free**。必须为**动态**方式创建的队列对象。 121 | 122 | ```c 123 | bool tk_queue_delete(struct tk_queue *queue); 124 | ``` 125 | 126 | | 参数 | 描述 | 127 | | ------ | --------------------------------------- | 128 | | queue | 要删除的队列对象 | 129 | | 返回值 | **true**:删除成功;**false**:删除失败 | 130 | 131 | #### **3.2.3** 静态初始化队列 132 | 133 | ```c 134 | bool tk_queue_init(struct tk_queue *queue, void *queuepool, uint16_t pool_size, uint16_t queue_size, bool keep_fresh); 135 | ``` 136 | 137 | | 参数 | 描述 | 138 | | ---------- | ------------------------------------------------------------ | 139 | | queue | 要初始化的队列对象 | 140 | | *queuepool | 队列缓存区 | 141 | | pool_size | 缓存区大小(单位字节) | 142 | | queue_size | 队列元素大小(单位字节) | 143 | | keep_fresh | 是否为保持最新模式,**true**:保持最新;**false**:默认(存满不能再存) | 144 | | 返回值 | **true**:初始化成功;**false**:初始化失败 | 145 | 146 | **队列创建示例:** 147 | 148 | ```c 149 | int main(int argc, char *argv[]) 150 | { 151 | /* 定义一个循环队列 */ 152 | struct tk_queue queue; 153 | /* 定义循环队列缓冲区 */ 154 | uint8_t queue_pool[100]; 155 | /* 静态方式创建一个循环队列"queue",缓存区为queue_pool,大小为queue_pool的大小,模式为保持最新 */ 156 | if( tk_queue_init(&queue, queue_pool, sizeof(queue_pool), 157 | sizeof(queue_pool[0]), true) == false){ 158 | printf("队列创建失败!\n"); 159 | } 160 | /* ... */ 161 | /* You can add your code under here. */ 162 | } 163 | 164 | ``` 165 | 166 | #### 3.2.4 静态脱离队列 167 | 168 | > **注意**: 会使缓存区脱离与队列的关联。必须为**静态**方式创建的队列对象。 169 | 170 | ```c 171 | bool tk_queue_detach(struct tk_queue *queue); 172 | ``` 173 | 174 | | 参数 | 描述 | 175 | | ------ | --------------------------------------- | 176 | | queue | 要脱离的队列对象 | 177 | | 返回值 | **true**:脱离成功;**false**:脱离失败 | 178 | 179 | #### 3.2.5 清空队列 180 | 181 | ```c 182 | bool tk_queue_clean(struct tk_queue *queue); 183 | ``` 184 | 185 | | 参数 | 描述 | 186 | | ------ | --------------------------------------- | 187 | | queue | 要清空的队列对象 | 188 | | 返回值 | **true**:清除成功;**false**:清除失败 | 189 | 190 | #### 3.2.6 判断队列是否为空 191 | 192 | ```c 193 | bool tk_queue_empty(struct tk_queue *queue); 194 | ``` 195 | 196 | | 参数 | 描述 | 197 | | ------ | ------------------------------- | 198 | | queue | 要查询的队列对象 | 199 | | 返回值 | **true**:空;**false**:不为空 | 200 | 201 | #### 3.2.7 判断队列是否已满 202 | 203 | ```c 204 | bool tk_queue_full(struct tk_queue *queue); 205 | ``` 206 | 207 | | 参数 | 描述 | 208 | | ------ | ------------------------------- | 209 | | queue | 要查询的队列对象 | 210 | | 返回值 | **true**:满;**false**:不为满 | 211 | 212 | #### 3.2.8 从队列中读取一个元素(不从队列中删除) 213 | 214 | ```c 215 | bool tk_queue_peep(struct tk_queue *queue, void *pval); 216 | ``` 217 | 218 | | 参数 | 描述 | 219 | | ------ | --------------------------------------- | 220 | | queue | 队列对象 | 221 | | *pval | 读取值地址 | 222 | | 返回值 | **true**:读取成功;**false**:读取失败 | 223 | 224 | #### 3.2.9 移除一个元素 225 | 226 | ```c 227 | bool tk_queue_remove(struct tk_queue *queue); 228 | ``` 229 | 230 | | 参数 | 描述 | 231 | | ------ | --------------------------------------- | 232 | | queue | 要移除元素的对象 | 233 | | 返回值 | **true**:移除成功;**false**:移除失败 | 234 | 235 | #### 3.2.10 向队列压入(入队)1个元素数据 236 | 237 | ```c 238 | bool tk_queue_push(struct tk_queue *queue, void *val); 239 | ``` 240 | 241 | | 参数 | 描述 | 242 | | ------ | ------------------------------- | 243 | | queue | 要压入的队列对象 | 244 | | *val | 压入值 | 245 | | 返回值 | **true**:成功;**false**:失败 | 246 | 247 | #### 3.2.11 从队列弹出(出队)1个元素数据 248 | 249 | ```c 250 | bool tk_queue_pop(struct tk_queue *queue, void *pval); 251 | ``` 252 | 253 | | 参数 | 描述 | 254 | | ------ | ------------------------------- | 255 | | queue | 要弹出的队列对象 | 256 | | *pval | 弹出值 | 257 | | 返回值 | **true**:成功;**false**:失败 | 258 | 259 | #### 3.2.12 查询队列当前数据长度 260 | 261 | ```c 262 | uint16_t tk_queue_curr_len(struct tk_queue *queue); 263 | ``` 264 | 265 | | 参数 | 描述 | 266 | | ------ | ---------------- | 267 | | queue | 要查询的队列对象 | 268 | | 返回值 | 队列数据当前长度 | 269 | 270 | #### 3.2.13 向队列压入(入队)多个元素数据 271 | 272 | ```c 273 | uint16_t tk_queue_push_multi(struct tk_queue *queue, void *pval, uint16_t len); 274 | ``` 275 | 276 | | 参数 | 描述 | 277 | | ------ | ---------------- | 278 | | queue | 要压入的队列对象 | 279 | | *pval | 压入数据首地址 | 280 | | len | 压入元素个数 | 281 | | 返回值 | 实际压入个数 | 282 | 283 | #### 3.2.14 从队列弹出(出队)多个元素数据 284 | 285 | ```c 286 | uint16_t tk_queue_pop_multi(struct tk_queue *queue, void *pval, uint16_t len); 287 | ``` 288 | 289 | | 参数 | 描述 | 290 | | ------ | -------------------- | 291 | | queue | 要弹出的队列对象 | 292 | | *pval | 存放弹出数据的首地址 | 293 | | len | 希望弹出的数据个数 | 294 | | 返回值 | 实际弹出个数 | 295 | 296 | 297 | 298 | ### 3.3 Timer 软件定时器API函数 299 | 300 | **** 301 | 302 | > 以下为详细API说明及简要示例程序,综合demo可查看[tk_timer_samples.c](./samples/tk_timer_samples.c)示例。 303 | 304 | #### 3.3.1 软件定时器功能初始化 305 | 306 | > **注意**:此函数在使用定时器功能最初调用,目的是创建定时器列表头结点,和配置tick获取回调函数。 307 | 308 | ```c 309 | bool tk_timer_func_init(uint32_t (*get_tick_func)(void)); 310 | ``` 311 | 312 | | 参数 | 描述 | 313 | | ------------- | ------------------------------------------- | 314 | | get_tick_func | 获取系统tick回调函数 | 315 | | 返回值 | **true**:初始化成功;**false**:初始化失败 | 316 | 317 | #### 3.3.2 动态创建定时器 318 | 319 | > **注意**:当配置**TOOLKIT_USING_TIMER**后,才能使用此函数。此函数需要用到**malloc**。 320 | 321 | ```c 322 | struct tk_timer *tk_timer_create(void(*timeout_callback)(struct tk_timer *timer)); 323 | ``` 324 | 325 | | 参数 | 描述 | 326 | | ---------------- | -------------------------------------- | 327 | | timeout_callback | 定时器超时回调函数,不使用可配置为NULL | 328 | | 返回值 | 创建的定时器对象(**NULL**为创建失败) | 329 | 330 | **定时器创建示例:**: 331 | 332 | ```c 333 | /* 定义获取系统tick回调函数 */ 334 | uint32_t get_sys_tick(void) 335 | { 336 | return tick; 337 | } 338 | 339 | /* 定时器超时回调函数 */ 340 | void timer_timeout_callback(struct tk_timer *timer) 341 | { 342 | printf("timeout_callback: timer timeout:%ld\n", get_sys_tick()); 343 | } 344 | 345 | int main(int argc, char *argv[]) 346 | { 347 | /* 初始化软件定时器功能,并配置tick获取回调函数*/ 348 | tk_timer_func_init(get_sys_tick); 349 | 350 | /* 定义定时器指针 */ 351 | tk_timer_t timer = NULL; 352 | /* 动态方式创建timer,并配置定时器超时回调函数 */ 353 | timer = tk_timer_create((tk_timer_timeout_callback *)timer_timeout_callback); 354 | if (timer == NULL) 355 | { 356 | printf("定时器创建失败!\n"); 357 | return 0; 358 | } 359 | /* ... */ 360 | /* You can add your code under here. */ 361 | return 0; 362 | } 363 | 364 | ``` 365 | 366 | #### 3.3.3 动态删除定时器 367 | 368 | > 当配置**TOOLKIT_USING_TIMER**后,才能使用此函数。此函数需要用到**free**。必须为**动态**方式创建的定时器对象。 369 | 370 | ```c 371 | bool tk_timer_delete(struct tk_timer *timer); 372 | ``` 373 | 374 | | 参数 | 描述 | 375 | | ------ | --------------------------------------- | 376 | | timer | 要删除的定时器对象 | 377 | | 返回值 | **true**:删除成功;**false**:删除失败 | 378 | 379 | #### 3.3.4 静态初始化定时器 380 | 381 | ```c 382 | bool tk_timer_init(struct tk_timer *timer, void (*timeout_callback)(struct tk_timer *timer)); 383 | ``` 384 | 385 | | 参数 | 描述 | 386 | | ---------------- | ------------------------------------------ | 387 | | timer | 要初始化的定时器对象 | 388 | | timeout_callback | 定时器超时回调函数,不使用可配置为**NULL** | 389 | | 返回值 | **true**:创建成功;**false**:创建失败 | 390 | 391 | **队列创建示例:** 392 | 393 | ```c 394 | /* 定义获取系统tick回调函数 */ 395 | uint32_t get_sys_tick(void) 396 | { 397 | return tick; 398 | } 399 | 400 | /* 定时器超时回调函数 */ 401 | void timer_timeout_callback(struct tk_timer *timer) 402 | { 403 | printf("timeout_callback: timer timeout:%ld\n", get_sys_tick()); 404 | } 405 | 406 | int main(int argc, char *argv[]) 407 | { 408 | /* 定义定时器timer */ 409 | struct tk_timer timer; 410 | bool result = tk_timer_init( &timer,(tk_timer_timeout_callback *)timer_timeout_callback); 411 | if (result == NULL) 412 | { 413 | printf("定时器创建失败!\n"); 414 | return 0; 415 | } 416 | /* ... */ 417 | /* You can add your code under here. */ 418 | return 0; 419 | } 420 | ``` 421 | 422 | #### 3.3.5 静态脱离定时器 423 | 424 | > **注意**: 会将timer从定时器链表中移除。必须为**静态**方式创建的定时器对象。 425 | 426 | ```c 427 | bool tk_timer_detach(struct tk_timer *timer); 428 | ``` 429 | 430 | | 参数 | 描述 | 431 | | ------ | --------------------------------------- | 432 | | timer | 要脱离的定时器对象 | 433 | | 返回值 | **true**:脱离成功;**false**:脱离失败 | 434 | 435 | #### 3.3.6 定时器启动 436 | 437 | ```c 438 | bool tk_timer_start(struct tk_timer *timer, tk_timer_mode mode, uint32_t delay_tick); 439 | ``` 440 | 441 | | 参数 | 描述 | 442 | | ---------- | ------------------------------------------------------------ | 443 | | timer | 要启动的定时器对象 | 444 | | mode | 工作模式,**单次:** *TIMER_MODE_SINGLE*;**循环:** *TIMER_MODE_LOOP* | 445 | | delay_tick | 定时器时长(单位tick) | 446 | | 返回值 | **true**:启动成功;**false**:启动失败 | 447 | 448 | #### 3.3.7 定时器停止 449 | 450 | ```c 451 | bool tk_timer_stop(struct tk_timer *timer); 452 | ``` 453 | 454 | | 参数 | 描述 | 455 | | ------ | --------------------------------------- | 456 | | timer | 要停止的定时器对象 | 457 | | 返回值 | **true**:停止成功;**false**:停止失败 | 458 | 459 | #### 3.3.8 定时器继续 460 | 461 | ```c 462 | bool tk_timer_continue(struct tk_timer *timer); 463 | ``` 464 | 465 | | 参数 | 描述 | 466 | | ------ | --------------------------------------- | 467 | | timer | 要继续的定时器对象 | 468 | | 返回值 | **true**:继续成功;**false**:继续失败 | 469 | 470 | #### 3.3.9 定时器重启 471 | 472 | > **注意**:重启时长为最后一次启动定时器时配置的时长。 473 | 474 | ```c 475 | bool tk_timer_restart(struct tk_timer *timer); 476 | ``` 477 | 478 | | 参数 | 描述 | 479 | | ------ | --------------------------------------- | 480 | | timer | 要重启的定时器对象 | 481 | | 返回值 | **true**:重启成功;**false**:重启失败 | 482 | 483 | #### 3.3.10 获取定时器模式 484 | 485 | ```c 486 | tk_timer_mode tk_timer_get_mode(struct tk_timer *timer); 487 | ``` 488 | 489 | | 参数 | 描述 | 490 | | ------ | ------------------ | 491 | | timer | 要获取的定时器对象 | 492 | | 返回值 | 定时器模式 | 493 | 494 | | 定时器模式 | 描述 | 495 | | ----------------- | -------- | 496 | | TIMER_MODE_SINGLE | 单次模式 | 497 | | TIMER_MODE_LOOP | 循环模式 | 498 | 499 | #### 3.3.11 获取定时器状态 500 | 501 | ```c 502 | tk_timer_state tk_timer_get_state(struct tk_timer *timer); 503 | ``` 504 | 505 | | 参数 | 描述 | 506 | | ------ | ------------------ | 507 | | timer | 要获取的定时器对象 | 508 | | 返回值 | 定时器状态 | 509 | 510 | | 定时器模式 | 描述 | 511 | | ------------------- | -------- | 512 | | TIMER_STATE_RUNNING | 运行状态 | 513 | | TIMER_STATE_STOP | 停止状态 | 514 | | TIMER_STATE_TIMEOUT | 超时状态 | 515 | 516 | #### 3.3.12 定时器处理 517 | 518 | ```c 519 | bool tk_timer_loop_handler(void); 520 | ``` 521 | 522 | | 参数 | 描述 | 523 | | ------ | ------------------------------------------------------------ | 524 | | 返回值 | **true**:正常;**false**:异常,在调用此函数前,未初始化定时器功能“*tk_timer_func_init*” | 525 | 526 | > **注意**:tk_timer_loop_handler函数要不断的循环调用。 527 | 528 | #### 3.3.13 超时回调函数 529 | 530 | **函数原型**: 531 | 532 | ```c 533 | typedef void (*timeout_callback)(struct tk_timer *timer); 534 | ``` 535 | 536 | > **说明**:超时回调函数可定义多个,即一个定时器对应一个回调函数,也可多个定时器对应一个回调函数。 537 | 538 | * **一对一** 539 | 540 | ```c 541 | /* 定义两个回调函数,对应定时器timer1和timer2 */ 542 | void timer1_timeout_callback(struct tk_timer *timer){ 543 | printf("定时器1超时!\n"); 544 | } 545 | void timer2_timeout_callback(struct tk_timer *timer){ 546 | printf("定时器2超时!\n"); 547 | } 548 | /* 创建两个定时器,配置单独超时回调函数 */ 549 | timer1 = tk_timer_create((timeout_callback *)timer1_timeout_callback); 550 | timer2 = tk_timer_create((timeout_callback *)timer2_timeout_callback); 551 | ``` 552 | 553 | * **多对一** 554 | 555 | ```c 556 | /* 定时器timer1和timer2共用一个回调函数,在回调函数做区分 */ 557 | void timer_timeout_callback(struct tk_timer *timer){ 558 | if (timer == timer1) 559 | printf("定时器1超时!\n"); 560 | else if (timer == timer2) 561 | printf("定时器2超时!\n"); 562 | } 563 | /* 创建两个定时器,使用相同的超时回调函数 */ 564 | timer1 = tk_timer_create((timeout_callback *)timer_timeout_callback); 565 | timer2 = tk_timer_create((timeout_callback *)timer_timeout_callback); 566 | ``` 567 | 568 | 569 | 570 | ### 3.4 Event 事件集API函数 571 | 572 | ------ 573 | 574 | > 以下为详细API说明及简要示例程序,综合demo可查看[tk_event_samples.c](./samples/tk_event_samples.c)示例。 575 | 576 | #### 3.4.1 动态创建一个事件 577 | 578 | > **注意**:当配置**TOOLKIT_USING_EVENT**后,才能使用此函数。此函数需要用到**malloc**。 579 | 580 | ```c 581 | struct tk_event *tk_event_create(void); 582 | ``` 583 | 584 | | 参数 | 描述 | 585 | | ------ | ---------------------------------- | 586 | | 返回值 | 创建的事件对象(**NULL**为创建失败) | 587 | 588 | #### 3.4.2 动态删除一个事件 589 | 590 | > 当配置**TOOLKIT_USING_TIMER**后,才能使用此函数。此函数需要用到**free**。必须为**动态**方式创建的事件对象。 591 | 592 | ```c 593 | bool tk_event_delete(struct tk_event *event); 594 | ``` 595 | 596 | | 参数 | 描述 | 597 | | ------ | --------------------------------------- | 598 | | event | 要删除的事件对象 | 599 | | 返回值 | **true**:删除成功;**false**:删除失败 | 600 | 601 | #### 3.4.3 静态初始化一个事件 602 | 603 | 604 | 605 | ```c 606 | bool tk_event_init(struct tk_event *event); 607 | ``` 608 | 609 | | 参数 | 描述 | 610 | | ------ | --------------------------------------- | 611 | | event | 要初始化的事件对象 | 612 | | 返回值 | **true**:创建成功;**false**:创建失败 | 613 | 614 | #### 3.4.4 发送事件标志 615 | 616 | ```c 617 | bool tk_event_send(struct tk_event *event, uint32_t event_set); 618 | ``` 619 | 620 | | 参数 | 描述 | 621 | | --------- | -------------------------------------------- | 622 | | event | 发送目标事件对象 | 623 | | event_set | 事件标志,每个标志占1Bit,发送多个标志可“\|” | 624 | | 返回值 | **true**:发送成功;**false**:发送失败 | 625 | 626 | #### 3.4.5 接收事件 627 | 628 | ```c 629 | bool tk_event_recv(struct tk_event *event, uint32_t event_set, uint8_t option, uint32_t *recved); 630 | ``` 631 | 632 | | 参数 | 描述 | 633 | | --------- | ------------------------------------------------------------ | 634 | | event | 接收目标事件对象 | 635 | | event_set | 感兴趣的标志,每个标志占1Bit,多个标志可“\|” | 636 | | option | 操作,**标志与**:TK_EVENT_OPTION_AND; **标志或**:TK_EVENT_OPTION_OR; **清除标志**:TK_EVENT_OPTION_CLEAR | 637 | | 返回值 | **true**:发送成功;**false**:发送失败 | 638 | 639 | 640 | 641 | -------------------------------------------------------------------------------- /README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/README.pdf -------------------------------------------------------------------------------- /include/toolkit.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2020 Cproape (911830982@qq.com) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Change Logs: 25 | * Date Author Notes 26 | * 2020-01-29 zhangran the first version 27 | * 2020-01-31 zhangran add event extern code 28 | * 2020-11-28 zhangran add queue peep&remove extern code 29 | * 2020-12-09 zhangran Modify event option type to prevent warning 30 | * 2023-07-31 zhangran tk_timer adds the user_data pointer 31 | */ 32 | #ifndef __TOOLKIT_H_ 33 | #define __TOOLKIT_H_ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #define TK_SW_VERSION "1.0.7" 43 | 44 | /* toolkit general */ 45 | #ifdef TOOLKIT_USING_ASSERT 46 | #define TK_DEBUG(...) printf(__VA_ARGS__) 47 | #define TK_ASSERT(EXPR) \ 48 | if (!(EXPR)) \ 49 | { \ 50 | TK_DEBUG("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \ 51 | while (1) \ 52 | ; \ 53 | } 54 | #else 55 | #define TK_ASSERT(EXPR) (void)EXPR 56 | #endif /* TOOLKIT_USING_ASSERT */ 57 | 58 | /* toolkit queue */ 59 | #ifdef TOOLKIT_USING_QUEUE 60 | struct tk_queue 61 | { 62 | bool keep_fresh; 63 | void *queue_pool; 64 | uint16_t queue_size; 65 | uint16_t max_queues; 66 | uint16_t front; 67 | uint16_t rear; 68 | uint16_t len; 69 | }; 70 | typedef struct tk_queue *tk_queue_t; 71 | 72 | #ifdef TK_QUEUE_USING_CREATE 73 | struct tk_queue *tk_queue_create(uint16_t queue_size, uint16_t max_queues, bool keep_fresh); 74 | bool tk_queue_delete(struct tk_queue *queue); 75 | #endif /* TK_QUEUE_USING_CREATE */ 76 | 77 | bool tk_queue_init(struct tk_queue *queue, void *queuepool, uint16_t pool_size, uint16_t queue_size, bool keep_fresh); 78 | bool tk_queue_detach(struct tk_queue *queue); 79 | bool tk_queue_clean(struct tk_queue *queue); 80 | bool tk_queue_empty(struct tk_queue *queue); 81 | bool tk_queue_full(struct tk_queue *queue); 82 | bool tk_queue_peep(struct tk_queue *queue, void *pval); 83 | bool tk_queue_remove(struct tk_queue *queue); 84 | bool tk_queue_push(struct tk_queue *queue, void *pval); 85 | bool tk_queue_pop(struct tk_queue *queue, void *pval); 86 | uint16_t tk_queue_curr_len(struct tk_queue *queue); 87 | uint16_t tk_queue_push_multi(struct tk_queue *queue, void *pval, uint16_t len); 88 | uint16_t tk_queue_pop_multi(struct tk_queue *queue, void *pval, uint16_t len); 89 | #endif /* TOOLKIT_USING_QUEUE */ 90 | 91 | /* toolkit timer */ 92 | #ifdef TOOLKIT_USING_TIMER 93 | 94 | struct tk_timer; 95 | 96 | typedef enum 97 | { 98 | TIMER_STATE_RUNNING = 0, 99 | TIMER_STATE_STOP, 100 | TIMER_STATE_TIMEOUT, 101 | } tk_timer_state; 102 | 103 | typedef enum 104 | { 105 | TIMER_MODE_SINGLE = 0, 106 | TIMER_MODE_LOOP, 107 | } tk_timer_mode; 108 | 109 | struct tk_timer 110 | { 111 | bool enable; 112 | tk_timer_state state; 113 | tk_timer_mode mode; 114 | uint32_t delay_tick; 115 | uint32_t timer_tick_timeout; 116 | struct tk_timer *prev; 117 | struct tk_timer *next; 118 | void *user_data; 119 | #ifdef TK_TIMER_USING_TIMEOUT_CALLBACK 120 | void(*timeout_callback)(struct tk_timer *timer); 121 | #endif /* TK_TIMER_USING_TIMEOUT_CALLBACK */ 122 | }; 123 | typedef struct tk_timer *tk_timer_t; 124 | 125 | bool tk_timer_func_init(uint32_t (*get_tick_func)(void)); 126 | 127 | #ifdef TK_TIMER_USING_CREATE 128 | struct tk_timer *tk_timer_create(void(*timeout_callback)(struct tk_timer *timer)); 129 | bool tk_timer_delete(struct tk_timer *timer); 130 | #endif /* TK_TIMER_USING_CREATE */ 131 | bool tk_timer_init(struct tk_timer *timer, void(*timeout_callback)(struct tk_timer *timer)); 132 | bool tk_timer_detach(struct tk_timer *timer); 133 | 134 | bool tk_timer_start(struct tk_timer *timer, tk_timer_mode mode, uint32_t delay_tick); 135 | bool tk_timer_stop(struct tk_timer *timer); 136 | bool tk_timer_continue(struct tk_timer *timer); 137 | bool tk_timer_restart(struct tk_timer *timer); 138 | tk_timer_mode tk_timer_get_mode(struct tk_timer *timer); 139 | tk_timer_state tk_timer_get_state(struct tk_timer *timer); 140 | bool tk_timer_loop_handler(void); 141 | #endif /* TOOLKIT_USING_TIMER */ 142 | 143 | /* toolkit event */ 144 | #ifdef TOOLKIT_USING_EVENT 145 | typedef enum 146 | { 147 | TK_EVENT_OPTION_AND = 0x01, 148 | TK_EVENT_OPTION_OR = 0x02, 149 | TK_EVENT_OPTION_CLEAR = 0x04, 150 | } tk_event_option; 151 | 152 | struct tk_event 153 | { 154 | uint32_t event_set; 155 | }; 156 | typedef struct tk_event *tk_event_t; 157 | 158 | #ifdef TK_EVENT_USING_CREATE 159 | struct tk_event *tk_event_create(void); 160 | bool tk_event_delete(struct tk_event *event); 161 | #endif /* TK_EVENT_USING_CREATE */ 162 | 163 | bool tk_event_init(struct tk_event *event); 164 | bool tk_event_send(struct tk_event *event, uint32_t event_set); 165 | bool tk_event_recv(struct tk_event *event, uint32_t event_set, uint8_t option, uint32_t *recved); 166 | #endif /* TOOLKIT_USING_EVENT */ 167 | 168 | #endif /* __TOOLKIT_H_ */ 169 | -------------------------------------------------------------------------------- /include/toolkit_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2020 Cproape (911830982@qq.com) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Change Logs: 25 | * Date Author Notes 26 | * 2020-01-29 zhangran the first version 27 | * 2020-01-31 zhangran add event define switch 28 | */ 29 | #ifndef __TOOLKIT_CFG_H_ 30 | #define __TOOLKIT_CFG_H_ 31 | 32 | /* toolkit Configuration item */ 33 | #define TOOLKIT_USING_ASSERT 34 | #define TOOLKIT_USING_QUEUE 35 | #define TOOLKIT_USING_TIMER 36 | #define TOOLKIT_USING_EVENT 37 | 38 | /* toolkit queue Configuration item */ 39 | #define TK_QUEUE_USING_CREATE 40 | 41 | /* toolkit timer Configuration item */ 42 | #define TK_TIMER_USING_CREATE 43 | //#define TK_TIMER_USING_INTERVAL 44 | #define TK_TIMER_USING_TIMEOUT_CALLBACK 45 | 46 | /* toolkit event Configuration item */ 47 | #define TK_EVENT_USING_CREATE 48 | 49 | #endif /* __TOOLKIT_CFG_H_ */ 50 | -------------------------------------------------------------------------------- /samples/tk_event_samples.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/samples/tk_event_samples.c -------------------------------------------------------------------------------- /samples/tk_queue_samples.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/samples/tk_queue_samples.c -------------------------------------------------------------------------------- /samples/tk_timer_samples.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/samples/tk_timer_samples.c -------------------------------------------------------------------------------- /src/tk_event.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/src/tk_event.c -------------------------------------------------------------------------------- /src/tk_queue.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/src/tk_queue.c -------------------------------------------------------------------------------- /src/tk_timer.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cproape/toolkit/64120e079446c77990d892865fa5a105fc46036f/src/tk_timer.c --------------------------------------------------------------------------------