multi_timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. {
  44. if ( target == handle ) return -1; //already exist.
  45. target = target->next;
  46. }
  47. handle->next = head_handle;
  48. head_handle = handle;
  49. return 0;
  50. }
  51. /**
  52. * @brief Stop the timer work, remove the handle off work list.
  53. * @param handle: target handle strcut.
  54. * @retval None
  55. */
  56. void timer_stop ( struct Timer* handle )
  57. {
  58. struct Timer** curr;
  59. for ( curr = &head_handle; *curr; )
  60. {
  61. struct Timer* entry = *curr;
  62. if ( entry == handle )
  63. {
  64. *curr = entry->next;
  65. // free(entry);
  66. }
  67. else
  68. curr = &entry->next;
  69. }
  70. }
  71. /**
  72. * @brief main loop.
  73. * @param None.
  74. * @retval None
  75. */
  76. void timer_loop()
  77. {
  78. struct Timer* target;
  79. for ( target = head_handle; target; target = target->next )
  80. {
  81. if ( ( ( target->timeout < TIMER_MAX ) && ( _timer_ticks >= target->timeout ) ) || \
  82. ( ( target->timeout >= TIMER_MAX ) && ( _timer_ticks + TIMER_MAX >= target->timeout ) )
  83. )
  84. {
  85. if ( target->repeat == 0 ) //Ò»´ÎÐÔ
  86. {
  87. timer_stop ( target );
  88. }
  89. else //Ñ­»·
  90. {
  91. target->timeout = _timer_ticks + target->repeat;
  92. }
  93. target->timeout_cb();
  94. }
  95. }
  96. }
  97. /**
  98. * @brief background ticks, timer repeat invoking interval 1ms.
  99. * @param None.
  100. * @retval None.
  101. */
  102. void timer_ticks()
  103. {
  104. _timer_ticks += TIMER_TICK;
  105. if ( _timer_ticks >= TIMER_MAX ) _timer_ticks = 0;
  106. }