multi_button.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * @File: multi_button.c
  3. * @Author: Zibin Zheng
  4. * @Date: 2018-01-23 20:36:01
  5. *
  6. * @LICENSE: Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
  7. * All rights reserved.
  8. *
  9. * @NOTE: The original author of MultiButton is Zibin Zheng.
  10. * Please contact the original author for authorization
  11. * before use.I(liu2guang) only adapt the library to
  12. * rt-thread and fix some bugs. I(liu2guang) am not
  13. * responsible for the authorization of the library.
  14. *
  15. * Change Logs:
  16. * Date Author Notes
  17. * 2018-01-23 liu2guang Adapter rtthread and Fix the bug.
  18. */
  19. #include "multi_button.h"
  20. #define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((button*)handle)
  21. static struct button* head_handle = NULL;
  22. /**
  23. * @brief Initializes the button struct handle.
  24. * @param handle: the button handle strcut.
  25. * @param pin_level: read the pin of the connet button level.
  26. * @param active_level: pin pressed level.
  27. * @retval None
  28. */
  29. void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level)
  30. {
  31. memset(handle, sizeof(struct button), 0);
  32. handle->event = (uint8_t)NONE_PRESS;
  33. handle->hal_button_Level = pin_level;
  34. handle->button_level = handle->hal_button_Level();
  35. handle->active_level = active_level;
  36. }
  37. /**
  38. * @brief Attach the button event callback function.
  39. * @param handle: the button handle strcut.
  40. * @param event: trigger event type.
  41. * @param cb: callback function.
  42. * @retval None
  43. */
  44. void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
  45. {
  46. handle->cb[event] = cb;
  47. }
  48. /**
  49. * @brief Inquire the button event happen.
  50. * @param handle: the button handle strcut.
  51. * @retval button event.
  52. */
  53. PressEvent get_button_event(struct button* handle)
  54. {
  55. return (PressEvent)(handle->event);
  56. }
  57. /**
  58. * @brief button driver core function, driver state machine.
  59. * @param handle: the button handle strcut.
  60. * @retval None
  61. */
  62. void button_handler(struct button* handle)
  63. {
  64. uint8_t read_gpio_level = handle->hal_button_Level();
  65. //ticks counter working..
  66. if((handle->state) > 0)
  67. {
  68. handle->ticks++;
  69. }
  70. /*------------button debounce handle---------------*/
  71. if(read_gpio_level != handle->button_level)
  72. {
  73. //not equal to prev one
  74. //continue read 3 times same new level change
  75. if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS)
  76. {
  77. handle->button_level = read_gpio_level;
  78. handle->debounce_cnt = 0;
  79. }
  80. }
  81. else
  82. {
  83. // leved not change ,counter reset.
  84. handle->debounce_cnt = 0;
  85. }
  86. /*-----------------State machine-------------------*/
  87. switch (handle->state)
  88. {
  89. case 0:
  90. if(handle->button_level == handle->active_level)
  91. {
  92. handle->event = (uint8_t)PRESS_DOWN;
  93. EVENT_CB(PRESS_DOWN);
  94. handle->ticks = 0;
  95. handle->repeat = 1;
  96. handle->state = 1;
  97. }
  98. else
  99. {
  100. handle->event = (uint8_t)NONE_PRESS;
  101. }
  102. break;
  103. case 1:
  104. if(handle->button_level != handle->active_level)
  105. {
  106. handle->event = (uint8_t)PRESS_UP;
  107. EVENT_CB(PRESS_UP);
  108. handle->ticks = 0;
  109. handle->state = 2;
  110. }
  111. else if(handle->ticks > LONG_TICKS)
  112. {
  113. handle->event = (uint8_t)LONG_RRESS_START;
  114. EVENT_CB(LONG_RRESS_START);
  115. handle->state = 5;
  116. }
  117. break;
  118. case 2:
  119. if(handle->button_level == handle->active_level)
  120. {
  121. handle->event = (uint8_t)PRESS_DOWN;
  122. EVENT_CB(PRESS_DOWN);
  123. handle->repeat++;
  124. handle->event = (uint8_t)PRESS_REPEAT;
  125. EVENT_CB(PRESS_REPEAT);
  126. handle->ticks = 0;
  127. handle->state = 3;
  128. }
  129. else if(handle->ticks > SHORT_TICKS)
  130. {
  131. if(handle->repeat == 1)
  132. {
  133. handle->event = (uint8_t)SINGLE_CLICK;
  134. EVENT_CB(SINGLE_CLICK);
  135. }
  136. else if(handle->repeat == 2)
  137. {
  138. handle->event = (uint8_t)DOUBLE_CLICK;
  139. EVENT_CB(DOUBLE_CLICK);
  140. }
  141. handle->state = 0;
  142. }
  143. break;
  144. case 3:
  145. if(handle->button_level != handle->active_level)
  146. {
  147. handle->event = (uint8_t)PRESS_UP;
  148. EVENT_CB(PRESS_UP);
  149. if(handle->ticks < SHORT_TICKS)
  150. {
  151. handle->ticks = 0;
  152. handle->state = 2;
  153. }
  154. else
  155. {
  156. handle->state = 0;
  157. }
  158. }
  159. break;
  160. case 5:
  161. if(handle->button_level == handle->active_level)
  162. {
  163. handle->event = (uint8_t)LONG_PRESS_HOLD;
  164. EVENT_CB(LONG_PRESS_HOLD);
  165. }
  166. else
  167. {
  168. handle->event = (uint8_t)PRESS_UP;
  169. EVENT_CB(PRESS_UP);
  170. handle->state = 0;
  171. }
  172. break;
  173. }
  174. }
  175. /**
  176. * @brief Start the button work, add the handle into work list.
  177. * @param handle: target handle strcut.
  178. * @retval 0: succeed. -1: already exist.
  179. */
  180. int button_start(struct button* handle)
  181. {
  182. struct button* target = head_handle;
  183. while(target)
  184. {
  185. if(target == handle)
  186. {
  187. return -1; //already exist.
  188. }
  189. target = target->next;
  190. }
  191. handle->next = head_handle;
  192. head_handle = handle;
  193. return 0;
  194. }
  195. /**
  196. * @brief Stop the button work, remove the handle off work list.
  197. * @param handle: target handle strcut.
  198. * @retval None
  199. */
  200. void button_stop(struct button* handle)
  201. {
  202. struct button** curr;
  203. for(curr = &head_handle; *curr;)
  204. {
  205. struct button* entry = *curr;
  206. if (entry == handle)
  207. {
  208. *curr = entry->next;
  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(void)
  222. {
  223. struct button* target;
  224. for(target = head_handle; target != NULL; target = target->next)
  225. {
  226. button_handler(target);
  227. }
  228. }