Skip to content

Commit 05a07fc

Browse files
committed
feat(zigbee): Add setLight APIs to manually operate lights
1 parent f5ce3f7 commit 05a07fc

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

Diff for: libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_EN
4141

4242
/********************* RGB LED functions **************************/
4343
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) {
44+
if(!state) {
45+
rgbLedWrite(LED_PIN, 0, 0, 0);
46+
return;
47+
}
4448
float brightness = (float)level / 255;
4549
rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness);
4650
}
@@ -98,6 +102,8 @@ void loop() {
98102
Zigbee.factoryReset();
99103
}
100104
}
105+
// Increase blightness by 50 every time the button is pressed
106+
zbColorLight.setLightLevel(zbColorLight.getLightLevel() + 50);
101107
}
102108
delay(100);
103109
}

Diff for: libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void loop() {
8181
Zigbee.factoryReset();
8282
}
8383
}
84+
// Toggle light by pressing the button
85+
zbLight.setLight(!zbLight.getLightState());
8486
}
8587
delay(100);
8688
}

Diff for: libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ void ZigbeeColorDimmableLight::calculateRGB(uint16_t x, uint16_t y, uint8_t &red
4747
blue = (uint8_t)(b * (float)255);
4848
}
4949

50+
void ZigbeeColorDimmableLight::calculateXY(uint8_t red, uint8_t green, uint8_t blue, uint16_t &x, uint16_t &y) {
51+
// Convert RGB to XYZ
52+
float r = (float)red / 255.0f;
53+
float g = (float)green / 255.0f;
54+
float b = (float)blue / 255.0f;
55+
56+
float X, Y, Z;
57+
RGB_TO_XYZ(r, g, b, X, Y, Z);
58+
59+
// Convert XYZ to xy chromaticity coordinates
60+
float color_x = X / (X + Y + Z);
61+
float color_y = Y / (X + Y + Z);
62+
63+
// Convert normalized xy to 16-bit values
64+
x = (uint16_t)(color_x * 65535.0f);
65+
y = (uint16_t)(color_y * 65535.0f);
66+
}
67+
5068
//set attribute method -> method overridden in child class
5169
void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {
5270
//check the data and call right method
@@ -109,4 +127,48 @@ void ZigbeeColorDimmableLight::lightChanged() {
109127
}
110128
}
111129

130+
void ZigbeeColorDimmableLight::setLight(bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue) {
131+
//Update all attributes
132+
_current_state = state;
133+
_current_level = level;
134+
_current_red = red;
135+
_current_green = green;
136+
_current_blue = blue;
137+
lightChanged();
138+
139+
log_v("Updating on/off light state to %d", state);
140+
/* Update light clusters */
141+
esp_zb_lock_acquire(portMAX_DELAY);
142+
//set on/off state
143+
esp_zb_zcl_set_attribute_val(
144+
_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
145+
);
146+
//set level
147+
esp_zb_zcl_set_attribute_val(
148+
_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
149+
);
150+
//set color
151+
uint16_t color_x, color_y;
152+
calculateXY(red, green, blue, color_x, color_y);
153+
esp_zb_zcl_set_attribute_val(
154+
_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
155+
);
156+
esp_zb_zcl_set_attribute_val(
157+
_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
158+
);
159+
esp_zb_lock_release();
160+
}
161+
162+
void ZigbeeColorDimmableLight::setLightState(bool state) {
163+
setLight(state, _current_level, _current_red, _current_green, _current_blue);
164+
}
165+
166+
void ZigbeeColorDimmableLight::setLightLevel(uint8_t level) {
167+
setLight(_current_state, level, _current_red, _current_green, _current_blue);
168+
}
169+
170+
void ZigbeeColorDimmableLight::setLightColor(uint8_t red, uint8_t green, uint8_t blue) {
171+
setLight(_current_state, _current_level, red, green, blue);
172+
}
173+
112174
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

Diff for: libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h

+22
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,31 @@ class ZigbeeColorDimmableLight : public ZigbeeEP {
2121
lightChanged();
2222
}
2323

24+
void setLightState(bool state);
25+
void setLightLevel(uint8_t level);
26+
void setLightColor(uint8_t red, uint8_t green, uint8_t blue);
27+
void setLight(bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue);
28+
29+
bool getLightState() {
30+
return _current_state;
31+
}
32+
uint8_t getLightLevel() {
33+
return _current_level;
34+
}
35+
uint8_t getLightRed() {
36+
return _current_red;
37+
}
38+
uint8_t getLightGreen() {
39+
return _current_green;
40+
}
41+
uint8_t getLightBlue() {
42+
return _current_blue;
43+
}
44+
2445
private:
2546
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;
2647
void calculateRGB(uint16_t x, uint16_t y, uint8_t &red, uint8_t &green, uint8_t &blue);
48+
void calculateXY(uint8_t red, uint8_t green, uint8_t blue, uint16_t &x, uint16_t &y);
2749

2850
uint16_t getCurrentColorX();
2951
uint16_t getCurrentColorY();

Diff for: libraries/Zigbee/src/ep/ZigbeeLight.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ void ZigbeeLight::lightChanged() {
3333
}
3434
}
3535

36+
void ZigbeeLight::setLight(bool state) {
37+
_current_state = state;
38+
lightChanged();
39+
40+
log_v("Updating on/off light state to %d", state);
41+
/* Update on/off light state */
42+
esp_zb_lock_acquire(portMAX_DELAY);
43+
esp_zb_zcl_set_attribute_val(
44+
_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
45+
);
46+
esp_zb_lock_release();
47+
}
48+
3649
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

Diff for: libraries/Zigbee/src/ep/ZigbeeLight.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ class ZigbeeLight : public ZigbeeEP {
1414
ZigbeeLight(uint8_t endpoint);
1515
~ZigbeeLight();
1616

17-
// Use tp set a cb function to be called on light change
17+
// Use to set a cb function to be called on light change
1818
void onLightChange(void (*callback)(bool)) {
1919
_on_light_change = callback;
2020
}
21+
// Use to restore light state
2122
void restoreLight() {
2223
lightChanged();
2324
}
25+
// Use to set light state after turning on/off manually in the device
26+
void setLight(bool state);
27+
28+
bool getLightState() {
29+
return _current_state;
30+
}
2431

2532
private:
2633
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;

0 commit comments

Comments
 (0)