touch_button.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2016-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "touch_element/touch_element.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* --------------------------------- General button instance default configuration --------------------------------- */
  20. #define TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG() \
  21. { \
  22. .threshold_divider = 0.8, \
  23. .default_lp_time = 1000 \
  24. }
  25. /* ------------------------------------------------------------------------------------------------------------------ */
  26. /**
  27. * @brief Button initialization configuration passed to touch_button_install
  28. */
  29. typedef struct {
  30. float threshold_divider; //!< Button channel threshold divider
  31. uint32_t default_lp_time; //!< Button default LongPress event time (ms)
  32. } touch_button_global_config_t;
  33. /**
  34. * @brief Button configuration (for new instance) passed to touch_button_create()
  35. */
  36. typedef struct {
  37. touch_pad_t channel_num; //!< Button channel number (index)
  38. float channel_sens; //!< Button channel sensitivity
  39. } touch_button_config_t;
  40. /**
  41. * @brief Button event type
  42. */
  43. typedef enum {
  44. TOUCH_BUTTON_EVT_ON_PRESS, //!< Button Press event
  45. TOUCH_BUTTON_EVT_ON_RELEASE, //!< Button Release event
  46. TOUCH_BUTTON_EVT_ON_LONGPRESS, //!< Button LongPress event
  47. TOUCH_BUTTON_EVT_MAX
  48. } touch_button_event_t;
  49. /**
  50. * @brief Button message type
  51. */
  52. typedef struct {
  53. touch_button_event_t event; //!< Button event
  54. } touch_button_message_t;
  55. typedef touch_elem_handle_t touch_button_handle_t; //!< Button handle
  56. typedef void(*touch_button_callback_t)(touch_button_handle_t, touch_button_message_t *, void *); //!< Button callback type
  57. /**
  58. * @brief Touch Button initialize
  59. *
  60. * This function initializes touch button global and acts on all
  61. * touch button instances.
  62. *
  63. * @param[in] global_config Button object initialization configuration
  64. *
  65. * @return
  66. * - ESP_OK: Successfully initialized touch button
  67. * - ESP_ERR_INVALID_STATE: Touch element library was not initialized
  68. * - ESP_ERR_INVALID_ARG: button_init is NULL
  69. * - ESP_ERR_NO_MEM: Insufficient memory
  70. */
  71. esp_err_t touch_button_install(const touch_button_global_config_t *global_config);
  72. /**
  73. * @brief Release resources allocated using touch_button_install()
  74. */
  75. void touch_button_uninstall(void);
  76. /**
  77. * @brief Create a new touch button instance
  78. *
  79. * @param[in] button_config Button configuration
  80. * @param[out] button_handle Button handle
  81. *
  82. * @note The sensitivity has to be explored in experiments,
  83. * Sensitivity = (Raw(touch) - Raw(release)) / Raw(release) * 100%
  84. *
  85. * @return
  86. * - ESP_OK: Successfully create touch button
  87. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  88. * - ESP_ERR_NO_MEM: Insufficient memory
  89. * - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
  90. */
  91. esp_err_t touch_button_create(const touch_button_config_t *button_config, touch_button_handle_t *button_handle);
  92. /**
  93. * @brief Release resources allocated using touch_button_create()
  94. *
  95. * @param[in] button_handle Button handle
  96. * @return
  97. * - ESP_OK: Successfully released resources
  98. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  99. * - ESP_ERR_INVALID_ARG: button_handle is null
  100. * - ESP_ERR_NOT_FOUND: Input handle is not a button handle
  101. */
  102. esp_err_t touch_button_delete(touch_button_handle_t button_handle);
  103. /**
  104. * @brief Touch button subscribes event
  105. *
  106. * This function uses event mask to subscribe to touch button events, once one of
  107. * the subscribed events occurs, the event message could be retrieved by calling
  108. * touch_element_message_receive() or input callback routine.
  109. *
  110. * @param[in] button_handle Button handle
  111. * @param[in] event_mask Button subscription event mask
  112. * @param[in] arg User input argument
  113. *
  114. * @note Touch button only support three kind of event masks, they are
  115. * TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE, TOUCH_ELEM_EVENT_ON_LONGPRESS.
  116. * You can use those event masks in any combination to achieve the desired effect.
  117. *
  118. * @return
  119. * - ESP_OK: Successfully subscribed touch button event
  120. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  121. * - ESP_ERR_INVALID_ARG: button_handle is null or event is not supported
  122. */
  123. esp_err_t touch_button_subscribe_event(touch_button_handle_t button_handle, uint32_t event_mask, void *arg);
  124. /**
  125. * @brief Touch button set dispatch method
  126. *
  127. * This function sets a dispatch method that the driver core will use
  128. * this method as the event notification method.
  129. *
  130. * @param[in] button_handle Button handle
  131. * @param[in] dispatch_method Dispatch method (By callback/event)
  132. *
  133. * @return
  134. * - ESP_OK: Successfully set dispatch method
  135. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  136. * - ESP_ERR_INVALID_ARG: button_handle is null or dispatch_method is invalid
  137. */
  138. esp_err_t touch_button_set_dispatch_method(touch_button_handle_t button_handle, touch_elem_dispatch_t dispatch_method);
  139. /**
  140. * @brief Touch button set callback
  141. *
  142. * This function sets a callback routine into touch element driver core,
  143. * when the subscribed events occur, the callback routine will be called.
  144. *
  145. * @param[in] button_handle Button handle
  146. * @param[in] button_callback User input callback
  147. *
  148. * @note Button message will be passed from the callback function and it will be destroyed when the callback function return.
  149. *
  150. * @warning Since this input callback routine runs on driver core (esp-timer callback routine),
  151. * it should not do something that attempts to Block, such as calling vTaskDelay().
  152. *
  153. * @return
  154. * - ESP_OK: Successfully set callback
  155. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  156. * - ESP_ERR_INVALID_ARG: button_handle or button_callback is null
  157. */
  158. esp_err_t touch_button_set_callback(touch_button_handle_t button_handle, touch_button_callback_t button_callback);
  159. /**
  160. * @brief Touch button set long press trigger time
  161. *
  162. * This function sets the threshold time (ms) for a long press event. If a button is pressed
  163. * and held for a period of time that exceeds the threshold time, a long press event is triggered.
  164. *
  165. * @param[in] button_handle Button handle
  166. * @param[in] threshold_time Threshold time (ms) of long press event occur
  167. *
  168. * @return
  169. * - ESP_OK: Successfully set the threshold time of long press event
  170. * - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
  171. * - ESP_ERR_INVALID_ARG: button_handle is null or time (ms) is not lager than 0
  172. */
  173. esp_err_t touch_button_set_longpress(touch_button_handle_t button_handle, uint32_t threshold_time);
  174. /**
  175. * @brief Touch button get message
  176. *
  177. * This function decodes the element message from touch_element_message_receive() and return
  178. * a button message pointer.
  179. *
  180. * @param[in] element_message element message
  181. *
  182. * @return Touch button message pointer
  183. */
  184. const touch_button_message_t* touch_button_get_message(const touch_elem_message_t* element_message);
  185. #ifdef __cplusplus
  186. }
  187. #endif