C语言语言——软件定时器软件定时器
都说程序设计程序设计 = 算法算法 + 数据结构数据结构。数据结构是挨踢必修课,不过好像学了数据结构之后也没用来做过啥。不知道做啥,就写
个软件定时器。
软件定时器数据结构软件定时器数据结构
typedef struct __software_timer{
u32 timeout; //初始化时间计数器
u32 repeat; //运行间隔:repeat > 0 :周期定时时间 repeat == 0 :只定时一次
void (*timeout_callback_handler)(void *para); //超时回调函数
struct __software_timer *next;
}software_timer_t;
判断软件定时器链表是否为空判断软件定时器链表是否为空
/**
* @brief: Determine if the software timer list is empty
* @param[in]: None
* @retval[out]: None
* @note:
* @author: AresXu
* @github:
* @version: v1.0.0
*/
static u8 is_softtimer_list_empty(void)
{
if(software_timer_list_head.next == NULL)
return TRUE;
else
return FALSE;
}
插入定时器到软件定时器链表插入定时器到软件定时器链表
链表使用单向链表单向链表。
/**
* @brief: Insert the software timer node into the linked list
* @param[in]: None
* @retval[out]: None
* @note:
* @author: AresXu
* @github:
* @version: v1.0.0
*/
static void insert_software_timer(software_timer_t *timer_handle)
{
software_timer_t *tmp;
software_timer_t *list_head = &software_timer_list_head;
if(is_softtimer_list_empty())
{
list_head->next = timer_handle;
}
else
{
tmp = list_head->next;
while(tmp->next)
{
if(timer_handle == tmp) //定时器已经存在
{
printf("The timer already exists");
return;
}
tmp = tmp->next;
}
tmp->next = timer_handle;
}
}
将定时器从软件定时器链表移除将定时器从软件定时器链表移除
/**
* @brief: Removes the software timer node from the list
* @param[in]: None
* @retval[out]: None