multi_button.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, 0, sizeof(struct button));
  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_PRESS_START;
  114. EVENT_CB(LONG_PRESS_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. EVENT_CB(PRESS_REPEAT);
  125. handle->ticks = 0;
  126. handle->state = 3;
  127. }
  128. else if(handle->ticks > SHORT_TICKS)
  129. {
  130. if(handle->repeat == 1)
  131. {
  132. handle->event = (uint8_t)SINGLE_CLICK;
  133. EVENT_CB(SINGLE_CLICK);
  134. }
  135. else if(handle->repeat == 2)
  136. {
  137. handle->event = (uint8_t)DOUBLE_CLICK;
  138. EVENT_CB(DOUBLE_CLICK);
  139. }
  140. handle->state = 0;
  141. }
  142. break;
  143. case 3:
  144. if(handle->button_level != handle->active_level)
  145. {
  146. handle->event = (uint8_t)PRESS_UP;
  147. EVENT_CB(PRESS_UP);
  148. if(handle->ticks < SHORT_TICKS)
  149. {
  150. handle->ticks = 0;
  151. handle->state = 2;
  152. }
  153. else
  154. {
  155. handle->state = 0;
  156. }
  157. }
  158. else if(handle->ticks > SHORT_TICKS)
  159. {
  160. handle->state = 0;
  161. }
  162. break;
  163. case 5:
  164. if(handle->button_level == handle->active_level)
  165. {
  166. handle->event = (uint8_t)LONG_PRESS_HOLD;
  167. if (handle->ticks % LONG_HOLD_CYC == 0)
  168. {
  169. EVENT_CB(LONG_PRESS_HOLD);
  170. }
  171. }
  172. else
  173. {
  174. handle->event = (uint8_t)PRESS_UP;
  175. EVENT_CB(PRESS_UP);
  176. handle->state = 0;
  177. }
  178. break;
  179. }
  180. }
  181. /**
  182. * @brief Start the button work, add the handle into work list.
  183. * @param handle: target handle strcut.
  184. * @retval 0: succeed. -1: already exist.
  185. */
  186. int button_start(struct button* handle)
  187. {
  188. struct button* target = head_handle;
  189. while(target)
  190. {
  191. if(target == handle)
  192. {
  193. return -1; //already exist.
  194. }
  195. target = target->next;
  196. }
  197. handle->next = head_handle;
  198. head_handle = handle;
  199. return 0;
  200. }
  201. /**
  202. * @brief Stop the button work, remove the handle off work list.
  203. * @param handle: target handle strcut.
  204. * @retval None
  205. */
  206. void button_stop(struct button* handle)
  207. {
  208. struct button** curr;
  209. for(curr = &head_handle; *curr;)
  210. {
  211. struct button* entry = *curr;
  212. if (entry == handle)
  213. {
  214. *curr = entry->next;
  215. return;
  216. }
  217. else
  218. {
  219. curr = &entry->next;
  220. }
  221. }
  222. }
  223. /**
  224. * @brief background ticks, timer repeat invoking interval 5ms.
  225. * @param None.
  226. * @retval None
  227. */
  228. void button_ticks(void)
  229. {
  230. struct button* target;
  231. for(target = head_handle; target != NULL; target = target->next)
  232. {
  233. button_handler(target);
  234. }
  235. }