@@ -47,6 +47,24 @@ void ZigbeeColorDimmableLight::calculateRGB(uint16_t x, uint16_t y, uint8_t &red
47
47
blue = (uint8_t )(b * (float )255 );
48
48
}
49
49
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
+
50
68
// set attribute method -> method overridden in child class
51
69
void ZigbeeColorDimmableLight::zbAttributeSet (const esp_zb_zcl_set_attr_value_message_t *message) {
52
70
// check the data and call right method
@@ -109,4 +127,48 @@ void ZigbeeColorDimmableLight::lightChanged() {
109
127
}
110
128
}
111
129
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
+
112
174
#endif // SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
0 commit comments