From 01dc3cb346bdee4a6d6fdf23e16398307f5833be Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Wed, 25 Dec 2024 16:57:15 +0800 Subject: [PATCH] example: Add user active mode trigger button for icd example Closes CON-1486 --- examples/icd_app/CMakeLists.txt | 15 +------ examples/icd_app/main/Kconfig.projbuild | 19 ++++++++ examples/icd_app/main/app_driver.cpp | 45 +++++++++++++++++++ examples/icd_app/main/app_main.cpp | 3 ++ examples/icd_app/main/app_priv.h | 4 ++ examples/icd_app/main/idf_component.yml | 3 ++ examples/icd_app/sdkconfig.defaults | 3 ++ examples/icd_app/sdkconfig.defaults.esp32c6 | 1 + .../icd_app/sdkconfig.defaults.esp32c6.lit | 3 ++ .../icd_app/sdkconfig.defaults.esp32h2.lit | 3 ++ 10 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 examples/icd_app/main/Kconfig.projbuild create mode 100644 examples/icd_app/main/app_driver.cpp create mode 100644 examples/icd_app/main/idf_component.yml diff --git a/examples/icd_app/CMakeLists.txt b/examples/icd_app/CMakeLists.txt index 784b9b4da..0a6b3d4ee 100644 --- a/examples/icd_app/CMakeLists.txt +++ b/examples/icd_app/CMakeLists.txt @@ -6,16 +6,6 @@ if(NOT DEFINED ENV{ESP_MATTER_PATH}) message(FATAL_ERROR "Please set ESP_MATTER_PATH to the path of esp-matter repo") endif(NOT DEFINED ENV{ESP_MATTER_PATH}) -if(NOT DEFINED ENV{ESP_MATTER_DEVICE_PATH}) - if("${IDF_TARGET}" STREQUAL "esp32h2") - set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32h2_devkit_c) - elseif("${IDF_TARGET}" STREQUAL "esp32c6") - set(ENV{ESP_MATTER_DEVICE_PATH} $ENV{ESP_MATTER_PATH}/device_hal/device/esp32c6_devkit_c) - else() - message(FATAL_ERROR "Unsupported IDF_TARGET") - endif() -endif(NOT DEFINED ENV{ESP_MATTER_DEVICE_PATH}) - set(PROJECT_VER "1.0") set(PROJECT_VER_NUMBER 1) @@ -24,14 +14,11 @@ set(MATTER_SDK_PATH ${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip) # This should be done before using the IDF_TARGET variable. include($ENV{IDF_PATH}/tools/cmake/project.cmake) -include($ENV{ESP_MATTER_DEVICE_PATH}/esp_matter_device.cmake) set(EXTRA_COMPONENT_DIRS "${ESP_MATTER_PATH}/examples/common" "${MATTER_SDK_PATH}/config/esp32/components" - "${ESP_MATTER_PATH}/components" - "${ESP_MATTER_PATH}/device_hal/device" - ${extra_components_dirs_append}) + "${ESP_MATTER_PATH}/components") project(icd_app) diff --git a/examples/icd_app/main/Kconfig.projbuild b/examples/icd_app/main/Kconfig.projbuild new file mode 100644 index 000000000..31f30ff58 --- /dev/null +++ b/examples/icd_app/main/Kconfig.projbuild @@ -0,0 +1,19 @@ +menu "Example Configuration" + config ENABLE_USER_ACTIVE_MODE_TRIGGER_BUTTON + bool "Enable User Active Mode Trigger Button" + default y + help + Enable a button to trigger user active mode, after click this button, the device will keep staying + active for ActiveModeDuration. + + config USER_ACTIVE_MODE_TRIGGER_BUTTON_PIN + int "User Active Mode Trigger Button Pin" + range 9 14 if IDF_TARGET_ESP32H2 + range 0 7 if IDF_TARGET_ESP32C6 + default 9 if IDF_TARGET_ESP32H2 + default 7 if IDF_TARGET_ESP32C6 + help + GPIO number of the active mode trigger button. Note that the boot button of ESP32-C6 DevKits is + GPIO9 which cannot be used to wake up the chip. + +endmenu diff --git a/examples/icd_app/main/app_driver.cpp b/examples/icd_app/main/app_driver.cpp new file mode 100644 index 000000000..cd3ea1cde --- /dev/null +++ b/examples/icd_app/main/app_driver.cpp @@ -0,0 +1,45 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#include + +#include + +#ifdef CONFIG_ENABLE_USER_ACTIVE_MODE_TRIGGER_BUTTON +using namespace chip::app::Clusters; +using namespace esp_matter; + +static constexpr char *TAG = "app_driver"; + +static void app_driver_button_toggle_cb(void *arg, void *data) +{ + // The device will stay active mode for Active Mode Threshold + chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { + chip::app::ICDNotifier::GetInstance().NotifyNetworkActivityNotification(); + }); +} + +app_driver_handle_t app_driver_button_init() +{ + /* Initialize button */ + button_config_t config; + memset(&config, 0, sizeof(button_config_t)); + config.type = BUTTON_TYPE_GPIO; + config.gpio_button_config.gpio_num = CONFIG_USER_ACTIVE_MODE_TRIGGER_BUTTON_PIN; + config.gpio_button_config.active_level = 0; + config.gpio_button_config.enable_power_save = true; + button_handle_t handle = iot_button_create(&config); + + iot_button_register_cb(handle, BUTTON_PRESS_DOWN, app_driver_button_toggle_cb, NULL); + return (app_driver_handle_t)handle; +} + +#endif // CONFIG_ENABLE_USER_ACTIVE_MODE_TRIGGER_BUTTON diff --git a/examples/icd_app/main/app_main.cpp b/examples/icd_app/main/app_main.cpp index 6cf326325..87e030075 100644 --- a/examples/icd_app/main/app_main.cpp +++ b/examples/icd_app/main/app_main.cpp @@ -139,6 +139,9 @@ extern "C" void app_main() #endif }; err = esp_pm_configure(&pm_config); +#endif +#ifdef CONFIG_ENABLE_USER_ACTIVE_MODE_TRIGGER_BUTTON + app_driver_button_init(); #endif /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; diff --git a/examples/icd_app/main/app_priv.h b/examples/icd_app/main/app_priv.h index c23600252..8d29114a9 100644 --- a/examples/icd_app/main/app_priv.h +++ b/examples/icd_app/main/app_priv.h @@ -11,6 +11,10 @@ #include #include +typedef void *app_driver_handle_t; + +app_driver_handle_t app_driver_button_init(); + #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include "esp_openthread_types.h" #endif diff --git a/examples/icd_app/main/idf_component.yml b/examples/icd_app/main/idf_component.yml new file mode 100644 index 000000000..dedd65dc8 --- /dev/null +++ b/examples/icd_app/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + espressif/button: + version: "^3.4.0" diff --git a/examples/icd_app/sdkconfig.defaults b/examples/icd_app/sdkconfig.defaults index c8b838393..ace5410a0 100644 --- a/examples/icd_app/sdkconfig.defaults +++ b/examples/icd_app/sdkconfig.defaults @@ -84,3 +84,6 @@ CONFIG_ICD_FAST_POLL_INTERVAL_MS=500 CONFIG_ICD_IDLE_MODE_INTERVAL_SEC=60 CONFIG_ICD_ACTIVE_MODE_INTERVAL_MS=1000 CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS=1000 + +# Enable power save for the button +CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE=y diff --git a/examples/icd_app/sdkconfig.defaults.esp32c6 b/examples/icd_app/sdkconfig.defaults.esp32c6 index 4f30ba6d2..9e3caa272 100644 --- a/examples/icd_app/sdkconfig.defaults.esp32c6 +++ b/examples/icd_app/sdkconfig.defaults.esp32c6 @@ -1,3 +1,4 @@ CONFIG_IDF_TARGET="esp32c6" CONFIG_ESP_PHY_MAC_BB_PD=y + diff --git a/examples/icd_app/sdkconfig.defaults.esp32c6.lit b/examples/icd_app/sdkconfig.defaults.esp32c6.lit index 4b16619f8..5118ed8a4 100644 --- a/examples/icd_app/sdkconfig.defaults.esp32c6.lit +++ b/examples/icd_app/sdkconfig.defaults.esp32c6.lit @@ -64,3 +64,6 @@ CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS=5000 CONFIG_ENABLE_ICD_LIT=y CONFIG_ICD_CLIENTS_SUPPORTED_PER_FABRIC=2 CONFIG_ICD_MAX_NOTIFICATION_SUBSCRIBERS=2 + +# Enable power save for the button +CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE=y diff --git a/examples/icd_app/sdkconfig.defaults.esp32h2.lit b/examples/icd_app/sdkconfig.defaults.esp32h2.lit index 3fcb84fe6..72d68960e 100644 --- a/examples/icd_app/sdkconfig.defaults.esp32h2.lit +++ b/examples/icd_app/sdkconfig.defaults.esp32h2.lit @@ -63,3 +63,6 @@ CONFIG_ICD_MAX_NOTIFICATION_SUBSCRIBERS=2 # Disable WiFi STA CONFIG_ENABLE_WIFI_STATION=n + +# Enable power save for the button +CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE=y