/* * jf_timer.h * * Created on: Aug 20, 2016 * Author: lort */ #ifndef JF_OS_JF_TIMER_H_ #define JF_OS_JF_TIMER_H_ #include "jf_types.h" typedef void (*time_fuc_t) (void *param); typedef struct { void *next; uint32 decounter; uint32 reroad; time_fuc_t fuc; void *param; }jf_timer_t; #endif /* JF_OS_JF_TIMER_H_ */
#include "jf_types.h" #include "jf_timer.h" #include "jf_mem.h" jf_timer_t *pjf_time = NULL; void jf_timer_poll(uint32 ticks) { jf_timer_t *pcur; if(pjf_time!=NULL) { pcur = pjf_time; while(pcur!=NULL) { if(pcur->param!=NULL) { if(pcur->decounter>=ticks) pcur->decounter-=ticks; else { pcur->decounter = 0; pcur->fuc(pcur->param); if(pcur->reroad) { pcur->decounter = pcur->reroad; } else { jf_free(pcur->param); pcur->param = NULL; } } } pcur = pcur->next; } } } int jf_timer_add(uint32 tim,uint32 reload, void *fuc,void *param) { jf_timer_t *pcur; jf_timer_t *pnew; pnew = jf_malloc(sizeof(jf_timer_t)); if(pnew!=NULL) { pnew->decounter = tim; pnew->reroad = reload; pnew->fuc = fuc; pnew->param = param; pnew->next = NULL; } else return -1; if(pjf_time==NULL) { pjf_time = pnew; return 0; } else { pcur = pjf_time; while(pcur->next!=NULL) { pcur = pcur->next; } pcur->next = pnew; return 0; } }
时间: 2024-10-29 23:44:17