flexible_button.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * @File: flexible_button.c
  3. * @Author: MurphyZhao
  4. * @Date: 2018-09-29
  5. *
  6. * Copyright (c) 2018-2019 MurphyZhao <d2014zjt@163.com>
  7. * https://github.com/murphyzhao
  8. * All rights reserved.
  9. * License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * Change logs:
  24. * Date Author Notes
  25. * 2018-09-29 MurphyZhao First add
  26. * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account
  27. * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks
  28. *
  29. */
  30. #include "flexible_button.h"
  31. #ifndef NULL
  32. #define NULL 0
  33. #endif
  34. #define EVENT_SET_AND_EXEC_CB(btn, evt) \
  35. do \
  36. { \
  37. btn->event = evt; \
  38. if(btn->cb) \
  39. btn->cb((flex_button_t*)btn); \
  40. } while(0)
  41. /**
  42. * BTN_IS_PRESSED
  43. *
  44. * 1: is pressed
  45. * 0: is not pressed
  46. */
  47. #define BTN_IS_PRESSED(i) (g_btn_status_reg & (1 << i))
  48. enum FLEX_BTN_STAGE
  49. {
  50. FLEX_BTN_STAGE_DEFAULT = 0,
  51. FLEX_BTN_STAGE_DOWN = 1,
  52. FLEX_BTN_STAGE_MULTIPLE_CLICK = 2
  53. };
  54. typedef uint32_t btn_type_t;
  55. static flex_button_t *btn_head = NULL;
  56. /**
  57. * g_logic_level
  58. *
  59. * The logic level of the button pressed,
  60. * Each bit represents a button.
  61. *
  62. * First registered button, the logic level of the button pressed is
  63. * at the low bit of g_logic_level.
  64. */
  65. btn_type_t g_logic_level = (btn_type_t)0;
  66. /**
  67. * g_btn_status_reg
  68. *
  69. * The status register of all button, each bit records the pressing state of a button.
  70. *
  71. * First registered button, the pressing state of the button is
  72. * at the low bit of g_btn_status_reg.
  73. */
  74. btn_type_t g_btn_status_reg = (btn_type_t)0;
  75. static uint8_t button_cnt = 0;
  76. /**
  77. * @brief Register a user button
  78. *
  79. * @param button: button structure instance
  80. * @return Number of keys that have been registered, or -1 when error
  81. */
  82. int32_t flex_button_register(flex_button_t *button)
  83. {
  84. flex_button_t *curr = btn_head;
  85. if (!button || (button_cnt > sizeof(btn_type_t) * 8))
  86. {
  87. return -1;
  88. }
  89. while (curr)
  90. {
  91. if(curr == button)
  92. {
  93. return -1; /* already exist. */
  94. }
  95. curr = curr->next;
  96. }
  97. /**
  98. * First registered button is at the end of the 'linked list'.
  99. * btn_head points to the head of the 'linked list'.
  100. */
  101. button->next = btn_head;
  102. button->status = FLEX_BTN_STAGE_DEFAULT;
  103. button->event = FLEX_BTN_PRESS_NONE;
  104. button->scan_cnt = 0;
  105. button->click_cnt = 0;
  106. button->max_multiple_clicks_interval = MAX_MULTIPLE_CLICKS_INTERVAL;
  107. btn_head = button;
  108. /**
  109. * First registered button, the logic level of the button pressed is
  110. * at the low bit of g_logic_level.
  111. */
  112. g_logic_level |= (button->pressed_logic_level << button_cnt);
  113. button_cnt ++;
  114. return button_cnt;
  115. }
  116. /**
  117. * @brief Read all key values in one scan cycle
  118. *
  119. * @param void
  120. * @return none
  121. */
  122. static void flex_button_read(void)
  123. {
  124. uint8_t i;
  125. flex_button_t* target;
  126. /* The button that was registered first, the button value is in the low position of raw_data */
  127. btn_type_t raw_data = 0;
  128. for(target = btn_head, i = button_cnt - 1;
  129. (target != NULL) && (target->usr_button_read != NULL);
  130. target = target->next, i--)
  131. {
  132. raw_data = raw_data | ((target->usr_button_read)(target) << i);
  133. }
  134. g_btn_status_reg = (~raw_data) ^ g_logic_level;
  135. }
  136. /**
  137. * @brief Handle all key events in one scan cycle.
  138. * Must be used after 'flex_button_read' API
  139. *
  140. * @param void
  141. * @return Activated button count
  142. */
  143. static uint8_t flex_button_process(void)
  144. {
  145. uint8_t i;
  146. uint8_t active_btn_cnt = 0;
  147. flex_button_t* target;
  148. for (target = btn_head, i = button_cnt - 1; target != NULL; target = target->next, i--)
  149. {
  150. if (target->status > FLEX_BTN_STAGE_DEFAULT)
  151. {
  152. target->scan_cnt ++;
  153. if (target->scan_cnt >= ((1 << (sizeof(target->scan_cnt) * 8)) - 1))
  154. {
  155. target->scan_cnt = target->long_hold_start_tick;
  156. }
  157. }
  158. switch (target->status)
  159. {
  160. case FLEX_BTN_STAGE_DEFAULT: /* stage: default(button up) */
  161. if (BTN_IS_PRESSED(i)) /* is pressed */
  162. {
  163. target->scan_cnt = 0;
  164. target->click_cnt = 0;
  165. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_DOWN);
  166. /* swtich to button down stage */
  167. target->status = FLEX_BTN_STAGE_DOWN;
  168. }
  169. else
  170. {
  171. target->event = FLEX_BTN_PRESS_NONE;
  172. }
  173. break;
  174. case FLEX_BTN_STAGE_DOWN: /* stage: button down */
  175. if (BTN_IS_PRESSED(i)) /* is pressed */
  176. {
  177. if (target->click_cnt > 0) /* multiple click */
  178. {
  179. if (target->scan_cnt > target->max_multiple_clicks_interval)
  180. {
  181. EVENT_SET_AND_EXEC_CB(target,
  182. target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ?
  183. target->click_cnt :
  184. FLEX_BTN_PRESS_REPEAT_CLICK);
  185. /* swtich to button down stage */
  186. target->status = FLEX_BTN_STAGE_DOWN;
  187. target->scan_cnt = 0;
  188. target->click_cnt = 0;
  189. }
  190. }
  191. else if (target->scan_cnt >= target->long_hold_start_tick_10000)
  192. {
  193. if (target->event != FLEX_BTN_PRESS_LONG_HOLD_10000)
  194. {
  195. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD_10000);
  196. }
  197. }
  198. else if (target->scan_cnt >= target->long_hold_start_tick)
  199. {
  200. if (target->event != FLEX_BTN_PRESS_LONG_HOLD)
  201. {
  202. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD);
  203. }
  204. }
  205. else if (target->scan_cnt >= target->long_press_start_tick)
  206. {
  207. if (target->event != FLEX_BTN_PRESS_LONG_START)
  208. {
  209. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_START);
  210. }
  211. }
  212. else if (target->scan_cnt >= target->short_press_start_tick)
  213. {
  214. if (target->event != FLEX_BTN_PRESS_SHORT_START)
  215. {
  216. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_SHORT_START);
  217. }
  218. }
  219. }
  220. else /* button up */
  221. {
  222. if (target->scan_cnt >= target->long_hold_start_tick_10000)
  223. {
  224. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD_UP_10000);
  225. target->status = FLEX_BTN_STAGE_DEFAULT;
  226. }
  227. else if (target->scan_cnt >= target->long_hold_start_tick)
  228. {
  229. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD_UP);
  230. target->status = FLEX_BTN_STAGE_DEFAULT;
  231. }
  232. else if (target->scan_cnt >= target->long_press_start_tick)
  233. {
  234. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_UP);
  235. target->status = FLEX_BTN_STAGE_DEFAULT;
  236. }
  237. else if (target->scan_cnt >= target->short_press_start_tick)
  238. {
  239. EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_SHORT_UP);
  240. target->status = FLEX_BTN_STAGE_DEFAULT;
  241. }
  242. else
  243. {
  244. /* swtich to multiple click stage */
  245. target->status = FLEX_BTN_STAGE_MULTIPLE_CLICK;
  246. target->click_cnt ++;
  247. }
  248. }
  249. break;
  250. case FLEX_BTN_STAGE_MULTIPLE_CLICK: /* stage: multiple click */
  251. if (BTN_IS_PRESSED(i)) /* is pressed */
  252. {
  253. /* swtich to button down stage */
  254. target->status = FLEX_BTN_STAGE_DOWN;
  255. target->scan_cnt = 0;
  256. }
  257. else
  258. {
  259. if (target->scan_cnt > target->max_multiple_clicks_interval)
  260. {
  261. EVENT_SET_AND_EXEC_CB(target,
  262. target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ?
  263. target->click_cnt :
  264. FLEX_BTN_PRESS_REPEAT_CLICK);
  265. /* swtich to default stage */
  266. target->status = FLEX_BTN_STAGE_DEFAULT;
  267. }
  268. }
  269. break;
  270. }
  271. if (target->status > FLEX_BTN_STAGE_DEFAULT)
  272. {
  273. active_btn_cnt ++;
  274. }
  275. }
  276. return active_btn_cnt;
  277. }
  278. /**
  279. * flex_button_event_read
  280. *
  281. * @brief Get the button event of the specified button.
  282. *
  283. * @param button: button structure instance
  284. * @return button event
  285. */
  286. flex_button_event_t flex_button_event_read(flex_button_t* button)
  287. {
  288. return (flex_button_event_t)(button->event);
  289. }
  290. /**
  291. * flex_button_scan
  292. *
  293. * @brief Start key scan.
  294. * Need to be called cyclically within the specified period.
  295. * Sample cycle: 5 - 20ms
  296. *
  297. * @param void
  298. * @return Activated button count
  299. */
  300. uint8_t flex_button_scan(void)
  301. {
  302. flex_button_read();
  303. return flex_button_process();
  304. }