multi_led.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "multi_led.h"
  2. #define EVENT_CB(state) if(handle->led_set_gpio) handle->led_set_gpio((bool)state)
  3. //button handle list head.
  4. static struct Led* head_handle = NULL;
  5. /**
  6. * @brief Initializes the button struct handle.
  7. * @param handle: the button handle strcut.
  8. * @param pin_level: read the HAL GPIO of the connet button level.
  9. * @param active_level: pressed GPIO level.
  10. * @retval None
  11. */
  12. void led_init_Head(void)
  13. {
  14. head_handle = NULL;
  15. }
  16. void led_init(struct Led* handle, LedCallback led_set_gpio)
  17. {
  18. memset(handle, 0, sizeof(struct Led));
  19. handle->led_set_gpio = led_set_gpio;
  20. }
  21. void led_set_state(struct Led* handle, u8 ID, u8 OnOff, u8 Type, u16 FlashTime, u16 CloseTime, bool Res )
  22. {
  23. if ( Type < LED_STATE_MAX )
  24. {
  25. handle->Play.ID = ID;
  26. handle->Play.Type = Type;
  27. if ( Type != LED_STATE4_NOT )
  28. {
  29. handle->Play.OnOff = OnOff;
  30. }
  31. handle->Play.FlashTime = FlashTime;
  32. handle->Play.CloseTime = CloseTime;
  33. handle->Play.Count = 0;
  34. handle->Play.Res = Res;
  35. }
  36. }
  37. /**
  38. * @brief Button driver core function, driver state machine.
  39. * @param handle: the button handle strcut.
  40. * @retval None
  41. */
  42. void led_handler(struct Led* handle)
  43. {
  44. switch ( handle->Play.Type )
  45. {
  46. case LED_STATE0_OFF: //关
  47. EVENT_CB(false);
  48. handle->Play.Type = LED_STATE_MAX;
  49. break;
  50. case LED_STATE1_ON: //开
  51. EVENT_CB(true);
  52. handle->Play.Type = LED_STATE_MAX;
  53. break;
  54. case LED_STATE2_FLASH: //闪烁
  55. handle->Play.Count += LED_SCAN;
  56. if ( handle->Play.Count >= handle->Play.FlashTime )
  57. {
  58. handle->Play.Count = 0;
  59. EVENT_CB(handle->Play.OnOff);
  60. handle->Play.OnOff = !handle->Play.OnOff;
  61. }
  62. break;
  63. case LED_STATE3_CONUT: //定时开关
  64. if ( handle->Play.CloseTime > 0 )
  65. {
  66. handle->Play.CloseTime -= LED_SCAN;
  67. EVENT_CB(handle->Play.Res); //开始
  68. }
  69. else
  70. {
  71. handle->Play.CloseTime = 0;
  72. EVENT_CB(!handle->Play.Res); //结束,与结果相反
  73. handle->Play.Type = LED_STATE_MAX;
  74. }
  75. break;
  76. case LED_STATE4_NOT: //取反
  77. EVENT_CB(!handle->Play.OnOff);
  78. handle->Play.Type = LED_STATE_MAX;
  79. break;
  80. case LED_STATE5_FLASH_DELAY: //闪烁,延时关闭
  81. if ( handle->Play.CloseTime > 0 )
  82. {
  83. handle->Play.CloseTime -= LED_SCAN;
  84. //闪烁
  85. handle->Play.Count += LED_SCAN; //开始
  86. if ( handle->Play.Count >= handle->Play.FlashTime )
  87. {
  88. handle->Play.Count = 0;
  89. EVENT_CB(handle->Play.OnOff);
  90. handle->Play.OnOff = !handle->Play.OnOff;
  91. }
  92. }
  93. else
  94. {
  95. handle->Play.CloseTime = 0;
  96. EVENT_CB(handle->Play.Res); //结束,与结果
  97. handle->Play.Type = LED_STATE_MAX;
  98. }
  99. break;
  100. }
  101. }
  102. /**
  103. * @brief Start the button work, add the handle into work list.
  104. * @param handle: target handle strcut.
  105. * @retval 0: succeed. -1: already exist.
  106. */
  107. int led_start(struct Led* handle)
  108. {
  109. struct Led* target = head_handle;
  110. while(target) {
  111. if(target == handle) return -1; //already exist.
  112. target = target->next;
  113. }
  114. handle->next = head_handle;
  115. head_handle = handle;
  116. return 0;
  117. }
  118. /**
  119. * @brief Stop the button work, remove the handle off work list.
  120. * @param handle: target handle strcut.
  121. * @retval None
  122. */
  123. void led_stop(struct Led* handle)
  124. {
  125. struct Led** curr;
  126. for(curr = &head_handle; *curr; ) {
  127. struct Led* entry = *curr;
  128. if (entry == handle) {
  129. *curr = entry->next;
  130. // free(entry);
  131. } else {
  132. curr = &entry->next;
  133. }
  134. }
  135. }
  136. /**
  137. * @brief background ticks, timer repeat invoking interval 5ms.
  138. * @param None.
  139. * @retval None
  140. */
  141. void led_ticks()
  142. {
  143. struct Led* target;
  144. for(target=head_handle; target; target=target->next) {
  145. led_handler(target);
  146. }
  147. }