/* * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "esp_pm.h" #include "iot_button.h" #include "esp_sleep.h" #include "esp_idf_version.h" #include "button_gpio.h" /* Most development boards have "boot" button attached to GPIO0. * You can also change this to another pin. */ #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C6 #define BOOT_BUTTON_NUM 9 #else #define BOOT_BUTTON_NUM 0 #endif #define BUTTON_ACTIVE_LEVEL 0 static const char *TAG = "button_power_save"; static void button_event_cb(void *arg, void *data) { iot_button_print_event((button_handle_t)arg); esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); if (cause != ESP_SLEEP_WAKEUP_UNDEFINED) { ESP_LOGI(TAG, "Wake up from light sleep, reason %d", cause); } } #if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY void button_enter_power_save(void *usr_data) { ESP_LOGI(TAG, "Can enter power save now"); esp_light_sleep_start(); } #endif void button_init(uint32_t button_num) { button_config_t btn_cfg = {0}; button_gpio_config_t gpio_cfg = { .gpio_num = button_num, .active_level = BUTTON_ACTIVE_LEVEL, .enable_power_save = true, }; button_handle_t btn; esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &gpio_cfg, &btn); assert(ret == ESP_OK); ret = iot_button_register_cb(btn, BUTTON_PRESS_DOWN, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_PRESS_UP, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_event_cb, NULL); ret |= iot_button_register_cb(btn, BUTTON_PRESS_END, NULL, button_event_cb, NULL); #if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY /*!< For enter Power Save */ button_power_save_config_t config = { .enter_power_save_cb = button_enter_power_save, }; ret |= iot_button_register_power_save_cb(&config); #endif ESP_ERROR_CHECK(ret); } #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) void power_save_init(void) { esp_pm_config_t pm_config = { .max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ, .min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ, #if CONFIG_FREERTOS_USE_TICKLESS_IDLE .light_sleep_enable = true #endif }; ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); } #else void power_save_init(void) { #if CONFIG_IDF_TARGET_ESP32 esp_pm_config_esp32_t pm_config = { #elif CONFIG_IDF_TARGET_ESP32S2 esp_pm_config_esp32s2_t pm_config = { #elif CONFIG_IDF_TARGET_ESP32C3 esp_pm_config_esp32c3_t pm_config = { #elif CONFIG_IDF_TARGET_ESP32S3 esp_pm_config_esp32s3_t pm_config = { #elif CONFIG_IDF_TARGET_ESP32C2 esp_pm_config_esp32c2_t pm_config = { #endif .max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ, .min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ, #if CONFIG_FREERTOS_USE_TICKLESS_IDLE .light_sleep_enable = true #endif }; ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); } #endif void app_main(void) { button_init(BOOT_BUTTON_NUM); #if CONFIG_ENTER_LIGHT_SLEEP_AUTO power_save_init(); #else esp_light_sleep_start(); #endif }