button.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/timers.h"
  18. #include "esp_log.h"
  19. #include "driver/gpio.h"
  20. #include "iot_button.h"
  21. #include "esp_timer.h"
  22. #define USE_ESP_TIMER CONFIG_BUTTON_USE_ESP_TIMER
  23. #if USE_ESP_TIMER
  24. #define STOP_TIMER(tmr) esp_timer_stop(tmr)
  25. #define DELETE_TIMER(tmr) esp_timer_delete(tmr)
  26. #else
  27. #define STOP_TIMER(tmr) xTimerStop(tmr, portMAX_DELAY)
  28. #define DELETE_TIMER(tmr) xTimerDelete(tmr, portMAX_DELAY);
  29. #endif
  30. #define IOT_CHECK(tag, a, ret) if(!(a)) { \
  31. ESP_LOGE(tag,"%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
  32. return (ret); \
  33. }
  34. #define ERR_ASSERT(tag, param) IOT_CHECK(tag, (param) == ESP_OK, ESP_FAIL)
  35. #define POINT_ASSERT(tag, param, ret) IOT_CHECK(tag, (param) != NULL, (ret))
  36. typedef enum {
  37. BUTTON_STATE_IDLE = 0,
  38. BUTTON_STATE_PUSH,
  39. BUTTON_STATE_PRESSED,
  40. } button_status_t;
  41. typedef struct button_dev button_dev_t;
  42. typedef struct btn_cb button_cb_t;
  43. struct btn_cb{
  44. TickType_t interval;
  45. button_cb cb;
  46. void* arg;
  47. #if !USE_ESP_TIMER
  48. TimerHandle_t tmr;
  49. #else
  50. esp_timer_handle_t tmr;
  51. #endif
  52. button_dev_t *pbtn;
  53. button_cb_t *next_cb;
  54. };
  55. struct button_dev{
  56. uint8_t io_num;
  57. uint8_t active_level;
  58. uint32_t serial_thres_sec;
  59. button_status_t state;
  60. button_cb_t tap_short_cb;
  61. button_cb_t tap_psh_cb;
  62. button_cb_t tap_rls_cb;
  63. button_cb_t press_serial_cb;
  64. button_cb_t* cb_head;
  65. };
  66. #define BUTTON_GLITCH_FILTER_TIME_MS CONFIG_BUTTON_IO_GLITCH_FILTER_TIME_MS
  67. static const char* TAG = "button";
  68. // static void button_press_cb(xTimerHandle tmr)
  69. static void button_press_cb(void* tmr)
  70. {
  71. #if !USE_ESP_TIMER
  72. button_cb_t* btn_cb = (button_cb_t*) pvTimerGetTimerID(tmr);
  73. #else
  74. button_cb_t* btn_cb = (button_cb_t*)(tmr);
  75. #endif
  76. button_dev_t* btn = btn_cb->pbtn;
  77. // low, then restart
  78. if (btn->active_level == gpio_get_level(btn->io_num)) {
  79. btn->state = BUTTON_STATE_PRESSED;
  80. if (btn_cb->cb) {
  81. btn_cb->cb(btn_cb->arg);
  82. }
  83. }
  84. }
  85. // static void button_tap_psh_cb(xTimerHandle tmr)
  86. static void button_tap_psh_cb(void* tmr)
  87. {
  88. #if !USE_ESP_TIMER
  89. button_cb_t* btn_cb = (button_cb_t*) pvTimerGetTimerID(tmr);
  90. #else
  91. button_cb_t* btn_cb = (button_cb_t*)(tmr);
  92. #endif
  93. button_dev_t* btn = btn_cb->pbtn;
  94. STOP_TIMER(btn->tap_rls_cb.tmr);
  95. int lv = gpio_get_level(btn->io_num);
  96. if (btn->active_level == lv) {
  97. // high, then key is up
  98. btn->state = BUTTON_STATE_PUSH;
  99. if (btn->press_serial_cb.tmr) {
  100. #if !USE_ESP_TIMER
  101. xTimerChangePeriod(btn->press_serial_cb.tmr, btn->serial_thres_sec*1000 / portTICK_PERIOD_MS, portMAX_DELAY);
  102. xTimerReset(btn->press_serial_cb.tmr, portMAX_DELAY);
  103. #else
  104. esp_timer_stop(btn->press_serial_cb.tmr);
  105. esp_timer_start_once(btn->press_serial_cb.tmr, btn->serial_thres_sec * 1000 * 1000);
  106. #endif
  107. }
  108. if (btn->tap_psh_cb.cb) {
  109. btn->tap_psh_cb.cb(btn->tap_psh_cb.arg);
  110. }
  111. } else {
  112. // 50ms, check if this is a real key up
  113. if (btn->tap_rls_cb.tmr) {
  114. STOP_TIMER(btn->tap_rls_cb.tmr);
  115. #if !USE_ESP_TIMER
  116. xTimerReset(btn->tap_rls_cb.tmr, portMAX_DELAY);
  117. #else
  118. esp_timer_start_once(btn->tap_rls_cb.tmr, btn->tap_rls_cb.interval * portTICK_PERIOD_MS * 1000);
  119. #endif
  120. }
  121. }
  122. }
  123. static void button_tap_rls_cb(void* tmr)
  124. {
  125. #if !USE_ESP_TIMER
  126. button_cb_t* btn_cb = (button_cb_t*) pvTimerGetTimerID(tmr);
  127. #else
  128. button_cb_t* btn_cb = (button_cb_t*)(tmr);
  129. #endif
  130. button_dev_t* btn = btn_cb->pbtn;
  131. STOP_TIMER(btn->tap_rls_cb.tmr);
  132. if (btn->active_level == gpio_get_level(btn->io_num)) {
  133. } else {
  134. // high, then key is up
  135. button_cb_t *pcb = btn->cb_head;
  136. while (pcb != NULL) {
  137. if (pcb->tmr != NULL) {
  138. STOP_TIMER(pcb->tmr);
  139. }
  140. pcb = pcb->next_cb;
  141. }
  142. if (btn->press_serial_cb.tmr && btn->press_serial_cb.tmr != NULL) {
  143. STOP_TIMER(btn->press_serial_cb.tmr);
  144. }
  145. if (btn->tap_short_cb.cb && btn->state == BUTTON_STATE_PUSH) {
  146. btn->tap_short_cb.cb(btn->tap_short_cb.arg);
  147. }
  148. if(btn->tap_rls_cb.cb && btn->state != BUTTON_STATE_IDLE) {
  149. btn->tap_rls_cb.cb(btn->tap_rls_cb.arg);
  150. }
  151. btn->state = BUTTON_STATE_IDLE;
  152. }
  153. }
  154. static void button_press_serial_cb(void* tmr)
  155. {
  156. #if !USE_ESP_TIMER
  157. button_dev_t* btn = (button_dev_t*) pvTimerGetTimerID(tmr);
  158. #else
  159. button_dev_t* btn = (button_dev_t*)(tmr);
  160. #endif
  161. if (btn->press_serial_cb.cb) {
  162. btn->press_serial_cb.cb(btn->press_serial_cb.arg);
  163. }
  164. #if !USE_ESP_TIMER
  165. xTimerChangePeriod(btn->press_serial_cb.tmr, btn->press_serial_cb.interval, portMAX_DELAY);
  166. xTimerReset(btn->press_serial_cb.tmr, portMAX_DELAY);
  167. #else
  168. esp_timer_stop(btn->press_serial_cb.tmr);
  169. esp_timer_start_once(btn->press_serial_cb.tmr, btn->press_serial_cb.interval * portTICK_PERIOD_MS * 1000);
  170. #endif
  171. }
  172. static void button_gpio_isr_handler(void* arg)
  173. {
  174. button_dev_t* btn = (button_dev_t*) arg;
  175. portBASE_TYPE HPTaskAwoken = pdFALSE;
  176. int level = gpio_get_level(btn->io_num);
  177. if (level == btn->active_level) {
  178. if (btn->tap_psh_cb.tmr) {
  179. #if !USE_ESP_TIMER
  180. xTimerStopFromISR(btn->tap_psh_cb.tmr, &HPTaskAwoken);
  181. xTimerResetFromISR(btn->tap_psh_cb.tmr, &HPTaskAwoken);
  182. #else
  183. esp_timer_stop(btn->tap_psh_cb.tmr);
  184. esp_timer_start_once(btn->tap_psh_cb.tmr, btn->tap_psh_cb.interval * portTICK_PERIOD_MS * 1000);
  185. #endif
  186. }
  187. button_cb_t *pcb = btn->cb_head;
  188. while (pcb != NULL) {
  189. if (pcb->tmr != NULL) {
  190. #if !USE_ESP_TIMER
  191. xTimerStopFromISR(pcb->tmr, &HPTaskAwoken);
  192. xTimerResetFromISR(pcb->tmr, &HPTaskAwoken);
  193. #else
  194. esp_timer_stop(pcb->tmr);
  195. esp_timer_start_once(pcb->tmr, pcb->interval * portTICK_PERIOD_MS * 1000);
  196. #endif
  197. }
  198. pcb = pcb->next_cb;
  199. }
  200. } else {
  201. // 50ms, check if this is a real key up
  202. if (btn->tap_rls_cb.tmr) {
  203. #if !USE_ESP_TIMER
  204. xTimerStopFromISR(btn->tap_rls_cb.tmr, &HPTaskAwoken);
  205. xTimerResetFromISR(btn->tap_rls_cb.tmr, &HPTaskAwoken);
  206. #else
  207. esp_timer_stop(btn->tap_rls_cb.tmr);
  208. esp_timer_start_once(btn->tap_rls_cb.tmr, btn->tap_rls_cb.interval * portTICK_PERIOD_MS * 1000);
  209. #endif
  210. }
  211. }
  212. if(HPTaskAwoken == pdTRUE) {
  213. portYIELD_FROM_ISR();
  214. }
  215. }
  216. #if !USE_ESP_TIMER
  217. static void button_free_tmr(xTimerHandle* tmr)
  218. #else
  219. static void button_free_tmr(esp_timer_handle_t *tmr)
  220. #endif
  221. {
  222. if (tmr && *tmr) {
  223. STOP_TIMER(*tmr);
  224. DELETE_TIMER(*tmr);
  225. *tmr = NULL;
  226. }
  227. }
  228. esp_err_t iot_button_delete(button_handle_t btn_handle)
  229. {
  230. POINT_ASSERT(TAG, btn_handle, ESP_ERR_INVALID_ARG);
  231. button_dev_t* btn = (button_dev_t*) btn_handle;
  232. gpio_set_intr_type(btn->io_num, GPIO_INTR_DISABLE);
  233. gpio_isr_handler_remove(btn->io_num);
  234. button_free_tmr(&btn->tap_rls_cb.tmr);
  235. button_free_tmr(&btn->tap_psh_cb.tmr);
  236. button_free_tmr(&btn->tap_short_cb.tmr);
  237. button_free_tmr(&btn->press_serial_cb.tmr);
  238. button_cb_t *pcb = btn->cb_head;
  239. while (pcb != NULL) {
  240. button_cb_t *cb_next = pcb->next_cb;
  241. button_free_tmr(&pcb->tmr);
  242. free(pcb);
  243. pcb = cb_next;
  244. }
  245. free(btn);
  246. return ESP_OK;
  247. }
  248. button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level)
  249. {
  250. #if USE_ESP_TIMER
  251. ets_printf("use esp timer !!!\n");
  252. esp_timer_init();
  253. #endif
  254. IOT_CHECK(TAG, gpio_num < GPIO_NUM_MAX, NULL);
  255. button_dev_t* btn = (button_dev_t*) calloc(1, sizeof(button_dev_t));
  256. POINT_ASSERT(TAG, btn, NULL);
  257. btn->active_level = active_level;
  258. btn->io_num = gpio_num;
  259. btn->state = BUTTON_STATE_IDLE;
  260. btn->tap_rls_cb.arg = NULL;
  261. btn->tap_rls_cb.cb = NULL;
  262. btn->tap_rls_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_PERIOD_MS;
  263. btn->tap_rls_cb.pbtn = btn;
  264. #if !USE_ESP_TIMER
  265. btn->tap_rls_cb.tmr = xTimerCreate("btn_rls_tmr", btn->tap_rls_cb.interval, pdFALSE,
  266. &btn->tap_rls_cb, button_tap_rls_cb);
  267. #else
  268. esp_timer_create_args_t tmr_param_rls;
  269. tmr_param_rls.arg = &btn->tap_rls_cb;
  270. tmr_param_rls.callback = button_tap_rls_cb;
  271. tmr_param_rls.dispatch_method = ESP_TIMER_TASK;
  272. tmr_param_rls.name = "btn_rls_tmr";
  273. esp_timer_create(&tmr_param_rls, &btn->tap_rls_cb.tmr);
  274. #endif
  275. btn->tap_psh_cb.arg = NULL;
  276. btn->tap_psh_cb.cb = NULL;
  277. btn->tap_psh_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_PERIOD_MS;
  278. btn->tap_psh_cb.pbtn = btn;
  279. #if !USE_ESP_TIMER
  280. btn->tap_psh_cb.tmr = xTimerCreate("btn_psh_tmr", btn->tap_psh_cb.interval, pdFALSE,
  281. &btn->tap_psh_cb, button_tap_psh_cb);
  282. #else
  283. esp_timer_create_args_t tmr_param_psh;
  284. tmr_param_psh.arg = &btn->tap_psh_cb;
  285. tmr_param_psh.callback = button_tap_psh_cb;
  286. tmr_param_psh.dispatch_method = ESP_TIMER_TASK;
  287. tmr_param_psh.name = "btn_psh_tmr";
  288. esp_timer_create(&tmr_param_psh, &btn->tap_psh_cb.tmr);
  289. #endif
  290. gpio_install_isr_service(0);
  291. gpio_config_t gpio_conf;
  292. gpio_conf.intr_type = GPIO_INTR_ANYEDGE;
  293. gpio_conf.mode = GPIO_MODE_INPUT;
  294. gpio_conf.pin_bit_mask = (1ULL << gpio_num);
  295. gpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  296. gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE;
  297. gpio_config(&gpio_conf);
  298. gpio_isr_handler_add(gpio_num, button_gpio_isr_handler, btn);
  299. return (button_handle_t) btn;
  300. }
  301. esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type)
  302. {
  303. button_dev_t* btn = (button_dev_t*) btn_handle;
  304. button_cb_t* btn_cb = NULL;
  305. if (type == BUTTON_CB_PUSH) {
  306. btn_cb = &btn->tap_psh_cb;
  307. } else if (type == BUTTON_CB_RELEASE) {
  308. btn_cb = &btn->tap_rls_cb;
  309. } else if (type == BUTTON_CB_TAP) {
  310. btn_cb = &btn->tap_short_cb;
  311. } else if (type == BUTTON_CB_SERIAL) {
  312. btn_cb = &btn->press_serial_cb;
  313. }
  314. btn_cb->cb = NULL;
  315. btn_cb->arg = NULL;
  316. btn_cb->pbtn = btn;
  317. button_free_tmr(&btn_cb->tmr);
  318. return ESP_OK;
  319. }
  320. esp_err_t iot_button_set_serial_cb(button_handle_t btn_handle, uint32_t start_after_sec, TickType_t interval_tick, button_cb cb, void* arg)
  321. {
  322. button_dev_t* btn = (button_dev_t*) btn_handle;
  323. btn->serial_thres_sec = start_after_sec;
  324. if (btn->press_serial_cb.tmr == NULL) {
  325. #if !USE_ESP_TIMER
  326. btn->press_serial_cb.tmr = xTimerCreate("btn_serial_tmr", btn->serial_thres_sec*1000 / portTICK_PERIOD_MS,
  327. pdFALSE, btn, button_press_serial_cb);
  328. #else
  329. esp_timer_create_args_t tmr_param_ser;
  330. tmr_param_ser.arg = btn;
  331. tmr_param_ser.callback = button_press_serial_cb;
  332. tmr_param_ser.dispatch_method = ESP_TIMER_TASK;
  333. tmr_param_ser.name = "btn_serial_tmr";
  334. esp_timer_create(&tmr_param_ser, &btn->press_serial_cb.tmr);
  335. #endif
  336. }
  337. btn->press_serial_cb.arg = arg;
  338. btn->press_serial_cb.cb = cb;
  339. btn->press_serial_cb.interval = interval_tick;
  340. btn->press_serial_cb.pbtn = btn;
  341. #if !USE_ESP_TIMER
  342. xTimerChangePeriod(btn->press_serial_cb.tmr, btn->serial_thres_sec*1000 / portTICK_PERIOD_MS, portMAX_DELAY);
  343. #endif
  344. return ESP_OK;
  345. }
  346. esp_err_t iot_button_set_evt_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg)
  347. {
  348. POINT_ASSERT(TAG, btn_handle, ESP_ERR_INVALID_ARG);
  349. button_dev_t* btn = (button_dev_t*) btn_handle;
  350. if (type == BUTTON_CB_PUSH) {
  351. btn->tap_psh_cb.arg = arg;
  352. btn->tap_psh_cb.cb = cb;
  353. btn->tap_psh_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
  354. btn->tap_psh_cb.pbtn = btn;
  355. #if !USE_ESP_TIMER
  356. xTimerChangePeriod(btn->tap_psh_cb.tmr, btn->tap_psh_cb.interval, portMAX_DELAY);
  357. #endif
  358. } else if (type == BUTTON_CB_RELEASE) {
  359. btn->tap_rls_cb.arg = arg;
  360. btn->tap_rls_cb.cb = cb;
  361. btn->tap_rls_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
  362. btn->tap_rls_cb.pbtn = btn;
  363. #if !USE_ESP_TIMER
  364. xTimerChangePeriod(btn->tap_rls_cb.tmr, btn->tap_psh_cb.interval, portMAX_DELAY);
  365. #endif
  366. } else if (type == BUTTON_CB_TAP) {
  367. btn->tap_short_cb.arg = arg;
  368. btn->tap_short_cb.cb = cb;
  369. btn->tap_short_cb.interval = BUTTON_GLITCH_FILTER_TIME_MS / portTICK_RATE_MS;
  370. btn->tap_short_cb.pbtn = btn;
  371. } else if (type == BUTTON_CB_SERIAL) {
  372. iot_button_set_serial_cb(btn_handle, 1, 1000 / portTICK_RATE_MS, cb, arg);
  373. }
  374. return ESP_OK;
  375. }
  376. esp_err_t iot_button_add_custom_cb(button_handle_t btn_handle, uint32_t press_sec, button_cb cb, void* arg)
  377. {
  378. POINT_ASSERT(TAG, btn_handle, ESP_ERR_INVALID_ARG);
  379. IOT_CHECK(TAG, press_sec != 0, ESP_ERR_INVALID_ARG);
  380. button_dev_t* btn = (button_dev_t*) btn_handle;
  381. button_cb_t* cb_new = (button_cb_t*) calloc(1, sizeof(button_cb_t));
  382. POINT_ASSERT(TAG, cb_new, ESP_FAIL);
  383. cb_new->arg = arg;
  384. cb_new->cb = cb;
  385. cb_new->interval = press_sec * 1000 / portTICK_PERIOD_MS;
  386. cb_new->pbtn = btn;
  387. #if !USE_ESP_TIMER
  388. cb_new->tmr = xTimerCreate("btn_press_tmr", cb_new->interval, pdFALSE, cb_new, button_press_cb);
  389. #else
  390. esp_timer_create_args_t tmr_param_cus;
  391. tmr_param_cus.arg = cb_new;
  392. tmr_param_cus.callback = button_press_cb;
  393. tmr_param_cus.dispatch_method = ESP_TIMER_TASK;
  394. tmr_param_cus.name = "btn_press_custom_tmr";
  395. esp_timer_create(&tmr_param_cus, &cb_new->tmr);
  396. #endif
  397. cb_new->next_cb = btn->cb_head;
  398. btn->cb_head = cb_new;
  399. return ESP_OK;
  400. }