Merge branch 'feature/touch_element_example_v4.3' into 'release/v4.3'

touch_element: add touch element lib examples (backport v4.3)

See merge request espressif/esp-idf!12571
This commit is contained in:
Michael (XIAO Xufeng) 2021-03-05 03:29:43 +00:00
commit d508182429
24 changed files with 1147 additions and 0 deletions

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_button)

View File

@ -0,0 +1,59 @@
| Supported Targets | ESP32-S2 |
| ----------------- | -------- |
# Touch button example
This example demonstrates how to use the Touch Element library of capacitive touch sensor and set up touch button.
## How to use example
### Configure the project
* Set the target of the build (where `{IDF_TARGET}` stands for the target chip such as `esp32s2`).
* Run `menuconfig` to select a dispatch method for the example.
### Build and Flash
Build the project and flash it to the target board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
```
I (331) Touch Button Example: Touch element library installed
I (331) Touch Button Example: Touch button installed
I (341) Touch Button Example: Touch buttons created
I (341) Touch Button Example: Touch element library start
I (1481) Touch Button Example: Button[1] Press
I (1701) Touch Button Example: Button[1] Release
I (2731) Touch Button Example: Button[2] Press
I (2921) Touch Button Example: Button[2] Release
I (3581) Touch Button Example: Button[5] Press
I (3781) Touch Button Example: Button[5] Release
I (3931) Touch Button Example: Button[4] Press
I (4121) Touch Button Example: Button[4] Release
I (4271) Touch Button Example: Button[3] Press
I (4491) Touch Button Example: Button[3] Release
I (4671) Touch Button Example: Button[6] Press
I (4891) Touch Button Example: Button[6] Release
I (5091) Touch Button Example: Button[7] Press
I (5311) Touch Button Example: Button[7] Release
I (5491) Touch Button Example: Button[8] Press
I (5741) Touch Button Example: Button[8] Release
I (5991) Touch Button Example: Button[9] Press
I (7991) Touch Button Example: Button[9] LongPress
I (9991) Touch Button Example: Button[9] LongPress
I (11991) Touch Button Example: Button[9] LongPress
I (12881) Touch Button Example: Button[9] Release
```
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@ -0,0 +1,6 @@
if(IDF_TARGET STREQUAL "esp32s2")
idf_component_register(SRCS "touch_button_example_main.c"
INCLUDE_DIRS ".")
else()
message(FATAL_ERROR "Touch button example only available on esp32s2 now")
endif()

View File

@ -0,0 +1,15 @@
menu "Example Configuration"
choice TOUCH_SENSOR_EXAMPLE_TYPE
bool "Select touch element dispatch method"
default TOUCH_ELEM_EVENT
help
Select touch element dispatch method (event task or callback) for this example.
config TOUCH_ELEM_EVENT
bool "Dispatch by event task"
config TOUCH_ELEM_CALLBACK
bool "Dispatch by callback"
endchoice
endmenu

View File

@ -0,0 +1,141 @@
/* Touch Sensor - Example
For other examples please check:
https://github.com/espressif/esp-idf/tree/master/examples
See README.md file to get detailed usage of this example.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "touch_element/touch_button.h"
#include "esp_log.h"
static const char *TAG = "Touch Button Example";
#define TOUCH_BUTTON_NUM 14
/* Touch buttons handle */
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM];
/* Touch buttons channel array */
static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = {
TOUCH_PAD_NUM1,
TOUCH_PAD_NUM2,
TOUCH_PAD_NUM3,
TOUCH_PAD_NUM4,
TOUCH_PAD_NUM5,
TOUCH_PAD_NUM6,
TOUCH_PAD_NUM7,
TOUCH_PAD_NUM8,
TOUCH_PAD_NUM9,
TOUCH_PAD_NUM10,
TOUCH_PAD_NUM11,
TOUCH_PAD_NUM12,
TOUCH_PAD_NUM13,
TOUCH_PAD_NUM14,
};
/* Touch buttons channel sensitivity array */
static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
0.1F,
};
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Button event handler task */
static void button_handler_task(void *arg)
{
(void) arg; //Unused
touch_elem_message_t element_message;
while (1) {
/* Waiting for touch element messages */
touch_element_message_receive(&element_message, portMAX_DELAY);
if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) {
continue;
}
/* Decode message */
const touch_button_message_t *button_message = touch_button_get_message(&element_message);
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg);
}
}
}
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Button callback routine */
static void button_handler(touch_button_handle_t out_handle, touch_button_message_t out_message, void *arg)
{
(void) out_handle; //Unused
if (out_message.event == TOUCH_BUTTON_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)arg);
} else if (out_message.event == TOUCH_BUTTON_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)arg);
} else if (out_message.event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)arg);
}
}
#endif
void app_main(void)
{
/* Initialize Touch Element library */
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&global_config));
ESP_LOGI(TAG, "Touch element library installed");
touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_button_install(&button_global_config));
ESP_LOGI(TAG, "Touch button installed");
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
touch_button_config_t button_config = {
.channel_num = channel_array[i],
.channel_sens = channel_sens_array[i]
};
/* Create Touch buttons */
ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
/* Subscribe touch button events (On Press, On Release, On LongPress) */
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,
(void *)channel_array[i]));
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_CALLBACK));
/* Register a handler function to handle event messages */
ESP_ERROR_CHECK(touch_button_set_callback(button_handle[i], button_handler));
#endif
/* Set LongPress event trigger threshold time */
ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 2000));
}
ESP_LOGI(TAG, "Touch buttons created");
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Create a handler task to handle event messages */
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
#endif
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
}

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_element_waterproof)

