Skip to content

Commit 09b15b4

Browse files
committed
refactor(touch): update touch related examples to use new API
1 parent 32d23f9 commit 09b15b4

32 files changed

+411
-846
lines changed

examples/peripherals/.build-test-rules.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,16 +461,9 @@ examples/peripherals/touch_sensor/touch_element:
461461
examples/peripherals/touch_sensor/touch_sens_basic:
462462
disable:
463463
- if: SOC_TOUCH_SENSOR_SUPPORTED != 1
464-
- if: SOC_TOUCH_SENSOR_VERSION == 1
465-
temporary: true
466-
reason: not supported yet
467464
depends_components:
468465
- esp_driver_touch_sens
469466

470-
examples/peripherals/touch_sensor/touch_sensor_v1:
471-
disable:
472-
- if: SOC_TOUCH_SENSOR_VERSION != 1
473-
474467
examples/peripherals/twai/twai_alert_and_recovery:
475468
disable:
476469
- if: SOC_TWAI_SUPPORTED != 1

examples/peripherals/touch_sensor/touch_sens_basic/README.md

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| Supported Targets | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2-
| ----------------- | -------- | -------- | -------- |
1+
| Supported Targets | ESP32 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- |
33

4-
# Capacity Touch Sensor Example (for hardware version 3)
4+
# Capacity Touch Sensor Example
55

66
(See the README.md file in the upper level 'examples' directory for more information about examples.)
77

@@ -34,89 +34,78 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui
3434

3535
## Example Output
3636

37-
You can see the following output in the monitor if the example runs successfully:
37+
You can see the following output in the monitor if the example runs successfully (take ESP32-P4 for example):
3838

3939
```
40-
W (461) touch: [sample_cfg_id 0] clock precision loss, expect 4000000 hz, got 4006725 hz
41-
W (461) touch: [sample_cfg_id 1] clock precision loss, expect 8000000 hz, got 8013450 hz
42-
W (461) touch: [sample_cfg_id 2] clock precision loss, expect 16000000 hz, got 16026900 hz
40+
Touch [CH 0] enabled on GPIO2
41+
Touch [CH 1] enabled on GPIO3
42+
Touch [CH 2] enabled on GPIO4
43+
Touch [CH 3] enabled on GPIO5
44+
=================================
4345
Initial benchmark and new threshold are:
44-
[CH 0] 0: 4114, 411 1: 2057, 205 2: 1028, 102
45-
[CH 1] 0: 4643, 464 1: 2322, 232 2: 1160, 116
46-
[CH 2] 0: 4848, 484 1: 2424, 242 2: 1211, 121
47-
[CH 3] 0: 4340, 434 1: 2170, 217 2: 1085, 108
46+
Touch [CH 0] 0: 5161, 77 1: 5121, 76 2: 2533, 37
47+
Touch [CH 1] 0: 5007, 75 1: 5036, 75 2: 2464, 36
48+
Touch [CH 2] 0: 5086, 76 1: 5056, 75 2: 2487, 37
49+
Touch [CH 3] 0: 4965, 74 1: 4989, 74 2: 2433, 36
4850
=================================
49-
benchmark [CH 0]: 4115 2056 1028
50-
chan_data [CH 0]: 4115 2056 1028
51+
benchmark [CH 0]: 5160 5121 2533
52+
smooth [CH 0]: 5160 5122 2533
5153
52-
benchmark [CH 1]: 4644 2322 1160
53-
chan_data [CH 1]: 4644 2322 1160
54+
benchmark [CH 1]: 5007 5036 2464
55+
smooth [CH 1]: 5007 5036 2464
5456
55-
benchmark [CH 2]: 4848 2423 1211
56-
chan_data [CH 2]: 4848 2423 1211
57+
benchmark [CH 2]: 5086 5056 2487
58+
smooth [CH 2]: 5086 5056 2487
5759
58-
benchmark [CH 3]: 4337 2168 1084
59-
chan_data [CH 3]: 4337 2168 1084
60+
benchmark [CH 3]: 4964 4989 2433
61+
smooth [CH 3]: 4964 4990 2433
6062
6163
=================================
62-
benchmark [CH 0]: 4109 2054 1027
63-
chan_data [CH 0]: 4109 2054 1027
64+
benchmark [CH 0]: 5159 5121 2533
65+
smooth [CH 0]: 5160 5121 2533
6466
65-
benchmark [CH 1]: 4638 2318 1158
66-
chan_data [CH 1]: 4638 2318 1158
67+
benchmark [CH 1]: 5005 5036 2464
68+
smooth [CH 1]: 5006 5035 2464
6769
68-
benchmark [CH 2]: 4843 2421 1210
69-
chan_data [CH 2]: 4845 2421 1210
70+
benchmark [CH 2]: 5085 5056 2487
71+
smooth [CH 2]: 5086 5056 2488
7072
71-
benchmark [CH 3]: 4334 2167 1084
72-
chan_data [CH 3]: 4334 2167 1083
73+
benchmark [CH 3]: 4964 4990 2433
74+
smooth [CH 3]: 4964 4990 2433
7375
...
7476
```
7577

