touch_element.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. Touch Element
  2. =============
  3. Overview
  4. --------
  5. Touch Element library provides a high level abstraction for building capacitive touch applications. The library's implementation gives a unified and friendly software interface thus allows for smooth and easy capacitive touch application development. The library is implemented atop the touch sensor driver (please see :doc:`Touch sensor driver API Reference <../peripherals/touch_pad>` for more information regarding low level API usage).
  6. Architecture
  7. ^^^^^^^^^^^^
  8. Touch Element library configures touch sensor peripherals via touch sensor driver. While some necessary hardware parameters should be passed to :cpp:func:`touch_element_install` and will be configured automatically only after calling :cpp:func:`touch_element_start`, because it will make great influence on the run-time system.
  9. These parameters include touch channel threshold, waterproof shield sensor driver-level and etc. Touch Element library sets touch sensor interrupt and esp-timer routine up and the hardware information of touch sensor (channel state, channel number) will be obtained in touch sensor interrupt service routine. When the specified channel event occurs, and those hardware information will be passed to the esp-timer callback routine, esp-timer callback routine will dispatch the touch sensor channel information to the touch elements(such as button, slider etc). Then runs the specified algorithm to update touch element's state or calculate its position, dispatch the result to user.
  10. So using Touch Element library, user doesn't need to care about the implementation of touch sensor peripheral, Touch Element library will handle most of the hardware information and pass the more meaningful messages to user event handler routine.
  11. Workflow of Touch Element library is illustrated in the picture below.
  12. .. figure:: /../_static/touch_element/te_architecture.svg
  13. :scale: 100 %
  14. :align: center
  15. Touch Element architecture
  16. The features in relation to the Touch Element library in {IDF_TARGET_NAME} are given in the table below.
  17. +--------------------------------------------------------------+---------------------+
  18. | Features | ESP32S2 |
  19. +==============================================================+=====================+
  20. | Touch Element waterproof | ✔ |
  21. +--------------------------------------------------------------+---------------------+
  22. | Touch Element button | ✔ |
  23. +--------------------------------------------------------------+---------------------+
  24. | Touch Element slider | ✔ |
  25. +--------------------------------------------------------------+---------------------+
  26. | Touch Element matrix button | ✔ |
  27. +--------------------------------------------------------------+---------------------+
  28. Peripheral
  29. ^^^^^^^^^^
  30. {IDF_TARGET_NAME} integrates one touch sensor peripheral with several physical channels.
  31. .. only:: esp32s2
  32. - 14 physical capacitive touch channels
  33. - Timer or software FSM trigger mode
  34. - Up to 5 kinds of interrupt(Upper threshold and lower threshold interrupt, measure one channel finish and measure all channels finish interrupt, measurement timeout interrupt)
  35. - Sleep mode wakeup source
  36. - Hardware internal de-noise
  37. - Hardware filter
  38. - Hardware waterproof sensor
  39. - Hardware proximity sensor
  40. .. only:: esp32
  41. - 10 physical capacitive touch channels
  42. - Timer or software FSM trigger mode
  43. - 2 kinds of interrupt(Greater than threshold and less than threshold)
  44. - Sleep mode wakeup source
  45. The channels are located as follows:
  46. ======================= =======================
  47. Channel {IDF_TARGET_NAME}
  48. ======================= =======================
  49. **Channel 0** **GPIO 0 (reserved)**
  50. **Channel 1** **GPIO 1**
  51. **Channel 2** **GPIO 2**
  52. **Channel 3** **GPIO 3**
  53. **Channel 4** **GPIO 4**
  54. **Channel 5** **GPIO 5**
  55. **Channel 6** **GPIO 6**
  56. **Channel 7** **GPIO 7**
  57. **Channel 8** **GPIO 8**
  58. **Channel 9** **GPIO 9**
  59. **Channel 10** **GPIO 10**
  60. **Channel 11** **GPIO 11**
  61. **Channel 12** **GPIO 12**
  62. **Channel 13** **GPIO 13**
  63. **Channel 14** **GPIO 14**
  64. ======================= =======================
  65. Terminology
  66. -----------
  67. The terms used in relation to the Touch Element library are given in the below.
  68. ======================= =========================================================================================
  69. Term Definition
  70. ======================= =========================================================================================
  71. **Touch sensor** Touch sensor peripheral inside the chip
  72. **Touch channel** Touch sensor channels inside the touch sensor peripheral
  73. **Touch pad** Off-chip physical solder pad (Generally inside the PCB)
  74. **De-noise channel** Internal de-noise channel (Is always Channel 0 and it is reserved)
  75. **Shield sensor** One of the waterproof sensor, use for compensating the influence of water drop
  76. **Guard sensor** One of the waterproof sensor, use for detecting the water stream
  77. **Shield channel** The channel that waterproof shield sensor connected to (Is always Channel 14)
  78. **Guard channel** The channel that waterproof guard sensor connected to
  79. **Shield pad** Off-chip physical solder pad (Generally is grids) and is connected to shield sensor
  80. **Guard pad** Off-chip physical solder pad (Is usually a ring) and is connected to guard sensor
  81. ======================= =========================================================================================
  82. .. figure:: /../_static/touch_element/te_component.svg
  83. :scale: 100 %
  84. :align: center
  85. Touch sensor application system components
  86. Touch Sensor Signal
  87. ^^^^^^^^^^^^^^^^^^^
  88. Each touch senor is able to provide the following types of signals:
  89. - Raw: The Raw signal is the unfiltered signal from the touch sensor
  90. - Smooth: The Smooth signal is a filtered version of the Raw signal via an internal hardware filter
  91. - Benchmark: The Benchmark signal is also a filtered signal that filters out extremely low-frequency noise.
  92. All of these signals can be obtained using touch sensor driver API.
  93. .. figure:: /../_static/touch_element/te_signal.png
  94. :scale: 40 %
  95. :align: center
  96. Touch sensor signals
  97. Touch Sensor Threshold
  98. ^^^^^^^^^^^^^^^^^^^^^^
  99. The Touch Sensor Threshold value is a configurable threshold value used to determine when a touch sensor is touched or not. When difference between the Smooth signal and the Benchmark signal becomes greater than the threshold value (i.e., ``(smooth - benchmark) > threshold``), the touch channel's state will be changed and a touch interrupt will be triggered simultaneously.
  100. .. figure:: /../_static/touch_element/te_threshold.svg
  101. :scale: 40 %
  102. :align: center
  103. Touch sensor signal threshold
  104. Sensitivity
  105. ^^^^^^^^^^^
  106. Important performance parameter of touch sensor, the larger it is, the better touch sensor will perform. It could be calculated by the format in below:
  107. .. math::
  108. Sensitivity = \frac{Signal_{press} - Signal_{release}}{Signal_{release}} = \frac{Signal_{delta}}{Signal_{benchmark}}
  109. Waterproof
  110. ^^^^^^^^^^
  111. Waterproof is a hardware feature of touch sensor which has guard sensor and shield sensor (Always connect to Channel 14) that has the ability to resist a degree influence of water drop and detect the water stream.
  112. Touch Button
  113. ^^^^^^^^^^^^
  114. Touch button consumes one channel of touch sensor, and it looks like as the picture below:
  115. .. figure:: /../_static/touch_element/te_button.svg
  116. :scale: 100 %
  117. :align: center
  118. Touch button
  119. Touch Slider
  120. ^^^^^^^^^^^^
  121. Touch slider consumes several channels(at least three channels) of touch sensor, the more channels consumed, the higher resolution and accuracy position it will perform. Touch slider looks like as the picture below:
  122. .. figure:: /../_static/touch_element/te_slider.svg
  123. :scale: 100 %
  124. :align: center
  125. Touch slider
  126. Touch Matrix
  127. ^^^^^^^^^^^^
  128. Touch matrix button consumes several channels(at least 2 + 2 = 4 channels), it gives a solution to use fewer channels and get more buttons. {IDF_TARGET_NAME} supports up to 49 buttons. Touch matrix button looks like as the picture below:
  129. .. figure:: /../_static/touch_element/te_matrix.svg
  130. :scale: 100 %
  131. :align: center
  132. Touch matrix
  133. Touch Element Library Usage
  134. ---------------------------
  135. Using this library should follow the initialization flow below:
  136. 1. To initialize Touch Element library by calling :cpp:func:`touch_element_install`
  137. 2. To initialize touch elements(button/slider etc) by calling :cpp:func:`touch_xxxx_install`
  138. 3. To create a new element instance by calling :cpp:func:`touch_xxxx_create`
  139. 4. To subscribe events by calling :cpp:func:`touch_xxxx_subscribe_event`
  140. 5. To choose a dispatch method by calling :cpp:func:`touch_xxxx_set_dispatch_method` that tells the library how to notify you while the subscribed event occur
  141. 6. (If dispatch by callback) Call :cpp:func:`touch_xxxx_set_callback` to set the event handler function.
  142. 7. To start Touch Element library by calling :cpp:func:`touch_element_start`
  143. 8. (If dispatch by callback) The callback will be called by the driver core when event happen, no need to do anything; (If dispatch by event task) create an event task and call :cpp:func:`touch_element_message_receive` to obtain messages in a loop.
  144. 9. [Optional] If user wants to suspend the Touch Element run-time system or for some reason that could not obtain the touch element message, :cpp:func:`touch_element_stop` should be called to suspend the Touch Element system and then resume it by calling :cpp:func:`touch_element_start` again.
  145. In code, the flow above may look like as follows:
  146. .. code-block:: c
  147. static touch_xxx_handle_t element_handle; //Declare a touch element handle
  148. //Define the subscribed event handler
  149. void event_handler(touch_xxx_handle_t out_handle, touch_xxx_message_t out_message, void *arg)
  150. {
  151. //Event handler logic
  152. }
  153. void app_main()
  154. {
  155. //Using the default initializer to config Touch Element library
  156. touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
  157. touch_element_install(&global_config);
  158. //Using the default initializer to config Touch elements
  159. touch_xxx_global_config_t elem_global_config = TOUCH_XXXX_GLOBAL_DEFAULT_CONFIG();
  160. touch_xxx_install(&elem_global_config);
  161. //Create a new instance
  162. touch_xxx_config_t element_config = {
  163. ...
  164. ...
  165. };
  166. touch_xxx_create(&element_config, &element_handle);
  167. //Subscribe the specified events by using the event mask
  168. touch_xxx_subscribe_event(element_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE, NULL);
  169. //Choose CALLBACK as the dispatch method
  170. touch_xxx_set_dispatch_method(element_handle, TOUCH_ELEM_DISP_CALLBACK);
  171. //Register the callback routine
  172. touch_xxx_set_callback(element_handle, event_handler);
  173. //Start Touch Element library processing
  174. touch_element_start();
  175. }
  176. Initialization
  177. ^^^^^^^^^^^^^^
  178. 1. To initialize Touch Element library, user has to configure touch sensor peripheral and Touch Element library by calling :cpp:func:`touch_element_install` with :cpp:type:`touch_elem_global_config_t`, the default initializer is available in :cpp:func:`TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG` and this default configuration is suitable for the most general application scene, and users are suggested not to change the default configuration before fully understanding Touch Sensor peripheral, because some changes might bring several impacts to the system.
  179. 2. To initialize the specified element, all the elements will not work before its constructor (:cpp:func:`touch_xxxx_install`) is called so as to save memory, so user has to call the constructor of each used touch element respectively, to set up the specified element.
  180. Touch Element Instance Startup
  181. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  182. 1. To create a new touch element instance by calling :cpp:func:`touch_xxxx_create`, selects channel and passes its `Sensitivity`_ for the new element instance.
  183. 2. To subscribe events by calling :cpp:func:`touch_xxxx_subscribe_event`, there several events in Touch Element library and the event mask is available on :idf_file:`components/touch_element/include/touch_element/touch_element.h`, user could use those events mask to subscribe specified event or combine them to subscribe multiple events.
  184. 3. To configure dispatch method by calling :cpp:func:`touch_xxxx_subscribe_event`, there are two dispatch methods in Touch Element library, one is :cpp:enumerator:`TOUCH_ELEM_DISP_EVENT`, the other one is :cpp:enumerator:`TOUCH_ELEM_DISP_CALLBACK`, it means that user could use two methods to obtain the touch element message and handle it.
  185. Events Processing
  186. ^^^^^^^^^^^^^^^^^
  187. If :cpp:enumerator:`TOUCH_ELEM_DISP_EVENT` dispatch method is configured, user need to startup an event handler task to obtain the touch element message, all the elements raw message could be obtained by calling :cpp:func:`touch_element_message_receive`, then extract the element-class-specific message by calling the corresponding message decoder (:cpp:func:`touch_xxxx_get_message`) to get the touch element's extracted message; If :cpp:enumerator:`TOUCH_ELEM_DISP_CALLBACK` dispatch method is configured, user need to pass an event handler by calling :cpp:func:`touch_xxxx_set_callback` before the touch elem starts working, all the element's extracted message will be passed to the event handler function.
  188. .. warning::
  189. Since the event handler function runs on the library driver core(The context located in esp-timer callback routine), user
  190. should not do something that attempts to block or delay, such as call :cpp:func:`vTaskDelay`.
  191. In code, the events handle procedure may look like as follows:
  192. .. code-block:: c
  193. /* ---------------------------------------------- TOUCH_ELEM_DISP_EVENT ----------------------------------------------- */
  194. void element_handler_task(void *arg)
  195. {
  196. touch_elem_message_t element_message;
  197. while(1) {
  198. if (touch_element_message_receive(&element_message, Timeout) == ESP_OK) {
  199. const touch_xxxx_message_t *extracted_message = touch_xxxx_get_message(&element_message); //Decode message
  200. ... //Event handler logic
  201. }
  202. }
  203. }
  204. void app_main()
  205. {
  206. ...
  207. touch_xxxx_set_dispatch_method(element_handle, TOUCH_ELEM_DISP_EVENT); //Set TOUCH_ELEM_DISP_EVENT as the dispatch method
  208. xTaskCreate(&element_handler_task, "element_handler_task", 2048, NULL, 5, NULL); //Create a handler task
  209. ...
  210. }
  211. /* -------------------------------------------------------------------------------------------------------------- */
  212. ...
  213. /* ---------------------------------------------- TOUCH_ELEM_DISP_CALLBACK ----------------------------------------------- */
  214. void element_handler(touch_xxxx_handle_t out_handle, touch_xxxx_message_t out_message, void *arg)
  215. {
  216. //Event handler logic
  217. }
  218. void app_main()
  219. {
  220. ...
  221. touch_xxxx_set_dispatch_method(element_handle, TOUCH_ELEM_DISP_CALLBACK); //Set TOUCH_ELEM_DISP_CALLBACK as the dispatch method
  222. touch_xxxx_set_callback(element_handle, element_handler); //Register an event handler function
  223. ...
  224. }
  225. /* -------------------------------------------------------------------------------------------------------------- */
  226. Waterproof Usage
  227. ^^^^^^^^^^^^^^^^
  228. 1. To initialize Touch Element waterproof, the waterproof shield sensor is always-on after Touch Element waterproof is initialized, however the waterproof guard sensor is optional, hence if user doesn't need the guard sensor, `TOUCH_WATERPROOF_GUARD_NOUSE` has to be passed to :cpp:func:`touch_element_waterproof_install` by the configuration struct.
  229. 2. To associate the touch element with the guard sensor, pass the touch element's handle to the Touch Element waterproof's masked list by calling :cpp:func:`touch_element_waterproof_add`. By associating a touch element with the Guard sensor, the touch element will be disabled when the guard sensor is triggered by a stream of water so as to protect the touch element.
  230. The Touch Element Waterproof example is available in `peripherals/touch_element/touch_element_waterproof` directory.
  231. In code, the waterproof configuration may look like as follows:
  232. .. code-block:: c
  233. void app_main()
  234. {
  235. ...
  236. touch_xxxx_install(); //Initialize instance (button, slider, etc)
  237. touch_xxxx_create(&element_handle); //Create a new Touch element
  238. ...
  239. touch_element_waterproof_install(); //Initialize Touch Element waterproof
  240. touch_element_waterproof_add(element_handle); //Let a element associates with guard sensor
  241. ...
  242. }
  243. Application Example
  244. -------------------
  245. All the Touch Element library examples could be found in the `peripherals/touch_element` directory of ESP-IDF examples.
  246. API Reference - Touch Element core
  247. ----------------------------------
  248. .. include-build-file:: inc/touch_element.inc
  249. API Reference - Touch Button
  250. ----------------------------------
  251. .. include-build-file:: inc/touch_button.inc
  252. API Reference - Touch Slider
  253. ----------------------------------
  254. .. include-build-file:: inc/touch_slider.inc
  255. API Reference - Touch Slider
  256. ----------------------------------
  257. .. include-build-file:: inc/touch_matrix.inc