View File

@ -0,0 +1,42 @@
| Supported Targets | ESP32-S2 |
| ----------------- | -------- |
# Touch Element waterproof Example
This example demonstrates how to use the Touch Element library of capacitive Touch Sensor and setup the touch elements with touch element waterproof protection.
## How to use example
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
This example's output maybe could not give a strong feeling to user since the waterproof function works
automatically and silently inside the Touch Element library
```
I (331) Touch Element Waterproof Example: Touch Element library install
I (331) Touch Element Waterproof Example: Touch Element waterproof install
I (341) Touch Element Waterproof Example: Touch button install
I (351) Touch Element Waterproof Example: Touch buttons create
I (3191) Touch Element Waterproof Example: Button[7] Press
I (4191) Touch Element Waterproof Example: Button[7] LongPress
I (5191) Touch Element Waterproof Example: Button[7] LongPress
I (5671) Touch Element Waterproof Example: Button[7] Release
I (12561) Touch Element Waterproof Example: Button[9] Press
I (12811) Touch Element Waterproof Example: Button[9] Release
```
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@ -0,0 +1,6 @@
if(IDF_TARGET STREQUAL "esp32s2")
idf_component_register(SRCS "waterproof_example_main.c"
INCLUDE_DIRS ".")
else()
message(FATAL_ERROR "Touch element waterproof example only available on esp32s2 now")
endif()

View File

@ -0,0 +1,10 @@
menu "Example Configuration"
config TOUCH_WATERPROOF_GUARD_ENABLE
bool "Enable touch sense waterproof guard sensor"
default y
help
This option enables touch sense waterproof guard sensor,
while the shield sensor is not optional.
endmenu

View File

@ -0,0 +1,98 @@
/* Touch Sensor waterproof - Example
For other examples please check:
https://github.com/espressif/esp-idf/tree/master/examples
See README.md file to get detailed usage of this example.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "touch_element/touch_button.h"
static const char *TAG = "Touch Element Waterproof Example";
#define TOUCH_BUTTON_NUM 3
/*< Touch buttons handle */
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; //Button handler
/* Touch buttons channel array */
static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = {
TOUCH_PAD_NUM7,
TOUCH_PAD_NUM9,
TOUCH_PAD_NUM11,
};
/* Touch buttons channel sensitivity array */
static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
0.15F,
0.15F,
0.15F,
};
static void button_handler_task(void *arg)
{
touch_elem_message_t element_message;
while (1) {
touch_element_message_receive(&element_message, portMAX_DELAY); //Block take
const touch_button_message_t *button_message = touch_button_get_message(&element_message);
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg);
}
}
}
void app_main(void)
{
/*< Initialize Touch Element library */
touch_elem_global_config_t element_global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&element_global_config));
ESP_LOGI(TAG, "Touch Element library install");
/*< Create and configure touch element waterproof */
touch_elem_waterproof_config_t waterproof_config = {
#ifdef CONFIG_TOUCH_WATERPROOF_GUARD_ENABLE
.guard_channel = TOUCH_PAD_NUM13,
#else
.guard_channel = TOUCH_WATERPROOF_GUARD_NOUSE,
#endif
.guard_sensitivity = 0.05F //The guard sensor sensitivity has to be explored in experiments
};
ESP_ERROR_CHECK(touch_element_waterproof_install(&waterproof_config));
ESP_LOGI(TAG, "Touch Element waterproof install");
touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_button_install(&button_global_config));
ESP_LOGI(TAG, "Touch button install");
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
touch_button_config_t button_config = {
.channel_num = channel_array[i],
.channel_sens = channel_sens_array[i]
};
/* Create touch button */
ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
/* Subscribe touch button event(Press, Release, LongPress) */
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,
(void *)channel_array[i]));
/* Button set dispatch method */
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
#ifdef CONFIG_TOUCH_WATERPROOF_GUARD_ENABLE
/* Add button element into waterproof guard sensor's protection */
ESP_ERROR_CHECK(touch_element_waterproof_add(button_handle[i]));
#endif
}
ESP_LOGI(TAG, "Touch buttons create");
/*< Create a monitor task to take Touch Button event */
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
touch_element_start();
}

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_elements_combination)

View File

