/* 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 #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"; #if CONFIG_IDF_TARGET_ESP32 #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> 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() { 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() { // Initialize touch pad peripheral, it will start a timer to run a filter ESP_LOGI(TAG, "Initializing touch pad"); 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); } #elif CONFIG_IDF_TARGET_ESP32S2BETA 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 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 - baseline) > baseline * threshold, the pad be actived. * If (raw_data - baseline) < baseline * threshold, the pad be inactived. */ 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_get_int_status(); evt.pad_status = touch_pad_get_status(); evt.pad_num = touch_pad_get_scan_curr(); if(evt.intr_mask & TOUCH_PAD_INTR_MASK_DONE) { touch_pad_filter_baseline_read(evt.pad_num, &evt.pad_val); } 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