forked from Lotlab/nrf52-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_timer.h
38 lines (32 loc) · 1.6 KB
/
app_timer.h
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
#pragma once
#include <stdbool.h>
#include <stdint.h>
typedef void (*task_t)(void);
typedef struct
{
uint16_t period;
task_t task;
} timer_info;
/* 定义一个定时器 */
#define TIMER_DEF(callback, interval) \
{ \
.period = interval, .task = callback \
}
#define TIMER_STRUCT_SIZE(data) (sizeof(data) / sizeof(timer_info))
/* 定义定时器的函数 */
#define TIMER_INIT(name, timers) \
static uint16_t name##_counter[TIMER_STRUCT_SIZE(timers)]; \
static void name##_tick() \
{ \
for (int i = 0; i < sizeof(name##_counter) / sizeof(uint16_t); i++) \
name##_counter[i]++; \
} \
static void name##_task_exec() \
{ \
for (int i = 0; i < sizeof(name##_counter) / sizeof(uint16_t); i++) { \
if (name##_counter[i] >= timers[i].period) { \
name##_counter[i] = 0; \
(*(timers[i].task))(); \
} \
} \
}