touch_element.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. * @brief Touch element sleep configuration passed to touch_element_enable_light_sleep or touch_element_enable_deep_sleep
  91. */
  92. typedef struct {
  93. uint16_t sample_count; //!< scan times in every measurement, normally equal to the 'sample_count' field in 'touch_elem_hw_config_t'.
  94. uint16_t sleep_cycle; //!< sleep_cycle decide the interval between two measurements, t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency), normally equal to the 'sleep_cycle' field in 'touch_elem_hw_config_t'.
  95. } touch_elem_sleep_config_t;
  96. /* ------------------------------------------------------------------------------------------------------------------ */
  97. typedef void *touch_elem_handle_t; //!< Touch element handle type
  98. typedef uint32_t touch_elem_event_t; //!< Touch element event type
  99. /**
  100. * @brief Touch element handle type
  101. */
  102. typedef enum {
  103. TOUCH_ELEM_TYPE_BUTTON, //!< Touch element button
  104. TOUCH_ELEM_TYPE_SLIDER, //!< Touch element slider
  105. TOUCH_ELEM_TYPE_MATRIX, //!< Touch element matrix button
  106. } touch_elem_type_t;
  107. /**
  108. * @brief Touch element event dispatch methods (event queue/callback)
  109. */
  110. typedef enum {
  111. TOUCH_ELEM_DISP_EVENT, //!< Event queue dispatch
  112. TOUCH_ELEM_DISP_CALLBACK, //!< Callback dispatch
  113. TOUCH_ELEM_DISP_MAX
  114. } touch_elem_dispatch_t;
  115. /**
  116. * @brief Touch element event message type from touch_element_message_receive()
  117. */
  118. typedef struct {
  119. touch_elem_handle_t handle; //!< Touch element handle
  120. touch_elem_type_t element_type; //!< Touch element type
  121. void *arg; //!< User input argument
  122. uint8_t child_msg[8]; //!< Encoded message
  123. } touch_elem_message_t;
  124. /* ------------------------------------------------------------------------------------------------------------------ */
  125. /**
  126. * @brief Touch element processing initialization
  127. *
  128. * @param[in] global_config Global initialization configuration structure
  129. *
  130. * @note To reinitialize the touch element object, call touch_element_uninstall() first
  131. *
  132. * @return
  133. * - ESP_OK: Successfully initialized
  134. * - ESP_ERR_INVALID_ARG: Invalid argument
  135. * - ESP_ERR_NO_MEM: Insufficient memory
  136. * - ESP_ERR_INVALID_STATE: Touch element is already initialized
  137. * - Others: Unknown touch driver layer or lower layer error
  138. */
  139. esp_err_t touch_element_install(const touch_elem_global_config_t *global_config);
  140. /**
  141. * @brief Touch element processing start
  142. *
  143. * This function starts the touch element processing system
  144. *
  145. * @note This function must only be called after all the touch element instances finished creating
  146. *
  147. * @return
  148. * - ESP_OK: Successfully started to process
  149. * - Others: Unknown touch driver layer or lower layer error
  150. */
  151. esp_err_t touch_element_start(void);
  152. /**
  153. * @brief Touch element processing stop
  154. *
  155. * This function stops the touch element processing system
  156. *
  157. * @note This function must be called before changing the system (hardware, software) parameters
  158. *
  159. * @return
  160. * - ESP_OK: Successfully stopped to process
  161. * - Others: Unknown touch driver layer or lower layer error
  162. */
  163. esp_err_t touch_element_stop(void);
  164. /**
  165. * @brief Release resources allocated using touch_element_install
  166. *
  167. */
  168. void touch_element_uninstall(void);
  169. /**
  170. * @brief Get current event message of touch element instance
  171. *
  172. * This function will receive the touch element message (handle, event type, etc...)
  173. * from te_event_give(). It will block until a touch element event or a timeout occurs.
  174. *
  175. * @param[out] element_message Touch element event message structure
  176. * @param[in] ticks_to_wait Number of FreeRTOS ticks to block for waiting event
  177. * @return
  178. * - ESP_OK: Successfully received touch element event
  179. * - ESP_ERR_INVALID_STATE: Touch element library is not initialized
  180. * - ESP_ERR_INVALID_ARG: element_message is null
  181. * - ESP_ERR_TIMEOUT: Timed out waiting for event
  182. */
  183. esp_err_t touch_element_message_receive(touch_elem_message_t *element_message, uint32_t ticks_to_wait);
  184. /**
  185. * @brief Touch element waterproof initialization
  186. *
  187. * This function enables the hardware waterproof, then touch element system uses Shield-Sensor
  188. * and Guard-Sensor to mitigate the influence of water-drop and water-stream.
  189. *
  190. * @param[in] waterproof_config Waterproof configuration
  191. *
  192. * @note If the waterproof function is used, Shield-Sensor can not be disabled and it will use channel 14 as
  193. * it's internal channel. Hence, the user can not use channel 14 for another propose. And the Guard-Sensor
  194. * is not necessary since it is optional.
  195. *
  196. * @note Shield-Sensor: It always uses channel 14 as the shield channel, so user must connect
  197. * the channel 14 and Shield-Layer in PCB since it will generate a synchronous signal automatically
  198. *
  199. * @note Guard-Sensor: This function is optional. If used, the user must connect the guard channel and Guard-Ring
  200. * in PCB. Any channels user wants to protect should be added into Guard-Ring in PCB.
  201. *
  202. * @return
  203. * - ESP_OK: Successfully initialized
  204. * - ESP_ERR_INVALID_STATE: Touch element library is not initialized
  205. * - ESP_ERR_INVALID_ARG: waterproof_config is null or invalid Guard-Sensor channel
  206. * - ESP_ERR_NO_MEM: Insufficient memory
  207. */
  208. esp_err_t touch_element_waterproof_install(const touch_elem_waterproof_config_t *waterproof_config);
  209. /**
  210. * @brief Release resources allocated using touch_element_waterproof_install()
  211. */
  212. void touch_element_waterproof_uninstall(void);
  213. /**
  214. * @brief Add a masked handle to protect while Guard-Sensor has been triggered
  215. *
  216. * This function will add an application handle (button, slider, etc...) as a masked handle. While Guard-Sensor
  217. * has been triggered, waterproof function will start working and lock the application internal state. While the
  218. * influence of water is reduced, the application will be unlock and reset into IDLE state.
  219. *
  220. * @param[in] element_handle Touch element instance handle
  221. *
  222. * @note The waterproof protection logic must follow the real circuit in PCB, it means that all of the channels
  223. * inside the input handle must be inside the Guard-Ring in real circuit.
  224. *
  225. * @return
  226. * - ESP_OK: Successfully added a masked handle
  227. * - ESP_ERR_INVALID_STATE: Waterproof is not initialized
  228. * - ESP_ERR_INVALID_ARG: element_handle is null
  229. */
  230. esp_err_t touch_element_waterproof_add(touch_elem_handle_t element_handle);
  231. /**
  232. * @brief Remove a masked handle to protect
  233. *
  234. * This function will remove an application handle from masked handle table.
  235. *
  236. * @param[in] element_handle Touch element instance handle
  237. *
  238. * @return
  239. * - ESP_OK: Successfully removed a masked handle
  240. * - ESP_ERR_INVALID_STATE: Waterproof is not initialized
  241. * - ESP_ERR_INVALID_ARG: element_handle is null
  242. * - ESP_ERR_NOT_FOUND: Failed to search element_handle from waterproof mask_handle list
  243. */
  244. esp_err_t touch_element_waterproof_remove(touch_elem_handle_t element_handle);
  245. /**
  246. * @brief Touch element light sleep initialization
  247. *
  248. * @note It should be called after touch button element installed.
  249. * Any of installed touch element can wake up from the light sleep
  250. *
  251. * @param[in] sleep_config Sleep configurations, set NULL to use default config
  252. * @return
  253. * - ESP_OK: Successfully initialized touch sleep
  254. * - ESP_ERR_INVALID_STATE: Touch element is not installed or touch sleep has been installed
  255. * - ESP_ERR_INVALID_ARG: inputed argument is NULL
  256. * - ESP_ERR_NO_MEM: no memory for touch sleep struct
  257. * - ESP_ERR_NOT_SUPPORTED: inputed wakeup_elem_handle is not touch_button_handle_t type, currently only touch_button_handle_t supported
  258. */
  259. esp_err_t touch_element_enable_light_sleep(const touch_elem_sleep_config_t *sleep_config);
  260. /**
  261. * @brief Release the resources that allocated by touch_element_enable_deep_sleep()
  262. *
  263. * This function will also disable the touch sensor to wake up the device
  264. *
  265. * @return
  266. * - ESP_OK: uninstall success
  267. * - ESP_ERR_INVALID_STATE: touch sleep has not been installed
  268. */
  269. esp_err_t touch_element_disable_light_sleep(void);
  270. /**
  271. * @brief Touch element deep sleep initialization
  272. *
  273. * This function will enable the device wake-up from deep sleep or light sleep by touch sensor
  274. *
  275. * @note It should be called after touch button element installed.
  276. * Only one touch button can be registered as the deep sleep wake-up button
  277. *
  278. * @param[in] wakeup_elem_handle Touch element instance handle for waking up the device, only support button element
  279. * @param[in] sleep_config Sleep configurations, set NULL to use default config
  280. *
  281. * @return
  282. * - ESP_OK: Successfully initialized touch sleep
  283. * - ESP_ERR_INVALID_STATE: Touch element is not installed or touch sleep has been installed
  284. * - ESP_ERR_INVALID_ARG: inputed argument is NULL
  285. * - ESP_ERR_NO_MEM: no memory for touch sleep struct
  286. * - ESP_ERR_NOT_SUPPORTED: inputed wakeup_elem_handle is not touch_button_handle_t type, currently only touch_button_handle_t supported
  287. */
  288. esp_err_t touch_element_enable_deep_sleep(touch_elem_handle_t wakeup_elem_handle, const touch_elem_sleep_config_t *sleep_config);
  289. /**
  290. * @brief Release the resources that allocated by touch_element_enable_deep_sleep()
  291. *
  292. * This function will also disable the touch sensor to wake up the device
  293. *
  294. * @return
  295. * - ESP_OK: uninstall success
  296. * - ESP_ERR_INVALID_STATE: touch sleep has not been installed
  297. */
  298. esp_err_t touch_element_disable_deep_sleep(void);
  299. /**
  300. * @brief Touch element wake up calibrations
  301. *
  302. * This function will also disable the touch sensor to wake up the device
  303. *
  304. * @return
  305. * - ESP_OK: uninstall success
  306. * - ESP_ERR_INVALID_STATE: touch sleep has not been installed
  307. */
  308. esp_err_t touch_element_sleep_enable_wakeup_calibration(touch_elem_handle_t element_handle, bool en);
  309. #ifdef __cplusplus
  310. }
  311. #endif