7678
And if you touch and release a button, you will see the following output:
7779

7880
```
7981
...
80-
I (1321) touch_callback: [CH 1] active
81-
=================================
82-
benchmark [CH 0]: 4111 2055 1027
83-
chan_data [CH 0]: 4111 2055 1027
84-
85-
benchmark [CH 1]: 4676 2339 1168
86-
chan_data [CH 1]: 17701 8798 4399
87-
88-
benchmark [CH 2]: 4870 2434 1217
89-
chan_data [CH 2]: 4867 2433 1217
90-
91-
benchmark [CH 3]: 4333 2165 1082
92-
chan_data [CH 3]: 4333 2165 1082
93-
82+
I (2861) touch_callback: [CH 0] active
9483
=================================
95-
benchmark [CH 0]: 4109 2053 1027
96-
chan_data [CH 0]: 4108 2053 1027
84+
benchmark [CH 0]: 5755 5425 2762
85+
smooth [CH 0]: 5997 5666 2841
9786
98-
benchmark [CH 1]: 4676 2339 1168
99-
chan_data [CH 1]: 11256 8817 4363
87+
benchmark [CH 1]: 5025 5049 2473
88+
smooth [CH 1]: 5025 5050 2473
10089
101-
benchmark [CH 2]: 4868 2434 1217
102-
chan_data [CH 2]: 4862 2429 1214
90+
benchmark [CH 2]: 5104 5066 2495
91+
smooth [CH 2]: 5105 5066 2495
10392
104-
benchmark [CH 3]: 4332 2165 1082
105-
chan_data [CH 3]: 4330 2164 1081
93+
benchmark [CH 3]: 4982 5002 2441
94+
smooth [CH 3]: 4982 5001 2441
10695
107-
I (1931) touch_callback: [CH 1] inactive
96+
I (3021) touch_callback: [CH 0] inactive
10897
=================================
109-
benchmark [CH 0]: 4106 2052 1026
110-
chan_data [CH 0]: 4106 2052 1026
98+
benchmark [CH 0]: 5756 5428 2763
99+
smooth [CH 0]: 5756 5428 2764
111100
112-
benchmark [CH 1]: 4649 2323 1161
113-
chan_data [CH 1]: 4650 2323 1161
101+
benchmark [CH 1]: 5025 5048 2473
102+
smooth [CH 1]: 5026 5048 2474
114103
115-
benchmark [CH 2]: 4847 2422 1211
116-
chan_data [CH 2]: 4846 2422 1211
104+
benchmark [CH 2]: 5104 5066 2495
105+
smooth [CH 2]: 5104 5066 2495
117106
118-
benchmark [CH 3]: 4329 2163 1082
119-
chan_data [CH 3]: 4329 2164 1082
107+
benchmark [CH 3]: 4981 5002 2441
108+
smooth [CH 3]: 4981 5002 2441
120109
...
121110
```
122111

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
idf_component_register(SRCS "touch_sens_basic_example_main.c"
2-
REQUIRES esp_driver_touch_sens
3-
INCLUDE_DIRS ".")
2+
REQUIRES esp_driver_touch_sens)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies:
2+
touch_sens_examples_common:
3+
path: ${IDF_PATH}/examples/peripherals/touch_sensor/touch_sens_examples_common

examples/peripherals/touch_sensor/touch_sens_basic/main/touch_sens_basic_example_main.c

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/
@@ -12,13 +12,21 @@
1212
#include "esp_check.h"
1313
#include "touch_sens_example_config.h"
1414

