1 Star 0 Fork 8

Anderson/vtor_elec_module

forked from vtor3478/vtor_elec_module 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vtor_timer.c 3.75 KB
一键复制 编辑 原始数据 按行查看 历史
#include "vtor_timer.h"
#ifdef __VTOR_TIMER__
// 一直指向所有任务的头,不可更改
VtorTimer* vtorTimerHeader = NULL;
static uint32_t VtorTimerTick = 0;
static uint32_t VtorTimerCnt = 0;
void VtorTimer_VoidFun()
{
}
void VtorTimer_Schedule(void)
{
VtorTimer* timer = vtorTimerHeader;
VtorTimerCnt++;
while (timer && NULL != timer->fun)
{
if (VtorTimer_VoidFun != timer->fun)
{
if(VTOR_TIMER_STATE_READY == timer->state)
{
uint32_t endTime = 0;
uint32_t startTime = VtorTimer_GetTick();
timer->state = VTOR_TIMER_STATE_RUNNING;
timer->fun();
if(VTOR_TIMER_STATE_RUNNING == timer->state)
{
timer->state = VTOR_TIMER_STATE_WAITING;
}
timer->runCnt++;
endTime = VtorTimer_GetTick();
timer->runTime = endTime - startTime;
}
}
timer++;
}
}
void VtorTimer_IncTick(uint32_t tick)
{
VtorTimer* timer = vtorTimerHeader;
VtorTimerTick += tick;
while (timer && NULL != timer->fun)
{
if (VtorTimer_VoidFun != timer->fun)
{
if(VTOR_TIMER_STATE_WAITING == timer->state)
{
timer->curTime += tick;
if (timer->curTime >= timer->interval)
{
timer->curTime = 0;
timer->state = VTOR_TIMER_STATE_READY;
}
}
}
timer++;
}
}
uint32_t VtorTimer_GetTick(void)
{
return VtorTimerTick;
}
uint32_t VtorTimer_GetCnt(void)
{
return VtorTimerCnt;
}
VtorTimer* VtorTimer_GetRunningTimer()
{
VtorTimer* timer = vtorTimerHeader;
while (NULL != timer->fun)
{
if(VTOR_TIMER_STATE_RUNNING == timer->state)
{
return timer;
}
timer++;
}
return NULL;
}
VtorTimer* VtorTimer_GetTimerByFun(VtorTimerFun fun)
{
VtorTimer* timer = vtorTimerHeader;
if (NULL == fun)
{
return NULL;
}
while(NULL != timer->fun)
{
if (fun == timer->fun)
{
return timer;
}
timer++;
}
return NULL;
}
VtorTimer* VtorTimer_Restart(VtorTimerFun fun, uint32_t interval)
{
VtorTimer* timer = NULL;
if (NULL == fun)
{
return NULL;
}
timer = VtorTimer_GetTimerByFun(fun);
if(NULL == timer)
{
timer = VtorTimer_GetTimerByFun(VtorTimer_VoidFun);
if(NULL == timer)
{
return NULL;
}
timer->fun = fun;
timer->state = VTOR_TIMER_STATE_STOP;
}
//if(VTOR_TIMER_STATE_STOP == timer->state)
{
VtorTimer_StartCallback(timer->fun);
timer->curTime = 0;
timer->state = VTOR_TIMER_STATE_WAITING;
}
timer->interval = interval;
return timer;
}
VtorTimer* VtorTimer_Start(VtorTimerFun fun, uint32_t interval)
{
VtorTimer* timer = NULL;
if (NULL == fun)
{
return NULL;
}
timer = VtorTimer_GetTimerByFun(fun);
if(NULL == timer)
{
timer = VtorTimer_GetTimerByFun(VtorTimer_VoidFun);
if(NULL == timer)
{
return NULL;
}
timer->fun = fun;
timer->state = VTOR_TIMER_STATE_STOP;
}
if(VTOR_TIMER_STATE_STOP == timer->state)
{
VtorTimer_StartCallback(timer->fun);
timer->curTime = 0;
timer->state = VTOR_TIMER_STATE_WAITING;
}
timer->interval = interval;
return timer;
}
void VtorTimer_RunNow(VtorTimerFun fun)
{
VtorTimer* timer = NULL;
timer = VtorTimer_GetTimerByFun(fun);
if(NULL != timer)
{
if(fun == timer->fun)
{
timer->state = VTOR_TIMER_STATE_READY;
timer->curTime = 0;
}
}
}
void VtorTimer_Delete(VtorTimerFun fun)
{
VtorTimer* timer = NULL;
timer = VtorTimer_GetTimerByFun(fun);
if(NULL != timer)
{
if (VTOR_TIMER_STATE_STOP != timer->state)
{
VtorTimer_StopCallback(fun);
timer->state = VTOR_TIMER_STATE_STOP;
timer->fun = VtorTimer_VoidFun;
}
timer->runCnt = 0;
timer->curTime = 0;
}
}
void VtorTimer_Stop(VtorTimerFun fun)
{
VtorTimer* timer = NULL;
if (NULL == fun)
{
return ;
}
timer = VtorTimer_GetTimerByFun(fun);
if(NULL != timer)
{
if (VTOR_TIMER_STATE_STOP != timer->state)
{
VtorTimer_StopCallback(fun);
timer->state = VTOR_TIMER_STATE_STOP;
}
timer->curTime = 0;
}
}
#endif // __VTOR_TIMER__
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Thomas_Ti/vtor_elec_module.git
git@gitee.com:Thomas_Ti/vtor_elec_module.git
Thomas_Ti
vtor_elec_module
vtor_elec_module
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385