1 /* Touch Pad Read Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "driver/touch_pad.h"
13 #include "esp_log.h"
14
15 #define TOUCH_BUTTON_NUM 14
16 #define TOUCH_CHANGE_CONFIG 0
17
18 static const char *TAG = "touch read";
19 static const touch_pad_t button[TOUCH_BUTTON_NUM] = {
20 TOUCH_PAD_NUM1,
21 TOUCH_PAD_NUM2,
22 TOUCH_PAD_NUM3,
23 TOUCH_PAD_NUM4,
24 TOUCH_PAD_NUM5,
25 TOUCH_PAD_NUM6,
26 TOUCH_PAD_NUM7,
27 TOUCH_PAD_NUM8,
28 TOUCH_PAD_NUM9,
29 TOUCH_PAD_NUM10,
30 TOUCH_PAD_NUM11,
31 TOUCH_PAD_NUM12,
32 TOUCH_PAD_NUM13,
33 TOUCH_PAD_NUM14
34 };
35
36 /*
37 Read values sensed at all available touch pads.
38 Print out values in a loop on a serial monitor.
39 */
tp_example_read_task(void * pvParameter)40 static void tp_example_read_task(void *pvParameter)
41 {
42 uint32_t touch_value;
43
44 /* Wait touch sensor init done */
45 vTaskDelay(100 / portTICK_RATE_MS);
46 printf("Touch Sensor read, the output format is: \nTouchpad num:[raw data]\n\n");
47
48 while (1) {
49 for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
50 touch_pad_read_raw_data(button[i], &touch_value); // read raw data.
51 printf("T%d: [%4d] ", button[i], touch_value);
52 }
53 printf("\n");
54 vTaskDelay(200 / portTICK_PERIOD_MS);
55 }
56 }
57
app_main(void)58 void app_main(void)
59 {
60 /* Initialize touch pad peripheral. */
61 touch_pad_init();
62 for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
63 touch_pad_config(button[i]);
64 }
65 #if TOUCH_CHANGE_CONFIG
66 /* If you want change the touch sensor default setting, please write here(after initialize). There are examples: */
67 touch_pad_set_meas_time(TOUCH_PAD_SLEEP_CYCLE_DEFAULT, TOUCH_PAD_SLEEP_CYCLE_DEFAULT);
68 touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
69 touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
70 for (int i = 0; i < TOUCH_BUTTON_NUM; i++) {
71 touch_pad_set_cnt_mode(i, TOUCH_PAD_SLOPE_DEFAULT, TOUCH_PAD_TIE_OPT_DEFAULT);
72 }
73 #endif
74 /* Denoise setting at TouchSensor 0. */
75 touch_pad_denoise_t denoise = {
76 /* The bits to be cancelled are determined according to the noise level. */
77 .grade = TOUCH_PAD_DENOISE_BIT4,
78 .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
79 };
80 touch_pad_denoise_set_config(&denoise);
81 touch_pad_denoise_enable();
82 ESP_LOGI(TAG, "Denoise function init");
83
84 /* Enable touch sensor clock. Work mode is "timer trigger". */
85 touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
86 touch_pad_fsm_start();
87
88 /* Start task to read values by pads. */
89 xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
90 }
91