From 1dff8bc75953d63c11df0db2a3389511d73e9672 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 10:01:24 -0300 Subject: [PATCH 01/10] feat(matter) adds Identification callback to all matter endpoints --- .../MatterOnIdentify/MatterOnIdentify.ino | 126 ++++++++++++++++++ .../Matter/examples/MatterOnIdentify/ci.json | 7 + libraries/Matter/src/Matter.cpp | 26 +++- libraries/Matter/src/MatterEndPoint.h | 2 + .../src/MatterEndpoints/MatterColorLight.h | 13 ++ .../MatterColorTemperatureLight.h | 14 ++ .../src/MatterEndpoints/MatterContactSensor.h | 13 ++ .../src/MatterEndpoints/MatterDimmableLight.h | 15 +++ .../MatterEnhancedColorLight.h | 13 ++ .../Matter/src/MatterEndpoints/MatterFan.h | 13 ++ .../src/MatterEndpoints/MatterGenericSwitch.h | 13 ++ .../MatterEndpoints/MatterHumiditySensor.h | 13 ++ .../MatterEndpoints/MatterOccupancySensor.h | 13 ++ .../src/MatterEndpoints/MatterOnOffLight.h | 15 +++ .../src/MatterEndpoints/MatterOnOffPlugin.h | 15 +++ .../MatterEndpoints/MatterPressureSensor.h | 13 ++ .../MatterEndpoints/MatterTemperatureSensor.h | 13 ++ 17 files changed, 335 insertions(+), 2 deletions(-) create mode 100644 libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino create mode 100644 libraries/Matter/examples/MatterOnIdentify/ci.json diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino new file mode 100644 index 00000000000..f55a20ae3ab --- /dev/null +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -0,0 +1,126 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is the smallest code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * It controls a GPIO that could be attached to a LED for visualization. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + * + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. + * It demonstrates how to use On Identify callback when the Identify Cluster is called. + * The Matter user APP can be used to request the device to identify itself by blinking the LED. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Single On/Off Light Endpoint - at least one per node +MatterOnOffLight OnOffLight; + +// Light GPIO that can be controlled by Matter APP +#ifdef LED_BUILTIN +const uint8_t ledPin = LED_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#endif + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// Button control - decommision the Matter Node +uint32_t button_time_stamp = 0; // debouncing control +bool button_state = false; // false = released | true = pressed +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Matter Protocol Endpoint (On/OFF Light) Callback +bool matterCB(bool state) { + digitalWrite(ledPin, state ? HIGH : LOW); + // This callback must return the success state to Matter core + return true; +} + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +void setup() { + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node + pinMode(buttonPin, INPUT_PULLUP); + // Initialize the LED GPIO + pinMode(ledPin, OUTPUT); + + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // On Identify Callback - Blink the LED + OnOffLight.onIdentify([](bool identifyIsActive, uint8_t counter) { + log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter); + if (identifyIsActive) { + // Start Blinking the light + OnOffLight.toggle(); + } else { + // Stop Blinking and restore the light to the its last state + OnOffLight.updateAccessory(); + } + return true; + }); + + // Associate a callback to the Matter Controller + OnOffLight.onChange(matterCB); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + if (!Matter.isDeviceCommissioned()) { + log_i("Matter Node is not commissioned yet."); + log_i("Initiate the device discovery in your Matter environment."); + log_i("Commission it to your Matter hub with the manual pairing code or QR code"); + log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + } +} + +void loop() { + // Check if the button has been pressed + if (digitalRead(buttonPin) == LOW && !button_state) { + // deals with button debouncing + button_time_stamp = millis(); // record the time while the button is pressed. + button_state = true; // pressed. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so + } + + delay(500); +} diff --git a/libraries/Matter/examples/MatterOnIdentify/ci.json b/libraries/Matter/examples/MatterOnIdentify/ci.json new file mode 100644 index 00000000000..556a8a9ee6b --- /dev/null +++ b/libraries/Matter/examples/MatterOnIdentify/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=huge_app", + "requires": [ + "CONFIG_SOC_WIFI_SUPPORTED=y", + "CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y" + ] +} diff --git a/libraries/Matter/src/Matter.cpp b/libraries/Matter/src/Matter.cpp index 89ef87b4db3..51ab9c91cf1 100644 --- a/libraries/Matter/src/Matter.cpp +++ b/libraries/Matter/src/Matter.cpp @@ -21,6 +21,7 @@ using namespace esp_matter; using namespace esp_matter::attribute; using namespace esp_matter::endpoint; +using namespace esp_matter::identification; using namespace chip::app::Clusters; constexpr auto k_timeout_seconds = 300; @@ -67,8 +68,29 @@ static esp_err_t app_attribute_update_cb( // This callback is invoked when clients interact with the Identify Cluster. // In the callback implementation, an endpoint can identify itself. (e.g., by flashing an LED or light). static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id, uint8_t effect_variant, void *priv_data) { - log_i("Identification callback: type: %u, effect: %u, variant: %u", type, effect_id, effect_variant); - return ESP_OK; + log_d("Identification callback to endpoint %d: type: %u, effect: %u, variant: %u", endpoint_id, effect_id, effect_variant); + esp_err_t err = ESP_OK; + MatterEndPoint *ep = (MatterEndPoint *)priv_data; // endpoint pointer to base class + // Identify the endpoint sending a counter to the application + static uint8_t counter = 0; + bool identifyIsActive = false; + + if (type == identification::callback_type_t::START) { + log_v("Identification callback: START"); + counter = 0; + identifyIsActive = true; + } else if (type == identification::callback_type_t::EFFECT) { + log_v("Identification callback: EFFECT"); + counter++; + } else if (type == identification::callback_type_t::STOP) { + identifyIsActive = false; + log_v("Identification callback: STOP"); + } + if (ep != NULL) { + err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive, counter) ? ESP_OK : ESP_FAIL; + } + + return err; } // This callback is invoked for all Matter events. The application can handle the events as required. diff --git a/libraries/Matter/src/MatterEndPoint.h b/libraries/Matter/src/MatterEndPoint.h index 99bff8470d3..7f791ff49d9 100644 --- a/libraries/Matter/src/MatterEndPoint.h +++ b/libraries/Matter/src/MatterEndPoint.h @@ -102,6 +102,8 @@ class MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0; + // This callback is invoked when clients interact with the Identify Cluster of an specific endpoint. + virtual bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) = 0; protected: uint16_t endpoint_id = 0; }; diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorLight.h b/libraries/Matter/src/MatterEndpoints/MatterColorLight.h index 13ff0decbc2..f579d555066 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterColorLight.h @@ -46,6 +46,18 @@ class MatterColorLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -71,5 +83,6 @@ class MatterColorLight : public MatterEndPoint { EndPointOnOffCB _onChangeOnOffCB = NULL; EndPointRGBColorCB _onChangeColorCB = NULL; EndPointCB _onChangeCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h index e886a184182..de51be4a227 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h @@ -51,6 +51,18 @@ class MatterColorTemperatureLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -85,5 +97,7 @@ class MatterColorTemperatureLight : public MatterEndPoint { EndPointBrightnessCB _onChangeBrightnessCB = NULL; EndPointTemperatureCB _onChangeTemperatureCB = NULL; EndPointCB _onChangeCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; + }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h index 257da785e53..c4e74d6c30f 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h @@ -46,9 +46,22 @@ class MatterContactSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: bool started = false; bool contactState = false; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h index aacce883277..aeb85fae2b5 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h @@ -43,8 +43,22 @@ class MatterDimmableLight : public MatterEndPoint { operator bool(); // returns current on/off light state void operator=(bool state); // turns light on or off + // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } + // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; void onChangeOnOff(EndPointOnOffCB onChangeCB) { @@ -69,5 +83,6 @@ class MatterDimmableLight : public MatterEndPoint { EndPointOnOffCB _onChangeOnOffCB = NULL; EndPointBrightnessCB _onChangeBrightnessCB = NULL; EndPointCB _onChangeCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h index 66ed1943b8d..599ae55ffa4 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h @@ -56,6 +56,18 @@ class MatterEnhancedColorLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -98,5 +110,6 @@ class MatterEnhancedColorLight : public MatterEndPoint { EndPointRGBColorCB _onChangeColorCB = NULL; EndPointTemperatureCB _onChangeTemperatureCB = NULL; EndPointCB _onChangeCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterFan.h b/libraries/Matter/src/MatterEndpoints/MatterFan.h index 232577b7bef..ba8772fd1be 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterFan.h +++ b/libraries/Matter/src/MatterEndpoints/MatterFan.h @@ -105,6 +105,18 @@ class MatterFan : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } // User Callback for whenever the Fan Mode (state) is changed by the Matter Controller using EndPointModeCB = std::function; @@ -133,6 +145,7 @@ class MatterFan : public MatterEndPoint { EndPointModeCB _onChangeModeCB = NULL; EndPointSpeedCB _onChangeSpeedCB = NULL; EndPointCB _onChangeCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // bitmap for Fan Sequence Modes (OFF, LOW, MEDIUM, HIGH, AUTO) static const uint8_t fanSeqModeOff = 0x01; diff --git a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h index 14118462932..895413f2fd3 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h +++ b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h @@ -32,8 +32,21 @@ class MatterGenericSwitch : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: bool started = false; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h index aed758b7b7a..7268947d18d 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h @@ -57,6 +57,18 @@ class MatterHumiditySensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: bool started = false; @@ -65,5 +77,6 @@ class MatterHumiditySensor : public MatterEndPoint { // internal function to set the raw humidity value (Matter Cluster) bool begin(uint16_t _rawHumidity); bool setRawHumidity(uint16_t _rawHumidity); + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h index 30f312a9841..61d4ad1bcb7 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h @@ -57,6 +57,18 @@ class MatterOccupancySensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: // bitmap for Occupancy Sensor Types @@ -69,5 +81,6 @@ class MatterOccupancySensor : public MatterEndPoint { bool started = false; bool occupancyState = false; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h index 6d140a9948e..4b1b5e4eabb 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h @@ -36,8 +36,22 @@ class MatterOnOffLight : public MatterEndPoint { operator bool(); // returns current light state void operator=(bool state); // turns light on or off + // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } + // User Callback for whenever the Light state is changed by the Matter Controller using EndPointCB = std::function; void onChange(EndPointCB onChangeCB) { @@ -52,5 +66,6 @@ class MatterOnOffLight : public MatterEndPoint { bool onOffState = false; // default initial state is off, but it can be changed by begin(bool) EndPointCB _onChangeCB = NULL; EndPointCB _onChangeOnOffCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h index 241726a3a46..cf733f4c497 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h @@ -36,8 +36,22 @@ class MatterOnOffPlugin : public MatterEndPoint { operator bool(); // returns current plugin state void operator=(bool state); // turns plugin on or off + // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } + // User Callback for whenever the Plugin state is changed by the Matter Controller using EndPointCB = std::function; void onChange(EndPointCB onChangeCB) { @@ -52,5 +66,6 @@ class MatterOnOffPlugin : public MatterEndPoint { bool onOffState = false; // default initial state is off, but it can be changed by begin(bool) EndPointCB _onChangeCB = NULL; EndPointCB _onChangeOnOffCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h index 9fdd90c6ebe..8ff4446c6b7 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h @@ -50,11 +50,24 @@ class MatterPressureSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: bool started = false; // implementation keeps pressure in hPa int16_t rawPressure = 0; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // internal function to set the raw pressure value (Matter Cluster) bool setRawPressure(int16_t _rawPressure); bool begin(int16_t _rawPressure); diff --git a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h index 826abac9a2a..cb50dc0ff5d 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h @@ -51,11 +51,24 @@ class MatterTemperatureSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); + // this function is invoked when clients interact with the Identify Cluster of an specific endpoint + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } protected: bool started = false; // implementation keeps temperature in 1/100th of a degree, any temperature unit int16_t rawTemperature = 0; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // internal function to set the raw temperature value (Matter Cluster) bool setRawTemperature(int16_t _rawTemperature); bool begin(int16_t _rawTemperature); From 0e22bb4bac0a65719ea56d82ddb4d0b1eee71fd9 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 10:58:49 -0300 Subject: [PATCH 02/10] feat(matter): moved all identify callback to endpoint.h --- .../MatterOnIdentify/MatterOnIdentify.ino | 34 ++++++++++--------- libraries/Matter/src/MatterEndPoint.h | 15 +++++++- .../src/MatterEndpoints/MatterColorLight.h | 13 ------- .../MatterColorTemperatureLight.h | 14 -------- .../src/MatterEndpoints/MatterContactSensor.h | 13 ------- .../src/MatterEndpoints/MatterDimmableLight.h | 13 ------- .../MatterEnhancedColorLight.h | 13 ------- .../Matter/src/MatterEndpoints/MatterFan.h | 13 ------- .../src/MatterEndpoints/MatterGenericSwitch.h | 13 ------- .../MatterEndpoints/MatterHumiditySensor.h | 13 ------- .../MatterEndpoints/MatterOccupancySensor.h | 13 ------- .../src/MatterEndpoints/MatterOnOffLight.h | 13 ------- .../src/MatterEndpoints/MatterOnOffPlugin.h | 13 ------- .../MatterEndpoints/MatterPressureSensor.h | 13 ------- .../MatterEndpoints/MatterTemperatureSensor.h | 13 ------- 15 files changed, 32 insertions(+), 187 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index f55a20ae3ab..9b183e0b175 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -32,6 +32,10 @@ // Single On/Off Light Endpoint - at least one per node MatterOnOffLight OnOffLight; +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + // Light GPIO that can be controlled by Matter APP #ifdef LED_BUILTIN const uint8_t ledPin = LED_BUILTIN; @@ -48,15 +52,23 @@ bool button_state = false; // false = released | true = pres const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission // Matter Protocol Endpoint (On/OFF Light) Callback -bool matterCB(bool state) { +bool onOffLightCallback(bool state) { digitalWrite(ledPin, state ? HIGH : LOW); // This callback must return the success state to Matter core return true; } -// WiFi is manually set and started -const char *ssid = "your-ssid"; // Change this to your WiFi SSID -const char *password = "your-password"; // Change this to your WiFi password +bool onIdentifyLightCallback(bool identifyIsActive, uint8_t counter) { + log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter); + if (identifyIsActive) { + // Start Blinking the light + OnOffLight.toggle(); + } else { + // Stop Blinking and restore the light to the its last state + OnOffLight.updateAccessory(); + } + return true; +} void setup() { // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node @@ -75,20 +87,10 @@ void setup() { OnOffLight.begin(); // On Identify Callback - Blink the LED - OnOffLight.onIdentify([](bool identifyIsActive, uint8_t counter) { - log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter); - if (identifyIsActive) { - // Start Blinking the light - OnOffLight.toggle(); - } else { - // Stop Blinking and restore the light to the its last state - OnOffLight.updateAccessory(); - } - return true; - }); + OnOffLight.onIdentify(onIdentifyLightCallback); // Associate a callback to the Matter Controller - OnOffLight.onChange(matterCB); + OnOffLight.onChange(onOffLightCallback); // Matter beginning - Last step, after all EndPoints are initialized Matter.begin(); diff --git a/libraries/Matter/src/MatterEndPoint.h b/libraries/Matter/src/MatterEndPoint.h index 7f791ff49d9..34b59bb18c8 100644 --- a/libraries/Matter/src/MatterEndPoint.h +++ b/libraries/Matter/src/MatterEndPoint.h @@ -103,8 +103,21 @@ class MatterEndPoint { virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0; // This callback is invoked when clients interact with the Identify Cluster of an specific endpoint. - virtual bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) = 0; + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + if (_onEndPointIdentifyCB) { + return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + } + return true; + } + // User callaback for the Identify Cluster functionality + using EndPointIdentifyCB = std::function; + void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { + _onEndPointIdentifyCB = onEndPointIdentifyCB; + } + + protected: uint16_t endpoint_id = 0; + EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorLight.h b/libraries/Matter/src/MatterEndpoints/MatterColorLight.h index f579d555066..13ff0decbc2 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterColorLight.h @@ -46,18 +46,6 @@ class MatterColorLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -83,6 +71,5 @@ class MatterColorLight : public MatterEndPoint { EndPointOnOffCB _onChangeOnOffCB = NULL; EndPointRGBColorCB _onChangeColorCB = NULL; EndPointCB _onChangeCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h index de51be4a227..e886a184182 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.h @@ -51,18 +51,6 @@ class MatterColorTemperatureLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -97,7 +85,5 @@ class MatterColorTemperatureLight : public MatterEndPoint { EndPointBrightnessCB _onChangeBrightnessCB = NULL; EndPointTemperatureCB _onChangeTemperatureCB = NULL; EndPointCB _onChangeCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; - }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h index c4e74d6c30f..257da785e53 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.h @@ -46,22 +46,9 @@ class MatterContactSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: bool started = false; bool contactState = false; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h index aeb85fae2b5..838fe364760 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h @@ -46,18 +46,6 @@ class MatterDimmableLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -83,6 +71,5 @@ class MatterDimmableLight : public MatterEndPoint { EndPointOnOffCB _onChangeOnOffCB = NULL; EndPointBrightnessCB _onChangeBrightnessCB = NULL; EndPointCB _onChangeCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h index 599ae55ffa4..66ed1943b8d 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.h @@ -56,18 +56,6 @@ class MatterEnhancedColorLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Light On/Off state is changed by the Matter Controller using EndPointOnOffCB = std::function; @@ -110,6 +98,5 @@ class MatterEnhancedColorLight : public MatterEndPoint { EndPointRGBColorCB _onChangeColorCB = NULL; EndPointTemperatureCB _onChangeTemperatureCB = NULL; EndPointCB _onChangeCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterFan.h b/libraries/Matter/src/MatterEndpoints/MatterFan.h index ba8772fd1be..232577b7bef 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterFan.h +++ b/libraries/Matter/src/MatterEndpoints/MatterFan.h @@ -105,18 +105,6 @@ class MatterFan : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Fan Mode (state) is changed by the Matter Controller using EndPointModeCB = std::function; @@ -145,7 +133,6 @@ class MatterFan : public MatterEndPoint { EndPointModeCB _onChangeModeCB = NULL; EndPointSpeedCB _onChangeSpeedCB = NULL; EndPointCB _onChangeCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // bitmap for Fan Sequence Modes (OFF, LOW, MEDIUM, HIGH, AUTO) static const uint8_t fanSeqModeOff = 0x01; diff --git a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h index 895413f2fd3..14118462932 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h +++ b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.h @@ -32,21 +32,8 @@ class MatterGenericSwitch : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: bool started = false; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h index 7268947d18d..aed758b7b7a 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h @@ -57,18 +57,6 @@ class MatterHumiditySensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: bool started = false; @@ -77,6 +65,5 @@ class MatterHumiditySensor : public MatterEndPoint { // internal function to set the raw humidity value (Matter Cluster) bool begin(uint16_t _rawHumidity); bool setRawHumidity(uint16_t _rawHumidity); - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h index 61d4ad1bcb7..30f312a9841 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.h @@ -57,18 +57,6 @@ class MatterOccupancySensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: // bitmap for Occupancy Sensor Types @@ -81,6 +69,5 @@ class MatterOccupancySensor : public MatterEndPoint { bool started = false; bool occupancyState = false; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h index 4b1b5e4eabb..b27a2530075 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h @@ -39,18 +39,6 @@ class MatterOnOffLight : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Light state is changed by the Matter Controller using EndPointCB = std::function; @@ -66,6 +54,5 @@ class MatterOnOffLight : public MatterEndPoint { bool onOffState = false; // default initial state is off, but it can be changed by begin(bool) EndPointCB _onChangeCB = NULL; EndPointCB _onChangeOnOffCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h index cf733f4c497..0b66be6c14e 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.h @@ -39,18 +39,6 @@ class MatterOnOffPlugin : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } // User Callback for whenever the Plugin state is changed by the Matter Controller using EndPointCB = std::function; @@ -66,6 +54,5 @@ class MatterOnOffPlugin : public MatterEndPoint { bool onOffState = false; // default initial state is off, but it can be changed by begin(bool) EndPointCB _onChangeCB = NULL; EndPointCB _onChangeOnOffCB = NULL; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h index 8ff4446c6b7..9fdd90c6ebe 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.h @@ -50,24 +50,11 @@ class MatterPressureSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: bool started = false; // implementation keeps pressure in hPa int16_t rawPressure = 0; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // internal function to set the raw pressure value (Matter Cluster) bool setRawPressure(int16_t _rawPressure); bool begin(int16_t _rawPressure); diff --git a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h index cb50dc0ff5d..826abac9a2a 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h +++ b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.h @@ -51,24 +51,11 @@ class MatterTemperatureSensor : public MatterEndPoint { // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); - // this function is invoked when clients interact with the Identify Cluster of an specific endpoint - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { - if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); - } - return true; - } - // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; - void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { - _onEndPointIdentifyCB = onEndPointIdentifyCB; - } protected: bool started = false; // implementation keeps temperature in 1/100th of a degree, any temperature unit int16_t rawTemperature = 0; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; // internal function to set the raw temperature value (Matter Cluster) bool setRawTemperature(int16_t _rawTemperature); bool begin(int16_t _rawTemperature); From cd7a775344ba671dd9c01c930882bd62d5a1d98b Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 11:47:07 -0300 Subject: [PATCH 03/10] fix(matter): missing logged message type value --- libraries/Matter/src/Matter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Matter/src/Matter.cpp b/libraries/Matter/src/Matter.cpp index 51ab9c91cf1..5d125ce6919 100644 --- a/libraries/Matter/src/Matter.cpp +++ b/libraries/Matter/src/Matter.cpp @@ -68,7 +68,7 @@ static esp_err_t app_attribute_update_cb( // This callback is invoked when clients interact with the Identify Cluster. // In the callback implementation, an endpoint can identify itself. (e.g., by flashing an LED or light). static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id, uint8_t effect_variant, void *priv_data) { - log_d("Identification callback to endpoint %d: type: %u, effect: %u, variant: %u", endpoint_id, effect_id, effect_variant); + log_d("Identification callback to endpoint %d: type: %u, effect: %u, variant: %u", endpoint_id, type, effect_id, effect_variant); esp_err_t err = ESP_OK; MatterEndPoint *ep = (MatterEndPoint *)priv_data; // endpoint pointer to base class // Identify the endpoint sending a counter to the application From 6f79e034495f6f8cea24ade0933810b336b43a23 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 15:48:41 -0300 Subject: [PATCH 04/10] fix(matter): fixes identify and double begin() call --- .../MatterOnIdentify/MatterOnIdentify.ino | 58 +++++++++++++++---- libraries/Matter/src/Matter.cpp | 10 +--- libraries/Matter/src/MatterEndPoint.h | 6 +- .../src/MatterEndpoints/MatterColorLight.cpp | 7 ++- .../MatterColorTemperatureLight.cpp | 27 +++++---- .../MatterEndpoints/MatterContactSensor.cpp | 5 ++ .../MatterEndpoints/MatterDimmableLight.cpp | 6 +- .../MatterEnhancedColorLight.cpp | 7 ++- .../Matter/src/MatterEndpoints/MatterFan.cpp | 7 ++- .../MatterEndpoints/MatterGenericSwitch.cpp | 9 ++- .../MatterEndpoints/MatterHumiditySensor.cpp | 5 ++ .../MatterEndpoints/MatterOccupancySensor.cpp | 5 ++ .../src/MatterEndpoints/MatterOnOffLight.cpp | 7 ++- .../src/MatterEndpoints/MatterOnOffPlugin.cpp | 7 ++- .../MatterEndpoints/MatterPressureSensor.cpp | 5 ++ .../MatterTemperatureSensor.cpp | 5 ++ 16 files changed, 134 insertions(+), 42 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 9b183e0b175..933b5a72367 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -51,6 +51,11 @@ uint32_t button_time_stamp = 0; // debouncing control bool button_state = false; // false = released | true = pressed const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission +// Identify Flag and blink time - Blink the LED +const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed +volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state +bool identifyBlink = false; // Blink state when in Identify state + // Matter Protocol Endpoint (On/OFF Light) Callback bool onOffLightCallback(bool state) { digitalWrite(ledPin, state ? HIGH : LOW); @@ -58,14 +63,19 @@ bool onOffLightCallback(bool state) { return true; } -bool onIdentifyLightCallback(bool identifyIsActive, uint8_t counter) { - log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter); +// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined +bool onIdentifyLightCallback(bool identifyIsActive) { + log_i("Identify Cluster is %s", identifyIsActive ? "Active" : "Inactive"); if (identifyIsActive) { - // Start Blinking the light - OnOffLight.toggle(); + // Start Blinking the light in loop() + identifyFlag = true; + identifyBlink = !OnOffLight; // Start with the inverted light state } else { // Stop Blinking and restore the light to the its last state - OnOffLight.updateAccessory(); + identifyFlag = false; + // force returning to the original state by toggling the light twice + OnOffLight.toggle(); + OnOffLight.toggle(); } return true; } @@ -76,12 +86,16 @@ void setup() { // Initialize the LED GPIO pinMode(ledPin, OUTPUT); + Serial.begin(115200); + // Manually connect to WiFi WiFi.begin(ssid, password); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); + Serial.print("."); } + Serial.println(); // Initialize at least one Matter EndPoint OnOffLight.begin(); @@ -95,16 +109,38 @@ void setup() { // Matter beginning - Last step, after all EndPoints are initialized Matter.begin(); + // Check Matter Accessory Commissioning state, which may change during execution of loop() if (!Matter.isDeviceCommissioned()) { - log_i("Matter Node is not commissioned yet."); - log_i("Initiate the device discovery in your Matter environment."); - log_i("Commission it to your Matter hub with the manual pairing code or QR code"); - log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); - log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Occupancy Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); } } void loop() { + // check if the Ligth is in identify state and blink it every 500ms (delay loop time) + if (identifyFlag) { +#ifdef LED_BUILTIN + uint8_t brightness = 32 * identifyBlink; + rgbLedWrite(identifyLedPin, brightness, 0, 0); +#else + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); +#endif + identifyBlink = !identifyBlink; + } + // Check if the button has been pressed if (digitalRead(buttonPin) == LOW && !button_state) { // deals with button debouncing @@ -124,5 +160,5 @@ void loop() { button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so } - delay(500); + delay(500); // works as a debounce for the button and also for the LED blink } diff --git a/libraries/Matter/src/Matter.cpp b/libraries/Matter/src/Matter.cpp index 5d125ce6919..1c80bff941e 100644 --- a/libraries/Matter/src/Matter.cpp +++ b/libraries/Matter/src/Matter.cpp @@ -30,11 +30,6 @@ static bool _matter_has_started = false; static node::config_t node_config; static node_t *deviceNode = NULL; -typedef void *app_driver_handle_t; -esp_err_t matter_light_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 -); - // This callback is called for every attribute update. The callback implementation shall // handle the desired attributes and return an appropriate error code. If the attribute // is not of your interest, please do not return an error code and strictly return ESP_OK. @@ -72,22 +67,19 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin esp_err_t err = ESP_OK; MatterEndPoint *ep = (MatterEndPoint *)priv_data; // endpoint pointer to base class // Identify the endpoint sending a counter to the application - static uint8_t counter = 0; bool identifyIsActive = false; if (type == identification::callback_type_t::START) { log_v("Identification callback: START"); - counter = 0; identifyIsActive = true; } else if (type == identification::callback_type_t::EFFECT) { log_v("Identification callback: EFFECT"); - counter++; } else if (type == identification::callback_type_t::STOP) { identifyIsActive = false; log_v("Identification callback: STOP"); } if (ep != NULL) { - err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive, counter) ? ESP_OK : ESP_FAIL; + err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive) ? ESP_OK : ESP_FAIL; } return err; diff --git a/libraries/Matter/src/MatterEndPoint.h b/libraries/Matter/src/MatterEndPoint.h index 34b59bb18c8..f269b713a5e 100644 --- a/libraries/Matter/src/MatterEndPoint.h +++ b/libraries/Matter/src/MatterEndPoint.h @@ -103,14 +103,14 @@ class MatterEndPoint { virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0; // This callback is invoked when clients interact with the Identify Cluster of an specific endpoint. - bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) { + bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled) { if (_onEndPointIdentifyCB) { - return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter); + return _onEndPointIdentifyCB(identifyIsEnabled); } return true; } // User callaback for the Identify Cluster functionality - using EndPointIdentifyCB = std::function; + using EndPointIdentifyCB = std::function; void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) { _onEndPointIdentifyCB = onEndPointIdentifyCB; } diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp b/libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp index 6e2a7910433..eaaf0bf2ffe 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp @@ -162,8 +162,13 @@ MatterColorLight::~MatterColorLight() { bool MatterColorLight::begin(bool initialState, espHsvColor_t _colorHSV) { ArduinoMatter::_init(); - rgb_color_light::config_t light_config; + if (getEndPointId() != 0) { + log_e("Matter RGB Color Light with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + rgb_color_light::config_t light_config; light_config.on_off.on_off = initialState; light_config.on_off.lighting.start_up_on_off = nullptr; onOffState = initialState; diff --git a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp index f54203b7928..5ef69749bb1 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp @@ -26,17 +26,17 @@ using namespace chip::app::Clusters; bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) { bool ret = true; if (!started) { - log_e("Matter CW_WW Light device has not begun."); + log_e("Matter Temperature Light device has not begun."); return false; } - log_d("CW_WW Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32); + log_d("Temperature Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32); if (endpoint_id == getEndPointId()) { switch (cluster_id) { case OnOff::Id: if (attribute_id == OnOff::Attributes::OnOff::Id) { - log_d("CW_WW Light On/Off State changed to %d", val->val.b); + log_d("Temperature Light On/Off State changed to %d", val->val.b); if (_onChangeOnOffCB != NULL) { ret &= _onChangeOnOffCB(val->val.b); } @@ -50,7 +50,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32 break; case LevelControl::Id: if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) { - log_d("CW_WW Light Brightness changed to %d", val->val.u8); + log_d("Temperature Light Brightness changed to %d", val->val.u8); if (_onChangeBrightnessCB != NULL) { ret &= _onChangeBrightnessCB(val->val.u8); } @@ -64,7 +64,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32 break; case ColorControl::Id: if (attribute_id == ColorControl::Attributes::ColorTemperatureMireds::Id) { - log_d("CW_WW Light Temperature changed to %d", val->val.u16); + log_d("Temperature Light Temperature changed to %d", val->val.u16); if (_onChangeTemperatureCB != NULL) { ret &= _onChangeTemperatureCB(val->val.u16); } @@ -89,8 +89,13 @@ MatterColorTemperatureLight::~MatterColorTemperatureLight() { bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, uint16_t ColorTemperature) { ArduinoMatter::_init(); - color_temperature_light::config_t light_config; + if (getEndPointId() != 0) { + log_e("Matter Temperature Light with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + color_temperature_light::config_t light_config; light_config.on_off.on_off = initialState; light_config.on_off.lighting.start_up_on_off = nullptr; onOffState = initialState; @@ -108,12 +113,12 @@ bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, u // endpoint handles can be used to add/modify clusters. endpoint_t *endpoint = color_temperature_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *)this); if (endpoint == nullptr) { - log_e("Failed to create CW_WW light endpoint"); + log_e("Failed to create Temperature Light endpoint"); return false; } setEndPointId(endpoint::get_id(endpoint)); - log_i("CW_WW Light created with endpoint_id %d", getEndPointId()); + log_i("Temperature Light created with endpoint_id %d", getEndPointId()); /* Mark deferred persistence for some attributes that might be changed rapidly */ cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id); @@ -134,7 +139,7 @@ void MatterColorTemperatureLight::end() { bool MatterColorTemperatureLight::setOnOff(bool newState) { if (!started) { - log_e("Matter CW_WW Light device has not begun."); + log_e("Matter Temperature Light device has not begun."); return false; } @@ -175,7 +180,7 @@ bool MatterColorTemperatureLight::toggle() { bool MatterColorTemperatureLight::setBrightness(uint8_t newBrightness) { if (!started) { - log_w("Matter CW_WW Light device has not begun."); + log_w("Matter Temperature Light device has not begun."); return false; } @@ -206,7 +211,7 @@ uint8_t MatterColorTemperatureLight::getBrightness() { bool MatterColorTemperatureLight::setColorTemperature(uint16_t newTemperature) { if (!started) { - log_w("Matter CW_WW Light device has not begun."); + log_w("Matter Temperature Light device has not begun."); return false; } diff --git a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp index fb08587c6f6..17b0fe7a247 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp @@ -43,6 +43,11 @@ MatterContactSensor::~MatterContactSensor() { bool MatterContactSensor::begin(bool _contactState) { ArduinoMatter::_init(); + if (getEndPointId() != 0) { + log_e("Matter Contact Sensor with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + contact_sensor::config_t contact_sensor_config; contact_sensor_config.boolean_state.state_value = _contactState; diff --git a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp index cd9830be8ac..9f6f872ca3e 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp @@ -75,8 +75,12 @@ MatterDimmableLight::~MatterDimmableLight() { bool MatterDimmableLight::begin(bool initialState, uint8_t brightness) { ArduinoMatter::_init(); - dimmable_light::config_t light_config; + if (getEndPointId() != 0) { + log_e("Matter Dimmable Light with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + dimmable_light::config_t light_config; light_config.on_off.on_off = initialState; light_config.on_off.lighting.start_up_on_off = nullptr; onOffState = initialState; diff --git a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp index 215e52b4137..022e62654df 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp @@ -178,8 +178,13 @@ MatterEnhancedColorLight::~MatterEnhancedColorLight() { bool MatterEnhancedColorLight::begin(bool initialState, espHsvColor_t _colorHSV, uint8_t brightness, uint16_t ColorTemperature) { ArduinoMatter::_init(); - enhanced_color_light::config_t light_config; + if (getEndPointId() != 0) { + log_e("Matter Enhanced ColorLight with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + enhanced_color_light::config_t light_config; light_config.on_off.on_off = initialState; light_config.on_off.lighting.start_up_on_off = nullptr; onOffState = initialState; diff --git a/libraries/Matter/src/MatterEndpoints/MatterFan.cpp b/libraries/Matter/src/MatterEndpoints/MatterFan.cpp index 12de176d176..f81d49f22a7 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterFan.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterFan.cpp @@ -85,7 +85,12 @@ bool MatterFan::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uin bool MatterFan::begin(uint8_t percent, FanMode_t fanMode, FanModeSequence_t fanModeSeq) { ArduinoMatter::_init(); - // endpoint handles can be used to add/modify clusters. + if (getEndPointId() != 0) { + log_e("Matter Fan with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + // endpoint handles can be used to add/modify clusters. fan::config_t fan_config; fan_config.fan_control.fan_mode = fanMode; fan_config.fan_control.percent_current = percent; diff --git a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp index bbf72af0a95..c8f2c4ec444 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp @@ -42,12 +42,17 @@ bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t clust bool MatterGenericSwitch::begin() { ArduinoMatter::_init(); - generic_switch::config_t switch_config; + if (getEndPointId() != 0) { + log_e("Matter Generic Switch with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + generic_switch::config_t switch_config; // endpoint handles can be used to add/modify clusters. endpoint_t *endpoint = generic_switch::create(node::get(), &switch_config, ENDPOINT_FLAG_NONE, (void *)this); if (endpoint == nullptr) { - log_e("Failed to create Generic switch endpoint"); + log_e("Failed to create Generic Switch endpoint"); return false; } // Add group cluster to the switch endpoint diff --git a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp index 7526b99a7b9..d31d0e43728 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp @@ -43,6 +43,11 @@ MatterHumiditySensor::~MatterHumiditySensor() { bool MatterHumiditySensor::begin(uint16_t _rawHumidity) { ArduinoMatter::_init(); + if (getEndPointId() != 0) { + log_e("Matter Humidity Sensor with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + // is it a valid percentage value? if (_rawHumidity > 10000) { log_e("Humidity Sensor Percentage value out of range [0..100]."); diff --git a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp index ad200bc2ad8..0d55c37708a 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp @@ -52,6 +52,11 @@ MatterOccupancySensor::~MatterOccupancySensor() { bool MatterOccupancySensor::begin(bool _occupancyState, OccupancySensorType_t _occupancySensorType) { ArduinoMatter::_init(); + if (getEndPointId() != 0) { + log_e("Matter Occupancy Sensor with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + occupancy_sensor::config_t occupancy_sensor_config; occupancy_sensor_config.occupancy_sensing.occupancy = _occupancyState; occupancy_sensor_config.occupancy_sensing.occupancy_sensor_type = _occupancySensorType; diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp index 1071b595e5b..3faba821528 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp @@ -59,8 +59,13 @@ MatterOnOffLight::~MatterOnOffLight() { bool MatterOnOffLight::begin(bool initialState) { ArduinoMatter::_init(); - on_off_light::config_t light_config; + if (getEndPointId() != 0) { + log_e("Matter On-Off Light with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + on_off_light::config_t light_config; light_config.on_off.on_off = initialState; light_config.on_off.lighting.start_up_on_off = nullptr; onOffState = initialState; diff --git a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp index 546da3b04e9..9b08958684c 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp @@ -59,8 +59,13 @@ MatterOnOffPlugin::~MatterOnOffPlugin() { bool MatterOnOffPlugin::begin(bool initialState) { ArduinoMatter::_init(); - on_off_plugin_unit::config_t plugin_config; + if (getEndPointId() != 0) { + log_e("Matter On-Off Plugin with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + + on_off_plugin_unit::config_t plugin_config; plugin_config.on_off.on_off = initialState; plugin_config.on_off.lighting.start_up_on_off = nullptr; diff --git a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp index f246ea81223..86d245d4041 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterPressureSensor.cpp @@ -42,6 +42,11 @@ MatterPressureSensor::~MatterPressureSensor() { bool MatterPressureSensor::begin(int16_t _rawPressure) { ArduinoMatter::_init(); + if (getEndPointId() != 0) { + log_e("Matter Pressure Sensor with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + pressure_sensor::config_t pressure_sensor_config; pressure_sensor_config.pressure_measurement.pressure_measured_value = _rawPressure; pressure_sensor_config.pressure_measurement.pressure_min_measured_value = nullptr; diff --git a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp index 6f59a5b2426..903e6b98d98 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterTemperatureSensor.cpp @@ -42,6 +42,11 @@ MatterTemperatureSensor::~MatterTemperatureSensor() { bool MatterTemperatureSensor::begin(int16_t _rawTemperature) { ArduinoMatter::_init(); + if (getEndPointId() != 0) { + log_e("Temperature Sensor with Endpoint Id %d device has already been created.", getEndPointId()); + return false; + } + temperature_sensor::config_t temperature_sensor_config; temperature_sensor_config.temperature_measurement.measured_value = _rawTemperature; temperature_sensor_config.temperature_measurement.min_measured_value = nullptr; From 7302be97925072e87a3fa927c387e5082ee41b38 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 15:50:59 -0300 Subject: [PATCH 05/10] fix(matter): log_i() leftover --- libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 933b5a72367..8a9da918443 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -65,7 +65,7 @@ bool onOffLightCallback(bool state) { // Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined bool onIdentifyLightCallback(bool identifyIsActive) { - log_i("Identify Cluster is %s", identifyIsActive ? "Active" : "Inactive"); + Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); if (identifyIsActive) { // Start Blinking the light in loop() identifyFlag = true; From 1ef603aab7a47d58db94bfe48473404a7e03674f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:13:45 +0000 Subject: [PATCH 06/10] ci(pre-commit): Apply automatic fixes --- .../MatterOnIdentify/MatterOnIdentify.ino | 328 +++++++++--------- libraries/Matter/src/Matter.cpp | 2 +- libraries/Matter/src/MatterEndPoint.h | 1 - .../Matter/src/MatterEndpoints/MatterFan.cpp | 4 +- .../MatterEndpoints/MatterGenericSwitch.cpp | 2 +- 5 files changed, 168 insertions(+), 169 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 8a9da918443..6dce8d863a6 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -1,164 +1,164 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/* - * This example is the smallest code that will create a Matter Device which can be - * commissioned and controlled from a Matter Environment APP. - * It controls a GPIO that could be attached to a LED for visualization. - * Additionally the ESP32 will send debug messages indicating the Matter activity. - * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. - * - * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. - * It demonstrates how to use On Identify callback when the Identify Cluster is called. - * The Matter user APP can be used to request the device to identify itself by blinking the LED. - */ - -// Matter Manager -#include -#include - -// List of Matter Endpoints for this Node -// Single On/Off Light Endpoint - at least one per node -MatterOnOffLight OnOffLight; - -// WiFi is manually set and started -const char *ssid = "your-ssid"; // Change this to your WiFi SSID -const char *password = "your-password"; // Change this to your WiFi password - -// Light GPIO that can be controlled by Matter APP -#ifdef LED_BUILTIN -const uint8_t ledPin = LED_BUILTIN; -#else -const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN -#endif - -// set your board USER BUTTON pin here - decommissioning button -const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. - -// Button control - decommision the Matter Node -uint32_t button_time_stamp = 0; // debouncing control -bool button_state = false; // false = released | true = pressed -const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission - -// Identify Flag and blink time - Blink the LED -const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed -volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state -bool identifyBlink = false; // Blink state when in Identify state - -// Matter Protocol Endpoint (On/OFF Light) Callback -bool onOffLightCallback(bool state) { - digitalWrite(ledPin, state ? HIGH : LOW); - // This callback must return the success state to Matter core - return true; -} - -// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined -bool onIdentifyLightCallback(bool identifyIsActive) { - Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); - if (identifyIsActive) { - // Start Blinking the light in loop() - identifyFlag = true; - identifyBlink = !OnOffLight; // Start with the inverted light state - } else { - // Stop Blinking and restore the light to the its last state - identifyFlag = false; - // force returning to the original state by toggling the light twice - OnOffLight.toggle(); - OnOffLight.toggle(); - } - return true; -} - -void setup() { - // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node - pinMode(buttonPin, INPUT_PULLUP); - // Initialize the LED GPIO - pinMode(ledPin, OUTPUT); - - Serial.begin(115200); - - // Manually connect to WiFi - WiFi.begin(ssid, password); - // Wait for connection - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - // Initialize at least one Matter EndPoint - OnOffLight.begin(); - - // On Identify Callback - Blink the LED - OnOffLight.onIdentify(onIdentifyLightCallback); - - // Associate a callback to the Matter Controller - OnOffLight.onChange(onOffLightCallback); - - // Matter beginning - Last step, after all EndPoints are initialized - Matter.begin(); - - // Check Matter Accessory Commissioning state, which may change during execution of loop() - if (!Matter.isDeviceCommissioned()) { - Serial.println(""); - Serial.println("Matter Node is not commissioned yet."); - Serial.println("Initiate the device discovery in your Matter environment."); - Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); - Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); - Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); - // waits for Matter Occupancy Sensor Commissioning. - uint32_t timeCount = 0; - while (!Matter.isDeviceCommissioned()) { - delay(100); - if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec - Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); - } - } - Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); - } -} - -void loop() { - // check if the Ligth is in identify state and blink it every 500ms (delay loop time) - if (identifyFlag) { -#ifdef LED_BUILTIN - uint8_t brightness = 32 * identifyBlink; - rgbLedWrite(identifyLedPin, brightness, 0, 0); -#else - digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); -#endif - identifyBlink = !identifyBlink; - } - - // Check if the button has been pressed - if (digitalRead(buttonPin) == LOW && !button_state) { - // deals with button debouncing - button_time_stamp = millis(); // record the time while the button is pressed. - button_state = true; // pressed. - } - - if (digitalRead(buttonPin) == HIGH && button_state) { - button_state = false; // released - } - - // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node - uint32_t time_diff = millis() - button_time_stamp; - if (button_state && time_diff > decommissioningTimeout) { - Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); - Matter.decommission(); - button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so - } - - delay(500); // works as a debounce for the button and also for the LED blink -} +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is the smallest code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * It controls a GPIO that could be attached to a LED for visualization. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + * + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. + * It demonstrates how to use On Identify callback when the Identify Cluster is called. + * The Matter user APP can be used to request the device to identify itself by blinking the LED. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Single On/Off Light Endpoint - at least one per node +MatterOnOffLight OnOffLight; + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +// Light GPIO that can be controlled by Matter APP +#ifdef LED_BUILTIN +const uint8_t ledPin = LED_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#endif + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// Button control - decommision the Matter Node +uint32_t button_time_stamp = 0; // debouncing control +bool button_state = false; // false = released | true = pressed +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Identify Flag and blink time - Blink the LED +const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed +volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state +bool identifyBlink = false; // Blink state when in Identify state + +// Matter Protocol Endpoint (On/OFF Light) Callback +bool onOffLightCallback(bool state) { + digitalWrite(ledPin, state ? HIGH : LOW); + // This callback must return the success state to Matter core + return true; +} + +// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined +bool onIdentifyLightCallback(bool identifyIsActive) { + Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); + if (identifyIsActive) { + // Start Blinking the light in loop() + identifyFlag = true; + identifyBlink = !OnOffLight; // Start with the inverted light state + } else { + // Stop Blinking and restore the light to the its last state + identifyFlag = false; + // force returning to the original state by toggling the light twice + OnOffLight.toggle(); + OnOffLight.toggle(); + } + return true; +} + +void setup() { + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node + pinMode(buttonPin, INPUT_PULLUP); + // Initialize the LED GPIO + pinMode(ledPin, OUTPUT); + + Serial.begin(115200); + + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // On Identify Callback - Blink the LED + OnOffLight.onIdentify(onIdentifyLightCallback); + + // Associate a callback to the Matter Controller + OnOffLight.onChange(onOffLightCallback); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + // Check Matter Accessory Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Occupancy Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); + } +} + +void loop() { + // check if the Ligth is in identify state and blink it every 500ms (delay loop time) + if (identifyFlag) { +#ifdef LED_BUILTIN + uint8_t brightness = 32 * identifyBlink; + rgbLedWrite(identifyLedPin, brightness, 0, 0); +#else + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); +#endif + identifyBlink = !identifyBlink; + } + + // Check if the button has been pressed + if (digitalRead(buttonPin) == LOW && !button_state) { + // deals with button debouncing + button_time_stamp = millis(); // record the time while the button is pressed. + button_state = true; // pressed. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so + } + + delay(500); // works as a debounce for the button and also for the LED blink +} diff --git a/libraries/Matter/src/Matter.cpp b/libraries/Matter/src/Matter.cpp index 1c80bff941e..af7c4c8657e 100644 --- a/libraries/Matter/src/Matter.cpp +++ b/libraries/Matter/src/Matter.cpp @@ -81,7 +81,7 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin if (ep != NULL) { err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive) ? ESP_OK : ESP_FAIL; } - + return err; } diff --git a/libraries/Matter/src/MatterEndPoint.h b/libraries/Matter/src/MatterEndPoint.h index f269b713a5e..6f99aa7cd33 100644 --- a/libraries/Matter/src/MatterEndPoint.h +++ b/libraries/Matter/src/MatterEndPoint.h @@ -115,7 +115,6 @@ class MatterEndPoint { _onEndPointIdentifyCB = onEndPointIdentifyCB; } - protected: uint16_t endpoint_id = 0; EndPointIdentifyCB _onEndPointIdentifyCB = NULL; diff --git a/libraries/Matter/src/MatterEndpoints/MatterFan.cpp b/libraries/Matter/src/MatterEndpoints/MatterFan.cpp index f81d49f22a7..1647490aa05 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterFan.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterFan.cpp @@ -85,12 +85,12 @@ bool MatterFan::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uin bool MatterFan::begin(uint8_t percent, FanMode_t fanMode, FanModeSequence_t fanModeSeq) { ArduinoMatter::_init(); - if (getEndPointId() != 0) { + if (getEndPointId() != 0) { log_e("Matter Fan with Endpoint Id %d device has already been created.", getEndPointId()); return false; } - // endpoint handles can be used to add/modify clusters. + // endpoint handles can be used to add/modify clusters. fan::config_t fan_config; fan_config.fan_control.fan_mode = fanMode; fan_config.fan_control.percent_current = percent; diff --git a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp index c8f2c4ec444..e20479af088 100644 --- a/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp +++ b/libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp @@ -43,7 +43,7 @@ bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t clust bool MatterGenericSwitch::begin() { ArduinoMatter::_init(); - if (getEndPointId() != 0) { + if (getEndPointId() != 0) { log_e("Matter Generic Switch with Endpoint Id %d device has already been created.", getEndPointId()); return false; } From 017f2df8efb8a58966f945962cb30f86452ff6d0 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 16:19:47 -0300 Subject: [PATCH 07/10] fix(matter): ci codespell --- .../MatterOnIdentify/MatterOnIdentify.ino | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 6dce8d863a6..f8f708ed5b2 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -1,3 +1,4 @@ +<<<<<<< Updated upstream // Copyright 2024 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -162,3 +163,169 @@ void loop() { delay(500); // works as a debounce for the button and also for the LED blink } +======= +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is the smallest code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * It controls a GPIO that could be attached to a LED for visualization. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + * + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. + * It demonstrates how to use On Identify callback when the Identify Cluster is called. + * The Matter user APP can be used to request the device to identify itself by blinking the LED. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Single On/Off Light Endpoint - at least one per node +MatterOnOffLight OnOffLight; + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +// Light GPIO that can be controlled by Matter APP +#ifdef LED_BUILTIN +const uint8_t ledPin = LED_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#endif + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// Button control - decommision the Matter Node +uint32_t button_time_stamp = 0; // debouncing control +bool button_state = false; // false = released | true = pressed +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Identify Flag and blink time - Blink the LED +const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed +volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state +bool identifyBlink = false; // Blink state when in Identify state + +// Matter Protocol Endpoint (On/OFF Light) Callback +bool onOffLightCallback(bool state) { + digitalWrite(ledPin, state ? HIGH : LOW); + // This callback must return the success state to Matter core + return true; +} + +// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined +bool onIdentifyLightCallback(bool identifyIsActive) { + Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); + if (identifyIsActive) { + // Start Blinking the light in loop() + identifyFlag = true; + identifyBlink = !OnOffLight; // Start with the inverted light state + } else { + // Stop Blinking and restore the light to the its last state + identifyFlag = false; + // force returning to the original state by toggling the light twice + OnOffLight.toggle(); + OnOffLight.toggle(); + } + return true; +} + +void setup() { + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node + pinMode(buttonPin, INPUT_PULLUP); + // Initialize the LED GPIO + pinMode(ledPin, OUTPUT); + + Serial.begin(115200); + + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // On Identify Callback - Blink the LED + OnOffLight.onIdentify(onIdentifyLightCallback); + + // Associate a callback to the Matter Controller + OnOffLight.onChange(onOffLightCallback); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + // Check Matter Accessory Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Occupancy Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); + } +} + +void loop() { + // check if the Light is in identify state and blink it every 500ms (delay loop time) + if (identifyFlag) { +#ifdef LED_BUILTIN + uint8_t brightness = 32 * identifyBlink; + rgbLedWrite(identifyLedPin, brightness, 0, 0); +#else + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); +#endif + identifyBlink = !identifyBlink; + } + + // Check if the button has been pressed + if (digitalRead(buttonPin) == LOW && !button_state) { + // deals with button debouncing + button_time_stamp = millis(); // record the time while the button is pressed. + button_state = true; // pressed. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so + } + + delay(500); // works as a debounce for the button and also for the LED blink +} +>>>>>>> Stashed changes From bce01087d282870311b03edfdfa65850b2e8f2c8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:20:35 +0000 Subject: [PATCH 08/10] ci(pre-commit): Apply automatic fixes --- .../MatterOnIdentify/MatterOnIdentify.ino | 328 +++++++++--------- 1 file changed, 164 insertions(+), 164 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index f8f708ed5b2..2779cb4cda7 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -164,168 +164,168 @@ void loop() { delay(500); // works as a debounce for the button and also for the LED blink } ======= -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/* - * This example is the smallest code that will create a Matter Device which can be - * commissioned and controlled from a Matter Environment APP. - * It controls a GPIO that could be attached to a LED for visualization. - * Additionally the ESP32 will send debug messages indicating the Matter activity. - * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. - * - * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. - * It demonstrates how to use On Identify callback when the Identify Cluster is called. - * The Matter user APP can be used to request the device to identify itself by blinking the LED. - */ - -// Matter Manager -#include -#include - -// List of Matter Endpoints for this Node -// Single On/Off Light Endpoint - at least one per node -MatterOnOffLight OnOffLight; - -// WiFi is manually set and started -const char *ssid = "your-ssid"; // Change this to your WiFi SSID -const char *password = "your-password"; // Change this to your WiFi password - -// Light GPIO that can be controlled by Matter APP -#ifdef LED_BUILTIN -const uint8_t ledPin = LED_BUILTIN; -#else -const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN -#endif - -// set your board USER BUTTON pin here - decommissioning button -const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. - -// Button control - decommision the Matter Node -uint32_t button_time_stamp = 0; // debouncing control -bool button_state = false; // false = released | true = pressed -const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission - -// Identify Flag and blink time - Blink the LED -const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed -volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state -bool identifyBlink = false; // Blink state when in Identify state - -// Matter Protocol Endpoint (On/OFF Light) Callback -bool onOffLightCallback(bool state) { - digitalWrite(ledPin, state ? HIGH : LOW); - // This callback must return the success state to Matter core - return true; -} - -// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined -bool onIdentifyLightCallback(bool identifyIsActive) { - Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); - if (identifyIsActive) { - // Start Blinking the light in loop() - identifyFlag = true; - identifyBlink = !OnOffLight; // Start with the inverted light state - } else { - // Stop Blinking and restore the light to the its last state - identifyFlag = false; - // force returning to the original state by toggling the light twice - OnOffLight.toggle(); - OnOffLight.toggle(); - } - return true; -} - -void setup() { - // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node - pinMode(buttonPin, INPUT_PULLUP); - // Initialize the LED GPIO - pinMode(ledPin, OUTPUT); - - Serial.begin(115200); - - // Manually connect to WiFi - WiFi.begin(ssid, password); - // Wait for connection - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - // Initialize at least one Matter EndPoint - OnOffLight.begin(); - - // On Identify Callback - Blink the LED - OnOffLight.onIdentify(onIdentifyLightCallback); - - // Associate a callback to the Matter Controller - OnOffLight.onChange(onOffLightCallback); - - // Matter beginning - Last step, after all EndPoints are initialized - Matter.begin(); - - // Check Matter Accessory Commissioning state, which may change during execution of loop() - if (!Matter.isDeviceCommissioned()) { - Serial.println(""); - Serial.println("Matter Node is not commissioned yet."); - Serial.println("Initiate the device discovery in your Matter environment."); - Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); - Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); - Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); - // waits for Matter Occupancy Sensor Commissioning. - uint32_t timeCount = 0; - while (!Matter.isDeviceCommissioned()) { - delay(100); - if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec - Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); - } - } - Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); - } -} - -void loop() { - // check if the Light is in identify state and blink it every 500ms (delay loop time) - if (identifyFlag) { -#ifdef LED_BUILTIN - uint8_t brightness = 32 * identifyBlink; - rgbLedWrite(identifyLedPin, brightness, 0, 0); -#else - digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); -#endif - identifyBlink = !identifyBlink; - } - - // Check if the button has been pressed - if (digitalRead(buttonPin) == LOW && !button_state) { - // deals with button debouncing - button_time_stamp = millis(); // record the time while the button is pressed. - button_state = true; // pressed. - } - - if (digitalRead(buttonPin) == HIGH && button_state) { - button_state = false; // released - } - - // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node - uint32_t time_diff = millis() - button_time_stamp; - if (button_state && time_diff > decommissioningTimeout) { - Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); - Matter.decommission(); - button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so - } - - delay(500); // works as a debounce for the button and also for the LED blink -} +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * This example is the smallest code that will create a Matter Device which can be + * commissioned and controlled from a Matter Environment APP. + * It controls a GPIO that could be attached to a LED for visualization. + * Additionally the ESP32 will send debug messages indicating the Matter activity. + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. + * + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. + * It demonstrates how to use On Identify callback when the Identify Cluster is called. + * The Matter user APP can be used to request the device to identify itself by blinking the LED. + */ + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Single On/Off Light Endpoint - at least one per node +MatterOnOffLight OnOffLight; + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +// Light GPIO that can be controlled by Matter APP +#ifdef LED_BUILTIN +const uint8_t ledPin = LED_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#endif + +// set your board USER BUTTON pin here - decommissioning button +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// Button control - decommision the Matter Node +uint32_t button_time_stamp = 0; // debouncing control +bool button_state = false; // false = released | true = pressed +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Identify Flag and blink time - Blink the LED +const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed +volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state +bool identifyBlink = false; // Blink state when in Identify state + +// Matter Protocol Endpoint (On/OFF Light) Callback +bool onOffLightCallback(bool state) { + digitalWrite(ledPin, state ? HIGH : LOW); + // This callback must return the success state to Matter core + return true; +} + +// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined +bool onIdentifyLightCallback(bool identifyIsActive) { + Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); + if (identifyIsActive) { + // Start Blinking the light in loop() + identifyFlag = true; + identifyBlink = !OnOffLight; // Start with the inverted light state + } else { + // Stop Blinking and restore the light to the its last state + identifyFlag = false; + // force returning to the original state by toggling the light twice + OnOffLight.toggle(); + OnOffLight.toggle(); + } + return true; +} + +void setup() { + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node + pinMode(buttonPin, INPUT_PULLUP); + // Initialize the LED GPIO + pinMode(ledPin, OUTPUT); + + Serial.begin(115200); + + // Manually connect to WiFi + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // On Identify Callback - Blink the LED + OnOffLight.onIdentify(onIdentifyLightCallback); + + // Associate a callback to the Matter Controller + OnOffLight.onChange(onOffLightCallback); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + + // Check Matter Accessory Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Occupancy Sensor Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); + } +} + +void loop() { + // check if the Light is in identify state and blink it every 500ms (delay loop time) + if (identifyFlag) { +#ifdef LED_BUILTIN + uint8_t brightness = 32 * identifyBlink; + rgbLedWrite(identifyLedPin, brightness, 0, 0); +#else + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); +#endif + identifyBlink = !identifyBlink; + } + + // Check if the button has been pressed + if (digitalRead(buttonPin) == LOW && !button_state) { + // deals with button debouncing + button_time_stamp = millis(); // record the time while the button is pressed. + button_state = true; // pressed. + } + + if (digitalRead(buttonPin) == HIGH && button_state) { + button_state = false; // released + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > decommissioningTimeout) { + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + Matter.decommission(); + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so + } + + delay(500); // works as a debounce for the button and also for the LED blink +} >>>>>>> Stashed changes From e2dc5b60f351c943b6c6f0a95135545f5a3aae05 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 16 Dec 2024 16:28:51 -0300 Subject: [PATCH 09/10] fix(matter): stashing merge error --- .../MatterOnIdentify/MatterOnIdentify.ino | 167 ------------------ 1 file changed, 167 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 2779cb4cda7..47183241c17 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -1,169 +1,3 @@ -<<<<<<< Updated upstream -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/* - * This example is the smallest code that will create a Matter Device which can be - * commissioned and controlled from a Matter Environment APP. - * It controls a GPIO that could be attached to a LED for visualization. - * Additionally the ESP32 will send debug messages indicating the Matter activity. - * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. - * - * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. - * It demonstrates how to use On Identify callback when the Identify Cluster is called. - * The Matter user APP can be used to request the device to identify itself by blinking the LED. - */ - -// Matter Manager -#include -#include - -// List of Matter Endpoints for this Node -// Single On/Off Light Endpoint - at least one per node -MatterOnOffLight OnOffLight; - -// WiFi is manually set and started -const char *ssid = "your-ssid"; // Change this to your WiFi SSID -const char *password = "your-password"; // Change this to your WiFi password - -// Light GPIO that can be controlled by Matter APP -#ifdef LED_BUILTIN -const uint8_t ledPin = LED_BUILTIN; -#else -const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN -#endif - -// set your board USER BUTTON pin here - decommissioning button -const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. - -// Button control - decommision the Matter Node -uint32_t button_time_stamp = 0; // debouncing control -bool button_state = false; // false = released | true = pressed -const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission - -// Identify Flag and blink time - Blink the LED -const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed -volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state -bool identifyBlink = false; // Blink state when in Identify state - -// Matter Protocol Endpoint (On/OFF Light) Callback -bool onOffLightCallback(bool state) { - digitalWrite(ledPin, state ? HIGH : LOW); - // This callback must return the success state to Matter core - return true; -} - -// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined -bool onIdentifyLightCallback(bool identifyIsActive) { - Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); - if (identifyIsActive) { - // Start Blinking the light in loop() - identifyFlag = true; - identifyBlink = !OnOffLight; // Start with the inverted light state - } else { - // Stop Blinking and restore the light to the its last state - identifyFlag = false; - // force returning to the original state by toggling the light twice - OnOffLight.toggle(); - OnOffLight.toggle(); - } - return true; -} - -void setup() { - // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node - pinMode(buttonPin, INPUT_PULLUP); - // Initialize the LED GPIO - pinMode(ledPin, OUTPUT); - - Serial.begin(115200); - - // Manually connect to WiFi - WiFi.begin(ssid, password); - // Wait for connection - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - // Initialize at least one Matter EndPoint - OnOffLight.begin(); - - // On Identify Callback - Blink the LED - OnOffLight.onIdentify(onIdentifyLightCallback); - - // Associate a callback to the Matter Controller - OnOffLight.onChange(onOffLightCallback); - - // Matter beginning - Last step, after all EndPoints are initialized - Matter.begin(); - - // Check Matter Accessory Commissioning state, which may change during execution of loop() - if (!Matter.isDeviceCommissioned()) { - Serial.println(""); - Serial.println("Matter Node is not commissioned yet."); - Serial.println("Initiate the device discovery in your Matter environment."); - Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); - Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); - Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); - // waits for Matter Occupancy Sensor Commissioning. - uint32_t timeCount = 0; - while (!Matter.isDeviceCommissioned()) { - delay(100); - if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec - Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); - } - } - Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); - } -} - -void loop() { - // check if the Ligth is in identify state and blink it every 500ms (delay loop time) - if (identifyFlag) { -#ifdef LED_BUILTIN - uint8_t brightness = 32 * identifyBlink; - rgbLedWrite(identifyLedPin, brightness, 0, 0); -#else - digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); -#endif - identifyBlink = !identifyBlink; - } - - // Check if the button has been pressed - if (digitalRead(buttonPin) == LOW && !button_state) { - // deals with button debouncing - button_time_stamp = millis(); // record the time while the button is pressed. - button_state = true; // pressed. - } - - if (digitalRead(buttonPin) == HIGH && button_state) { - button_state = false; // released - } - - // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node - uint32_t time_diff = millis() - button_time_stamp; - if (button_state && time_diff > decommissioningTimeout) { - Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); - Matter.decommission(); - button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so - } - - delay(500); // works as a debounce for the button and also for the LED blink -} -======= // Copyright 2024 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -328,4 +162,3 @@ void loop() { delay(500); // works as a debounce for the button and also for the LED blink } ->>>>>>> Stashed changes From 71e57eaf52e6d53dcdaeeaa613b91a81234fce01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:29:30 +0000 Subject: [PATCH 10/10] ci(pre-commit): Apply automatic fixes --- .../Matter/examples/MatterOnIdentify/MatterOnIdentify.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino index 47183241c17..b2e77900e95 100644 --- a/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino +++ b/libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino @@ -69,7 +69,7 @@ bool onIdentifyLightCallback(bool identifyIsActive) { if (identifyIsActive) { // Start Blinking the light in loop() identifyFlag = true; - identifyBlink = !OnOffLight; // Start with the inverted light state + identifyBlink = !OnOffLight; // Start with the inverted light state } else { // Stop Blinking and restore the light to the its last state identifyFlag = false; @@ -136,7 +136,7 @@ void loop() { uint8_t brightness = 32 * identifyBlink; rgbLedWrite(identifyLedPin, brightness, 0, 0); #else - digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); #endif identifyBlink = !identifyBlink; } @@ -160,5 +160,5 @@ void loop() { button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so } - delay(500); // works as a debounce for the button and also for the LED blink + delay(500); // works as a debounce for the button and also for the LED blink }