@ -0,0 +1,138 @@
| Supported Targets | ESP32-S2 |
| ----------------- | -------- |
# Touch button example
This example demonstrates how to use the Touch Element library of capacitive touch sensor and set up more than one type of touch elements and handle all the event messages in one task.
## How to use example
### Configure the project
* Set the target of the build (where `{IDF_TARGET}` stands for the target chip such as `esp32s2`).
### Build and Flash
Build the project and flash it to the target board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
```
I (331) Touch Elements Combination Example: Touch element library installed
I (331) Touch Elements Combination Example: Touch button installed
I (341) Touch Elements Combination Example: Touch buttons created
I (351) Touch Elements Combination Example: Touch slider installed
I (351) Touch Elements Combination Example: Touch slider created
I (361) Touch Elements Combination Example: Touch element library start
I (1841) Touch Elements Combination Example: Button[6] Press
I (1971) Touch Elements Combination Example: Button[6] Release
I (2201) Touch Elements Combination Example: Button[8] Press
I (2351) Touch Elements Combination Example: Button[8] Release
I (2561) Touch Elements Combination Example: Button[10] Press
I (2721) Touch Elements Combination Example: Button[10] Release
I (3431) Touch Elements Combination Example: Slider Press, position: 0
I (3441) Touch Elements Combination Example: Slider Calculate, position: 0
I (3451) Touch Elements Combination Example: Slider Calculate, position: 0
I (3461) Touch Elements Combination Example: Slider Calculate, position: 0
I (3471) Touch Elements Combination Example: Slider Calculate, position: 0
I (3481) Touch Elements Combination Example: Slider Calculate, position: 0
I (3491) Touch Elements Combination Example: Slider Calculate, position: 0
I (3501) Touch Elements Combination Example: Slider Calculate, position: 1
I (3511) Touch Elements Combination Example: Slider Calculate, position: 1
I (3521) Touch Elements Combination Example: Slider Calculate, position: 2
I (3531) Touch Elements Combination Example: Slider Calculate, position: 2
I (3541) Touch Elements Combination Example: Slider Calculate, position: 3
I (3551) Touch Elements Combination Example: Slider Calculate, position: 4
I (3561) Touch Elements Combination Example: Slider Calculate, position: 5
I (3571) Touch Elements Combination Example: Slider Calculate, position: 6
I (3581) Touch Elements Combination Example: Slider Calculate, position: 7
I (3591) Touch Elements Combination Example: Slider Calculate, position: 8
I (3601) Touch Elements Combination Example: Slider Calculate, position: 10
I (3611) Touch Elements Combination Example: Slider Calculate, position: 11
I (3621) Touch Elements Combination Example: Slider Calculate, position: 12
I (3631) Touch Elements Combination Example: Slider Calculate, position: 13
I (3641) Touch Elements Combination Example: Slider Calculate, position: 15
I (3651) Touch Elements Combination Example: Slider Calculate, position: 16
I (3661) Touch Elements Combination Example: Slider Calculate, position: 17
I (3671) Touch Elements Combination Example: Slider Calculate, position: 19
I (3681) Touch Elements Combination Example: Slider Calculate, position: 20
I (3691) Touch Elements Combination Example: Slider Calculate, position: 21
I (3701) Touch Elements Combination Example: Slider Calculate, position: 23
I (3711) Touch Elements Combination Example: Slider Calculate, position: 24
I (3721) Touch Elements Combination Example: Slider Calculate, position: 26
I (3731) Touch Elements Combination Example: Slider Calculate, position: 27
I (3741) Touch Elements Combination Example: Slider Calculate, position: 28
I (3751) Touch Elements Combination Example: Slider Calculate, position: 29
I (3761) Touch Elements Combination Example: Slider Calculate, position: 31
I (3771) Touch Elements Combination Example: Slider Calculate, position: 32
I (3781) Touch Elements Combination Example: Slider Calculate, position: 33
I (3791) Touch Elements Combination Example: Slider Calculate, position: 34
I (3801) Touch Elements Combination Example: Slider Calculate, position: 36
I (3811) Touch Elements Combination Example: Slider Calculate, position: 37
I (3821) Touch Elements Combination Example: Slider Calculate, position: 38
I (3831) Touch Elements Combination Example: Slider Calculate, position: 39
I (3841) Touch Elements Combination Example: Slider Calculate, position: 41
I (3851) Touch Elements Combination Example: Slider Calculate, position: 42
I (3861) Touch Elements Combination Example: Slider Calculate, position: 43
I (3871) Touch Elements Combination Example: Slider Calculate, position: 45
I (3881) Touch Elements Combination Example: Slider Calculate, position: 47
I (3891) Touch Elements Combination Example: Slider Calculate, position: 48
I (3901) Touch Elements Combination Example: Slider Calculate, position: 50
I (3911) Touch Elements Combination Example: Slider Calculate, position: 52
I (3921) Touch Elements Combination Example: Slider Calculate, position: 53
I (3931) Touch Elements Combination Example: Slider Calculate, position: 55
I (3941) Touch Elements Combination Example: Slider Calculate, position: 57
I (3951) Touch Elements Combination Example: Slider Calculate, position: 58
I (3961) Touch Elements Combination Example: Slider Calculate, position: 60
I (3971) Touch Elements Combination Example: Slider Calculate, position: 61
I (3981) Touch Elements Combination Example: Slider Calculate, position: 62
I (3991) Touch Elements Combination Example: Slider Calculate, position: 64
I (4001) Touch Elements Combination Example: Slider Calculate, position: 65
I (4011) Touch Elements Combination Example: Slider Calculate, position: 66
I (4021) Touch Elements Combination Example: Slider Calculate, position: 68
I (4031) Touch Elements Combination Example: Slider Calculate, position: 69
I (4041) Touch Elements Combination Example: Slider Calculate, position: 70
I (4051) Touch Elements Combination Example: Slider Calculate, position: 72
I (4061) Touch Elements Combination Example: Slider Calculate, position: 73
I (4071) Touch Elements Combination Example: Slider Calculate, position: 75
I (4081) Touch Elements Combination Example: Slider Calculate, position: 76
I (4091) Touch Elements Combination Example: Slider Calculate, position: 77
I (4101) Touch Elements Combination Example: Slider Calculate, position: 79
I (4111) Touch Elements Combination Example: Slider Calculate, position: 80
I (4121) Touch Elements Combination Example: Slider Calculate, position: 81
I (4131) Touch Elements Combination Example: Slider Calculate, position: 83
I (4141) Touch Elements Combination Example: Slider Calculate, position: 84
I (4151) Touch Elements Combination Example: Slider Calculate, position: 85
I (4161) Touch Elements Combination Example: Slider Calculate, position: 86
I (4171) Touch Elements Combination Example: Slider Calculate, position: 88
I (4181) Touch Elements Combination Example: Slider Calculate, position: 89
I (4191) Touch Elements Combination Example: Slider Calculate, position: 90
I (4201) Touch Elements Combination Example: Slider Calculate, position: 91
I (4211) Touch Elements Combination Example: Slider Calculate, position: 92
I (4221) Touch Elements Combination Example: Slider Calculate, position: 93
I (4231) Touch Elements Combination Example: Slider Calculate, position: 94
I (4241) Touch Elements Combination Example: Slider Calculate, position: 95
I (4251) Touch Elements Combination Example: Slider Calculate, position: 96
I (4261) Touch Elements Combination Example: Slider Calculate, position: 96
I (4271) Touch Elements Combination Example: Slider Calculate, position: 97
I (4281) Touch Elements Combination Example: Slider Calculate, position: 98
I (4291) Touch Elements Combination Example: Slider Calculate, position: 99
I (4301) Touch Elements Combination Example: Slider Calculate, position: 99
I (4311) Touch Elements Combination Example: Slider Calculate, position: 100
I (4321) Touch Elements Combination Example: Slider Calculate, position: 100
I (4331) Touch Elements Combination Example: Slider Calculate, position: 100
I (4341) Touch Elements Combination Example: Slider Calculate, position: 101
I (4351) Touch Elements Combination Example: Slider Release, position: 101
```
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@ -0,0 +1,6 @@
if(IDF_TARGET STREQUAL "esp32s2")
idf_component_register(SRCS "touch_elements_example_main.c"
INCLUDE_DIRS ".")
else()
message(FATAL_ERROR "Touch elements combination example only available on esp32s2 now")
endif()

