touch_button.h 7.3 KB

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