add touch element deep sleep example and auto sleep example

This commit is contained in:
Kang Zuoling 2021-05-28 17:42:38 +08:00 committed by laokaiyao
parent 82bf6c0935
commit 0f1eb82acd
11 changed files with 262 additions and 61 deletions

View File

@ -380,7 +380,9 @@ static void te_intr_cb(void *arg)
if (touch_trig_diff & 0x1) {
if (touch_trig_status & BIT(pad_num)) {
if (s_te_obj->sleep_handle != NULL) {
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock);
#endif
}
te_intr_msg.channel_state = TE_STATE_PRESS;
te_intr_msg.intr_type = TE_INTR_PRESS;
@ -444,7 +446,9 @@ static void te_proc_timer_cb(void *arg)
te_object_update_state(te_intr_msg);
if (te_intr_msg.intr_type == TE_INTR_RELEASE) {
if (s_te_obj->sleep_handle != NULL) {
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock);
#endif
}
}
} else if (te_intr_msg.intr_type == TE_INTR_SCAN_DONE) {
@ -453,7 +457,9 @@ static void te_proc_timer_cb(void *arg)
te_object_set_threshold(); //TODO: add set threshold error processing
ESP_LOGD(TE_DEBUG_TAG, "Set threshold");
if (s_te_obj->sleep_handle != NULL) {
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock);
#endif
}
}
if (waterproof_check_state()) {
@ -1009,10 +1015,12 @@ esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config)
s_te_obj->sleep_handle->non_volatile_threshold = threshold_shadow;
#endif
#ifdef CONFIG_PM_ENABLE
ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock);
TE_CHECK_GOTO(ret == ESP_OK, cleanup);
ret = esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock);
TE_CHECK_GOTO(ret == ESP_OK, cleanup);
#endif
return ESP_OK;
cleanup:
@ -1030,10 +1038,12 @@ void touch_element_sleep_uninstall(void)
{
esp_err_t ret;
if (s_te_obj->sleep_handle->pm_lock != NULL) {
#ifdef CONFIG_PM_ENABLE
ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock);
if (ret != ESP_OK) {
abort();
}
#endif
}
if (s_te_obj->sleep_handle->wakeup_handle != NULL) {
te_button_handle_t button_handle = s_te_obj->sleep_handle->wakeup_handle;

View File

@ -1,2 +0,0 @@
idf_component_register(SRCS "touch_element_sleep.c"
INCLUDE_DIRS ".")

View File

@ -1,15 +0,0 @@
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

View File

@ -5,4 +5,4 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(touch_element_sleep)
project(touch_elem_auto_sleep)

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "example_main.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,17 @@
menu "Example Configuration"
config TE_PM_ENABLE
bool "Touch Element power management"
default y
config TE_MAX_CPU_FREQ
int "Touch Element sleep DFS maximum cpu frequency"
depends on PM_DFS_INIT_AUTO
default ESP32S2_DEFAULT_CPU_FREQ_MHZ
config TE_MIN_CPU_FREQ
int "Touch Element sleep DFS minimum cpu frequency"
depends on PM_DFS_INIT_AUTO
default 10
endmenu

View File

@ -14,14 +14,12 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "touch_element/touch_button.h"
#include "esp_log.h"
#include "esp_sleep.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_pm.h"
#include "touch_element/touch_button.h"
static const char *TAG = "Touch elem auto sleep";
static const char *TAG = "Touch Button Example";
#define TOUCH_BUTTON_NUM 5
/* Touch buttons handle */
@ -45,7 +43,6 @@ static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
0.03F,
};
#ifdef CONFIG_TOUCH_ELEM_EVENT
/* Button event handler task */
static void button_handler_task(void *arg)
{
@ -68,34 +65,24 @@ static void button_handler_task(void *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);
if (out_handle == button_handle[0]) {
// esp_deep_sleep_start();
} else if (out_handle == button_handle[1]) {
esp_deep_sleep_start();
}
} 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)
{
// esp_pm_config_esp32s2_t pm_config = {
// .max_freq_mhz = 160,
// .min_freq_mhz = 160,
// .light_sleep_enable = true
// };
// ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
#ifdef CONFIG_PM_ENABLE //System power management
esp_pm_config_esp32s2_t pm_config = {
#ifdef CONFIG_PM_DFS_INIT_AUTO //Power management dynamic frequency scaling
.max_freq_mhz = CONFIG_TE_MAX_CPU_FREQ,
.min_freq_mhz = CONFIG_TE_MIN_CPU_FREQ,
#endif
#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE //FreeRTOS tickless
.light_sleep_enable = true
#else
.light_sleep_enable = false
#endif
};
ESP_ERROR_CHECK( esp_pm_configure(&pm_config));
#endif
/* Initialize Touch Element library */
touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
ESP_ERROR_CHECK(touch_element_install(&global_config));
@ -114,30 +101,23 @@ void app_main(void)
/* 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], 1000));
}
ESP_LOGI(TAG, "Touch buttons created");
#ifdef CONFIG_TE_PM_ENABLE
touch_elem_sleep_config_t sleep_config = {
.scan_time = global_config.hardware.sample_count,
.sleep_time = global_config.hardware.sleep_cycle,
};
ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config));
ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(button_handle[0]));
ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(button_handle[0], true));
touch_pad_sleep_channel_t sleep_channel_info;
touch_pad_sleep_channel_get_info(&sleep_channel_info);
printf("----------%d\n", sleep_channel_info.touch_num);
#endif
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
vTaskDelay(pdMS_TO_TICKS(1000));
}

View File

@ -0,0 +1,8 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five 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_elem_deep_sleep)

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "touch_elem_deep_sleep.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,69 @@
menu "Example Configuration"
choice
prompt "Select a button as Deep Sleep wakeup source"
default TE_WAKEUP_USE_BUTTON_1
config TE_WAKEUP_USE_BUTTON_1
bool "Button 1"
config TE_WAKEUP_USE_BUTTON_2
bool "Button 2"
config TE_WAKEUP_USE_BUTTON_3
bool "Button 3"
config TE_WAKEUP_USE_BUTTON_4
bool "Button 4"
config TE_WAKEUP_USE_BUTTON_5
bool "Button 5"
endchoice
config TE_WAKEUP_BUTTON_INDEX
int
default 0 if TE_WAKEUP_USE_BUTTON_1
default 1 if TE_WAKEUP_USE_BUTTON_2
default 2 if TE_WAKEUP_USE_BUTTON_3
default 3 if TE_WAKEUP_USE_BUTTON_4
default 4 if TE_WAKEUP_USE_BUTTON_5
choice
prompt "Select a button as Deep Sleep entry"
default TE_ENTRY_USE_BUTTON_1
config TE_ENTRY_USE_BUTTON_1
bool "Button 1"
config TE_ENTRY_USE_BUTTON_2
bool "Button 2"
config TE_ENTRY_USE_BUTTON_3
bool "Button 3"
config TE_ENTRY_USE_BUTTON_4
bool "Button 4"
config TE_ENTRY_USE_BUTTON_5
bool "Button 5"
endchoice
config TE_ENTRY_BUTTON_INDEX
int
default 0 if TE_ENTRY_USE_BUTTON_1
default 1 if TE_ENTRY_USE_BUTTON_2
default 2 if TE_ENTRY_USE_BUTTON_3
default 3 if TE_ENTRY_USE_BUTTON_4
default 4 if TE_ENTRY_USE_BUTTON_5
config TE_SKIP_CALIBRATION
bool "Touch Element skip wakeup calibration"
depends on TE_SKIP_DSLEEP_WAKEUP_CALIBRATION
default y
endmenu

View File

@ -0,0 +1,130 @@
/* 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 "esp_log.h"
#include "esp_sleep.h"
#include "touch_element/touch_button.h"
static const char *TAG = "Touch Button Example";
#define TOUCH_BUTTON_NUM 5
/* Touch buttons handle */
static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM];
static touch_button_handle_t wakeup_button_handle = NULL;
static touch_button_handle_t entry_button_handle = NULL;
/* 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 buttons channel sensitivity array */
static const float channel_sens_array[TOUCH_BUTTON_NUM] = {
0.03F,
0.03F,
0.03F,
0.03F,
0.03F,
};
/* 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 (element_message.handle == entry_button_handle) {
if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) {
ESP_LOGI(TAG, "Entering Deep sleep ...");
fflush(stdout);
esp_deep_sleep_start();
}
}
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)
{
esp_sleep_wakeup_cause_t wakeup_ret = esp_sleep_get_wakeup_cause();
printf("------%d\n", wakeup_ret);
/* 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) */
if (i == CONFIG_TE_WAKEUP_BUTTON_INDEX || i == CONFIG_TE_ENTRY_BUTTON_INDEX) {
continue;
} else {
ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE, (void *)channel_array[i]));
}
/* Set EVENT as the dispatch method */
ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT));
}
wakeup_button_handle = button_handle[CONFIG_TE_WAKEUP_BUTTON_INDEX];
entry_button_handle = button_handle[CONFIG_TE_ENTRY_BUTTON_INDEX];
ESP_ERROR_CHECK(touch_button_subscribe_event(entry_button_handle, TOUCH_ELEM_EVENT_ON_PRESS, (void *)channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX]));
ESP_ERROR_CHECK(touch_button_set_dispatch_method(entry_button_handle, TOUCH_ELEM_DISP_EVENT));
ESP_LOGI(TAG, "Touch buttons created");
touch_elem_sleep_config_t sleep_config = {
.scan_time = global_config.hardware.sample_count,
.sleep_time = global_config.hardware.sleep_cycle,
};
ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config));
ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(wakeup_button_handle));
#ifdef CONFIG_TE_SKIP_CALIBRATION
ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(wakeup_button_handle, true));
#endif
touch_element_start();
ESP_LOGI(TAG, "Touch element library start");
xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL);
ESP_LOGI(TAG, "Press Button[%d] to enter sleep and press Button[%d] to wakeup system", channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX], channel_array[CONFIG_TE_WAKEUP_BUTTON_INDEX]);
}