touch_element: add touch element lib example

This commit is contained in:
Kang Zuoling
2021-01-18 13:27:57 +08:00
committed by Kang Zuo Ling
parent 1a53bd39a4
commit cd8e874c8d
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_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();
}