mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-06 08:37:29 +02: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_pad_interrupt)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := touch_pad_interrupt
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,44 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# Touch Pad Interrupt Example
|
||||
|
||||
## ESP32 platform
|
||||
|
||||
Demonstrates how to set up ESP32's capacitive touch pad peripheral to trigger interrupt when a pad is touched. It also shows how to detect the touch event by the software for sensor designs when greater touch detection sensitivity is required.
|
||||
|
||||
ESP32 supports touch detection by configuring hardware registers. The hardware periodically detects the pulse counts. If the number of pulse counts exceeds the set threshold, a hardware interrupt will be generated to notify the application layer that a certain touch sensor channel may be triggered.
|
||||
|
||||
For the sensor designs when the pad is covered a glass or plastic, the difference caused by a 'touch' action could be very small. In such a case we are using software pooling and algorithms to reduce noise to still be able to detect small changes of the pulse counts. In certain cases we may need to use additional routines to adjust the threshold level dynamically as it may change depending on environment conditions.
|
||||
|
||||
Comparison of the two modes:
|
||||
|
||||
- The hardware interrupt mode occupies less CPU resources, but only a single threshold can be set and cannot support various software algorithms.
|
||||
- The continuous pooling is flexible and supports various software algorithms. However, it also costs CPU overhead
|
||||
|
||||
The application is cycling between the interrupt mode and the pooling mode with a filter, to compare performance of the touch sensor system in both scenarios:
|
||||
|
||||
```
|
||||
I (6303) Touch pad: Waiting for any pad being touched...
|
||||
I (6733) Touch pad: T6 activated!
|
||||
I (7333) Touch pad: T5 activated!
|
||||
I (7723) Touch pad: T3 activated!
|
||||
I (8043) Touch pad: T2 activated!
|
||||
I (8883) Touch pad: T4 activated!
|
||||
I (9523) Touch pad: T7 activated!
|
||||
I (12503) Touch pad: Waiting for any pad being touched...
|
||||
I (15483) Touch pad: T6 activated!
|
||||
I (16253) Touch pad: T5 activated!
|
||||
I (17903) Touch pad: Waiting for any pad being touched...
|
||||
I (22903) Touch pad: Waiting for any pad being touched...
|
||||
```
|
||||
|
||||
Note: Sensing threshold is set up automatically at start up by performing simple calibration. Application is reading current value for each pad and assuming two thirds of this value as the sensing threshold. Do not touch pads on application start up, otherwise sensing may not work correctly.
|
||||
|
||||
## Reference Information
|
||||
|
||||
For a simpler example how to configure and read capacitive touch pads, please refer to [touch_pad_read](../touch_pad_read).
|
||||
|
||||
Design and implementation of the touch sensor system is a complex process. The [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/release/v1.0/documents/touch_pad_solution/touch_sensor_design_en.md) contains several ESP32 specific notes and comments to optimize the design and get the best out of the application with sensors controlled with the ESP32.
|
||||
|
||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "tp_interrupt_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,175 @@
|
||||
/* Touch Pad Interrupt 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 <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "driver/touch_pad.h"
|
||||
#include "soc/rtc_periph.h"
|
||||
#include "soc/sens_periph.h"
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
|
||||
static const char *TAG = "Touch pad";
|
||||
|
||||
#define TOUCH_THRESH_NO_USE (0)
|
||||
#define TOUCH_THRESH_PERCENT (80)
|
||||
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
|
||||
|
||||
static bool s_pad_activated[TOUCH_PAD_MAX];
|
||||
static uint32_t s_pad_init_val[TOUCH_PAD_MAX];
|
||||
|
||||
/*
|
||||
Read values sensed at all available touch pads.
|
||||
Use 2 / 3 of read value as the threshold
|
||||
to trigger interrupt when the pad is touched.
|
||||
Note: this routine demonstrates a simple way
|
||||
to configure activation threshold for the touch pads.
|
||||
Do not touch any pads when this routine
|
||||
is running (on application start).
|
||||
*/
|
||||
static void tp_example_set_thresholds(void)
|
||||
{
|
||||
uint16_t touch_value;
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
//read filtered value
|
||||
touch_pad_read_filtered(i, &touch_value);
|
||||
s_pad_init_val[i] = touch_value;
|
||||
ESP_LOGI(TAG, "test init: touch pad [%d] val is %d", i, touch_value);
|
||||
//set interrupt threshold.
|
||||
ESP_ERROR_CHECK(touch_pad_set_thresh(i, touch_value * 2 / 3));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Check if any of touch pads has been activated
|
||||
by reading a table updated by rtc_intr()
|
||||
If so, then print it out on a serial monitor.
|
||||
Clear related entry in the table afterwards
|
||||
|
||||
In interrupt mode, the table is updated in touch ISR.
|
||||
|
||||
In filter mode, we will compare the current filtered value with the initial one.
|
||||
If the current filtered value is less than 80% of the initial value, we can
|
||||
regard it as a 'touched' event.
|
||||
When calling touch_pad_init, a timer will be started to run the filter.
|
||||
This mode is designed for the situation that the pad is covered
|
||||
by a 2-or-3-mm-thick medium, usually glass or plastic.
|
||||
The difference caused by a 'touch' action could be very small, but we can still use
|
||||
filter mode to detect a 'touch' event.
|
||||
*/
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
static int show_message;
|
||||
int change_mode = 0;
|
||||
int filter_mode = 0;
|
||||
while (1) {
|
||||
if (filter_mode == 0) {
|
||||
//interrupt mode, enable touch interrupt
|
||||
touch_pad_intr_enable();
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
if (s_pad_activated[i] == true) {
|
||||
ESP_LOGI(TAG, "T%d activated!", i);
|
||||
// Wait a while for the pad being released
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
// Clear information on pad activation
|
||||
s_pad_activated[i] = false;
|
||||
// Reset the counter triggering a message
|
||||
// that application is running
|
||||
show_message = 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//filter mode, disable touch interrupt
|
||||
touch_pad_intr_disable();
|
||||
touch_pad_clear_status();
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
uint16_t value = 0;
|
||||
touch_pad_read_filtered(i, &value);
|
||||
if (value < s_pad_init_val[i] * TOUCH_THRESH_PERCENT / 100) {
|
||||
ESP_LOGI(TAG, "T%d activated!", i);
|
||||
ESP_LOGI(TAG, "value: %d; init val: %d", value, s_pad_init_val[i]);
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
// Reset the counter to stop changing mode.
|
||||
change_mode = 1;
|
||||
show_message = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
|
||||
// If no pad is touched, every couple of seconds, show a message
|
||||
// that application is running
|
||||
if (show_message++ % 500 == 0) {
|
||||
ESP_LOGI(TAG, "Waiting for any pad being touched...");
|
||||
}
|
||||
// Change mode if no pad is touched for a long time.
|
||||
// We can compare the two different mode.
|
||||
if (change_mode++ % 2000 == 0) {
|
||||
filter_mode = !filter_mode;
|
||||
ESP_LOGW(TAG, "Change mode...%s", filter_mode == 0 ? "interrupt mode" : "filter mode");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Handle an interrupt triggered when a pad is touched.
|
||||
Recognize what pad has been touched and save it in a table.
|
||||
*/
|
||||
static void tp_example_rtc_intr(void *arg)
|
||||
{
|
||||
uint32_t pad_intr = touch_pad_get_status();
|
||||
//clear interrupt
|
||||
touch_pad_clear_status();
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
if ((pad_intr >> i) & 0x01) {
|
||||
s_pad_activated[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Before reading touch pad, we need to initialize the RTC IO.
|
||||
*/
|
||||
static void tp_example_touch_pad_init(void)
|
||||
{
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
//init RTC IO and mode for touch pad.
|
||||
touch_pad_config(i, TOUCH_THRESH_NO_USE);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// Initialize touch pad peripheral, it will start a timer to run a filter
|
||||
ESP_LOGI(TAG, "Initializing touch pad");
|
||||
ESP_ERROR_CHECK(touch_pad_init());
|
||||
// If use interrupt trigger mode, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
|
||||
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
|
||||
// Set reference voltage for charging/discharging
|
||||
// For most usage scenarios, we recommend using the following combination:
|
||||
// the high reference valtage will be 2.7V - 1V = 1.7V, The low reference voltage will be 0.5V.
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
|
||||
// Init touch pad IO
|
||||
tp_example_touch_pad_init();
|
||||
// Initialize and start a software filter to detect slight change of capacitance.
|
||||
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
|
||||
// Set thresh hold
|
||||
tp_example_set_thresholds();
|
||||
// Register touch interrupt ISR
|
||||
touch_pad_isr_register(tp_example_rtc_intr, NULL);
|
||||
// Start a task to show what pads have been touched
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
#endif // CONFIG_IDF_TARGET_ESP32
|
||||
@@ -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_pad_read)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := touch_pad_read
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,31 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# Touch Pad Read Example
|
||||
|
||||
## ESP32 plaform
|
||||
|
||||
Read and display raw values or IIR filtered values from capacitive touch pad sensors.
|
||||
|
||||
Once configured, ESP32 is continuously measuring capacitance of touch pad sensors. Measurement is reflected as numeric value inversely related to sensor's capacitance. The capacitance is bigger when sensor is touched with a finger and the measured value smaller. In opposite situation, when finger is released, capacitance is smaller and the measured value bigger.
|
||||
|
||||
To detect when a sensor is touched and when not, each particular design should be calibrated by obtaining both measurements for each individual sensor. Then a threshold between both values should be established. Using specific threshold, API is then able to distinguish whether specific sensor is touched or released.
|
||||
|
||||
ESP32 supports reading up to ten capacitive touch pad sensors T0 - T9, connected to specific GPIO pins. For information on available pins please refer to [Technical Reference Manual](https://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf). Application initializes all ten sensor pads. Then in a loop reads sensors T0 - T9 and displays obtained values (after a colon) on a serial terminal:
|
||||
|
||||
```
|
||||
Touch Sensor filter mode read, the output format is:
|
||||
Touchpad num:[raw data, filtered data]
|
||||
|
||||
T0:[1072,1071] T1:[ 475, 475] T2:[1004,1003] T3:[1232,1231] T4:[1675,1676] T5:[1146,1146] T6:[1607,1607] T7:[1118,1118] T8:[1695,1695] T9:[1223,1222]
|
||||
T0:[1072,1071] T1:[ 475, 475] T2:[1003,1003] T3:[1231,1231] T4:[1676,1676] T5:[1146,1146] T6:[1607,1607] T7:[1118,1118] T8:[1694,1694] T9:[1222,1221]
|
||||
T0:[1071,1071] T1:[ 475, 475] T2:[1004,1004] T3:[1231,1231] T4:[1678,1677] T5:[1147,1146] T6:[1607,1607] T7:[1118,1118] T8:[1694,1694] T9:[1222,1221]
|
||||
```
|
||||
|
||||
## Reference Information
|
||||
|
||||
For hardware and firmware design guidelines on ESP32 touch sensor system, please refer to [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/release/v1.0/documents/touch_pad_solution/touch_sensor_design_en.md), where you may find comprehensive information on how to design and implement touch sensing applications, such as linear slider, wheel slider, matrix buttons and spring buttons.
|
||||
|
||||
There is another similar example that demonstrates how to perform simple calibration and trigger an interrupt when a pat is touched - see [touch_pad_interrupt](../touch_pad_interrupt).
|
||||
|
||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "tp_read_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,76 @@
|
||||
/* Touch Pad Read 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 <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/touch_pad.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
|
||||
#define TOUCH_PAD_NO_CHANGE (-1)
|
||||
#define TOUCH_THRESH_NO_USE (0)
|
||||
#define TOUCH_FILTER_MODE_EN (1)
|
||||
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
|
||||
/*
|
||||
Read values sensed at all available touch pads.
|
||||
Print out values in a loop on a serial monitor.
|
||||
*/
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
uint16_t touch_value;
|
||||
uint16_t touch_filter_value;
|
||||
#if TOUCH_FILTER_MODE_EN
|
||||
printf("Touch Sensor filter mode read, the output format is: \nTouchpad num:[raw data, filtered data]\n\n");
|
||||
#else
|
||||
printf("Touch Sensor normal mode read, the output format is: \nTouchpad num:[raw data]\n\n");
|
||||
#endif
|
||||
while (1) {
|
||||
for (int i = 0; i < TOUCH_PAD_MAX; i++) {
|
||||
#if TOUCH_FILTER_MODE_EN
|
||||
// If open the filter mode, please use this API to get the touch pad count.
|
||||
touch_pad_read_raw_data(i, &touch_value);
|
||||
touch_pad_read_filtered(i, &touch_filter_value);
|
||||
printf("T%d:[%4d,%4d] ", i, touch_value, touch_filter_value);
|
||||
#else
|
||||
touch_pad_read(i, &touch_value);
|
||||
printf("T%d:[%4d] ", i, touch_value);
|
||||
#endif
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
static void tp_example_touch_pad_init(void)
|
||||
{
|
||||
for (int i = 0;i< TOUCH_PAD_MAX;i++) {
|
||||
touch_pad_config(i, TOUCH_THRESH_NO_USE);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// Initialize touch pad peripheral.
|
||||
// The default fsm mode is software trigger mode.
|
||||
ESP_ERROR_CHECK(touch_pad_init());
|
||||
// Set reference voltage for charging/discharging
|
||||
// In this case, the high reference valtage will be 2.7V - 1V = 1.7V
|
||||
// The low reference voltage will be 0.5
|
||||
// The larger the range, the larger the pulse count value.
|
||||
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
|
||||
tp_example_touch_pad_init();
|
||||
#if TOUCH_FILTER_MODE_EN
|
||||
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
|
||||
#endif
|
||||
// Start task to read values sensed by pads
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
#endif // CONFIG_IDF_TARGET_ESP32
|
||||
@@ -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)
|
||||
@@ -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
|
||||
+141
@@ -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");
|
||||
}
|
||||
+6
@@ -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)
|
||||
@@ -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.
|
||||
+6
@@ -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()
|
||||
+10
@@ -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
|
||||
+98
@@ -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();
|
||||
}
|
||||
+6
@@ -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)
|
||||
@@ -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.
|
||||
+6
@@ -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()
|
||||
+162
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
+132
@@ -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");
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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.
|
||||
@@ -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()
|
||||
@@ -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
|
||||
+123
@@ -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");
|
||||
}
|
||||
@@ -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_pad_interrupt)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := touch_pad_interrupt
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,36 @@
|
||||
| Supported Targets | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | -------- | -------- |
|
||||
|
||||
# Touch Pad Interrupt Example
|
||||
|
||||
## ESP32-S2, ESP32-S3 platform
|
||||
|
||||
Demonstrates how to set up ESP32-S2's capacitive touch pad peripheral to trigger interrupt when a pad is touched. It also shows how to detect the touch event by the software for sensor designs when greater touch detection sensitivity is required.
|
||||
|
||||
ESP32-S2 supports touch detection by configuring hardware registers. The hardware periodically detects the pulse counts. If the number of pulse counts exceeds the set threshold, a hardware interrupt will be generated to notify the application layer that a certain touch sensor channel may be triggered.
|
||||
|
||||
The application is cycling between the interrupt mode and the pooling mode with a filter, to compare performance of the touch sensor system in both scenarios:
|
||||
|
||||
```
|
||||
I (304) Touch pad: Initializing touch pad
|
||||
I (304) Touch pad: Denoise function init
|
||||
I (304) Touch pad: touch pad waterproof init
|
||||
I (304) Touch pad: touch pad filter init 2
|
||||
I (414) Touch pad: test init: touch pad [7] base 7382, thresh 1476
|
||||
I (414) Touch pad: test init: touch pad [9] base 7349, thresh 1469
|
||||
I (414) Touch pad: test init: touch pad [11] base 8047, thresh 1609
|
||||
I (414) Touch pad: test init: touch pad [13] base 8104, thresh 810
|
||||
I (5954) Touch pad: TouchSensor [9] be actived, status mask 0x200
|
||||
W (6034) Touch pad: TouchSensor [13] be actived, enter guard mode
|
||||
W (6034) Touch pad: In guard mode. No response
|
||||
W (6174) Touch pad: TouchSensor [13] be actived, exit guard mode
|
||||
I (6194) Touch pad: TouchSensor [9] be inactived, status mask 0x0
|
||||
```
|
||||
|
||||
## Reference Information
|
||||
|
||||
For a simpler example how to configure and read capacitive touch pads, please refer to [touch_pad_read](../touch_pad_read).
|
||||
|
||||
Design and implementation of the touch sensor system is a complex process. The [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/release/v1.0/documents/touch_pad_solution/touch_sensor_design_en.md) contains several ESP32 specific notes and comments to optimize the design and get the best out of the application with sensors controlled with the ESP32.
|
||||
|
||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "tp_interrupt_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,213 @@
|
||||
/* Touch Pad Interrupt 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 <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/touch_pad.h"
|
||||
#include "soc/rtc_periph.h"
|
||||
#include "soc/sens_periph.h"
|
||||
|
||||
static const char *TAG = "Touch pad";
|
||||
|
||||
static QueueHandle_t que_touch = NULL;
|
||||
typedef struct touch_msg {
|
||||
touch_pad_intr_mask_t intr_mask;
|
||||
uint32_t pad_num;
|
||||
uint32_t pad_status;
|
||||
uint32_t pad_val;
|
||||
} touch_event_t;
|
||||
|
||||
#define TOUCH_BUTTON_NUM 4
|
||||
#define TOUCH_BUTTON_WATERPROOF_ENABLE 1
|
||||
#define TOUCH_BUTTON_DENOISE_ENABLE 1
|
||||
#define TOUCH_CHANGE_CONFIG 0
|
||||
|
||||
static const touch_pad_t button[TOUCH_BUTTON_NUM] = {
|
||||
TOUCH_PAD_NUM7, // 'SELECT' button.
|
||||
TOUCH_PAD_NUM9, // 'MENU' button.
|
||||
TOUCH_PAD_NUM11, // 'BACK' button.
|
||||
TOUCH_PAD_NUM13, // Guard ring for waterproof design.
|
||||
// If this pad be touched, other pads no response.
|
||||
};
|
||||
|
||||
/*
|
||||
* Touch threshold. The threshold determines the sensitivity of the touch.
|
||||
* This threshold is derived by testing changes in readings from different touch channels.
|
||||
* If (raw_data - benchmark) > benchmark * threshold, the pad be activated.
|
||||
* If (raw_data - benchmark) < benchmark * threshold, the pad be inactivated.
|
||||
*/
|
||||
static const float button_threshold[TOUCH_BUTTON_NUM] = {
|
||||
0.2, // 20%.
|
||||
0.2, // 20%.
|
||||
0.2, // 20%.
|
||||
0.1, // 10%.
|
||||
};
|
||||
|
||||
/*
|
||||
Handle an interrupt triggered when a pad is touched.
|
||||
Recognize what pad has been touched and save it in a table.
|
||||
*/
|
||||
static void touchsensor_interrupt_cb(void *arg)
|
||||
{
|
||||
int task_awoken = pdFALSE;
|
||||
touch_event_t evt;
|
||||
|
||||
evt.intr_mask = touch_pad_read_intr_status_mask();
|
||||
evt.pad_status = touch_pad_get_status();
|
||||
evt.pad_num = touch_pad_get_current_meas_channel();
|
||||
|
||||
xQueueSendFromISR(que_touch, &evt, &task_awoken);
|
||||
if (task_awoken == pdTRUE) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
|
||||
static void tp_example_set_thresholds(void)
|
||||
{
|
||||
uint32_t touch_value;
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
//read benchmark value
|
||||
touch_pad_read_benchmark(button[i], &touch_value);
|
||||
//set interrupt threshold.
|
||||
touch_pad_set_thresh(button[i], touch_value * button_threshold[i]);
|
||||
ESP_LOGI(TAG, "touch pad [%d] base %d, thresh %d", \
|
||||
button[i], touch_value, (uint32_t)(touch_value * button_threshold[i]));
|
||||
}
|
||||
}
|
||||
|
||||
static void touchsensor_filter_set(touch_filter_mode_t mode)
|
||||
{
|
||||
/* Filter function */
|
||||
touch_filter_config_t filter_info = {
|
||||
.mode = mode, // Test jitter and filter 1/4.
|
||||
.debounce_cnt = 1, // 1 time count.
|
||||
.noise_thr = 0, // 50%
|
||||
.jitter_step = 4, // use for jitter mode.
|
||||
.smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
|
||||
};
|
||||
touch_pad_filter_set_config(&filter_info);
|
||||
touch_pad_filter_enable();
|
||||
ESP_LOGI(TAG, "touch pad filter init");
|
||||
}
|
||||
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
touch_event_t evt = {0};
|
||||
static uint8_t guard_mode_flag = 0;
|
||||
/* Wait touch sensor init done */
|
||||
vTaskDelay(50 / portTICK_RATE_MS);
|
||||
tp_example_set_thresholds();
|
||||
|
||||
while (1) {
|
||||
int ret = xQueueReceive(que_touch, &evt, (portTickType)portMAX_DELAY);
|
||||
if (ret != pdTRUE) {
|
||||
continue;
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
|
||||
/* if guard pad be touched, other pads no response. */
|
||||
if (evt.pad_num == button[3]) {
|
||||
guard_mode_flag = 1;
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be activated, enter guard mode", evt.pad_num);
|
||||
} else {
|
||||
if (guard_mode_flag == 0) {
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be activated, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "In guard mode. No response");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
|
||||
/* if guard pad be touched, other pads no response. */
|
||||
if (evt.pad_num == button[3]) {
|
||||
guard_mode_flag = 0;
|
||||
ESP_LOGW(TAG, "TouchSensor [%d] be inactivated, exit guard mode", evt.pad_num);
|
||||
} else {
|
||||
if (guard_mode_flag == 0) {
|
||||
ESP_LOGI(TAG, "TouchSensor [%d] be inactivated, status mask 0x%x", evt.pad_num, evt.pad_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
|
||||
ESP_LOGI(TAG, "The touch sensor group measurement is done [%d].", evt.pad_num);
|
||||
}
|
||||
if (evt.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
|
||||
/* Add your exception handling in here. */
|
||||
ESP_LOGI(TAG, "Touch sensor channel %d measure timeout. Skip this exception channel!!", evt.pad_num);
|
||||
touch_pad_timeout_resume(); // Point on the next channel to measure.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
if (que_touch == NULL) {
|
||||
que_touch = xQueueCreate(TOUCH_BUTTON_NUM, sizeof(touch_event_t));
|
||||
}
|
||||
// Initialize touch pad peripheral, it will start a timer to run a filter
|
||||
ESP_LOGI(TAG, "Initializing touch pad");
|
||||
/* Initialize touch pad peripheral. */
|
||||
touch_pad_init();
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_config(button[i]);
|
||||
}
|
||||
|
||||
#if TOUCH_CHANGE_CONFIG
|
||||
/* If you want change the touch sensor default setting, please write here(after initialize). There are examples: */
|
||||
touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
|
||||
touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
|
||||
touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_DEFAULT, TOUCH_PAD_TIE_OPT_DEFAULT);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if TOUCH_BUTTON_DENOISE_ENABLE
|
||||
/* Denoise setting at TouchSensor 0. */
|
||||
touch_pad_denoise_t denoise = {
|
||||
/* The bits to be cancelled are determined according to the noise level. */
|
||||
.grade = TOUCH_PAD_DENOISE_BIT4,
|
||||
/* By adjusting the parameters, the reading of T0 should be approximated to the reading of the measured channel. */
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L4,
|
||||
};
|
||||
touch_pad_denoise_set_config(&denoise);
|
||||
touch_pad_denoise_enable();
|
||||
ESP_LOGI(TAG, "Denoise function init");
|
||||
#endif
|
||||
|
||||
#if TOUCH_BUTTON_WATERPROOF_ENABLE
|
||||
/* Waterproof function */
|
||||
touch_pad_waterproof_t waterproof = {
|
||||
.guard_ring_pad = button[3], // If no ring pad, set 0;
|
||||
/* It depends on the number of the parasitic capacitance of the shield pad.
|
||||
Based on the touch readings of T14 and T0, estimate the size of the parasitic capacitance on T14
|
||||
and set the parameters of the appropriate hardware. */
|
||||
.shield_driver = TOUCH_PAD_SHIELD_DRV_L2,
|
||||
};
|
||||
touch_pad_waterproof_set_config(&waterproof);
|
||||
touch_pad_waterproof_enable();
|
||||
ESP_LOGI(TAG, "touch pad waterproof init");
|
||||
#endif
|
||||
|
||||
/* Filter setting */
|
||||
touchsensor_filter_set(TOUCH_PAD_FILTER_IIR_16);
|
||||
touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX);
|
||||
/* Register touch interrupt ISR, enable intr type. */
|
||||
touch_pad_isr_register(touchsensor_interrupt_cb, NULL, TOUCH_PAD_INTR_MASK_ALL);
|
||||
/* If you have other touch algorithm, you can get the measured value after the `TOUCH_PAD_INTR_MASK_SCAN_DONE` interrupt is generated. */
|
||||
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
|
||||
|
||||
/* Enable touch sensor clock. Work mode is "timer trigger". */
|
||||
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
|
||||
touch_pad_fsm_start();
|
||||
|
||||
// Start a task to show what pads have been touched
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
@@ -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_pad_read)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := touch_pad_read
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,31 @@
|
||||
| Supported Targets | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | -------- | -------- |
|
||||
|
||||
# Touch Pad Read Example
|
||||
|
||||
## ESP32-S2, ESP32-S3 platform
|
||||
|
||||
Read and display raw values from capacitive touch pad sensors.
|
||||
|
||||
Once configured, ESP32-S2 is continuously measuring capacitance of touch pad sensors. Measurement is reflected as numeric value inversely related to sensor's capacitance. The capacitance is bigger when sensor is touched with a finger and the measured value bigger. In opposite situation, when finger is released, capacitance is smaller and the measured value smaller.
|
||||
|
||||
To detect when a sensor is touched and when not, each particular design should be calibrated by obtaining both measurements for each individual sensor. Then a threshold between both values should be established. Using specific threshold, API is then able to distinguish whether specific sensor is touched or released. For ESP32-S2, the hardware integrates the edge detection algorithm, which can achieve the purpose of detecting touch actions by configuring appropriate parameters. Please find the example in [touch_pad_interrupt](../touch_pad_interrupt).
|
||||
|
||||
ESP32-S2 supports reading up to 14 capacitive touch pad sensors T1 - T14, connected to specific GPIO pins. For information on available pins please refer to ESP32-S2 Technical Reference Manual. T0 is the internal denoise channel used to filter out system noise and there is no corresponding external GPIO. Application initializes 14 sensor pads. Then in a loop reads sensors T1 - T14 and displays obtained values (after a colon) on a serial terminal:
|
||||
|
||||
```
|
||||
Touch Sensor read, the output format is:
|
||||
Touchpad num:[raw data]
|
||||
|
||||
T1: [6473] T2: [6507] T3: [6638] T4: [8917] T5: [9053] T6: [7190] T7: [7176] T8: [7416] T9: [7145] T10: [7387] T11: [7973] T12: [7776] T13: [8151] T14: [8190]
|
||||
T1: [6463] T2: [6512] T3: [6643] T4: [8920] T5: [9050] T6: [7191] T7: [7176] T8: [7416] T9: [7143] T10: [7387] T11: [7974] T12: [7778] T13: [8152] T14: [8192]
|
||||
T1: [6476] T2: [6508] T3: [6641] T4: [8919] T5: [9053] T6: [7190] T7: [7177] T8: [7416] T9: [7143] T10: [7386] T11: [7974] T12: [7776] T13: [8153] T14: [8193]
|
||||
```
|
||||
|
||||
## Reference Information
|
||||
|
||||
For hardware and firmware design guidelines on ESP32 touch sensor system, please refer to [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/release/v1.0/documents/touch_pad_solution/touch_sensor_design_en.md), where you may find comprehensive information on how to design and implement touch sensing applications, such as linear slider, wheel slider, matrix buttons and spring buttons.
|
||||
|
||||
There is another similar example that demonstrates how to perform simple calibration and trigger an interrupt when a pat is touched - see [touch_pad_interrupt](../touch_pad_interrupt).
|
||||
|
||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "tp_read_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,90 @@
|
||||
/* Touch Pad Read 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 <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/touch_pad.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TOUCH_BUTTON_NUM 14
|
||||
#define TOUCH_CHANGE_CONFIG 0
|
||||
|
||||
static const char *TAG = "touch read";
|
||||
static const touch_pad_t button[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
|
||||
};
|
||||
|
||||
/*
|
||||
Read values sensed at all available touch pads.
|
||||
Print out values in a loop on a serial monitor.
|
||||
*/
|
||||
static void tp_example_read_task(void *pvParameter)
|
||||
{
|
||||
uint32_t touch_value;
|
||||
|
||||
/* Wait touch sensor init done */
|
||||
vTaskDelay(100 / portTICK_RATE_MS);
|
||||
printf("Touch Sensor read, the output format is: \nTouchpad num:[raw data]\n\n");
|
||||
|
||||
while (1) {
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_read_raw_data(button[i], &touch_value); // read raw data.
|
||||
printf("T%d: [%4d] ", button[i], touch_value);
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
/* Initialize touch pad peripheral. */
|
||||
touch_pad_init();
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_config(button[i]);
|
||||
}
|
||||
#if TOUCH_CHANGE_CONFIG
|
||||
/* If you want change the touch sensor default setting, please write here(after initialize). There are examples: */
|
||||
touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT);
|
||||
touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
|
||||
touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
|
||||
for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
|
||||
touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_DEFAULT, TOUCH_PAD_TIE_OPT_DEFAULT);
|
||||
}
|
||||
#endif
|
||||
/* Denoise setting at TouchSensor 0. */
|
||||
touch_pad_denoise_t denoise = {
|
||||
/* The bits to be cancelled are determined according to the noise level. */
|
||||
.grade = TOUCH_PAD_DENOISE_BIT4,
|
||||
.cap_level = TOUCH_PAD_DENOISE_CAP_L4,
|
||||
};
|
||||
touch_pad_denoise_set_config(&denoise);
|
||||
touch_pad_denoise_enable();
|
||||
ESP_LOGI(TAG, "Denoise function init");
|
||||
|
||||
/* Enable touch sensor clock. Work mode is "timer trigger". */
|
||||
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
|
||||
touch_pad_fsm_start();
|
||||
|
||||
/* Start task to read values by pads. */
|
||||
xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user