Skip to content

Commit 48cda62

Browse files
committed
Merge branch 'feature/touch_sense_example' into 'master'
touch_element: add touch element lib examples Closes IDF-2665 See merge request espressif/esp-idf!11826
2 parents e3045d9 + cd8e874 commit 48cda62

24 files changed

+1147
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(touch_button)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
| Supported Targets | ESP32-S2 |
2+
| ----------------- | -------- |
3+
4+
# Touch button example
5+
6+
This example demonstrates how to use the Touch Element library of capacitive touch sensor and set up touch button.
7+
8+
## How to use example
9+
10+
### Configure the project
11+
12+
* Set the target of the build (where `{IDF_TARGET}` stands for the target chip such as `esp32s2`).
13+
* Run `menuconfig` to select a dispatch method for the example.
14+
15+
### Build and Flash
16+
17+
Build the project and flash it to the target board, then run monitor tool to view serial output:
18+
19+
```
20+
idf.py -p PORT flash monitor
21+
```
22+
23+
(Replace PORT with the name of the serial port to use.)
24+
25+
(To exit the serial monitor, type ``Ctrl-]``.)
26+
27+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
28+
29+
## Example Output
30+
31+
```
32+
I (331) Touch Button Example: Touch element library installed
33+
I (331) Touch Button Example: Touch button installed
34+
I (341) Touch Button Example: Touch buttons created
35+
I (341) Touch Button Example: Touch element library start
36+
I (1481) Touch Button Example: Button[1] Press
37+
I (1701) Touch Button Example: Button[1] Release
38+
I (2731) Touch Button Example: Button[2] Press
39+
I (2921) Touch Button Example: Button[2] Release
40+
I (3581) Touch Button Example: Button[5] Press
41+
I (3781) Touch Button Example: Button[5] Release
42+
I (3931) Touch Button Example: Button[4] Press
43+
I (4121) Touch Button Example: Button[4] Release
44+
I (4271) Touch Button Example: Button[3] Press
45+
I (4491) Touch Button Example: Button[3] Release
46+
I (4671) Touch Button Example: Button[6] Press
47+
I (4891) Touch Button Example: Button[6] Release
48+
I (5091) Touch Button Example: Button[7] Press
49+
I (5311) Touch Button Example: Button[7] Release
50+
I (5491) Touch Button Example: Button[8] Press
51+
I (5741) Touch Button Example: Button[8] Release
52+
I (5991) Touch Button Example: Button[9] Press
53+
I (7991) Touch Button Example: Button[9] LongPress
54+
I (9991) Touch Button Example: Button[9] LongPress
55+
I (11991) Touch Button Example: Button[9] LongPress
56+
I (12881) Touch Button Example: Button[9] Release
57+
```
58+
59+
See the README.md file in the upper level 'examples' directory for more information about examples.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if(IDF_TARGET STREQUAL "esp32s2")
2+
idf_component_register(SRCS "touch_button_example_main.c"
3+
INCLUDE_DIRS ".")
4+
else()
5+
message(FATAL_ERROR "Touch button example only available on esp32s2 now")
6+
endif()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
menu "Example Configuration"
2+
3+
choice TOUCH_SENSOR_EXAMPLE_TYPE
4+
bool "Select touch element dispatch method"
5+
default TOUCH_ELEM_EVENT
6+
help
7+
Select touch element dispatch method (event task or callback) for this example.
8+
9+
config TOUCH_ELEM_EVENT
10+
bool "Dispatch by event task"
11+
config TOUCH_ELEM_CALLBACK
12+
bool "Dispatch by callback"
13+
endchoice
14+
15+
endmenu
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Touch Sensor - Example
2+
3+
For other examples please check:
4+
https://github.com/espressif/esp-idf/tree/master/examples
5+
6+
See README.md file to get detailed usage of this example.
7+
8+
This example code is in the Public Domain (or CC0 licensed, at your option.)
9+
10+
Unless required by applicable law or agreed to in writing, this
11+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
CONDITIONS OF ANY KIND, either express or implied.
13+
*/
14+
15+
#include "freertos/FreeRTOS.h"
16+
#include "freertos/task.h"
17+
#include "touch_element/touch_button.h"
18+
#include "esp_log.h"
19+
20+
static const char *TAG = "Touch Button Example";
21+
#define TOUCH_BUTTON_NUM 14
22+
23+
/* Touch buttons handle */
24+
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM];
25+
26+
/* Touch buttons channel array */
27+
static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = {
28+
TOUCH_PAD_NUM1,
29+
TOUCH_PAD_NUM2,
30+
TOUCH_PAD_NUM3,
31+
TOUCH_PAD_NUM4,
32+
TOUCH_PAD_NUM5,
33+
TOUCH_PAD_NUM6,
34+
TOUCH_PAD_NUM7,
35+
TOUCH_PAD_NUM8,
36+
TOUCH_PAD_NUM9,
37+
TOUCH_PAD_NUM10,
38+
TOUCH_PAD_NUM11,
39+
TOUCH_PAD_NUM12,
40+
TOUCH_PAD_NUM13,
41+
TOUCH_PAD_NUM14,
42+
};
43+
44+
/* Touch buttons channel sensitivity array */
45+
static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
46+
0.1F,
47+
0.1F,
48+
0.1F,
49+
0.1F,
50+
0.1F,
51+
0.1F,
52+
0.1F,
53+
0.1F,
54+
0.1F,
55+
0.1F,
56+
0.1F,
57+
0.1F,
58+
0.1F,
59+
0.1F,
60+
};
61+
62+
#ifdef CONFIG_TOUCH_ELEM_EVENT
63+
/* Button event handler task */
64+
static void button_handler_task(void *arg)
65+
{
66+
(void) arg; //Unused
67+
touch_elem_message_t element_message;
68+
while (1) {
69+
/* Waiting for touch element messages */
70+
touch_element_message_receive(&element_message, portMAX_DELAY);
71+
if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) {
72+
continue;
73+
}
74+
/* Decode message */
75+
const touch_button_message_t *button_message = touch_button_get_message(&element_message);
76+
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
77+
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg);
78+
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
79+
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg);
80+
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
81+
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg);
82+
}
83+
}
84+
}
85+
#elif CONFIG_TOUCH_ELEM_CALLBACK
86+
/* Button callback routine */
87+
static void button_handler(touch_button_handle_t out_handle, touch_button_message_t out_message, void *arg)
88+
{
89+
(void) out_handle; //Unused
90+
if (out_message.event == TOUCH_BUTTON_EVT_ON_PRESS) {
91+
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)arg);
92+
} else if (out_message.event == TOUCH_BUTTON_EVT_ON_RELEASE) {
93+
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)arg);
94+
} else if (out_message.event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
95+
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)arg);
96+
}
97+
}
98+
#endif
99+
100+
void app_main(void)
101+
{
102+
/* Initialize Touch Element library */
103+
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
104+
ESP_ERROR_CHECK(touch_element_install(&global_config));
105+
ESP_LOGI(TAG, "Touch element library installed");
106+
107+
touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
108+
ESP_ERROR_CHECK(touch_button_install(&button_global_config));
109+
ESP_LOGI(TAG, "Touch button installed");
110+
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
111+
touch_button_config_t button_config = {
112+
.channel_num = channel_array[i],
113+
.channel_sens = channel_sens_array[i]
114+
};
115+
/* Create Touch buttons */
116+
ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
117+
/* Subscribe touch button events (On Press, On Release, On LongPress) */
118+
ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS,
119+
(void *)channel_array[i]));
120+
#ifdef CONFIG_TOUCH_ELEM_EVENT
121+
/* Set EVENT as the dispatch method */
122+
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
123+
#elif CONFIG_TOUCH_ELEM_CALLBACK
124+
/* Set EVENT as the dispatch method */
125+
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_CALLBACK));
126+
/* Register a handler function to handle event messages */
127+
ESP_ERROR_CHECK(touch_button_set_callback(button_handle[i], button_handler));
128+
#endif
129+
/* Set LongPress event trigger threshold time */
130+
ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 2000));
131+
}
132+
ESP_LOGI(TAG, "Touch buttons created");
133+
134+
#ifdef CONFIG_TOUCH_ELEM_EVENT
135+
/* Create a handler task to handle event messages */
136+
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
137+
#endif
138+
139+
touch_element_start();
140+
ESP_LOGI(TAG, "Touch element library start");
141+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(touch_element_waterproof)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
| Supported Targets | ESP32-S2 |
2+
| ----------------- | -------- |
3+
4+
# Touch Element waterproof Example
5+
6+
This example demonstrates how to use the Touch Element library of capacitive Touch Sensor and setup the touch elements with touch element waterproof protection.
7+
8+
## How to use example
9+
10+
### Build and Flash
11+
12+
Build the project and flash it to the board, then run monitor tool to view serial output:
13+
14+
```
15+
idf.py -p PORT flash monitor
16+
```
17+
18+
(Replace PORT with the name of the serial port to use.)
19+
20+
(To exit the serial monitor, type ``Ctrl-]``.)
21+
22+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
23+
24+
## Example Output
25+
26+
This example's output maybe could not give a strong feeling to user since the waterproof function works
27+
automatically and silently inside the Touch Element library
28+
29+
```
30+
I (331) Touch Element Waterproof Example: Touch Element library install
31+
I (331) Touch Element Waterproof Example: Touch Element waterproof install
32+
I (341) Touch Element Waterproof Example: Touch button install
33+
I (351) Touch Element Waterproof Example: Touch buttons create
34+
I (3191) Touch Element Waterproof Example: Button[7] Press
35+
I (4191) Touch Element Waterproof Example: Button[7] LongPress
36+
I (5191) Touch Element Waterproof Example: Button[7] LongPress
37+
I (5671) Touch Element Waterproof Example: Button[7] Release
38+
I (12561) Touch Element Waterproof Example: Button[9] Press
39+
I (12811) Touch Element Waterproof Example: Button[9] Release
40+
```
41+
42+
See the README.md file in the upper level 'examples' directory for more information about examples.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if(IDF_TARGET STREQUAL "esp32s2")
2+
idf_component_register(SRCS "waterproof_example_main.c"
3+
INCLUDE_DIRS ".")
4+
else()
5+
message(FATAL_ERROR "Touch element waterproof example only available on esp32s2 now")
6+
endif()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
menu "Example Configuration"
2+
3+
config TOUCH_WATERPROOF_GUARD_ENABLE
4+
bool "Enable touch sense waterproof guard sensor"
5+
default y
6+
help
7+
This option enables touch sense waterproof guard sensor,
8+
while the shield sensor is not optional.
9+
10+
endmenu
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Touch Sensor waterproof - Example
2+
3+
For other examples please check:
4+
https://github.com/espressif/esp-idf/tree/master/examples
5+
6+
See README.md file to get detailed usage of this example.
7+
8+
This example code is in the Public Domain (or CC0 licensed, at your option.)
9+
10+
Unless required by applicable law or agreed to in writing, this
11+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
CONDITIONS OF ANY KIND, either express or implied.
13+
*/
14+
15+
#include "freertos/FreeRTOS.h"
16+
#include "freertos/task.h"
17+
#include "esp_log.h"
18+
#include "touch_element/touch_button.h"
19+
20+
static const char *TAG = "Touch Element Waterproof Example";
21+
#define TOUCH_BUTTON_NUM 3
22+
23+
/*< Touch buttons handle */
24+
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; //Button handler
25+
26+
/* Touch buttons channel array */
27+
static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = {
28+
TOUCH_PAD_NUM7,
29+
TOUCH_PAD_NUM9,
30+
TOUCH_PAD_NUM11,
31+
};
32+
33+
/* Touch buttons channel sensitivity array */
34+
static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
35+
0.15F,
36+
0.15F,
37+
0.15F,
38+
};
39+
40+
static void button_handler_task(void *arg)
41+
{
42+
touch_elem_message_t element_message;
43+
while (1) {
44+
touch_element_message_receive(&element_message, portMAX_DELAY); //Block take
45+
const touch_button_message_t *button_message = touch_button_get_message(&element_message);
46+
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
47+
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg);
48+
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
49+
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg);
50+
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
51+
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg);
52+
}
53+
}
54+
}
55+
56+
void app_main(void)
57+
{
58+
/*< Initialize Touch Element library */
59+
touch_elem_global_config_t element_global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
60+
ESP_ERROR_CHECK(touch_element_install(&element_global_config));
61+
ESP_LOGI(TAG, "Touch Element library install");
62+
/*< Create and configure touch element waterproof */
63+
touch_elem_waterproof_config_t waterproof_config = {
64+
#ifdef CONFIG_TOUCH_WATERPROOF_GUARD_ENABLE
65+
.guard_channel = TOUCH_PAD_NUM13,
66+
#else
67+
.guard_channel = TOUCH_WATERPROOF_GUARD_NOUSE,
68+
#endif
69+
.guard_sensitivity = 0.05F //The guard sensor sensitivity has to be explored in experiments
70+
};
71+
ESP_ERROR_CHECK(touch_element_waterproof_install(&waterproof_config));
72+
ESP_LOGI(TAG, "Touch Element waterproof install");
73+
74+
touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
75+
ESP_ERROR_CHECK(touch_button_install(&button_global_config));
76+
ESP_LOGI(TAG, "Touch button install");
77+
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
78+
touch_button_config_t button_config = {
79+
.channel_num = channel_array[i],
80+
.channel_sens = channel_sens_array[i]
81+
};
82+
/* Create touch button */
83+
ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
84+
/* Subscribe touch button event(Press, Release, LongPress) */
85+
ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS,
86+
(void *)channel_array[i]));
87+
/* Button set dispatch method */
88+
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
89+
#ifdef CONFIG_TOUCH_WATERPROOF_GUARD_ENABLE
90+
/* Add button element into waterproof guard sensor's protection */
91+
ESP_ERROR_CHECK(touch_element_waterproof_add(button_handle[i]));
92+
#endif
93+
}
94+
ESP_LOGI(TAG, "Touch buttons create");
95+
/*< Create a monitor task to take Touch Button event */
96+
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
97+
touch_element_start();
98+
}

0 commit comments

Comments
 (0)