iot_button.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifndef _IOT_BUTTON_H_
  14. #define _IOT_BUTTON_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "driver/gpio.h"
  19. #include "freertos/FreeRTOS.h"
  20. typedef void (* button_cb)(void*);
  21. typedef void* button_handle_t;
  22. typedef enum {
  23. BUTTON_ACTIVE_HIGH = 1, /*!<button active level: high level*/
  24. BUTTON_ACTIVE_LOW = 0, /*!<button active level: low level*/
  25. } button_active_t;
  26. typedef enum {
  27. BUTTON_CB_PUSH = 0, /*!<button push callback event */
  28. BUTTON_CB_RELEASE, /*!<button release callback event */
  29. BUTTON_CB_TAP, /*!<button quick tap callback event(will not trigger if there already is a "PRESS" event) */
  30. BUTTON_CB_SERIAL, /*!<button serial trigger callback event */
  31. } button_cb_type_t;
  32. /**
  33. * @brief Init button functions
  34. *
  35. * @param gpio_num GPIO index of the pin that the button uses
  36. * @param active_level button hardware active level.
  37. * For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level.
  38. *
  39. * @return A button_handle_t handle to the created button object, or NULL in case of error.
  40. */
  41. button_handle_t iot_button_create(gpio_num_t gpio_num, button_active_t active_level);
  42. /**
  43. * @brief Register a callback function for a serial trigger event.
  44. *
  45. * @param btn_handle handle of the button object
  46. * @start_after_sec define the time after which to start serial trigger action
  47. * @interval_tick serial trigger interval
  48. * @cb callback function for "TAP" action.
  49. * @arg Parameter for callback function
  50. * @note
  51. * Button callback functions execute in the context of the timer service task.
  52. * It is therefore essential that button callback functions never attempt to block.
  53. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  54. * or specify a non zero block time when accessing a queue or a semaphore.
  55. * @return
  56. * - ESP_OK Success
  57. * - ESP_FAIL Parameter error
  58. */
  59. 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);
  60. /**
  61. * @brief Register a callback function for a button_cb_type_t action.
  62. *
  63. * @param btn_handle handle of the button object
  64. * @param type callback function type
  65. * @param cb callback function for "TAP" action.
  66. * @param arg Parameter for callback function
  67. * @note
  68. * Button callback functions execute in the context of the timer service task.
  69. * It is therefore essential that button callback functions never attempt to block.
  70. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  71. * or specify a non zero block time when accessing a queue or a semaphore.
  72. * @return
  73. * - ESP_OK Success
  74. * - ESP_FAIL Parameter error
  75. */
  76. esp_err_t iot_button_set_evt_cb(button_handle_t btn_handle, button_cb_type_t type, button_cb cb, void* arg);
  77. /**
  78. * @brief
  79. *
  80. * @param btn_handle handle of the button object
  81. * @param press_sec the callback function would be called if you press the button for a specified period of time
  82. * @param cb callback function for "PRESS" action.
  83. * @param arg Parameter for callback function
  84. *
  85. * @note
  86. * Button callback functions execute in the context of the timer service task.
  87. * It is therefore essential that button callback functions never attempt to block.
  88. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  89. * or specify a non zero block time when accessing a queue or a semaphore.
  90. * @return
  91. * - ESP_OK Success
  92. * - ESP_FAIL Parameter error
  93. */
  94. esp_err_t iot_button_add_custom_cb(button_handle_t btn_handle, uint32_t press_sec, button_cb cb, void* arg);
  95. /**
  96. * @brief Delete button object and free memory
  97. * @param btn_handle handle of the button object
  98. *
  99. * @return
  100. * - ESP_OK Success
  101. * - ESP_FAIL Parameter error
  102. */
  103. esp_err_t iot_button_delete(button_handle_t btn_handle);
  104. /**
  105. * @brief Remove callback
  106. *
  107. * @param btn_handle The handle of the button object
  108. * @param type callback function event type
  109. *
  110. * @return
  111. * - ESP_OK Success
  112. */
  113. esp_err_t iot_button_rm_cb(button_handle_t btn_handle, button_cb_type_t type);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #ifdef __cplusplus
  118. /**
  119. * class of button
  120. * simple usage:
  121. * CButton* btn = new CButton(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL, BUTTON_SERIAL_TRIGGER, 3);
  122. * btn->add_cb(BUTTON_CB_PUSH, button_tap_cb, (void*) push, 50 / portTICK_PERIOD_MS);
  123. * btn->add_custom_cb(5, button_press_5s_cb, NULL);
  124. * ......
  125. * delete btn;
  126. */
  127. class CButton
  128. {
  129. private:
  130. button_handle_t m_btn_handle;
  131. /**
  132. * prevent copy constructing
  133. */
  134. CButton(const CButton&);
  135. CButton& operator = (const CButton&);
  136. public:
  137. /**
  138. * @brief constructor of CButton
  139. *
  140. * @param gpio_num GPIO index of the pin that the button uses
  141. * @param active_level button hardware active level.
  142. * For "BUTTON_ACTIVE_LOW" it means when the button pressed, the GPIO will read low level.
  143. */
  144. CButton(gpio_num_t gpio_num, button_active_t active_level = BUTTON_ACTIVE_LOW);
  145. ~CButton();
  146. /**
  147. * @brief Register a callback function for a button_cb_type_t action.
  148. *
  149. * @param type callback function type
  150. * @param cb callback function for "TAP" action.
  151. * @param arg Parameter for callback function
  152. * @note
  153. * Button callback functions execute in the context of the timer service task.
  154. * It is therefore essential that button callback functions never attempt to block.
  155. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  156. * or specify a non zero block time when accessing a queue or a semaphore.
  157. * @return
  158. * - ESP_OK Success
  159. * - ESP_FAIL Parameter error
  160. */
  161. esp_err_t set_evt_cb(button_cb_type_t type, button_cb cb, void* arg);
  162. /**
  163. * @brief Register a callback function for a serial trigger event.
  164. *
  165. * @param btn_handle handle of the button object
  166. * @start_after_sec define the time after which to start serial trigger action
  167. * @interval_tick serial trigger interval
  168. * @cb callback function for "TAP" action.
  169. * @arg Parameter for callback function
  170. * @note
  171. * Button callback functions execute in the context of the timer service task.
  172. * It is therefore essential that button callback functions never attempt to block.
  173. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  174. * or specify a non zero block time when accessing a queue or a semaphore.
  175. * @return
  176. * - ESP_OK Success
  177. * - ESP_FAIL Parameter error
  178. */
  179. esp_err_t set_serial_cb(button_cb cb, void* arg, TickType_t interval_tick, uint32_t start_after_sec);
  180. /**
  181. * @brief
  182. *
  183. * @param press_sec the callback function would be called if you press the button for a specified period of time
  184. * @param cb callback function for "PRESS" action.
  185. * @param arg Parameter for callback function
  186. *
  187. * @note
  188. * Button callback functions execute in the context of the timer service task.
  189. * It is therefore essential that button callback functions never attempt to block.
  190. * For example, a button callback function must not call vTaskDelay(), vTaskDelayUntil(),
  191. * or specify a non zero block time when accessing a queue or a semaphore.
  192. * @return
  193. * - ESP_OK Success
  194. * - ESP_FAIL Parameter error
  195. */
  196. esp_err_t add_custom_cb(uint32_t press_sec, button_cb cb, void* arg);
  197. /**
  198. * @brief Remove callback
  199. *
  200. * @param type callback function event type
  201. *
  202. * @return
  203. * - ESP_OK Success
  204. */
  205. esp_err_t rm_cb(button_cb_type_t type);
  206. };
  207. #endif
  208. #endif