From cdc1a42084b9e3d21035895d95210505f192c229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:22:56 +0100 Subject: [PATCH 1/2] feat(zigbee): Add setLight APIs to manually operate lights --- .../Zigbee_Color_Dimmable_Light.ino | 6 ++ .../Zigbee_On_Off_Light.ino | 2 + .../src/ep/ZigbeeColorDimmableLight.cpp | 62 +++++++++++++++++++ .../Zigbee/src/ep/ZigbeeColorDimmableLight.h | 22 +++++++ libraries/Zigbee/src/ep/ZigbeeLight.cpp | 13 ++++ libraries/Zigbee/src/ep/ZigbeeLight.h | 9 ++- 6 files changed, 113 insertions(+), 1 deletion(-) diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino index c03d26d3aba..a515b39cf6e 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino @@ -41,6 +41,10 @@ ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_EN /********************* RGB LED functions **************************/ void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) { + if(!state) { + rgbLedWrite(LED_PIN, 0, 0, 0); + return; + } float brightness = (float)level / 255; rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness); } @@ -98,6 +102,8 @@ void loop() { Zigbee.factoryReset(); } } + // Increase blightness by 50 every time the button is pressed + zbColorLight.setLightLevel(zbColorLight.getLightLevel() + 50); } delay(100); } diff --git a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino index 30e3cd2d109..53d43895365 100644 --- a/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino @@ -81,6 +81,8 @@ void loop() { Zigbee.factoryReset(); } } + // Toggle light by pressing the button + zbLight.setLight(!zbLight.getLightState()); } delay(100); } diff --git a/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp b/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp index 08828f7c280..f034daba54a 100644 --- a/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp @@ -47,6 +47,24 @@ void ZigbeeColorDimmableLight::calculateRGB(uint16_t x, uint16_t y, uint8_t &red blue = (uint8_t)(b * (float)255); } +void ZigbeeColorDimmableLight::calculateXY(uint8_t red, uint8_t green, uint8_t blue, uint16_t &x, uint16_t &y) { + // Convert RGB to XYZ + float r = (float)red / 255.0f; + float g = (float)green / 255.0f; + float b = (float)blue / 255.0f; + + float X, Y, Z; + RGB_TO_XYZ(r, g, b, X, Y, Z); + + // Convert XYZ to xy chromaticity coordinates + float color_x = X / (X + Y + Z); + float color_y = Y / (X + Y + Z); + + // Convert normalized xy to 16-bit values + x = (uint16_t)(color_x * 65535.0f); + y = (uint16_t)(color_y * 65535.0f); +} + //set attribute method -> method overridden in child class void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { //check the data and call right method @@ -109,4 +127,48 @@ void ZigbeeColorDimmableLight::lightChanged() { } } +void ZigbeeColorDimmableLight::setLight(bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue) { + //Update all attributes + _current_state = state; + _current_level = level; + _current_red = red; + _current_green = green; + _current_blue = blue; + lightChanged(); + + log_v("Updating on/off light state to %d", state); + /* Update light clusters */ + esp_zb_lock_acquire(portMAX_DELAY); + //set on/off state + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ON_OFF, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID, &_current_state, false + ); + //set level + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID, &_current_level, false + ); + //set color + uint16_t color_x, color_y; + calculateXY(red, green, blue, color_x, color_y); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_X_ID, &color_x, false + ); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_Y_ID, &color_y, false + ); + esp_zb_lock_release(); +} + +void ZigbeeColorDimmableLight::setLightState(bool state) { + setLight(state, _current_level, _current_red, _current_green, _current_blue); +} + +void ZigbeeColorDimmableLight::setLightLevel(uint8_t level) { + setLight(_current_state, level, _current_red, _current_green, _current_blue); +} + +void ZigbeeColorDimmableLight::setLightColor(uint8_t red, uint8_t green, uint8_t blue) { + setLight(_current_state, _current_level, red, green, blue); +} + #endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h b/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h index 1edb6b5468c..9fa59dcfffc 100644 --- a/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h +++ b/libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h @@ -21,9 +21,31 @@ class ZigbeeColorDimmableLight : public ZigbeeEP { lightChanged(); } + void setLightState(bool state); + void setLightLevel(uint8_t level); + void setLightColor(uint8_t red, uint8_t green, uint8_t blue); + void setLight(bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue); + + bool getLightState() { + return _current_state; + } + uint8_t getLightLevel() { + return _current_level; + } + uint8_t getLightRed() { + return _current_red; + } + uint8_t getLightGreen() { + return _current_green; + } + uint8_t getLightBlue() { + return _current_blue; + } + private: void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override; void calculateRGB(uint16_t x, uint16_t y, uint8_t &red, uint8_t &green, uint8_t &blue); + void calculateXY(uint8_t red, uint8_t green, uint8_t blue, uint16_t &x, uint16_t &y); uint16_t getCurrentColorX(); uint16_t getCurrentColorY(); diff --git a/libraries/Zigbee/src/ep/ZigbeeLight.cpp b/libraries/Zigbee/src/ep/ZigbeeLight.cpp index 6b602db35c1..100efe34a86 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLight.cpp +++ b/libraries/Zigbee/src/ep/ZigbeeLight.cpp @@ -33,4 +33,17 @@ void ZigbeeLight::lightChanged() { } } +void ZigbeeLight::setLight(bool state) { + _current_state = state; + lightChanged(); + + log_v("Updating on/off light state to %d", state); + /* Update on/off light state */ + esp_zb_lock_acquire(portMAX_DELAY); + esp_zb_zcl_set_attribute_val( + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ON_OFF, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID, &_current_state, false + ); + esp_zb_lock_release(); +} + #endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED diff --git a/libraries/Zigbee/src/ep/ZigbeeLight.h b/libraries/Zigbee/src/ep/ZigbeeLight.h index 1f57bbf66e5..9b8fc409d4a 100644 --- a/libraries/Zigbee/src/ep/ZigbeeLight.h +++ b/libraries/Zigbee/src/ep/ZigbeeLight.h @@ -14,13 +14,20 @@ class ZigbeeLight : public ZigbeeEP { ZigbeeLight(uint8_t endpoint); ~ZigbeeLight(); - // Use tp set a cb function to be called on light change + // Use to set a cb function to be called on light change void onLightChange(void (*callback)(bool)) { _on_light_change = callback; } + // Use to restore light state void restoreLight() { lightChanged(); } + // Use to control light state + void setLight(bool state); + // Use to get light state + bool getLightState() { + return _current_state; + } private: void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override; From 3be6664c0b6d68bc9a7c35307cc26ac76c8e159d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 08:38:39 +0000 Subject: [PATCH 2/2] ci(pre-commit): Apply automatic fixes --- .../Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino index a515b39cf6e..7acf6e362df 100644 --- a/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino +++ b/libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino @@ -41,7 +41,7 @@ ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_EN /********************* RGB LED functions **************************/ void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) { - if(!state) { + if (!state) { rgbLedWrite(LED_PIN, 0, 0, 0); return; }