touch_element.h 13 KB

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