View File

@ -0,0 +1,162 @@
/* Touch Sensor - Example
For other examples please check:
https://github.com/espressif/esp-idf/tree/master/examples
See README.md file to get detailed usage of this example.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "touch_element/touch_button.h"
#include "touch_element/touch_slider.h"
#include "esp_log.h"
static const char *TAG = "Touch Elements Combination Example";
#define TOUCH_BUTTON_NUM 3
#define TOUCH_SLIDER_CHANNEL_NUM 5
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; //Touch buttons handle
static touch_slider_handle_t slider_handle; //Touch slider handle
/* Touch buttons channel array */
static const touch_pad_t button_channel_array[TOUCH_BUTTON_NUM] = {
TOUCH_PAD_NUM6,
TOUCH_PAD_NUM8,
TOUCH_PAD_NUM10
};
/* Touch buttons channel sensitivity array */
static const float button_channel_sens_array[TOUCH_BUTTON_NUM] = {
0.1F,
0.1F,
0.1F
};
/* Touch slider channels array */
static const touch_pad_t slider_channel_array[TOUCH_SLIDER_CHANNEL_NUM] = {
TOUCH_PAD_NUM5,
TOUCH_PAD_NUM7,
TOUCH_PAD_NUM9,
TOUCH_PAD_NUM11,
TOUCH_PAD_NUM12,
};
/* Touch slider channels sensitivity array */
static const float slider_channel_sens_array[TOUCH_SLIDER_CHANNEL_NUM] = {
0.252F,
0.246F,
0.277F,
0.250F,
0.257F,
};
static void button_handler(touch_elem_message_t element_message)
{
const touch_button_message_t *button_message = touch_button_get_message(&element_message);
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg);
} else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg);
}
}
static void slider_handler(touch_elem_message_t element_message)
{
const touch_slider_message_t *slider_message = touch_slider_get_message(&element_message);
if (slider_message->event == TOUCH_SLIDER_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Slider Press, position: %d", slider_message->position);
} else if (slider_message->event == TOUCH_SLIDER_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Slider Release, position: %d", slider_message->position);
} else if (slider_message->event == TOUCH_SLIDER_EVT_ON_CALCULATION) {
ESP_LOGI(TAG, "Slider Calculate, position: %d", slider_message->position);
}
}
static void event_handler_task(void *arg)
{
(void) arg; //Unused
touch_elem_message_t element_message;
while (1) {
/* Waiting for touch element messages */
touch_element_message_receive(&element_message, portMAX_DELAY);
switch (element_message.element_type) {
case TOUCH_ELEM_TYPE_BUTTON:
button_handler(element_message);
break;
case TOUCH_ELEM_TYPE_SLIDER:
slider_handler(element_message);
break;
default:
ESP_LOGW(TAG, "Unknown element message");
break;
}
}
}
void button_example_init(void)
{
touch_button_global_config_t global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_button_install(&global_config));
ESP_LOGI(TAG, "Touch button installed");
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
touch_button_config_t button_config = {
.channel_num = button_channel_array[i],
.channel_sens = button_channel_sens_array[i]
};
/* Create Touch buttons */
ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i]));
/* Subscribe touch button events (On Press, On Release, On LongPress) */
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,
(void *)button_channel_array[i]));
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
/* Set LongPress event trigger threshold time */
ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 2000));
}
ESP_LOGI(TAG, "Touch buttons created");
}
void slider_example_init(void)
{
touch_slider_global_config_t global_config = TOUCH_SLIDER_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_slider_install(&global_config));
ESP_LOGI(TAG, "Touch slider installed");
/* Create Touch slider */
touch_slider_config_t slider_config = {
.channel_array = slider_channel_array,
.sensitivity_array = slider_channel_sens_array,
.channel_num = (sizeof(slider_channel_array) / sizeof(slider_channel_array[0])),
.position_range = 101
};
ESP_ERROR_CHECK(touch_slider_create(&slider_config, &slider_handle));
/* Subscribe touch slider events (On Press, On Release, On Calculation) */
ESP_ERROR_CHECK(touch_slider_subscribe_event(slider_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_CALCULATION, NULL));
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_slider_set_dispatch_method(slider_handle, TOUCH_ELEM_DISP_EVENT));
ESP_LOGI(TAG, "Touch slider created");
}
void app_main(void)
{
/* Initialize Touch Element library */
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&global_config));
ESP_LOGI(TAG, "Touch element library installed");
button_example_init();
slider_example_init();
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
/* Create a handler task to handle event messages */
xTaskCreate(&event_handler_task, "event_handler_task", 4 * 1024, NULL, 5, NULL);
}

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_matrix)

