mirror of
https://github.com/espressif/esp-idf.git
synced 2025-12-11 02:07:46 +00:00
touch_element: add touch element lib example
This commit is contained in:
committed by
Kang Zuo Ling
parent
1a53bd39a4
commit
cd8e874c8d
@@ -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)
|
||||
59
examples/peripherals/touch_element/touch_button/README.md
Normal file
59
examples/peripherals/touch_element/touch_button/README.md
Normal 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.
|
||||
@@ -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()
|
||||
@@ -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
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user