From 7f97c973117550bbcd8f590afdcd3bf3780551fb Mon Sep 17 00:00:00 2001 From: shripad621git Date: Wed, 12 Apr 2023 18:40:03 +0530 Subject: [PATCH] Provision to add buttons to generic_switch example Added the gpio_button struct to create the gpio buttons. Mapped the buttons to corresponding endpoints. Changed app_main.cpp to make creation of buttons more easy. Added CONFIG_FACTORY_COMMISSIONABLE_DATA_PROVIDER AND CONFIG_FACTORY_DEVICE_INFO_PROVIDER in sdkconfig.defaults to keep the app consistent with changes in providers code. --- docs/en/developing.rst | 1 + .../generic_switch/main/Kconfig.projbuild | 4 + examples/generic_switch/main/app_driver.cpp | 43 ++++++---- examples/generic_switch/main/app_main.cpp | 83 ++++++++++++++++--- examples/generic_switch/main/app_priv.h | 18 +++- examples/generic_switch/sdkconfig.defaults | 3 + 6 files changed, 122 insertions(+), 30 deletions(-) diff --git a/docs/en/developing.rst b/docs/en/developing.rst index aa81b9ee9..f9f231587 100644 --- a/docs/en/developing.rst +++ b/docs/en/developing.rst @@ -28,6 +28,7 @@ The Prerequisites for ESP-IDF and Matter: + 2.1.1.1 Windows 10 ~~~~~~~~~~~~~~~~~~ diff --git a/examples/generic_switch/main/Kconfig.projbuild b/examples/generic_switch/main/Kconfig.projbuild index 95c87644c..5a5e52c60 100644 --- a/examples/generic_switch/main/Kconfig.projbuild +++ b/examples/generic_switch/main/Kconfig.projbuild @@ -11,4 +11,8 @@ config GENERIC_SWITCH_TYPE_MOMENTARY bool "Generic Switch Type Momentary" endchoice + config MAX_CONFIGURABLE_BUTTONS + int "Maximum physical configurable buttons" + default 5 + range 1 5 endmenu diff --git a/examples/generic_switch/main/app_driver.cpp b/examples/generic_switch/main/app_driver.cpp index 9a9358c77..0df1f68e3 100644 --- a/examples/generic_switch/main/app_driver.cpp +++ b/examples/generic_switch/main/app_driver.cpp @@ -21,7 +21,6 @@ using namespace esp_matter; using namespace esp_matter::cluster; static const char *TAG = "app_driver"; -extern uint16_t switch_endpoint_id; esp_err_t app_driver_attribute_update(app_driver_handle_t driver_handle, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) @@ -34,7 +33,8 @@ esp_err_t app_driver_attribute_update(app_driver_handle_t driver_handle, uint16_ static void app_driver_button_switch_latched(void *arg, void *data) { ESP_LOGI(TAG, "Switch lached pressed"); - + gpio_button * button = (gpio_button*)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); @@ -48,7 +48,8 @@ static void app_driver_button_switch_latched(void *arg, void *data) static void app_driver_button_initial_pressed(void *arg, void *data) { ESP_LOGI(TAG, "Initial button pressed"); - + gpio_button * button = (gpio_button*)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); @@ -60,9 +61,10 @@ static void app_driver_button_initial_pressed(void *arg, void *data) 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; @@ -74,7 +76,6 @@ static void app_driver_button_release(void *arg, void *data) } else{ ESP_LOGI(TAG, "Long button release"); - // Release moves Position from 1 (press) to 0 (idle) uint8_t previousPosition = 1; uint8_t newPosition = 0; @@ -88,8 +89,9 @@ static void app_driver_button_release(void *arg, void *data) static void app_driver_button_long_pressed(void *arg, void *data) { - ESP_LOGI(TAG, "Long button pressed"); - + ESP_LOGI(TAG, "Long button pressed "); + gpio_button *button = (gpio_button *)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); @@ -104,7 +106,8 @@ static int current_number_of_presses_counted = 1; static void app_driver_button_multipress_ongoing(void *arg, void *data) { ESP_LOGI(TAG, "Multipress Ongoing"); - + gpio_button * button = (gpio_button *)data; + int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; // Press moves Position from 0 (idle) to 1 (press) uint8_t newPosition = 1; current_number_of_presses_counted++; @@ -118,7 +121,8 @@ static void app_driver_button_multipress_ongoing(void *arg, void *data) static void app_driver_button_multipress_complete(void *arg, void *data) { ESP_LOGI(TAG, "Multipress Complete"); - + gpio_button * button = (gpio_button *)data; + int switch_endpoint_id = (button != NULL) ? get_endpoint(button) : 1; // Press moves Position from 0 (idle) to 1 (press) uint8_t previousPosition = 1; uint8_t newPosition = 0; @@ -133,21 +137,28 @@ static void app_driver_button_multipress_complete(void *arg, void *data) } #endif -app_driver_handle_t app_driver_button_init() +app_driver_handle_t app_driver_button_init(gpio_button * button) { /* Initialize button */ button_config_t config = button_driver_get_config(); + 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(handle, BUTTON_DOUBLE_CLICK, app_driver_button_switch_latched, NULL); + 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(handle, BUTTON_PRESS_DOWN, app_driver_button_initial_pressed, NULL); - iot_button_register_cb(handle, BUTTON_PRESS_UP, app_driver_button_release, NULL); - iot_button_register_cb(handle, BUTTON_LONG_PRESS_START, app_driver_button_long_pressed, NULL); - iot_button_register_cb(handle, BUTTON_PRESS_REPEAT, app_driver_button_multipress_ongoing, NULL); - iot_button_register_cb(handle, BUTTON_PRESS_REPEAT_DONE, app_driver_button_multipress_complete, NULL); + 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)handle; } diff --git a/examples/generic_switch/main/app_main.cpp b/examples/generic_switch/main/app_main.cpp index 14b9991e4..f64a1dc70 100644 --- a/examples/generic_switch/main/app_main.cpp +++ b/examples/generic_switch/main/app_main.cpp @@ -18,7 +18,9 @@ #include static const char *TAG = "app_main"; -uint16_t switch_endpoint_id = 0; + +static uint16_t configured_buttons = 0; +static button_endpoint button_list[CONFIG_MAX_CONFIGURABLE_BUTTONS]; using namespace esp_matter; using namespace esp_matter::attribute; @@ -82,30 +84,47 @@ static esp_err_t app_attribute_update_cb(attribute::callback_type_t type, uint16 return err; } -extern "C" void app_main() +static esp_err_t create_button(struct gpio_button* button, node_t* node) { esp_err_t err = ESP_OK; - /* Initialize the ESP NVS layer */ - nvs_flash_init(); - /* Initialize driver */ - app_driver_handle_t button_handle = app_driver_button_init(); - - /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ - node::config_t node_config; - node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); + app_driver_handle_t button_handle = app_driver_button_init(button); + /* Create a new endpoint. */ generic_switch::config_t switch_config; endpoint_t *endpoint = generic_switch::create(node, &switch_config, ENDPOINT_FLAG_NONE, button_handle); /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ - if (!node || !endpoint) { + if (!node || !endpoint) + { ESP_LOGE(TAG, "Matter node creation failed"); + err = ESP_FAIL; + return err; } - switch_endpoint_id = endpoint::get_id(endpoint); - ESP_LOGI(TAG, "Generic Switch created with endpoint_id %d", switch_endpoint_id); + for (int i = 0; i < configured_buttons; i++) { + if (button_list[i].button == button) { + break; + } + } + + /* Check for maximum physical buttons that can be configured. */ + if (configured_buttons #include +#include + +struct gpio_button +{ + gpio_num_t GPIO_PIN_VALUE; +}; + +struct button_endpoint +{ + gpio_button* button; + uint16_t endpoint; +}; + +extern int get_endpoint(gpio_button* button); typedef void *app_driver_handle_t; @@ -17,10 +31,12 @@ typedef void *app_driver_handle_t; * * This initializes the button driver associated with the selected board. * + * @param[in] button Pointer to `gpio_button`.For boot button value is NULL. + * * @return Handle on success. * @return NULL in case of failure. */ -app_driver_handle_t app_driver_button_init(); +app_driver_handle_t app_driver_button_init(gpio_button *button = NULL); /** Driver Update * diff --git a/examples/generic_switch/sdkconfig.defaults b/examples/generic_switch/sdkconfig.defaults index f856903c3..70d61c5e2 100644 --- a/examples/generic_switch/sdkconfig.defaults +++ b/examples/generic_switch/sdkconfig.defaults @@ -40,6 +40,9 @@ CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=n # Enable for fixed-label CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER=y CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER=y +CONFIG_FACTORY_DEVICE_INFO_PROVIDER=y +CONFIG_FACTORY_COMMISSIONABLE_DATA_PROVIDER=y + # Use compact attribute storage mode CONFIG_ESP_MATTER_NVS_USE_COMPACT_ATTR_STORAGE=y