View File

@ -0,0 +1,53 @@
| Supported Targets | ESP32-S2 |
| ----------------- | -------- |
# Touch Element basic example (EVENT)
This example demonstrates how to use the Touch Element library of capacitive touch sensor and set up touch matrix.
## How to use example
### Configure the project
* Set the target of the build (where `{IDF_TARGET}` stands for the target chip such as `esp32` or `esp32s2`).
* Run `menuconfig` to select a dispatch method for the example.
### Build and Flash
Build the project and flash it to the target board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
```
I (331) Touch Matrix Example: Touch element library installed
I (331) Touch Matrix Example: Touch matrix installed
I (341) Touch Matrix Example: Touch matrix created
I (341) Touch Matrix Example: Touch element library start
I (1951) Touch Matrix Example: Matrix Press, axis: (0, 0) index: 0
I (2131) Touch Matrix Example: Matrix Release, axis: (0, 0) index: 0
I (3121) Touch Matrix Example: Matrix Press, axis: (1, 1) index: 4
I (3281) Touch Matrix Example: Matrix Release, axis: (1, 1) index: 4
I (4621) Touch Matrix Example: Matrix Press, axis: (2, 0) index: 6
I (4801) Touch Matrix Example: Matrix Release, axis: (2, 0) index: 6
I (5381) Touch Matrix Example: Matrix Press, axis: (2, 2) index: 8
I (5571) Touch Matrix Example: Matrix Release, axis: (2, 2) index: 8
I (6221) Touch Matrix Example: Matrix Press, axis: (0, 2) index: 2
I (6441) Touch Matrix Example: Matrix Release, axis: (0, 2) index: 2
I (7551) Touch Matrix Example: Matrix Press, axis: (1, 1) index: 4
I (8551) Touch Matrix Example: Matrix LongPress, axis: (1, 1) index: 4
I (9551) Touch Matrix Example: Matrix LongPress, axis: (1, 1) index: 4
I (10551) Touch Matrix Example: Matrix LongPress, axis: (1, 1) index: 4
I (11031) Touch Matrix Example: Matrix Release, axis: (1, 1) index: 4
```
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@ -0,0 +1,6 @@
if(IDF_TARGET STREQUAL "esp32s2")
idf_component_register(SRCS "touch_matrix_example_main.c"
INCLUDE_DIRS ".")
else()
message(FATAL_ERROR "Touch matrix example only available on esp32s2 now")
endif()

