第一次提交
This commit is contained in:
165
app/app_frm_timer.c
Normal file
165
app/app_frm_timer.c
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "app_config.h"
|
||||
#include "interface.h"
|
||||
|
||||
#include "app_frm_timer.h"
|
||||
#include "app_frm_signal.h"
|
||||
|
||||
// 全局计时器列表头指针
|
||||
static Timer *timer_list = NULL;
|
||||
|
||||
// 添加一个静态变量来跟踪当前定时器数量
|
||||
static uint32_t current_timer_count = 0;
|
||||
|
||||
// 初始化定时器
|
||||
void timerInit(Timer *timer)
|
||||
{
|
||||
if (timer == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
timer->target_time = 0;
|
||||
timer->elapsed_time = 0;
|
||||
timer->start_time = 0;
|
||||
timer->active = 0;
|
||||
timer->priority = 1; // 默认优先级为1
|
||||
timer->next = NULL;
|
||||
}
|
||||
|
||||
// 修改 timerStart 函数
|
||||
void timerStart(Timer *timer, uint32_t target_time, unsigned char priority)
|
||||
{
|
||||
if (timer == NULL || target_time == 0)
|
||||
{
|
||||
printf("Error: Null timer pointer or invalid target time\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查定时器是否已经在列表中
|
||||
Timer *current = timer_list;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current == timer)
|
||||
{
|
||||
// 定时器已在列表中, 只需更新其参数
|
||||
irq_state_t saved_state = enter_critical_section();
|
||||
|
||||
timer->target_time = target_time;
|
||||
timer->start_time = getCurrentTime();
|
||||
timer->elapsed_time = 0;
|
||||
timer->active = 1;
|
||||
timer->priority = priority;
|
||||
|
||||
exit_critical_section(saved_state);
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
// 检查是否达到最大定时器数量
|
||||
if (current_timer_count >= MAX_TIMERS)
|
||||
{
|
||||
printf("Error: Maximum number of timers reached\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化新定时器
|
||||
irq_state_t saved_state = enter_critical_section();
|
||||
|
||||
timer->target_time = target_time;
|
||||
timer->start_time = getCurrentTime();
|
||||
timer->elapsed_time = 0;
|
||||
timer->active = 1;
|
||||
timer->priority = priority;
|
||||
|
||||
// 将定时器插入到定时器列表
|
||||
timer->next = timer_list;
|
||||
timer_list = timer;
|
||||
current_timer_count++;
|
||||
|
||||
exit_critical_section(saved_state);
|
||||
}
|
||||
|
||||
// 停止定时器
|
||||
void timerStop(Timer *timer)
|
||||
{
|
||||
if (timer == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
irq_state_t saved_state = enter_critical_section();
|
||||
|
||||
Timer *current = timer_list;
|
||||
Timer *prev = NULL;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current == timer)
|
||||
{
|
||||
// 从链表中移除定时器
|
||||
if (prev == NULL)
|
||||
{
|
||||
timer_list = current->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = current->next;
|
||||
}
|
||||
current_timer_count--;
|
||||
timer->active = 0;
|
||||
timer->next = NULL; // 断开链接
|
||||
break;
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
exit_critical_section(saved_state);
|
||||
}
|
||||
|
||||
// 更新所有定时器, 在定时器中断中调用
|
||||
void timerUpdateAll(void)
|
||||
{
|
||||
irq_state_t saved_state = enter_critical_section();
|
||||
|
||||
Timer *prev = NULL;
|
||||
Timer *current = timer_list;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current->active)
|
||||
{
|
||||
current->elapsed_time++;
|
||||
if (current->elapsed_time >= current->target_time)
|
||||
{
|
||||
current->active = 0;
|
||||
// 发送定时器过期消息
|
||||
publishMessage((void *)(uintptr_t)current, current->priority);
|
||||
|
||||
// 从链表中移除已过期的定时器
|
||||
if (prev == NULL)
|
||||
{
|
||||
timer_list = current->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = current->next;
|
||||
}
|
||||
current_timer_count--;
|
||||
Timer *expired_timer = current;
|
||||
current = current->next;
|
||||
expired_timer->next = NULL; // 断开链接
|
||||
continue; // 跳过前进 prev 指针
|
||||
}
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
exit_critical_section(saved_state);
|
||||
}
|
||||
|
||||
// 获取当前定时器数量
|
||||
uint32_t getCurrentTimerCount(void)
|
||||
{
|
||||
return current_timer_count;
|
||||
}
|
||||
Reference in New Issue
Block a user