15-
static touch_sensor_handle_t s_sens_handle = NULL;
16-
static touch_channel_handle_t s_chan_handle[EXAMPLE_TOUCH_CHANNEL_NUM] = {};
15+
// Touch version 3 supports multiple sample configurations (i.e. supports frequency hopping),
16+
// others only have one set of sample configurations.
17+
#define EXAMPLE_TOUCH_SAMPLE_CFG_NUM TOUCH_SAMPLE_CFG_NUM // Up to 'TOUCH_SAMPLE_CFG_NUM'
18+
#define EXAMPLE_TOUCH_CHANNEL_NUM 4
19+
#define EXAMPLE_TOUCH_CHAN_INIT_SCAN_TIMES 3
20+
21+
ESP_STATIC_ASSERT(EXAMPLE_TOUCH_SAMPLE_CFG_NUM <= TOUCH_SAMPLE_CFG_NUM, "sample configuration number exceed the supported number");
22+
ESP_STATIC_ASSERT(EXAMPLE_TOUCH_CHANNEL_NUM <= (TOUCH_MAX_CHAN_ID - TOUCH_MIN_CHAN_ID + 1), "touch channel number exceed the max supported number ");
23+
1724
// Active threshold to benchmark ratio. (i.e., touch will be activated when data >= benchmark * (1 + ratio))
1825
static float s_thresh2bm_ratio[EXAMPLE_TOUCH_CHANNEL_NUM] = {
1926
[0 ... EXAMPLE_TOUCH_CHANNEL_NUM - 1] = 0.015f, // 1.5%
2027
};
2128
// The touch channel IDs that used in this example
29+
// For the corresponding GPIOs of these channel, please refer to 'touch_sensor_channel.h'
2230
static int s_channel_id[EXAMPLE_TOUCH_CHANNEL_NUM] = {
2331
TOUCH_MIN_CHAN_ID,
2432
TOUCH_MIN_CHAN_ID + 1,
@@ -38,58 +46,73 @@ bool example_touch_on_inactive_callback(touch_sensor_handle_t sens_handle, const
3846
return false;
3947
}
4048

41-
static void example_touch_do_initial_scanning(void)
49+
static void example_touch_do_initial_scanning(touch_sensor_handle_t sens_handle, touch_channel_handle_t chan_handle[])
4250
{
4351
/* Enable the touch sensor to do the initial scanning, so that to initialize the channel data */
44-
ESP_ERROR_CHECK(touch_sensor_enable(s_sens_handle));
52+
ESP_ERROR_CHECK(touch_sensor_enable(sens_handle));
4553

4654
/* Scan the enabled touch channels for several times, to make sure the initial channel data is stable */
4755
for (int i = 0; i < EXAMPLE_TOUCH_CHAN_INIT_SCAN_TIMES; i++) {
48-
ESP_ERROR_CHECK(touch_sensor_trigger_oneshot_scanning(s_sens_handle, 2000));
56+
ESP_ERROR_CHECK(touch_sensor_trigger_oneshot_scanning(sens_handle, 2000));
4957
}
5058

5159
/* Disable the touch channel to rollback the state */
52-
ESP_ERROR_CHECK(touch_sensor_disable(s_sens_handle));
60+
ESP_ERROR_CHECK(touch_sensor_disable(sens_handle));
5361

5462
/* (Optional) Read the initial channel benchmark and reconfig the channel active threshold accordingly */
5563
printf("Initial benchmark and new threshold are:\n");
5664
for (int i = 0; i < EXAMPLE_TOUCH_CHANNEL_NUM; i++) {
5765
/* Read the initial benchmark of the touch channel */
5866
uint32_t benchmark[EXAMPLE_TOUCH_SAMPLE_CFG_NUM] = {};
59-
ESP_ERROR_CHECK(touch_channel_read_data(s_chan_handle[i], TOUCH_CHAN_DATA_TYPE_BENCHMARK, benchmark));
67+
#if SOC_TOUCH_SUPPORT_BENCHMARK
68+
ESP_ERROR_CHECK(touch_channel_read_data(chan_handle[i], TOUCH_CHAN_DATA_TYPE_BENCHMARK, benchmark));
69+
#else
70+
/* Read smooth data instead if the touch V1 hardware does not support benchmark */
71+
ESP_ERROR_CHECK(touch_channel_read_data(chan_handle[i], TOUCH_CHAN_DATA_TYPE_SMOOTH, benchmark));
72+
#endif // SOC_TOUCH_SUPPORT_BENCHMARK
6073
/* Calculate the proper active thresholds regarding the initial benchmark */
61-
printf("[CH %d]", i);
74+
printf("Touch [CH %d]", s_channel_id[i]);
75+
/* Generate the default channel configuration and then update the active threshold based on the real benchmark */
6276
touch_channel_config_t chan_cfg = EXAMPLE_TOUCH_CHAN_CFG_DEFAULT();
6377
for (int j = 0; j < EXAMPLE_TOUCH_SAMPLE_CFG_NUM; j++) {
78+
#if SOC_TOUCH_SENSOR_VERSION == 1
79+
// Touch V1 (ESP32) uses absolute threshold.
80+
chan_cfg.abs_active_thresh[j] = (uint32_t)(benchmark[j] * (1 - s_thresh2bm_ratio[i]));
81+
printf(" %d: %"PRIu32", %"PRIu32"\t", j, benchmark[j], chan_cfg.abs_active_thresh[j]);
82+
#else
6483
chan_cfg.active_thresh[j] = (uint32_t)(benchmark[j] * s_thresh2bm_ratio[i]);
6584
printf(" %d: %"PRIu32", %"PRIu32"\t", j, benchmark[j], chan_cfg.active_thresh[j]);
85+
#endif // SOC_TOUCH_SENSOR_VERSION == 1
6686
}
6787
printf("\n");
6888
/* Update the channel configuration */
69-
ESP_ERROR_CHECK(touch_sensor_reconfig_channel(s_chan_handle[i], &chan_cfg));
89+
ESP_ERROR_CHECK(touch_sensor_reconfig_channel(chan_handle[i], &chan_cfg));
7090
}
7191
}
7292

7393
void app_main(void)
7494
{
75-
/* Use the default sample configurations */
76-
touch_sensor_sample_config_t sample_cfg[EXAMPLE_TOUCH_SAMPLE_CFG_NUM] = EXAMPLE_TOUCH_SAMPLE_CFG_DEFAULT();
77-
/* Allocate new touch controller handle */
78-
touch_sensor_config_t sens_cfg = TOUCH_SENSOR_DEFAULT_BASIC_CONFIG(EXAMPLE_TOUCH_SAMPLE_CFG_NUM, sample_cfg);
79-
ESP_ERROR_CHECK(touch_sensor_new_controller(&sens_cfg, &s_sens_handle));
95+
/* Handles of touch sensor */
96+
touch_sensor_handle_t sens_handle = NULL;
97+
touch_channel_handle_t chan_handle[EXAMPLE_TOUCH_CHANNEL_NUM];
8098

81-
/* Configure the touch sensor filter */
82-
touch_sensor_filter_config_t filter_cfg = TOUCH_SENSOR_DEFAULT_FILTER_CONFIG();
83-
ESP_ERROR_CHECK(touch_sensor_config_filter(s_sens_handle, &filter_cfg));
99+
/* Step 1: Create a new touch sensor controller handle with default sample configuration */
100+
touch_sensor_sample_config_t sample_cfg[TOUCH_SAMPLE_CFG_NUM] = EXAMPLE_TOUCH_SAMPLE_CFG_DEFAULT();
101+
touch_sensor_config_t sens_cfg = TOUCH_SENSOR_DEFAULT_BASIC_CONFIG(EXAMPLE_TOUCH_SAMPLE_CFG_NUM, sample_cfg);
102+
ESP_ERROR_CHECK(touch_sensor_new_controller(&sens_cfg, &sens_handle));
84103

104+
/* Step 2: Create and enable the new touch channel handles with default configurations */
85105
/** Following is about setting the touch channel active threshold of each sample configuration.
86106
*
87107
* @How to Determine:
88108
* As the actual threshold is affected by various factors in real application,
89109
* we need to run the touch app first to get the `benchmark` and the `smooth_data` that being touched.
90110
*
91111
* @Formula:
92-
* threshold = benchmark * coeff, (coeff for example, 0.1%~20%)
112+
* Touch V1 uses absolute threshold, and it has no benchmark, so you can use untouched smooth data instead:
113+
* abs_active_thresh = benchmark * (1 - coeff), (coeff for example, 0.1%~20%)
114+
* Touch V2/V3 uses relative threshold:
115+
* active_thresh = benchmark * coeff, (coeff for example, 0.1%~20%)
93116
* Please adjust the coeff to guarantee the threshold < smooth_data - benchmark
94117
*
95118
* @Typical Practice:
@@ -102,48 +125,55 @@ void app_main(void)
102125
touch_channel_config_t chan_cfg = EXAMPLE_TOUCH_CHAN_CFG_DEFAULT();
103126
/* Allocate new touch channel on the touch controller */
104127
for (int i = 0; i < EXAMPLE_TOUCH_CHANNEL_NUM; i++) {
105-
ESP_ERROR_CHECK(touch_sensor_new_channel(s_sens_handle, s_channel_id[i], &chan_cfg, &s_chan_handle[i]));
128+
ESP_ERROR_CHECK(touch_sensor_new_channel(sens_handle, s_channel_id[i], &chan_cfg, &chan_handle[i]));
129+
/* Display the touch channel corresponding GPIO number, you can also know from `touch_sensor_channel.h` */
130+
touch_chan_info_t chan_info = {};
131+
ESP_ERROR_CHECK(touch_sensor_get_channel_info(chan_handle[i], &chan_info));
132+
printf("Touch [CH %d] enabled on GPIO%d\n", s_channel_id[i], chan_info.chan_gpio);
106133
}
134+
printf("=================================\n");
135+
136+
/* Step 3: Confiture the default filter for the touch sensor (Note: Touch V1 uses software filter) */
137+
touch_sensor_filter_config_t filter_cfg = TOUCH_SENSOR_DEFAULT_FILTER_CONFIG();
138+
ESP_ERROR_CHECK(touch_sensor_config_filter(sens_handle, &filter_cfg));
107139

108-
/* Do the initial scanning to initialize the touch channel data
140+
/* Step 4: Do the initial scanning to initialize the touch channel data
109141
* Without this step, the channel data in the first read will be invalid
110142
*/
111-
example_touch_do_initial_scanning();
143+
example_touch_do_initial_scanning(sens_handle, chan_handle);
112144

113-
/* Register the touch sensor callbacks, here only take `active` and `deactivate` event for example */
145+
/* Step 5: Register the touch sensor callbacks, here only take `active` and `inactive` event for example */
114146
touch_event_callbacks_t callbacks = {
115147
.on_active = example_touch_on_active_callback,
116148
.on_inactive = example_touch_on_inactive_callback,
117-
.on_measure_done = NULL,
118-
.on_scan_done = NULL,
119-
.on_timeout = NULL,
120-
.on_proximity_meas_done = NULL,
121149
};
122-
ESP_ERROR_CHECK(touch_sensor_register_callbacks(s_sens_handle, &callbacks, NULL));
150+
ESP_ERROR_CHECK(touch_sensor_register_callbacks(sens_handle, &callbacks, NULL));
123151

124-
/* Enable the touch sensor */
125-
ESP_ERROR_CHECK(touch_sensor_enable(s_sens_handle));
152+
/* Step 6: Enable the touch sensor */
153+
ESP_ERROR_CHECK(touch_sensor_enable(sens_handle));
126154

127-
/* Start continuous scanning, you can also trigger oneshot scanning manually */
128-
ESP_ERROR_CHECK(touch_sensor_start_continuous_scanning(s_sens_handle));
155+
/* Step 7: Start continuous scanning, you can also trigger oneshot scanning manually */
156+
ESP_ERROR_CHECK(touch_sensor_start_continuous_scanning(sens_handle));
129157

130-
uint32_t benchmark[EXAMPLE_TOUCH_SAMPLE_CFG_NUM] = {};
131-
uint32_t chan_data[EXAMPLE_TOUCH_SAMPLE_CFG_NUM] = {};
158+
/* Step8: Print the sampled data of each enabled touch channel */
159+
uint32_t data[EXAMPLE_TOUCH_SAMPLE_CFG_NUM] = {};
132160
while (1) {
133161
printf("=================================\n");
134162
for (int i = 0; i < EXAMPLE_TOUCH_CHANNEL_NUM; i++) {
163+
#if SOC_TOUCH_SUPPORT_BENCHMARK
135164
/* Read and print the benchmark of each sample configuration */
136-
ESP_ERROR_CHECK(touch_channel_read_data(s_chan_handle[i], TOUCH_CHAN_DATA_TYPE_BENCHMARK, benchmark));
165+
ESP_ERROR_CHECK(touch_channel_read_data(chan_handle[i], TOUCH_CHAN_DATA_TYPE_BENCHMARK, data));
137166
printf("benchmark [CH %d]:", s_channel_id[i]);
138167
for (int j = 0; j < EXAMPLE_TOUCH_SAMPLE_CFG_NUM; j++) {
139-
printf(" %"PRIu32, benchmark[j]);
168+
printf(" %"PRIu32, data[j]);
140169
}
141170
printf("\n");
171+
#endif
142172
/* Read and print the channel data of each sample configuration */
143-
ESP_ERROR_CHECK(touch_channel_read_data(s_chan_handle[i], TOUCH_CHAN_DATA_TYPE_SMOOTH, chan_data));
144-
printf("chan_data [CH %d]:", s_channel_id[i]);
173+
ESP_ERROR_CHECK(touch_channel_read_data(chan_handle[i], TOUCH_CHAN_DATA_TYPE_SMOOTH, data));
174+
printf("smooth [CH %d]:", s_channel_id[i]);
145175
for (int j = 0; j < EXAMPLE_TOUCH_SAMPLE_CFG_NUM; j++) {
146-
printf(" %"PRIu32, chan_data[j]);
176+
printf(" %"PRIu32, data[j]);
147177
}
148178
printf("\n\n");
149179
}

0 commit comments

Comments
 (0)