multi_button.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2016 Zibin Zheng <[email protected]>
  3. * All rights reserved
  4. * 修改:
  5. kinve.2017-12-11: 长按周期触发
  6. */
  7. #include "multi_button.h"
  8. #define EVENT_CB(ev) if(handle->cb[ev])handle->cb[ev]((Button*)handle)
  9. //button handle list head.
  10. static struct Button* head_handle = NULL;
  11. /**
  12. * @brief Initializes the button struct handle.
  13. * @param handle: the button handle strcut.
  14. * @param pin_level: read the HAL GPIO of the connet button level.
  15. * @param active_level: pressed GPIO level.
  16. * @retval None
  17. */
  18. void button_init_Head ( void )
  19. {
  20. head_handle = NULL;
  21. }
  22. void button_init ( struct Button* handle, uint8_t ( *pin_level ) (), uint8_t active_level )
  23. {
  24. memset ( handle, 0, sizeof ( struct Button ) );
  25. handle->event = ( uint8_t ) NONE_PRESS;
  26. handle->hal_button_Level = pin_level;
  27. handle->button_level = handle->hal_button_Level();
  28. handle->active_level = active_level;
  29. }
  30. /**
  31. * @brief Attach the button event callback function.
  32. * @param handle: the button handle strcut.
  33. * @param event: trigger event type.
  34. * @param cb: callback function.
  35. * @retval None
  36. */
  37. void button_attach ( struct Button* handle, PressEvent event, BtnCallback cb )
  38. {
  39. handle->cb[event] = cb;
  40. }
  41. /**
  42. * @brief Inquire the button event happen.
  43. * @param handle: the button handle strcut.
  44. * @retval button event.
  45. */
  46. PressEvent get_button_event ( struct Button* handle )
  47. {
  48. return ( PressEvent ) ( handle->event );
  49. }
  50. /**
  51. * @brief Button driver core function, driver state machine.
  52. * @param handle: the button handle strcut.
  53. * @retval None
  54. */
  55. void button_handler ( struct Button* handle )
  56. {
  57. uint8_t read_gpio_level = handle->hal_button_Level();
  58. //ticks counter working..
  59. if ( ( handle->state ) > 0 ) handle->ticks++;
  60. /*------------button debounce handle---------------*/
  61. if ( read_gpio_level != handle->button_level ) //not equal to prev one
  62. {
  63. //continue read 3 times same new level change
  64. if ( ++ ( handle->debounce_cnt ) >= DEBOUNCE_TICKS )
  65. {
  66. handle->button_level = read_gpio_level;
  67. handle->debounce_cnt = 0;
  68. }
  69. }
  70. else //leved not change ,counter reset.
  71. {
  72. handle->debounce_cnt = 0;
  73. }
  74. /*-----------------State machine-------------------*/
  75. switch ( handle->state )
  76. {
  77. case 0:
  78. if ( handle->button_level == handle->active_level ) //start press down
  79. {
  80. handle->event = ( uint8_t ) PRESS_DOWN;
  81. EVENT_CB ( PRESS_DOWN );
  82. handle->ticks = 0;
  83. handle->repeat = 1;
  84. handle->state = 1;
  85. }
  86. else
  87. {
  88. handle->event = ( uint8_t ) NONE_PRESS;
  89. }
  90. break;
  91. case 1:
  92. if ( handle->button_level != handle->active_level ) //released press up
  93. {
  94. handle->event = ( uint8_t ) PRESS_UP;
  95. EVENT_CB ( PRESS_UP );
  96. handle->ticks = 0;
  97. handle->state = 2;
  98. }
  99. else if ( handle->ticks > LONG_TICKS )
  100. {
  101. handle->event = ( uint8_t ) LONG_RRESS_START;
  102. EVENT_CB ( LONG_RRESS_START );
  103. handle->state = 5;
  104. }
  105. break;
  106. case 2:
  107. if ( handle->button_level == handle->active_level ) //press down again
  108. {
  109. handle->event = ( uint8_t ) PRESS_DOWN;
  110. EVENT_CB ( PRESS_DOWN );
  111. handle->repeat++;
  112. if ( handle->repeat == 2 )
  113. {
  114. EVENT_CB ( DOUBLE_CLICK ); // repeat hit
  115. }
  116. EVENT_CB ( PRESS_REPEAT ); // repeat hit
  117. handle->ticks = 0;
  118. handle->state = 3;
  119. }
  120. else if ( handle->ticks > SHORT_TICKS ) //released timeout
  121. {
  122. if ( handle->repeat == 1 )
  123. {
  124. handle->event = ( uint8_t ) SINGLE_CLICK;
  125. EVENT_CB ( SINGLE_CLICK );
  126. }
  127. else if ( handle->repeat == 2 )
  128. {
  129. handle->event = ( uint8_t ) DOUBLE_CLICK;
  130. }
  131. handle->state = 0;
  132. }
  133. break;
  134. case 3:
  135. if ( handle->button_level != handle->active_level ) //released press up
  136. {
  137. handle->event = ( uint8_t ) PRESS_UP;
  138. EVENT_CB ( PRESS_UP );
  139. if ( handle->ticks < SHORT_TICKS )
  140. {
  141. handle->ticks = 0;
  142. handle->state = 2; //repeat press
  143. }
  144. else
  145. {
  146. handle->state = 0;
  147. }
  148. }
  149. break;
  150. case 5:
  151. if ( handle->button_level == handle->active_level )
  152. {
  153. #ifndef LONG_TICKS_TRIG
  154. //continue hold trigger
  155. handle->event = ( uint8_t ) LONG_PRESS_HOLD;
  156. EVENT_CB ( LONG_PRESS_HOLD );
  157. #else
  158. //长按后tick每周期触发
  159. //增加长按周期触发
  160. if ( handle->ticks > ( LONG_TICKS + LONG_TICKS_TRIG ) )
  161. {
  162. handle->ticks = LONG_TICKS;//重新启动
  163. handle->event = ( uint8_t ) LONG_PRESS_HOLD;
  164. EVENT_CB ( LONG_PRESS_HOLD );
  165. }
  166. #endif
  167. }
  168. else //releasd
  169. {
  170. handle->event = ( uint8_t ) PRESS_UP;
  171. EVENT_CB ( PRESS_UP );
  172. handle->state = 0; //reset
  173. }
  174. break;
  175. }
  176. }
  177. /**
  178. * @brief Start the button work, add the handle into work list.
  179. * @param handle: target handle strcut.
  180. * @retval 0: succeed. -1: already exist.
  181. */
  182. int button_start ( struct Button* handle )
  183. {
  184. struct Button* target = head_handle;
  185. while ( target )
  186. {
  187. if ( target == handle ) return -1; //already exist.
  188. target = target->next;
  189. }
  190. handle->next = head_handle;
  191. head_handle = handle;
  192. return 0;
  193. }
  194. /**
  195. * @brief Stop the button work, remove the handle off work list.
  196. * @param handle: target handle strcut.
  197. * @retval None
  198. */
  199. void button_stop ( struct Button* handle )
  200. {
  201. struct Button** curr;
  202. for ( curr = &head_handle; *curr; )
  203. {
  204. struct Button* entry = *curr;
  205. if ( entry == handle )
  206. {
  207. *curr = entry->next;
  208. // free(entry);
  209. }
  210. else
  211. {
  212. curr = &entry->next;
  213. }
  214. }
  215. }
  216. /**
  217. * @brief background ticks, timer repeat invoking interval 5ms.
  218. * @param None.
  219. * @retval None
  220. */
  221. void button_ticks()
  222. {
  223. struct Button* target;
  224. for ( target = head_handle; target; target = target->next )
  225. {
  226. button_handler ( target );
  227. }
  228. }