#include "pt-timer.h" #ifdef __USE_WT_TIMER_ static volatile uint32_t baseTick=0; uint32_t getSysTick(void){return baseTick;} #endif void PTTimerTick(pt_timer_t *pool) { #ifdef __USE_WT_TIMER_ baseTick++; #else pt_timer_t *timer = pool->next; if ( timer ) { if ( timer->ticks ) { timer->ticks--; } if (0 == timer->ticks) { timer->ms = 0; while( timer ) // 将后续超时的定时器都移除 { timer = timer->next; if (timer && (0 == timer->ticks)) { timer->ms = 0; } else { break; } } pool->next = timer; } } #endif } void PTTimerInitPool(pt_timer_t *pool) { #ifndef __USE_WT_TIMER_ pool->ticks = 0; pool->next = NULL; #endif } extern unsigned long sysTimeCounter; void PTTimerStart(pt_timer_t *pool, pt_timer_t *timer, uint16_t ms) { #ifdef __USE_WT_TIMER_ timer->ms=baseTick+ms; #else pt_timer_t *p = pool; PTTimerStop(pool, timer); timer->next = NULL; timer->ms = ms; timer->ticks = ms;//_ms(ms); if (0 == ms) return; while( p->next ) { if (timer->ticks < p->next->ticks) { p->next->ticks -= timer->ticks; timer->next = p->next; p->next = timer; break; } else { timer->ticks -= p->next->ticks; // timer->ticks 有可能为 0 } p = p->next; } if (NULL == p->next) { p->next = timer; } #endif } boolean PTTimerIsExpired(pt_timer_t *timer) { #ifdef __USE_WT_TIMER_ return (baseTick>=timer->ms); #else return (0 == timer->ms); #endif } void PTTimerStop(pt_timer_t *pool, pt_timer_t *timer) { #ifndef __USE_WT_TIMER_ pt_timer_t *p = pool; if (timer && timer->ms) { while( p->next ) { if (p->next == timer) { if ( timer->next ) { timer->next->ticks += timer->ticks; } p->next = timer->next; timer->ms = 0; timer->next = NULL; break; } p = p->next; } } #endif }