#ifndef EASY_TIMER_H #define EASY_TIMER_H #ifdef __cplusplus extern "C" { #endif #ifndef u32 #define u32 unsigned int #define u16 unsigned short #define u8 unsigned char #endif #include "list/list.h" typedef struct _easy_timer_t { u32 time_wait_cfg; u32 time_wait_now; void (*timer_handler)(void); u8 time_out : 1; u8 time_start : 1; } _easy_timer_t; extern list_t easy_timer_list; extern void (*easy_timer_sleep_func)(void); #define TIMER_CONTINUE_TRIGGER 0 #define TIMER_SIGNAL_TRIGGER 1 /** * @brief 注册定时器句柄 * @param _timer_handler 定时器回调函数 * @param _time_out 超时标志位 TIMER_CONTINUE_TRIGGER: 持续触发模式 , * TIMER_SIGNAL_TRIGGER: 单词触发模式 * @param _timer_cfg 定时器超时时间,单位ms */ #define EXPORT_EASY_TIMER_HANDLE(_timer_handler, _time_out, _timer_cfg) \ _easy_timer_t _timer_handler##_timer = { \ .time_wait_cfg = _timer_cfg, \ .time_wait_now = 0, \ .timer_handler = _timer_handler, \ .time_out = _time_out, \ .time_start = 1, \ }; \ EXPORT_LIST_NODE(easy_timer_list, _timer_handler##_timer); /** * @brief 注册睡眠函数 */ #define EXPORT_SLEEP_FUNC(_sleep_func) \ int __attribute__((constructor)) export_sleep_func_##_sleep_func(void) { \ extern void (*easy_timer_sleep_func)(void); \ easy_timer_sleep_func = _sleep_func; \ return 0; \ } /** * @brief 定时器核心函数 */ void easy_timer_core(); #define EASY_TIMER_START(_timer_handler) \ { \ extern _easy_timer_t _timer_handler##_timer; \ _timer_handler##_timer.time_start = 1; \ _timer_handler##_timer.time_wait_now = 0; \ } #define EASY_TIMER_STOP(_timer_handler) \ { \ extern _easy_timer_t _timer_handler##_timer; \ _timer_handler##_timer.time_start = 0; \ _timer_handler##_timer.time_wait_now = 0; \ } #define EASY_TIMER_SET_TIME(_timer_handler, _time_cfg) \ { \ extern _easy_timer_t _timer_handler##_timer; \ _timer_handler##_timer.time_wait_cfg = _time_cfg; \ _timer_handler##_timer.time_wait_now = 0; \ } #ifdef __cplusplus } #endif #endif // EASY_TIMER_H