touch_sensor.h 12 KB

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