mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-25 01:37:22 +00:00
update touch sensor examples
This commit is contained in:
@@ -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)
|
@@ -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.
|
@@ -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()
|
@@ -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,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");
|
||||
}
|
Reference in New Issue
Block a user