touch_sensor.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Copyright 2019 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * @brief Set touch sensor FSM start
  19. * @note Start FSM after the touch sensor FSM mode is set.
  20. * @note Call this function will reset benchmark of all touch channels.
  21. * @return
  22. * - ESP_OK on success
  23. */
  24. esp_err_t touch_pad_fsm_start(void);
  25. /**
  26. * @brief Stop touch sensor FSM.
  27. * @return
  28. * - ESP_OK on success
  29. */
  30. esp_err_t touch_pad_fsm_stop(void);
  31. /**
  32. * @brief Trigger a touch sensor measurement, only support in SW mode of FSM
  33. * @return
  34. * - ESP_OK on success
  35. */
  36. esp_err_t touch_pad_sw_start(void);
  37. /**
  38. * @brief Set touch sensor times of charge and discharge and sleep time.
  39. * Excessive total time will slow down the touch response.
  40. * Too small measurement time will not be sampled enough, resulting in inaccurate measurements.
  41. *
  42. * @note The greater the duty cycle of the measurement time, the more system power is consumed.
  43. * @param sleep_cycle The touch sensor will sleep after each measurement.
  44. * sleep_cycle decide the interval between each measurement.
  45. * t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency).
  46. * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function.
  47. * @param meas_timers The times of charge and discharge in each measure process of touch channels.
  48. * The timer frequency is 8Mhz. Range: 0 ~ 0xffff.
  49. * Recommended typical value: Modify this value to make the measurement time around 1ms.
  50. * @return
  51. * - ESP_OK on success
  52. */
  53. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_times);
  54. /**
  55. * @brief Get touch sensor times of charge and discharge and sleep time
  56. * @param sleep_cycle Pointer to accept sleep cycle number
  57. * @param meas_times Pointer to accept measurement times count.
  58. * @return
  59. * - ESP_OK on success
  60. */
  61. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_times);
  62. /**
  63. * @brief Set connection type of touch channel in idle status.
  64. * When a channel is in measurement mode, other initialized channels are in idle mode.
  65. * The touch channel is generally adjacent to the trace, so the connection state of the idle channel
  66. * affects the stability and sensitivity of the test channel.
  67. * The `CONN_HIGHZ`(high resistance) setting increases the sensitivity of touch channels.
  68. * The `CONN_GND`(grounding) setting increases the stability of touch channels.
  69. * @param type Select idle channel connect to high resistance state or ground.
  70. * @return
  71. * - ESP_OK on success
  72. */
  73. esp_err_t touch_pad_set_inactive_connect(touch_pad_conn_type_t type);
  74. /**
  75. * @brief Set connection type of touch channel in idle status.
  76. * When a channel is in measurement mode, other initialized channels are in idle mode.
  77. * The touch channel is generally adjacent to the trace, so the connection state of the idle channel
  78. * affects the stability and sensitivity of the test channel.
  79. * The `CONN_HIGHZ`(high resistance) setting increases the sensitivity of touch channels.
  80. * The `CONN_GND`(grounding) setting increases the stability of touch channels.
  81. * @param type Pointer to connection type.
  82. * @return
  83. * - ESP_OK on success
  84. */
  85. esp_err_t touch_pad_get_inactive_connect(touch_pad_conn_type_t *type);
  86. /**
  87. * @brief Set the trigger threshold of touch sensor.
  88. * The threshold determines the sensitivity of the touch sensor.
  89. * The threshold is the original value of the trigger state minus the benchmark value.
  90. * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be trigered.
  91. * @param touch_num touch pad index
  92. * @param threshold threshold of touch sensor. Should be less than the max change value of touch.
  93. * @return
  94. * - ESP_OK on success
  95. */
  96. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint32_t threshold);
  97. /**
  98. * @brief Get touch sensor trigger threshold
  99. * @param touch_num touch pad index
  100. * @param threshold pointer to accept threshold
  101. * @return
  102. * - ESP_OK on success
  103. * - ESP_ERR_INVALID_ARG if argument is wrong
  104. */
  105. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint32_t *threshold);
  106. /**
  107. * @brief Register touch channel into touch sensor scan group.
  108. * The working mode of the touch sensor is cyclically scanned.
  109. * This function will set the scan bits according to the given bitmask.
  110. * @note If set this mask, the FSM timer should be stop firsty.
  111. * @note The touch sensor that in scan map, should be deinit GPIO function firstly by `touch_pad_io_init`.
  112. * @param enable_mask bitmask of touch sensor scan group.
  113. * e.g. TOUCH_PAD_NUM14 -> BIT(14)
  114. * @return
  115. * - ESP_OK on success
  116. */
  117. esp_err_t touch_pad_set_channel_mask(uint16_t enable_mask);
  118. /**
  119. * @brief Get the touch sensor scan group bit mask.
  120. * @param enable_mask Pointer to bitmask of touch sensor scan group.
  121. * e.g. TOUCH_PAD_NUM14 -> BIT(14)
  122. * @return
  123. * - ESP_OK on success
  124. */
  125. esp_err_t touch_pad_get_channel_mask(uint16_t *enable_mask);
  126. /**
  127. * @brief Clear touch channel from touch sensor scan group.
  128. * The working mode of the touch sensor is cyclically scanned.
  129. * This function will clear the scan bits according to the given bitmask.
  130. * @note If clear all mask, the FSM timer should be stop firsty.
  131. * @param enable_mask bitmask of touch sensor scan group.
  132. * e.g. TOUCH_PAD_NUM14 -> BIT(14)
  133. * @return
  134. * - ESP_OK on success
  135. */
  136. esp_err_t touch_pad_clear_channel_mask(uint16_t enable_mask);
  137. /**
  138. * @brief Configure parameter for each touch channel.
  139. * @note Touch num 0 is denoise channel, please use `touch_pad_denoise_enable` to set denoise function
  140. * @param touch_num touch pad index
  141. * @return
  142. * - ESP_OK Success
  143. * - ESP_ERR_INVALID_ARG if argument wrong
  144. * - ESP_FAIL if touch pad not initialized
  145. */
  146. esp_err_t touch_pad_config(touch_pad_t touch_num);
  147. /**
  148. * @brief Reset the whole of touch module.
  149. * @note Call this funtion after `touch_pad_fsm_stop`,
  150. * @return
  151. * - ESP_OK Success
  152. */
  153. esp_err_t touch_pad_reset(void);
  154. /**
  155. * @brief Check touch sensor measurement status.
  156. * If doing measurement, the flag will be clear.
  157. * If finish measurement. the flag will be set.
  158. * @return
  159. * - TRUE finish measurement
  160. * - FALSE doing measurement
  161. */
  162. bool touch_pad_meas_is_done(void);
  163. /**
  164. * @brief Get the current measure channel.
  165. * Touch sensor measurement is cyclic scan mode.
  166. * @return
  167. * - touch channel number
  168. */
  169. touch_pad_t touch_pad_get_current_meas_channel(void);
  170. /**
  171. * @brief Get the touch sensor interrupt status mask.
  172. * @return
  173. * - touch intrrupt bit
  174. */
  175. uint32_t touch_pad_read_intr_status_mask(void);
  176. /**
  177. * @brief Enable touch sensor interrupt by bitmask.
  178. * @param type interrupt type
  179. * @return
  180. * - ESP_OK on success
  181. */
  182. esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask);
  183. /**
  184. * @brief Disable touch sensor interrupt by bitmask.
  185. * @param type interrupt type
  186. * @return
  187. * - ESP_OK on success
  188. */
  189. esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask);
  190. /**
  191. * @brief Register touch-pad ISR.
  192. * The handler will be attached to the same CPU core that this function is running on.
  193. * @param fn Pointer to ISR handler
  194. * @param arg Parameter for ISR
  195. * @return
  196. * - ESP_OK Success ;
  197. * - ESP_ERR_INVALID_ARG GPIO error
  198. * - ESP_ERR_NO_MEM No memory
  199. */
  200. esp_err_t touch_pad_isr_register(intr_handler_t fn, void* arg, touch_pad_intr_mask_t intr_mask);
  201. /**
  202. * @brief get raw data of touch sensor.
  203. * @note After the initialization is complete, the "raw_data" is max value. You need to wait for a measurement
  204. * cycle before you can read the correct touch value.
  205. * @param touch_num touch pad index
  206. * @param raw_data pointer to accept touch sensor value
  207. * @return
  208. * - ESP_OK Success
  209. * - ESP_FAIL Touch channel 0 havent this parameter.
  210. */
  211. esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data);
  212. /**
  213. * @brief get benchmark of touch sensor.
  214. * @note After initialization, the benchmark value is the maximum during the first measurement period.
  215. * @param touch_num touch pad index
  216. * @param touch_value pointer to accept touch sensor value
  217. * @return
  218. * - ESP_OK Success
  219. * - ESP_ERR_INVALID_ARG Touch channel 0 havent this parameter.
  220. */
  221. esp_err_t touch_pad_filter_read_benchmark(touch_pad_t touch_num, uint32_t *basedata);
  222. /**
  223. * @brief Force reset benchmark to raw data of touch sensor.
  224. * @param touch_num touch pad index
  225. * - TOUCH_PAD_MAX Reset basaline of all channels
  226. * @return
  227. * - ESP_OK Success
  228. */
  229. esp_err_t touch_pad_reset_benchmark(touch_pad_t touch_num);
  230. /**
  231. * @brief set parameter of touch sensor filter and detection algorithm.
  232. * For more details on the detection algorithm, please refer to the application documentation.
  233. * @param filter_info select filter type and threshold of detection algorithm
  234. * @return
  235. * - ESP_OK Success
  236. */
  237. esp_err_t touch_pad_filter_set_config(touch_filter_config_t *filter_info);
  238. /**
  239. * @brief get parameter of touch sensor filter and detection algorithm.
  240. * For more details on the detection algorithm, please refer to the application documentation.
  241. * @param filter_info select filter type and threshold of detection algorithm
  242. * @return
  243. * - ESP_OK Success
  244. */
  245. esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info);
  246. /**
  247. * @brief enable touch sensor filter for detection algorithm.
  248. * For more details on the detection algorithm, please refer to the application documentation.
  249. * @return
  250. * - ESP_OK Success
  251. */
  252. esp_err_t touch_pad_filter_enable(void);
  253. /**
  254. * @brief disable touch sensor filter for detection algorithm.
  255. * For more details on the detection algorithm, please refer to the application documentation.
  256. * @return
  257. * - ESP_OK Success
  258. */
  259. esp_err_t touch_pad_filter_disable(void);
  260. /**
  261. * @brief set parameter of denoise pad (TOUCH_PAD_NUM0).
  262. * T0 is an internal channel that does not have a corresponding external GPIO.
  263. * T0 will work simultaneously with the measured channel Tn. Finally, the actual
  264. * measured value of Tn is the value after subtracting lower bits of T0.
  265. * This denoise function filters out interference introduced on all channels,
  266. * such as noise introduced by the power supply and external EMI.
  267. * @param denoise parameter of denoise
  268. * @return
  269. * - ESP_OK Success
  270. */
  271. esp_err_t touch_pad_denoise_set_config(touch_pad_denoise_t *denoise);
  272. /**
  273. * @brief get parameter of denoise pad (TOUCH_PAD_NUM0).
  274. * @param denoise Pointer to parameter of denoise
  275. * @return
  276. * - ESP_OK Success
  277. */
  278. esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise);
  279. /**
  280. * @brief enable denoise function.
  281. * T0 is an internal channel that does not have a corresponding external GPIO.
  282. * T0 will work simultaneously with the measured channel Tn. Finally, the actual
  283. * measured value of Tn is the value after subtracting lower bits of T0.
  284. * This denoise function filters out interference introduced on all channels,
  285. * such as noise introduced by the power supply and external EMI.
  286. * @return
  287. * - ESP_OK Success
  288. */
  289. esp_err_t touch_pad_denoise_enable(void);
  290. /**
  291. * @brief disable denoise function.
  292. * @return
  293. * - ESP_OK Success
  294. */
  295. esp_err_t touch_pad_denoise_disable(void);
  296. /**
  297. * @brief Get denoise measure value (TOUCH_PAD_NUM0).
  298. * @param denoise value of denoise
  299. * @return
  300. * - ESP_OK Success
  301. */
  302. esp_err_t touch_pad_denoise_read_data(uint32_t *data);
  303. /**
  304. * @brief set parameter of waterproof function.
  305. *
  306. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel.
  307. * Guard pad is used to detect the large area of water covering the touch panel.
  308. * Shield pad is used to shield the influence of water droplets covering the touch panel.
  309. * It is generally designed as a grid and is placed around the touch buttons.
  310. *
  311. * @param waterproof parameter of waterproof
  312. * @return
  313. * - ESP_OK Success
  314. */
  315. esp_err_t touch_pad_waterproof_set_config(touch_pad_waterproof_t *waterproof);
  316. /**
  317. * @brief get parameter of waterproof function.
  318. * @param waterproof parameter of waterproof
  319. * @return
  320. * - ESP_OK Success
  321. */
  322. esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof);
  323. /**
  324. * @brief Enable parameter of waterproof function.
  325. * Should be called after function ``touch_pad_waterproof_set_config``.
  326. * @return
  327. * - ESP_OK Success
  328. */
  329. esp_err_t touch_pad_waterproof_enable(void);
  330. /**
  331. * @brief Disable parameter of waterproof function.
  332. * @return
  333. * - ESP_OK Success
  334. */
  335. esp_err_t touch_pad_waterproof_disable(void);
  336. /**
  337. * @brief Set parameter of proximity channel. Three proximity sensing channels can be set.
  338. * The proximity sensor measurement is the accumulation of touch channel measurements.
  339. * @note If stop the proximity function for the channel, point this proximity channel to `TOUCH_PAD_NUM0`.
  340. * @param proximity parameter of proximity
  341. * @return
  342. * - ESP_OK Success
  343. */
  344. esp_err_t touch_pad_proximity_set_config(touch_pad_proximity_t *proximity);
  345. /**
  346. * @brief Get parameter of proximity channel. Three proximity sensing channels can be set.
  347. * The proximity sensor measurement is the accumulation of touch channel measurements.
  348. * @param proximity parameter of proximity
  349. * @return
  350. * - ESP_OK Success
  351. */
  352. esp_err_t touch_pad_proximity_get_config(touch_pad_proximity_t *proximity);
  353. /**
  354. * @brief Get measure count of proximity channel.
  355. * The proximity sensor measurement is the accumulation of touch channel measurements.
  356. * @param touch_num touch pad index
  357. * @param proximity parameter of proximity
  358. * @return
  359. * - ESP_OK Success
  360. * - ESP_ERR_INVALID_ARG parameter is NULL
  361. */
  362. esp_err_t touch_pad_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt);
  363. /**
  364. * @brief Get the accumulated measurement of the proximity sensor.
  365. * The proximity sensor measurement is the accumulation of touch channel measurements.
  366. * @param touch_num touch pad index
  367. * @param measure_out If the accumulation process does not end, the `measure_out` is the process value.
  368. * @return
  369. * - ESP_OK Success
  370. */
  371. esp_err_t touch_pad_proximity_data_get(touch_pad_t touch_num, uint32_t *measure_out);
  372. /**
  373. * @brief Set parameter of touch sensor in sleep mode.
  374. * In order to achieve low power consumption in sleep mode, other circuits except the RTC part of the register are in a power-off state.
  375. * Only one touch channel is supported in the sleep state, which can be used as a wake-up function.
  376. * If in non-sleep mode, the sleep parameters do not work.
  377. * @param slp_config touch pad config
  378. * @return
  379. * - ESP_OK Success
  380. */
  381. esp_err_t touch_pad_sleep_channel_config(touch_pad_sleep_channel_t *slp_config);
  382. /**
  383. * @brief Read benchmark of touch sensor in sleep mode.
  384. * @param benchmark pointer to accept touch sensor benchmark value
  385. * @return
  386. * - ESP_OK Success
  387. * - ESP_ERR_INVALID_ARG parameter is NULL
  388. */
  389. esp_err_t touch_pad_sleep_channel_read_benchmark(uint32_t *benchmark);
  390. /**
  391. * @brief Read debounce of touch sensor in sleep mode.
  392. * @param debounce pointer to accept touch sensor debounce value
  393. * @return
  394. * - ESP_OK Success
  395. * - ESP_ERR_INVALID_ARG parameter is NULL
  396. */
  397. esp_err_t touch_pad_sleep_channel_read_debounce(uint32_t *debounce);
  398. /**
  399. * @brief Read proximity count of touch sensor in sleep mode.
  400. * @param proximity_cnt pointer to accept touch sensor proximity count value
  401. * @return
  402. * - ESP_OK Success
  403. * - ESP_ERR_INVALID_ARG parameter is NULL
  404. */
  405. esp_err_t touch_pad_sleep_channel_read_proximity_cnt(uint32_t *proximity_cnt);
  406. #ifdef __cplusplus
  407. }
  408. #endif