multi_led.c 4.5 KB

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