View File

@ -0,0 +1,15 @@
menu "Example Configuration"
choice TOUCH_SENSOR_EXAMPLE_TYPE
bool "Select touch element dispatch method"
default TOUCH_ELEM_EVENT
help
Select touch element dispatch method (event task or callback) for this example.
config TOUCH_ELEM_EVENT
bool "Dispatch by event task"
config TOUCH_ELEM_CALLBACK
bool "Dispatch by callback"
endchoice
endmenu

View File

@ -0,0 +1,132 @@
/* Touch Sensor - Example
For other examples please check:
https://github.com/espressif/esp-idf/tree/master/examples
See README.md file to get detailed usage of this example.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "touch_element/touch_matrix.h"
#include "esp_log.h"
static const char *TAG = "Touch Matrix Example";
#define X_AXIS_CHANNEL_NUM 3
#define Y_AXIS_CHANNEL_NUM 3
static touch_matrix_handle_t matrix_handle;
/* Touch Matrix Button x-axis channels array */
static const touch_pad_t x_axis_channel[X_AXIS_CHANNEL_NUM] = {
TOUCH_PAD_NUM5,
TOUCH_PAD_NUM7,
TOUCH_PAD_NUM9,
};
/* Touch Matrix Button y-axis channels array */
static const touch_pad_t y_axis_channel[Y_AXIS_CHANNEL_NUM] = {
TOUCH_PAD_NUM11,
TOUCH_PAD_NUM12,
TOUCH_PAD_NUM14,
};
/* Touch Matrix Button x-axis channels sensitivity array */
static const float x_axis_channel_sens[X_AXIS_CHANNEL_NUM] = {
0.1F,
0.1F,
0.1F,
};
/* Touch Matrix Button y-axis channel sensitivity array */
static const float y_axis_channel_sens[Y_AXIS_CHANNEL_NUM] = {
0.1F,
0.1F,
0.1F,
};
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Matrix event handler task */
static void matrix_handler_task(void *arg)
{
(void) arg; //Unused
touch_elem_message_t element_message;
while (1) {
/* Waiting for touch element messages */
touch_element_message_receive(&element_message, portMAX_DELAY); //Block take
if (element_message.element_type != TOUCH_ELEM_TYPE_MATRIX) {
continue;
}
/* Decode message */
const touch_matrix_message_t *matrix_message = touch_matrix_get_message(&element_message);
if (matrix_message->event == TOUCH_MATRIX_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Matrix Press, axis: (%d, %d) index: %d", matrix_message->position.x_axis, matrix_message->position.y_axis, matrix_message->position.index);
} else if (matrix_message->event == TOUCH_MATRIX_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Matrix Release, axis: (%d, %d) index: %d", matrix_message->position.x_axis, matrix_message->position.y_axis, matrix_message->position.index);
} else if (matrix_message->event == TOUCH_MATRIX_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Matrix LongPress, axis: (%d, %d) index: %d", matrix_message->position.x_axis, matrix_message->position.y_axis, matrix_message->position.index);
}
}
}
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Matrix callback routine */
void matrix_handler(touch_matrix_handle_t out_handle, touch_matrix_message_t out_message, void *arg)
{
(void) arg; //Unused
if (out_handle != matrix_handle) {
return;
}
if (out_message.event == TOUCH_MATRIX_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Matrix Press, axis: (%d, %d) index: %d", out_message.position.x_axis, out_message.position.y_axis, out_message.position.index);
} else if (out_message.event == TOUCH_MATRIX_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Matrix Release, axis: (%d, %d) index: %d", out_message.position.x_axis, out_message.position.y_axis, out_message.position.index);
} else if (out_message.event == TOUCH_MATRIX_EVT_ON_LONGPRESS) {
ESP_LOGI(TAG, "Matrix LongPress, axis: (%d, %d) index: %d", out_message.position.x_axis, out_message.position.y_axis, out_message.position.index);
}
}
#endif
void app_main(void)
{
/* Initialize Touch Element library */
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&global_config));
ESP_LOGI(TAG, "Touch element library installed");
touch_matrix_global_config_t matrix_global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_matrix_install(&matrix_global_config));
ESP_LOGI(TAG, "Touch matrix installed");
/* Create Touch Matrix Button */
touch_matrix_config_t matrix_config = {
.x_channel_array = x_axis_channel,
.y_channel_array = y_axis_channel,
.x_sensitivity_array = x_axis_channel_sens,
.y_sensitivity_array = y_axis_channel_sens,
.x_channel_num = (sizeof(x_axis_channel) / sizeof(x_axis_channel[0])),
.y_channel_num = (sizeof(y_axis_channel) / sizeof(y_axis_channel[0]))
};
ESP_ERROR_CHECK(touch_matrix_create(&matrix_config, &matrix_handle));
/* Subscribe touch matrix events (On Press, On Release, On LongPress) */
ESP_ERROR_CHECK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS, NULL));
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_EVENT));
/* Create a handler task to handle event messages */
xTaskCreate(&matrix_handler_task, "matrix_handler_task", 4 * 1024, NULL, 5, NULL);
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Set CALLBACK as the dispatch method */
ESP_ERROR_CHECK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_CALLBACK));
/* Register a handler function to handle event messages */
ESP_ERROR_CHECK(touch_matrix_set_callback(matrix_handle, matrix_handler));
#endif
ESP_LOGI(TAG, "Touch matrix created");
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
}

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_slider)

