button.c 15 KB

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