a24d2f4c9137219ebd1ce28dc97f8964e6ce9081.svn-base 9.7 KB

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