pt-timer.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "pt-timer.h"
  2. #ifdef __USE_WT_TIMER_
  3. static volatile uint32_t baseTick=0;
  4. uint32_t getSysTick(void){return baseTick;}
  5. #endif
  6. void
  7. PTTimerTick(pt_timer_t *pool)
  8. {
  9. #ifdef __USE_WT_TIMER_
  10. baseTick++;
  11. #else
  12. pt_timer_t *timer = pool->next;
  13. if ( timer )
  14. {
  15. if ( timer->ticks )
  16. {
  17. timer->ticks--;
  18. }
  19. if (0 == timer->ticks)
  20. {
  21. timer->ms = 0;
  22. while( timer ) // 将后续超时的定时器都移除
  23. {
  24. timer = timer->next;
  25. if (timer && (0 == timer->ticks))
  26. {
  27. timer->ms = 0;
  28. }
  29. else
  30. {
  31. break;
  32. }
  33. }
  34. pool->next = timer;
  35. }
  36. }
  37. #endif
  38. }
  39. void
  40. PTTimerInitPool(pt_timer_t *pool)
  41. {
  42. #ifndef __USE_WT_TIMER_
  43. pool->ticks = 0;
  44. pool->next = NULL;
  45. #endif
  46. }
  47. extern unsigned long sysTimeCounter;
  48. void
  49. PTTimerStart(pt_timer_t *pool, pt_timer_t *timer, uint16_t ms)
  50. {
  51. #ifdef __USE_WT_TIMER_
  52. timer->ms=baseTick+ms;
  53. #else
  54. pt_timer_t *p = pool;
  55. PTTimerStop(pool, timer);
  56. timer->next = NULL;
  57. timer->ms = ms;
  58. timer->ticks = ms;//_ms(ms);
  59. if (0 == ms) return;
  60. while( p->next )
  61. {
  62. if (timer->ticks < p->next->ticks)
  63. {
  64. p->next->ticks -= timer->ticks;
  65. timer->next = p->next;
  66. p->next = timer;
  67. break;
  68. }
  69. else
  70. {
  71. timer->ticks -= p->next->ticks; // timer->ticks 有可能为 0
  72. }
  73. p = p->next;
  74. }
  75. if (NULL == p->next)
  76. {
  77. p->next = timer;
  78. }
  79. #endif
  80. }
  81. boolean
  82. PTTimerIsExpired(pt_timer_t *timer)
  83. {
  84. #ifdef __USE_WT_TIMER_
  85. return (baseTick>=timer->ms);
  86. #else
  87. return (0 == timer->ms);
  88. #endif
  89. }
  90. void
  91. PTTimerStop(pt_timer_t *pool, pt_timer_t *timer)
  92. {
  93. #ifndef __USE_WT_TIMER_
  94. pt_timer_t *p = pool;
  95. if (timer && timer->ms)
  96. {
  97. while( p->next )
  98. {
  99. if (p->next == timer)
  100. {
  101. if ( timer->next )
  102. {
  103. timer->next->ticks += timer->ticks;
  104. }
  105. p->next = timer->next;
  106. timer->ms = 0;
  107. timer->next = NULL;
  108. break;
  109. }
  110. p = p->next;
  111. }
  112. }
  113. #endif
  114. }