multi_timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2016 Zibin Zheng <[email protected]>
  3. * All rights reserved
  4. */
  5. #include "multi_timer.h"
  6. //timer handle list head.
  7. static struct Timer* head_handle = NULL;
  8. //Timer ticks
  9. //static uint32_t _timer_ticks = 0;
  10. static uint16_t _timer_ticks = 0;
  11. /**
  12. * @brief Initializes the timer struct handle.
  13. * @param handle: the timer handle strcut.
  14. * @param timeout_cb: timeout callback.
  15. * @param repeat: repeat interval time.
  16. * @retval None
  17. */
  18. void timer_init_Head(void)
  19. {
  20. head_handle = NULL;
  21. }
  22. void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
  23. {
  24. // memset(handle, 0, sizeof(struct Timer));
  25. handle->timeout_cb = timeout_cb;
  26. handle->timeout = _timer_ticks + timeout;
  27. handle->repeat = repeat;
  28. }
  29. void timer_app(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
  30. {
  31. timer_init(handle, timeout_cb, timeout, repeat);
  32. timer_start(handle);
  33. }
  34. /**
  35. * @brief Start the timer work, add the handle into work list.
  36. * @param btn: target handle strcut.
  37. * @retval 0: succeed. -1: already exist.
  38. */
  39. int timer_start(struct Timer* handle)
  40. {
  41. struct Timer* target = head_handle;
  42. while(target) {
  43. if(target == handle) return -1; //already exist.
  44. target = target->next;
  45. }
  46. handle->next = head_handle;
  47. head_handle = handle;
  48. return 0;
  49. }
  50. /**
  51. * @brief Stop the timer work, remove the handle off work list.
  52. * @param handle: target handle strcut.
  53. * @retval None
  54. */
  55. void timer_stop(struct Timer* handle)
  56. {
  57. struct Timer** curr;
  58. for(curr = &head_handle; *curr; ) {
  59. struct Timer* entry = *curr;
  60. if (entry == handle) {
  61. *curr = entry->next;
  62. // free(entry);
  63. } else
  64. curr = &entry->next;
  65. }
  66. }
  67. /**
  68. * @brief main loop.
  69. * @param None.
  70. * @retval None
  71. */
  72. void timer_loop()
  73. {
  74. struct Timer* target;
  75. for(target=head_handle; target; target=target->next) {
  76. if(((target->timeout < TIMER_MAX) && (_timer_ticks >= target->timeout)) || \
  77. ((target->timeout >= TIMER_MAX) && (_timer_ticks+TIMER_MAX >= target->timeout))
  78. )
  79. {
  80. if(target->repeat == 0) { //Ò»´ÎÐÔ
  81. timer_stop(target);
  82. } else { //Ñ­»·
  83. target->timeout = _timer_ticks + target->repeat;
  84. }
  85. target->timeout_cb();
  86. }
  87. }
  88. }
  89. /**
  90. * @brief background ticks, timer repeat invoking interval 1ms.
  91. * @param None.
  92. * @retval None.
  93. */
  94. void timer_ticks()
  95. {
  96. _timer_ticks += TIMER_TICK;
  97. if(_timer_ticks >= TIMER_MAX) _timer_ticks = 0;
  98. }