touch_sensor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include "driver/touch_sensor_common.h"
  18. /**
  19. * @brief Configure touch pad interrupt threshold.
  20. *
  21. * @note If FSM mode is set to TOUCH_FSM_MODE_TIMER, this function will be blocked for one measurement cycle and wait for data to be valid.
  22. *
  23. * @param touch_num touch pad index
  24. * @param threshold interrupt threshold,
  25. *
  26. * @return
  27. * - ESP_OK Success
  28. * - ESP_ERR_INVALID_ARG if argument wrong
  29. * - ESP_FAIL if touch pad not initialized
  30. */
  31. esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold);
  32. /**
  33. * @brief get touch sensor counter value.
  34. * Each touch sensor has a counter to count the number of charge/discharge cycles.
  35. * When the pad is not 'touched', we can get a number of the counter.
  36. * When the pad is 'touched', the value in counter will get smaller because of the larger equivalent capacitance.
  37. *
  38. * @note This API requests hardware measurement once. If IIR filter mode is enabled,
  39. * please use 'touch_pad_read_raw_data' interface instead.
  40. *
  41. * @param touch_num touch pad index
  42. * @param touch_value pointer to accept touch sensor value
  43. *
  44. * @return
  45. * - ESP_OK Success
  46. * - ESP_ERR_INVALID_ARG Touch pad parameter error
  47. * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
  48. * - ESP_FAIL Touch pad not initialized
  49. */
  50. esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value);
  51. /**
  52. * @brief get filtered touch sensor counter value by IIR filter.
  53. *
  54. * @note touch_pad_filter_start has to be called before calling touch_pad_read_filtered.
  55. * This function can be called from ISR
  56. *
  57. * @param touch_num touch pad index
  58. * @param touch_value pointer to accept touch sensor value
  59. *
  60. * @return
  61. * - ESP_OK Success
  62. * - ESP_ERR_INVALID_ARG Touch pad parameter error
  63. * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
  64. * - ESP_FAIL Touch pad not initialized
  65. */
  66. esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value);
  67. /**
  68. * @brief get raw data (touch sensor counter value) from IIR filter process.
  69. * Need not request hardware measurements.
  70. *
  71. * @note touch_pad_filter_start has to be called before calling touch_pad_read_raw_data.
  72. * This function can be called from ISR
  73. *
  74. * @param touch_num touch pad index
  75. * @param touch_value pointer to accept touch sensor value
  76. *
  77. * @return
  78. * - ESP_OK Success
  79. * - ESP_ERR_INVALID_ARG Touch pad parameter error
  80. * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0.
  81. * - ESP_FAIL Touch pad not initialized
  82. */
  83. esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value);
  84. /**
  85. * @brief Callback function that is called after each IIR filter calculation.
  86. * @note This callback is called in timer task in each filtering cycle.
  87. * @note This callback should not be blocked.
  88. * @param raw_value The latest raw data(touch sensor counter value) that
  89. * points to all channels(raw_value[0..TOUCH_PAD_MAX-1]).
  90. * @param filtered_value The latest IIR filtered data(calculated from raw data) that
  91. * points to all channels(filtered_value[0..TOUCH_PAD_MAX-1]).
  92. *
  93. */
  94. typedef void (* filter_cb_t)(uint16_t *raw_value, uint16_t *filtered_value);
  95. /**
  96. * @brief Register the callback function that is called after each IIR filter calculation.
  97. * @note The 'read_cb' callback is called in timer task in each filtering cycle.
  98. * @param read_cb Pointer to filtered callback function.
  99. * If the argument passed in is NULL, the callback will stop.
  100. * @return
  101. * - ESP_OK Success
  102. * - ESP_ERR_INVALID_ARG set error
  103. */
  104. esp_err_t touch_pad_set_filter_read_cb(filter_cb_t read_cb);
  105. /**
  106. * @brief Register touch-pad ISR.
  107. * The handler will be attached to the same CPU core that this function is running on.
  108. * @param fn Pointer to ISR handler
  109. * @param arg Parameter for ISR
  110. * @return
  111. * - ESP_OK Success ;
  112. * - ESP_ERR_INVALID_ARG GPIO error
  113. * - ESP_ERR_NO_MEM No memory
  114. */
  115. esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg);
  116. /**
  117. * @brief Set touch sensor measurement and sleep time.
  118. * Excessive total time will slow down the touch response.
  119. * Too small measurement time will not be sampled enough, resulting in inaccurate measurements.
  120. *
  121. * @note The greater the duty cycle of the measurement time, the more system power is consumed.
  122. * @param sleep_cycle The touch sensor will sleep after each measurement.
  123. * sleep_cycle decide the interval between each measurement.
  124. * t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency).
  125. * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function.
  126. * @param meas_cycle The duration of the touch sensor measurement.
  127. * t_meas = meas_cycle / 8M, the maximum measure time is 0xffff / 8M = 8.19 ms
  128. * @return
  129. * - ESP_OK on success
  130. */
  131. esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle);
  132. /**
  133. * @brief Get touch sensor measurement and sleep time
  134. * @param sleep_cycle Pointer to accept sleep cycle number
  135. * @param meas_cycle Pointer to accept measurement cycle count.
  136. * @return
  137. * - ESP_OK on success
  138. */
  139. esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle);
  140. /**
  141. * @brief Trigger a touch sensor measurement, only support in SW mode of FSM
  142. * @return
  143. * - ESP_OK on success
  144. */
  145. esp_err_t touch_pad_sw_start(void);
  146. /**
  147. * @brief Set touch sensor interrupt threshold
  148. * @param touch_num touch pad index
  149. * @param threshold threshold of touchpad count, refer to touch_pad_set_trigger_mode to see how to set trigger mode.
  150. * @return
  151. * - ESP_OK on success
  152. * - ESP_ERR_INVALID_ARG if argument is wrong
  153. */
  154. esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold);
  155. /**
  156. * @brief Get touch sensor interrupt threshold
  157. * @param touch_num touch pad index
  158. * @param threshold pointer to accept threshold
  159. * @return
  160. * - ESP_OK on success
  161. * - ESP_ERR_INVALID_ARG if argument is wrong
  162. */
  163. esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold);
  164. /**
  165. * @brief Set touch sensor interrupt trigger mode.
  166. * Interrupt can be triggered either when counter result is less than
  167. * threshold or when counter result is more than threshold.
  168. * @param mode touch sensor interrupt trigger mode
  169. * @return
  170. * - ESP_OK on success
  171. * - ESP_ERR_INVALID_ARG if argument is wrong
  172. */
  173. esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode);
  174. /**
  175. * @brief Get touch sensor interrupt trigger mode
  176. * @param mode pointer to accept touch sensor interrupt trigger mode
  177. * @return
  178. * - ESP_OK on success
  179. */
  180. esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode);
  181. /**
  182. * @brief Set touch sensor interrupt trigger source. There are two sets of touch signals.
  183. * Set1 and set2 can be mapped to several touch signals. Either set will be triggered
  184. * if at least one of its touch signal is 'touched'. The interrupt can be configured to be generated
  185. * if set1 is triggered, or only if both sets are triggered.
  186. * @param src touch sensor interrupt trigger source
  187. * @return
  188. * - ESP_OK on success
  189. * - ESP_ERR_INVALID_ARG if argument is wrong
  190. */
  191. esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src);
  192. /**
  193. * @brief Get touch sensor interrupt trigger source
  194. * @param src pointer to accept touch sensor interrupt trigger source
  195. * @return
  196. * - ESP_OK on success
  197. */
  198. esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src);
  199. /**
  200. * @brief Set touch sensor group mask.
  201. * Touch pad module has two sets of signals, 'Touched' signal is triggered only if
  202. * at least one of touch pad in this group is "touched".
  203. * This function will set the register bits according to the given bitmask.
  204. * @param set1_mask bitmask of touch sensor signal group1, it's a 10-bit value
  205. * @param set2_mask bitmask of touch sensor signal group2, it's a 10-bit value
  206. * @param en_mask bitmask of touch sensor work enable, it's a 10-bit value
  207. * @return
  208. * - ESP_OK on success
  209. * - ESP_ERR_INVALID_ARG if argument is wrong
  210. */
  211. esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask);
  212. /**
  213. * @brief Get touch sensor group mask.
  214. * @param set1_mask pointer to accept bitmask of touch sensor signal group1, it's a 10-bit value
  215. * @param set2_mask pointer to accept bitmask of touch sensor signal group2, it's a 10-bit value
  216. * @param en_mask pointer to accept bitmask of touch sensor work enable, it's a 10-bit value
  217. * @return
  218. * - ESP_OK on success
  219. */
  220. esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask);
  221. /**
  222. * @brief Clear touch sensor group mask.
  223. * Touch pad module has two sets of signals, Interrupt is triggered only if
  224. * at least one of touch pad in this group is "touched".
  225. * This function will clear the register bits according to the given bitmask.
  226. * @param set1_mask bitmask touch sensor signal group1, it's a 10-bit value
  227. * @param set2_mask bitmask touch sensor signal group2, it's a 10-bit value
  228. * @param en_mask bitmask of touch sensor work enable, it's a 10-bit value
  229. * @return
  230. * - ESP_OK on success
  231. * - ESP_ERR_INVALID_ARG if argument is wrong
  232. */
  233. esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask);
  234. /**
  235. * @brief To enable touch pad interrupt
  236. * @return
  237. * - ESP_OK on success
  238. */
  239. esp_err_t touch_pad_intr_enable(void);
  240. /**
  241. * @brief To disable touch pad interrupt
  242. * @return
  243. * - ESP_OK on success
  244. */
  245. esp_err_t touch_pad_intr_disable(void);
  246. /**
  247. * @brief To clear touch pad interrupt
  248. * @return
  249. * - ESP_OK on success
  250. */
  251. esp_err_t touch_pad_intr_clear(void);
  252. /**
  253. * @brief set touch pad filter calibration period, in ms.
  254. * Need to call touch_pad_filter_start before all touch filter APIs
  255. * @param new_period_ms filter period, in ms
  256. * @return
  257. * - ESP_OK Success
  258. * - ESP_ERR_INVALID_STATE driver state error
  259. * - ESP_ERR_INVALID_ARG parameter error
  260. */
  261. esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms);
  262. /**
  263. * @brief get touch pad filter calibration period, in ms
  264. * Need to call touch_pad_filter_start before all touch filter APIs
  265. * @param p_period_ms pointer to accept period
  266. * @return
  267. * - ESP_OK Success
  268. * - ESP_ERR_INVALID_STATE driver state error
  269. * - ESP_ERR_INVALID_ARG parameter error
  270. */
  271. esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms);
  272. /**
  273. * @brief start touch pad filter function
  274. * This API will start a filter to process the noise in order to prevent false triggering
  275. * when detecting slight change of capacitance.
  276. * Need to call touch_pad_filter_start before all touch filter APIs
  277. *
  278. * @note This filter uses FreeRTOS timer, which is dispatched from a task with
  279. * priority 1 by default on CPU 0. So if some application task with higher priority
  280. * takes a lot of CPU0 time, then the quality of data obtained from this filter will be affected.
  281. * You can adjust FreeRTOS timer task priority in menuconfig.
  282. * @param filter_period_ms filter calibration period, in ms
  283. * @return
  284. * - ESP_OK Success
  285. * - ESP_ERR_INVALID_ARG parameter error
  286. * - ESP_ERR_NO_MEM No memory for driver
  287. * - ESP_ERR_INVALID_STATE driver state error
  288. */
  289. esp_err_t touch_pad_filter_start(uint32_t filter_period_ms);
  290. /**
  291. * @brief stop touch pad filter function
  292. * Need to call touch_pad_filter_start before all touch filter APIs
  293. * @return
  294. * - ESP_OK Success
  295. * - ESP_ERR_INVALID_STATE driver state error
  296. */
  297. esp_err_t touch_pad_filter_stop(void);
  298. /**
  299. * @brief delete touch pad filter driver and release the memory
  300. * Need to call touch_pad_filter_start before all touch filter APIs
  301. * @return
  302. * - ESP_OK Success
  303. * - ESP_ERR_INVALID_STATE driver state error
  304. */
  305. esp_err_t touch_pad_filter_delete(void);
  306. #ifdef __cplusplus
  307. }
  308. #endif