mirror of
https://github.com/espressif/esp-matter.git
synced 2026-04-27 11:03:05 +00:00
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.
This commit is contained in:
@@ -28,6 +28,7 @@ The Prerequisites for ESP-IDF and Matter:
|
||||
|
||||
|
||||
|
||||
|
||||
2.1.1.1 Windows 10
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
#include <app_reset.h>
|
||||
|
||||
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 <CONFIG_MAX_CONFIGURABLE_BUTTONS) {
|
||||
button_list[configured_buttons].button = button;
|
||||
button_list[configured_buttons].endpoint = endpoint::get_id(endpoint);
|
||||
configured_buttons++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Cannot configure more buttons");
|
||||
err = ESP_FAIL;
|
||||
return err;
|
||||
}
|
||||
|
||||
static uint16_t generic_switch_endpoint_id = 0;
|
||||
generic_switch_endpoint_id = endpoint::get_id(endpoint);
|
||||
ESP_LOGI(TAG, "Generic Switch created with endpoint_id %d", generic_switch_endpoint_id);
|
||||
|
||||
cluster::fixed_label::config_t fl_config;
|
||||
cluster_t *fl_cluster = cluster::fixed_label::create(endpoint, &fl_config, CLUSTER_FLAG_SERVER);
|
||||
@@ -115,6 +134,7 @@ extern "C" void app_main()
|
||||
|
||||
/* Add additional features to the node */
|
||||
cluster_t *cluster = cluster::get(endpoint, Switch::Id);
|
||||
|
||||
#if CONFIG_GENERIC_SWITCH_TYPE_LATCHING
|
||||
cluster::switch_cluster::feature::latching_switch::add(cluster);
|
||||
#endif
|
||||
@@ -122,6 +142,42 @@ extern "C" void app_main()
|
||||
#if CONFIG_GENERIC_SWITCH_TYPE_MOMENTARY
|
||||
cluster::switch_cluster::feature::momentary_switch::add(cluster);
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int get_endpoint(gpio_button* button) {
|
||||
for (int i = 0; i < configured_buttons; i++) {
|
||||
if (button_list[i].button == button) {
|
||||
return button_list[i].endpoint;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
extern "C" void app_main()
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
|
||||
/* Initialize the ESP NVS layer */
|
||||
nvs_flash_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);
|
||||
|
||||
/* Call for Boot button */
|
||||
err = create_button(NULL, node);
|
||||
|
||||
/* Use the code snippet commented below to create more physical buttons. */
|
||||
|
||||
/* // Creating a gpio button. More buttons can be created in the same fashion specifying GPIO_PIN_VALUE.
|
||||
* struct gpio_button button;
|
||||
* button.GPIO_PIN_VALUE = GPIO_NUM_6;
|
||||
* // Call to createButton function to configure your button.
|
||||
* create_button(&button, node);
|
||||
*/
|
||||
|
||||
/* Matter start */
|
||||
err = esp_matter::start(app_event_cb);
|
||||
if (err != ESP_OK) {
|
||||
@@ -149,4 +205,5 @@ extern "C" void app_main()
|
||||
esp_matter::console::wifi_register_commands();
|
||||
esp_matter::console::init();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,20 @@
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_matter.h>
|
||||
#include <hal/gpio_types.h>
|
||||
|
||||
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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user