diff --git a/examples/generic_switch/main/app_driver.cpp b/examples/generic_switch/main/app_driver.cpp index 22876b341..803d5cabf 100644 --- a/examples/generic_switch/main/app_driver.cpp +++ b/examples/generic_switch/main/app_driver.cpp @@ -10,11 +10,17 @@ #include #include -#include "bsp/esp-bsp.h" #include #include #include +#include + +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S3 +#define BUTTON_GPIO_PIN GPIO_NUM_0 +#else // CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2 +#define BUTTON_GPIO_PIN GPIO_NUM_9 +#endif using namespace chip::app::Clusters; using namespace esp_matter; @@ -37,11 +43,11 @@ static void app_driver_button_switch_latched(void *arg, void *data) int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; // Press moves Position from 0 (idle) to 1 (press) uint8_t newPosition = 1; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // SwitchLatched event takes newPosition as event data - switch_cluster::event::send_switch_latched(switch_endpoint_id, newPosition); - lock::chip_stack_unlock(); + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // SwitchLatched event takes newPosition as event data + switch_cluster::event::send_switch_latched(switch_endpoint_id, newPosition); + }); } #endif #if CONFIG_GENERIC_SWITCH_TYPE_MOMENTARY @@ -52,38 +58,39 @@ static void app_driver_button_initial_pressed(void *arg, void *data) int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; // Press moves Position from 0 (idle) to 1 (press) uint8_t newPosition = 1; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // InitialPress event takes newPosition as event data - switch_cluster::event::send_initial_press(switch_endpoint_id, newPosition); - lock::chip_stack_unlock(); + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // InitialPress event takes newPosition as event data + switch_cluster::event::send_initial_press(switch_endpoint_id, newPosition); + }); } static void app_driver_button_release(void *arg, void *data) { + gpio_button *button = (gpio_button *)data; int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; - if(iot_button_get_ticks_time((button_handle_t)arg) < 5000){ - ESP_LOGI(TAG, "Short button release"); - // Release moves Position from 1 (press) to 0 (idle) - uint8_t previousPosition = 1; - uint8_t newPosition = 0; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // ShortRelease event takes previousPosition as event data - switch_cluster::event::send_short_release(switch_endpoint_id, previousPosition); - lock::chip_stack_unlock(); - } - else{ - ESP_LOGI(TAG, "Long button release"); - // Release moves Position from 1 (press) to 0 (idle) - uint8_t previousPosition = 1; - uint8_t newPosition = 0; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // LongRelease event takes previousPositionPosition as event data - switch_cluster::event::send_long_release(switch_endpoint_id, previousPosition); - lock::chip_stack_unlock(); + + if (iot_button_get_ticks_time((button_handle_t)arg) < 5000) { + ESP_LOGI(TAG, "Short button release"); + // Release moves Position from 1 (press) to 0 (idle) + uint8_t previousPosition = 1; + uint8_t newPosition = 0; + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, previousPosition, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // ShortRelease event takes previousPosition as event data + switch_cluster::event::send_short_release(switch_endpoint_id, previousPosition); + }); + } else { + ESP_LOGI(TAG, "Long button release"); + // Release moves Position from 1 (press) to 0 (idle) + uint8_t previousPosition = 1; + uint8_t newPosition = 0; + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, previousPosition, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // LongRelease event takes previousPositionPosition as event data + switch_cluster::event::send_long_release(switch_endpoint_id, previousPosition); + }); } } @@ -94,11 +101,11 @@ static void app_driver_button_long_pressed(void *arg, void *data) int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; // Press moves Position from 0 (idle) to 1 (press) uint8_t newPosition = 1; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // LongPress event takes newPosition as event data - switch_cluster::event::send_long_press(switch_endpoint_id, newPosition); - lock::chip_stack_unlock(); + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // LongPress event takes newPosition as event data + switch_cluster::event::send_long_press(switch_endpoint_id, newPosition); + }); } static int current_number_of_presses_counted = 1; @@ -111,11 +118,11 @@ static void app_driver_button_multipress_ongoing(void *arg, void *data) // Press moves Position from 0 (idle) to 1 (press) uint8_t newPosition = 1; current_number_of_presses_counted++; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // MultiPress Ongoing event takes newPosition and current_number_of_presses_counted as event data - switch_cluster::event::send_multi_press_ongoing(switch_endpoint_id, newPosition, current_number_of_presses_counted); - lock::chip_stack_unlock(); + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // MultiPress Ongoing event takes newPosition and current_number_of_presses_counted as event data + switch_cluster::event::send_multi_press_ongoing(switch_endpoint_id, newPosition, current_number_of_presses_counted); + }); } static void app_driver_button_multipress_complete(void *arg, void *data) @@ -126,33 +133,44 @@ static void app_driver_button_multipress_complete(void *arg, void *data) // Press moves Position from 0 (idle) to 1 (press) uint8_t previousPosition = 1; uint8_t newPosition = 0; - int total_number_of_presses_counted = current_number_of_presses_counted; - lock::chip_stack_lock(portMAX_DELAY); - chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); - // MultiPress Complete event takes previousPosition and total_number_of_presses_counted as event data - switch_cluster::event::send_multi_press_complete(switch_endpoint_id, previousPosition, total_number_of_presses_counted); - // Reset current_number_of_presses_counted to initial value - current_number_of_presses_counted = 1; - lock::chip_stack_unlock(); + static int total_number_of_presses_counted = current_number_of_presses_counted; + chip::DeviceLayer::SystemLayer().ScheduleLambda([switch_endpoint_id, previousPosition, newPosition]() { + chip::app::Clusters::Switch::Attributes::CurrentPosition::Set(switch_endpoint_id, newPosition); + // MultiPress Complete event takes previousPosition and total_number_of_presses_counted as event data + switch_cluster::event::send_multi_press_complete(switch_endpoint_id, previousPosition, total_number_of_presses_counted); + // Reset current_number_of_presses_counted to initial value + current_number_of_presses_counted = 1; + }); } #endif app_driver_handle_t app_driver_button_init(gpio_button * button) { - /* Initialize button */ - button_handle_t btns[BSP_BUTTON_NUM]; - ESP_ERROR_CHECK(bsp_iot_button_create(btns, NULL, BSP_BUTTON_NUM)); + button_config_t config = { + .type = BUTTON_TYPE_GPIO, + .gpio_button_config = { + .gpio_num = BUTTON_GPIO_PIN, + .active_level = 0, + } + }; + + if (button != NULL) { + config.type = button_type_t::BUTTON_TYPE_GPIO; + config.gpio_button_config.gpio_num = button->GPIO_PIN_VALUE; + } + button_handle_t handle = iot_button_create(&config); + #if CONFIG_GENERIC_SWITCH_TYPE_LATCHING - iot_button_register_cb(btns[0], BUTTON_DOUBLE_CLICK, app_driver_button_switch_latched, button); + iot_button_register_cb(handle, BUTTON_DOUBLE_CLICK, app_driver_button_switch_latched, button); #endif #if CONFIG_GENERIC_SWITCH_TYPE_MOMENTARY - iot_button_register_cb(btns[0], BUTTON_PRESS_DOWN, app_driver_button_initial_pressed, button); - iot_button_register_cb(btns[0], BUTTON_PRESS_UP, app_driver_button_release, button); - iot_button_register_cb(btns[0], BUTTON_LONG_PRESS_START, app_driver_button_long_pressed, button); - iot_button_register_cb(btns[0], BUTTON_PRESS_REPEAT, app_driver_button_multipress_ongoing, button); - iot_button_register_cb(btns[0], BUTTON_PRESS_REPEAT_DONE, app_driver_button_multipress_complete, button); + iot_button_register_cb(handle, BUTTON_PRESS_DOWN, app_driver_button_initial_pressed, button); + iot_button_register_cb(handle, BUTTON_PRESS_UP, app_driver_button_release, button); + iot_button_register_cb(handle, BUTTON_LONG_PRESS_START, app_driver_button_long_pressed, button); + iot_button_register_cb(handle, BUTTON_PRESS_REPEAT, app_driver_button_multipress_ongoing, button); + iot_button_register_cb(handle, BUTTON_PRESS_REPEAT_DONE, app_driver_button_multipress_complete, button); #endif - return (app_driver_handle_t)btns[0]; + return (app_driver_handle_t)handle; } diff --git a/examples/generic_switch/main/app_main.cpp b/examples/generic_switch/main/app_main.cpp index 9b92a8975..38dfe871b 100644 --- a/examples/generic_switch/main/app_main.cpp +++ b/examples/generic_switch/main/app_main.cpp @@ -54,10 +54,10 @@ constexpr const uint8_t kTagPositionLeft = 0; // Common Position Namespace: 8, tag: 1 (Right) constexpr const uint8_t kTagPositionRight = 1; -const Descriptor::Structs::SemanticTagStruct::Type gEp0TagList[] = { +const Descriptor::Structs::SemanticTagStruct::Type gEp1TagList[] = { {.namespaceID = kNamespaceSwitches, .tag = kTagSwitchOn}, {.namespaceID = kNamespacePosition, .tag = kTagPositionRight}}; -const Descriptor::Structs::SemanticTagStruct::Type gEp1TagList[] = { +const Descriptor::Structs::SemanticTagStruct::Type gEp2TagList[] = { {.namespaceID = kNamespaceSwitches, .tag = kTagSwitchOff}, {.namespaceID = kNamespacePosition, .tag = kTagPositionLeft}}; @@ -241,8 +241,8 @@ extern "C" void app_main() enable_insights(insights_auth_key_start); #endif - SetTagList(0, chip::Span(gEp0TagList)); SetTagList(1, chip::Span(gEp1TagList)); + SetTagList(2, chip::Span(gEp2TagList)); nvs_handle_t handle; nvs_open_from_partition(CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL, "chip-factory", NVS_READWRITE, &handle); diff --git a/examples/generic_switch/main/idf_component.yml b/examples/generic_switch/main/idf_component.yml index 547b39ec2..8a96dc5f6 100644 --- a/examples/generic_switch/main/idf_component.yml +++ b/examples/generic_switch/main/idf_component.yml @@ -4,5 +4,5 @@ dependencies: rules: # will add "optional_component" only when all if clauses are True - if: "idf_version >=5.0" - if: "target in [esp32c2]" - esp_bsp_generic: - version: "^1.1.0" + + espressif/button: "=2.5.0" diff --git a/examples/generic_switch/sdkconfig.defaults b/examples/generic_switch/sdkconfig.defaults index a40ff0a4a..4baf067bd 100644 --- a/examples/generic_switch/sdkconfig.defaults +++ b/examples/generic_switch/sdkconfig.defaults @@ -52,10 +52,3 @@ CONFIG_MBEDTLS_HKDF_C=y # Increase LwIP IPv6 address number to 6 (MAX_FABRIC + 1) # unique local addresses for fabrics(MAX_FABRIC), a link local address(1) CONFIG_LWIP_IPV6_NUM_ADDRESSES=6 - -# ESP32-DevKitC Settings -# Buttons -CONFIG_BSP_BUTTONS_NUM=1 -CONFIG_BSP_BUTTON_1_TYPE_GPIO=y -CONFIG_BSP_BUTTON_1_GPIO=0 -CONFIG_BSP_BUTTON_1_LEVEL=0 diff --git a/examples/generic_switch/sdkconfig.defaults.esp32c2 b/examples/generic_switch/sdkconfig.defaults.esp32c2 index c3041e3e7..2070d38fa 100644 --- a/examples/generic_switch/sdkconfig.defaults.esp32c2 +++ b/examples/generic_switch/sdkconfig.defaults.esp32c2 @@ -164,10 +164,3 @@ CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE=256 # ESP Matter CONFIG_ESP_MATTER_MAX_DEVICE_TYPE_COUNT=4 CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=4 - -# ESP32-C2-DevKitM-1 Settings -# Buttons -CONFIG_BSP_BUTTONS_NUM=1 -CONFIG_BSP_BUTTON_1_TYPE_GPIO=y -CONFIG_BSP_BUTTON_1_GPIO=0 -CONFIG_BSP_BUTTON_1_LEVEL=0 diff --git a/examples/generic_switch/sdkconfig.defaults.esp32c6 b/examples/generic_switch/sdkconfig.defaults.esp32c6 index 8c6594280..42508614a 100644 --- a/examples/generic_switch/sdkconfig.defaults.esp32c6 +++ b/examples/generic_switch/sdkconfig.defaults.esp32c6 @@ -38,9 +38,3 @@ CONFIG_BUTTON_LONG_PRESS_TIME_MS=5000 # Enable chip shell CONFIG_ENABLE_CHIP_SHELL=y -# ESP32-C6-DevKitM-1 Settings -# Buttons -CONFIG_BSP_BUTTONS_NUM=1 -CONFIG_BSP_BUTTON_1_TYPE_GPIO=y -CONFIG_BSP_BUTTON_1_GPIO=9 -CONFIG_BSP_BUTTON_1_LEVEL=0 diff --git a/examples/generic_switch/sdkconfig.defaults.esp32h2 b/examples/generic_switch/sdkconfig.defaults.esp32h2 index eae225eb3..ecf5f60f4 100644 --- a/examples/generic_switch/sdkconfig.defaults.esp32h2 +++ b/examples/generic_switch/sdkconfig.defaults.esp32h2 @@ -54,13 +54,6 @@ CONFIG_ENABLE_CHIP_SHELL=y # Enable DS Peripheral CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=y -# ESP32-H2-DevKitM-1 Settings -# Buttons -CONFIG_BSP_BUTTONS_NUM=1 -CONFIG_BSP_BUTTON_1_TYPE_GPIO=y -CONFIG_BSP_BUTTON_1_GPIO=9 -CONFIG_BSP_BUTTON_1_LEVEL=0 - # Disable persist subscriptions CONFIG_ENABLE_PERSIST_SUBSCRIPTIONS=n @@ -69,3 +62,4 @@ CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL_FOR_THREAD=5000 CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL_FOR_THREAD=5000 CONFIG_MRP_RETRY_INTERVAL_SENDER_BOOST_FOR_THREAD=5000 CONFIG_MRP_MAX_RETRANS=3 + diff --git a/examples/generic_switch/sdkconfig.defaults.esp32s3 b/examples/generic_switch/sdkconfig.defaults.esp32s3 index 3c908d0db..be528bc12 100644 --- a/examples/generic_switch/sdkconfig.defaults.esp32s3 +++ b/examples/generic_switch/sdkconfig.defaults.esp32s3 @@ -31,9 +31,3 @@ CONFIG_BUTTON_LONG_PRESS_TIME_MS=5000 # Enable HKDF in mbedtls CONFIG_MBEDTLS_HKDF_C=y -# ESP32-S3-DevKitC-1 Settings -# Buttons -CONFIG_BSP_BUTTONS_NUM=1 -CONFIG_BSP_BUTTON_1_TYPE_GPIO=y -CONFIG_BSP_BUTTON_1_GPIO=0 -CONFIG_BSP_BUTTON_1_LEVEL=0