123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #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
- }
|