touch_element.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "driver/touch_sensor.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* -------------------------------- General hardware & system default configuration -------------------------------- */
  12. /* Since those are important hardware and algorithm parameters, user should not change them before knowing all details*/
  13. /* ------------------------------------------------------------------------------------------------------------------ */
  14. #define TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG() \
  15. { \
  16. .hardware = { \
  17. .upper_voltage = TOUCH_HVOLT_2V7, \
  18. .voltage_attenuation = TOUCH_HVOLT_ATTEN_0V5, \
  19. .lower_voltage = TOUCH_LVOLT_0V5, \
  20. .suspend_channel_polarity = TOUCH_PAD_CONN_HIGHZ, \
  21. .denoise_level = TOUCH_PAD_DENOISE_BIT4, \
  22. .denoise_equivalent_cap = TOUCH_PAD_DENOISE_CAP_L0, \
  23. .smooth_filter_mode = TOUCH_PAD_SMOOTH_IIR_2, \
  24. .benchmark_filter_mode = TOUCH_PAD_FILTER_IIR_16, \
  25. .sample_count = 500, \
  26. .sleep_cycle = 0xf, \
  27. .benchmark_debounce_count = 2, \
  28. .benchmark_calibration_threshold = 2, \
  29. .benchmark_jitter_step = 5 \
  30. }, \
  31. .software = { \
  32. .waterproof_threshold_divider = 0.8, \
  33. .processing_period = 10, \
  34. .intr_message_size = 14, \
  35. .event_message_size = 20 \
  36. } \
  37. }
  38. /* ------------------------------------------------------------------------------------------------------------------ */
  39. /* ---------------------------------------------- Event subscription ----------------------------------------------- */
  40. #define TOUCH_ELEM_EVENT_NONE BIT(0) //!< None event
  41. #define TOUCH_ELEM_EVENT_ON_PRESS BIT(1) //!< On Press event
  42. #define TOUCH_ELEM_EVENT_ON_RELEASE BIT(2) //!< On Release event
  43. #define TOUCH_ELEM_EVENT_ON_LONGPRESS BIT(3) //!< On LongPress event
  44. #define TOUCH_ELEM_EVENT_ON_CALCULATION BIT(4) //!< On Calculation event
  45. /* ------------------------------------------------------------------------------------------------------------------ */
  46. #define TOUCH_WATERPROOF_GUARD_NOUSE (0) //!< Waterproof no use guard sensor
  47. /* -------------------------------- Global hardware & software configuration struct --------------------------------- */
  48. /**
  49. * @brief Touch element software configuration
  50. */
  51. typedef struct {
  52. float waterproof_threshold_divider; //!< Waterproof guard channel threshold divider
  53. uint8_t processing_period; //!< Processing period(ms)
  54. uint8_t intr_message_size; //!< Interrupt message queue size
  55. uint8_t event_message_size; //!< Event message queue size
  56. } touch_elem_sw_config_t;
  57. /**
  58. * @brief Touch element hardware configuration
  59. */
  60. typedef struct {
  61. touch_high_volt_t upper_voltage; //!< Touch sensor channel upper charge voltage
  62. touch_volt_atten_t voltage_attenuation; //!< Touch sensor channel upper charge voltage attenuation (Diff voltage is upper - attenuation - lower)
  63. touch_low_volt_t lower_voltage; //!< Touch sensor channel lower charge voltage
  64. touch_pad_conn_type_t suspend_channel_polarity; //!< Suspend channel polarity (High Impedance State or GND)
  65. touch_pad_denoise_grade_t denoise_level; //!< Internal de-noise level
  66. touch_pad_denoise_cap_t denoise_equivalent_cap; //!< Internal de-noise channel (Touch channel 0) equivalent capacitance
  67. touch_smooth_mode_t smooth_filter_mode; //!< Smooth value filter mode (This only apply to touch_pad_filter_read_smooth())
  68. touch_filter_mode_t benchmark_filter_mode; //!< Benchmark filter mode
  69. uint16_t sample_count; //!< The count of sample in each measurement of touch sensor
  70. uint16_t sleep_cycle; //!< The cycle (RTC slow clock) of sleep
  71. uint8_t benchmark_debounce_count; //!< Benchmark debounce count
  72. uint8_t benchmark_calibration_threshold; //!< Benchmark calibration threshold
  73. uint8_t benchmark_jitter_step; //!< Benchmark jitter filter step (This only works at while benchmark filter mode is jitter filter)
  74. } touch_elem_hw_config_t;
  75. /**
  76. * @brief Touch element global configuration passed to touch_element_install
  77. */
  78. typedef struct {
  79. touch_elem_hw_config_t hardware; //!< Hardware configuration
  80. touch_elem_sw_config_t software; //!< Software configuration
  81. } touch_elem_global_config_t;
  82. /**
  83. * @brief Touch element waterproof configuration passed to touch_element_waterproof_install
  84. */
  85. typedef struct {
  86. touch_pad_t guard_channel; //!< Waterproof Guard-Sensor channel number (index)
  87. float guard_sensitivity; //!< Waterproof Guard-Sensor sensitivity
  88. } touch_elem_waterproof_config_t;
  89. /* ------------------------------------------------------------------------------------------------------------------ */
  90. typedef void *touch_elem_handle_t; //!< Touch element handle type
  91. typedef uint32_t touch_elem_event_t; //!< Touch element event type
  92. /**
  93. * @brief Touch element handle type
  94. */
  95. typedef enum {
  96. TOUCH_ELEM_TYPE_BUTTON, //!< Touch element button
  97. TOUCH_ELEM_TYPE_SLIDER, //!< Touch element slider
  98. TOUCH_ELEM_TYPE_MATRIX, //!< Touch element matrix button
  99. } touch_elem_type_t;
  100. /**
  101. * @brief Touch element event dispatch methods (event queue/callback)
  102. */
  103. typedef enum {
  104. TOUCH_ELEM_DISP_EVENT, //!< Event queue dispatch
  105. TOUCH_ELEM_DISP_CALLBACK, //!< Callback dispatch
  106. TOUCH_ELEM_DISP_MAX
  107. } touch_elem_dispatch_t;
  108. /**
  109. * @brief Touch element event message type from touch_element_message_receive()
  110. */
  111. typedef struct {
  112. touch_elem_handle_t handle; //!< Touch element handle
  113. touch_elem_type_t element_type; //!< Touch element type
  114. void *arg; //!< User input argument
  115. uint8_t child_msg[8]; //!< Encoded message
  116. } touch_elem_message_t;
  117. /* ------------------------------------------------------------------------------------------------------------------ */
  118. /**
  119. * @brief Touch element processing initialization
  120. *
  121. * @param[in] global_config Global initialization configuration structure
  122. *
  123. * @note To reinitialize the touch element object, call touch_element_uninstall() first
  124. *
  125. * @return
  126. * - ESP_OK: Successfully initialized
  127. * - ESP_ERR_INVALID_ARG: Invalid argument
  128. * - ESP_ERR_NO_MEM: Insufficient memory
  129. * - ESP_ERR_INVALID_STATE: Touch element is already initialized
  130. * - Others: Unknown touch driver layer or lower layer error
  131. */
  132. esp_err_t touch_element_install(const touch_elem_global_config_t *global_config);
  133. /**
  134. * @brief Touch element processing start
  135. *
  136. * This function starts the touch element processing system
  137. *
  138. * @note This function must only be called after all the touch element instances finished creating
  139. *
  140. * @return
  141. * - ESP_OK: Successfully started to process
  142. * - Others: Unknown touch driver layer or lower layer error
  143. */
  144. esp_err_t touch_element_start(void);
  145. /**
  146. * @brief Touch element processing stop
  147. *
  148. * This function stops the touch element processing system
  149. *
  150. * @note This function must be called before changing the system (hardware, software) parameters
  151. *
  152. * @return
  153. * - ESP_OK: Successfully stopped to process
  154. * - Others: Unknown touch driver layer or lower layer error
  155. */
  156. esp_err_t touch_element_stop(void);
  157. /**
  158. * @brief Release resources allocated using touch_element_install
  159. *
  160. */
  161. void touch_element_uninstall(void);
  162. /**
  163. * @brief Get current event message of touch element instance
  164. *
  165. * This function will receive the touch element message (handle, event type, etc...)
  166. * from te_event_give(). It will block until a touch element event or a timeout occurs.
  167. *
  168. * @param[out] element_message Touch element event message structure
  169. * @param[in] ticks_to_wait Number of FreeRTOS ticks to block for waiting event
  170. * @return
  171. * - ESP_OK: Successfully received touch element event
  172. * - ESP_ERR_INVALID_STATE: Touch element library is not initialized
  173. * - ESP_ERR_INVALID_ARG: element_message is null
  174. * - ESP_ERR_TIMEOUT: Timed out waiting for event
  175. */
  176. esp_err_t touch_element_message_receive(touch_elem_message_t *element_message, uint32_t ticks_to_wait);
  177. /**
  178. * @brief Touch element waterproof initialization
  179. *
  180. * This function enables the hardware waterproof, then touch element system uses Shield-Sensor
  181. * and Guard-Sensor to mitigate the influence of water-drop and water-stream.
  182. *
  183. * @param[in] waterproof_config Waterproof configuration
  184. *
  185. * @note If the waterproof function is used, Shield-Sensor can not be disabled and it will use channel 14 as
  186. * it's internal channel. Hence, the user can not use channel 14 for another propose. And the Guard-Sensor
  187. * is not necessary since it is optional.
  188. *
  189. * @note Shield-Sensor: It always uses channel 14 as the shield channel, so user must connect
  190. * the channel 14 and Shield-Layer in PCB since it will generate a synchronous signal automatically
  191. *
  192. * @note Guard-Sensor: This function is optional. If used, the user must connect the guard channel and Guard-Ring
  193. * in PCB. Any channels user wants to protect should be added into Guard-Ring in PCB.
  194. *
  195. * @return
  196. * - ESP_OK: Successfully initialized
  197. * - ESP_ERR_INVALID_STATE: Touch element library is not initialized
  198. * - ESP_ERR_INVALID_ARG: waterproof_config is null or invalid Guard-Sensor channel
  199. * - ESP_ERR_NO_MEM: Insufficient memory
  200. */
  201. esp_err_t touch_element_waterproof_install(const touch_elem_waterproof_config_t *waterproof_config);
  202. /**
  203. * @brief Release resources allocated using touch_element_waterproof_install()
  204. */
  205. void touch_element_waterproof_uninstall(void);
  206. /**
  207. * @brief Add a masked handle to protect while Guard-Sensor has been triggered
  208. *
  209. * This function will add an application handle (button, slider, etc...) as a masked handle. While Guard-Sensor
  210. * has been triggered, waterproof function will start working and lock the application internal state. While the
  211. * influence of water is reduced, the application will be unlock and reset into IDLE state.
  212. *
  213. * @param[in] element_handle Touch element instance handle
  214. *
  215. * @note The waterproof protection logic must follow the real circuit in PCB, it means that all of the channels
  216. * inside the input handle must be inside the Guard-Ring in real circuit.
  217. *
  218. * @return
  219. * - ESP_OK: Successfully added a masked handle
  220. * - ESP_ERR_INVALID_STATE: Waterproof is not initialized
  221. * - ESP_ERR_INVALID_ARG: element_handle is null
  222. */
  223. esp_err_t touch_element_waterproof_add(touch_elem_handle_t element_handle);
  224. /**
  225. * @brief Remove a masked handle to protect
  226. *
  227. * This function will remove an application handle from masked handle table.
  228. *
  229. * @param[in] element_handle Touch element instance handle
  230. *
  231. * @return
  232. * - ESP_OK: Successfully removed a masked handle
  233. * - ESP_ERR_INVALID_STATE: Waterproof is not initialized
  234. * - ESP_ERR_INVALID_ARG: element_handle is null
  235. * - ESP_ERR_NOT_FOUND: Failed to search element_handle from waterproof mask_handle list
  236. */
  237. esp_err_t touch_element_waterproof_remove(touch_elem_handle_t element_handle);
  238. typedef struct {
  239. uint16_t scan_time;
  240. uint16_t sleep_time;
  241. } touch_elem_sleep_config_t;
  242. esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config);
  243. void touch_element_sleep_uninstall(void);
  244. esp_err_t touch_element_sleep_add_wakeup(touch_elem_handle_t element_handle);
  245. esp_err_t touch_element_sleep_remove_wakeup(touch_elem_handle_t element_handle);
  246. esp_err_t touch_element_sleep_add_wakeup_channel(touch_pad_t wakeup_channel);
  247. esp_err_t touch_element_sleep_remove_wakeup_channel(touch_pad_t wakeup_channel);
  248. #ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION
  249. esp_err_t touch_element_sleep_config_wakeup_calibration(touch_elem_handle_t element_handle, bool en);
  250. #endif
  251. #ifdef __cplusplus
  252. }
  253. #endif