View File

@ -0,0 +1,84 @@
| Supported Targets | ESP32-S2 |
| ----------------- | -------- |
# Touch Element basic example (EVENT)
This example demonstrates how to use the Touch Element library of capacitive touch sensor and set up touch slider.
## How to use example
### Configure the project
* Set the target of the build (where `{IDF_TARGET}` stands for the target chip such as `esp32s2`).
* Run `menuconfig` to select a dispatch method for the example.
### Build and Flash
Build the project and flash it to the target board, then run monitor tool to view serial output:
```
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
```
I (331) Touch Slider Example: Touch element library installed
I (331) Touch Slider Example: Touch slider installed
I (341) Touch Slider Example: Touch slider created
I (341) Touch Slider Example: Touch element library start
I (1911) Touch Slider Example: Slider Press, position: 0
I (1921) Touch Slider Example: Slider Calculate, position: 0
I (1931) Touch Slider Example: Slider Calculate, position: 0
I (1941) Touch Slider Example: Slider Calculate, position: 0
I (1951) Touch Slider Example: Slider Calculate, position: 0
I (1961) Touch Slider Example: Slider Calculate, position: 0
I (1971) Touch Slider Example: Slider Calculate, position: 0
I (1981) Touch Slider Example: Slider Calculate, position: 0
I (1991) Touch Slider Example: Slider Calculate, position: 0
I (2001) Touch Slider Example: Slider Calculate, position: 0
I (2011) Touch Slider Example: Slider Calculate, position: 0
I (2021) Touch Slider Example: Slider Calculate, position: 1
I (2031) Touch Slider Example: Slider Calculate, position: 1
I (2041) Touch Slider Example: Slider Calculate, position: 2
I (2051) Touch Slider Example: Slider Calculate, position: 2
I (2061) Touch Slider Example: Slider Calculate, position: 4
I (2071) Touch Slider Example: Slider Calculate, position: 5
I (2081) Touch Slider Example: Slider Calculate, position: 6
I (2091) Touch Slider Example: Slider Calculate, position: 8
I (2101) Touch Slider Example: Slider Calculate, position: 10
I (2111) Touch Slider Example: Slider Calculate, position: 12
I (2121) Touch Slider Example: Slider Calculate, position: 15
I (2131) Touch Slider Example: Slider Calculate, position: 17
I (2141) Touch Slider Example: Slider Calculate, position: 19
I (2151) Touch Slider Example: Slider Calculate, position: 22
I (2161) Touch Slider Example: Slider Calculate, position: 24
I (2171) Touch Slider Example: Slider Calculate, position: 26
I (2181) Touch Slider Example: Slider Calculate, position: 29
I (2191) Touch Slider Example: Slider Calculate, position: 31
I (2201) Touch Slider Example: Slider Calculate, position: 33
I (2211) Touch Slider Example: Slider Calculate, position: 35
I (2221) Touch Slider Example: Slider Calculate, position: 37
I (2231) Touch Slider Example: Slider Calculate, position: 40
I (2241) Touch Slider Example: Slider Calculate, position: 42
I (2251) Touch Slider Example: Slider Calculate, position: 44
I (2261) Touch Slider Example: Slider Calculate, position: 46
I (2271) Touch Slider Example: Slider Calculate, position: 48
I (2281) Touch Slider Example: Slider Calculate, position: 50
I (2291) Touch Slider Example: Slider Calculate, position: 52
I (2301) Touch Slider Example: Slider Calculate, position: 54
I (2311) Touch Slider Example: Slider Calculate, position: 56
I (2321) Touch Slider Example: Slider Calculate, position: 57
I (2331) Touch Slider Example: Slider Calculate, position: 59
I (2341) Touch Slider Example: Slider Calculate, position: 60
I (2351) Touch Slider Example: Slider Calculate, position: 61
I (2361) Touch Slider Example: Slider Release, position: 61
```
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@ -0,0 +1,6 @@
if(IDF_TARGET STREQUAL "esp32s2")
idf_component_register(SRCS "touch_slider_example_main.c"
INCLUDE_DIRS ".")
else()
message(FATAL_ERROR "Touch slider example only available on esp32s2 now")
endif()

View File

@ -0,0 +1,15 @@
menu "Example Configuration"
choice TOUCH_SENSOR_EXAMPLE_TYPE
bool "Select touch element dispatch method"
default TOUCH_ELEM_EVENT
help
Select touch element dispatch method (event task or callback) for this example.
config TOUCH_ELEM_EVENT
bool "Dispatch by event task"
config TOUCH_ELEM_CALLBACK
bool "Dispatch by callback"
endchoice
endmenu

View File

@ -0,0 +1,123 @@
/* Touch Sensor - Example
For other examples please check:
https://github.com/espressif/esp-idf/tree/master/examples
See README.md file to get detailed usage of this example.
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "touch_element/touch_slider.h"
#include "esp_log.h"
static const char *TAG = "Touch Slider Example";
#define TOUCH_SLIDER_CHANNEL_NUM 5
static touch_slider_handle_t slider_handle; //Touch slider handle
static const touch_pad_t channel_array[TOUCH_SLIDER_CHANNEL_NUM] = { //Touch slider channels array
TOUCH_PAD_NUM5,
TOUCH_PAD_NUM7,
TOUCH_PAD_NUM9,
TOUCH_PAD_NUM11,
TOUCH_PAD_NUM12,
};
/**
* Using finger slide from slider's beginning to the ending, and output the RAW channel signal, then calculate all the
* channels sensitivity of the slider, and you can decrease or increase the detection sensitivity by adjusting the threshold divider
* which locates in touch_slider_global_config_t. Please keep in mind that the real sensitivity totally depends on the
* physical characteristics, if you want to decrease or increase the detection sensitivity, keep the ratio of those channels the same.
*/
static const float channel_sens_array[TOUCH_SLIDER_CHANNEL_NUM] = { //Touch slider channels sensitivity array
0.252F,
0.246F,
0.277F,
0.250F,
0.257F,
};
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Slider event handler task */
static void slider_handler_task(void *arg)
{
(void) arg; //Unused
touch_elem_message_t element_message;
while (1) {
/* Waiting for touch element messages */
if (touch_element_message_receive(&element_message, portMAX_DELAY) == ESP_OK) {
if (element_message.element_type != TOUCH_ELEM_TYPE_SLIDER) {
continue;
}
/* Decode message */
const touch_slider_message_t *slider_message = touch_slider_get_message(&element_message);
if (slider_message->event == TOUCH_SLIDER_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Slider Press, position: %d", slider_message->position);
} else if (slider_message->event == TOUCH_SLIDER_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Slider Release, position: %d", slider_message->position);
} else if (slider_message->event == TOUCH_SLIDER_EVT_ON_CALCULATION) {
ESP_LOGI(TAG, "Slider Calculate, position: %d", slider_message->position);
}
}
}
}
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Slider callback routine */
void slider_handler(touch_slider_handle_t out_handle, touch_slider_message_t out_message, void *arg)
{
(void) arg; //Unused
if (out_handle != slider_handle) {
return;
}
if (out_message.event == TOUCH_SLIDER_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Slider Press, position: %d", out_message.position);
} else if (out_message.event == TOUCH_SLIDER_EVT_ON_RELEASE) {
ESP_LOGI(TAG, "Slider Release, position: %d", out_message.position);
} else if (out_message.event == TOUCH_SLIDER_EVT_ON_CALCULATION) {
ESP_LOGI(TAG, "Slider Calculate, position: %d", out_message.position);
}
}
#endif
void app_main(void)
{
/* Initialize Touch Element library */
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&global_config));
ESP_LOGI(TAG, "Touch element library installed");
touch_slider_global_config_t slider_global_config = TOUCH_SLIDER_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_slider_install(&slider_global_config));
ESP_LOGI(TAG, "Touch slider installed");
/* Create Touch slider */
touch_slider_config_t slider_config = {
.channel_array = channel_array,
.sensitivity_array = channel_sens_array,
.channel_num = (sizeof(channel_array) / sizeof(channel_array[0])),
.position_range = 101
};
ESP_ERROR_CHECK(touch_slider_create(&slider_config, &slider_handle));
/* Subscribe touch slider events (On Press, On Release, On Calculation) */
ESP_ERROR_CHECK(touch_slider_subscribe_event(slider_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_CALCULATION, NULL));
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_slider_set_dispatch_method(slider_handle, TOUCH_ELEM_DISP_EVENT));
/* Create a handler task to handle event messages */
xTaskCreate(&slider_handler_task, "slider_handler_task", 4 * 1024, NULL, 5, NULL);
#elif CONFIG_TOUCH_ELEM_CALLBACK
/* Set CALLBACK as the dispatch method */
ESP_ERROR_CHECK(touch_slider_set_dispatch_method(slider_handle, TOUCH_ELEM_DISP_CALLBACK));
/* Register a handler function to handle event messages */
ESP_ERROR_CHECK(touch_slider_set_callback(slider_handle, slider_handler));
#endif
ESP_LOGI(TAG, "Touch slider created");
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
}