@@ -13,9 +13,7 @@ ZigbeeColorDimmableLight::ZigbeeColorDimmableLight(uint8_t endpoint) : ZigbeeEP(
13
13
// set default values
14
14
_current_state = false ;
15
15
_current_level = 255 ;
16
- _current_red = 255 ;
17
- _current_green = 255 ;
18
- _current_blue = 255 ;
16
+ _current_color = {255 , 255 , 255 };
19
17
}
20
18
21
19
uint16_t ZigbeeColorDimmableLight::getCurrentColorX () {
@@ -32,39 +30,27 @@ uint16_t ZigbeeColorDimmableLight::getCurrentColorY() {
32
30
->data_p );
33
31
}
34
32
35
- void ZigbeeColorDimmableLight::calculateRGB (uint16_t x, uint16_t y, uint8_t &red, uint8_t &green, uint8_t &blue) {
36
- float r, g, b, color_x, color_y;
37
- color_x = (float )x / 65535 ;
38
- color_y = (float )y / 65535 ;
39
-
40
- float color_X = color_x / color_y;
41
- float color_Z = (1 - color_x - color_y) / color_y;
42
-
43
- XYZ_TO_RGB (color_X, 1 , color_Z, r, g, b);
44
-
45
- red = (uint8_t )(r * (float )255 );
46
- green = (uint8_t )(g * (float )255 );
47
- blue = (uint8_t )(b * (float )255 );
33
+ uint16_t ZigbeeColorDimmableLight::getCurrentColorHue () {
34
+ return (*(uint16_t *)esp_zb_zcl_get_attribute (
35
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID
36
+ )
37
+ ->data_p );
48
38
}
49
39
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 );
40
+ uint8_t ZigbeeColorDimmableLight::getCurrentColorSaturation () {
41
+ return (*(uint16_t *)esp_zb_zcl_get_attribute (
42
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_SATURATION_ID
43
+ )
44
+ ->data_p );
66
45
}
67
46
47
+ // uint8_t ZigbeeColorDimmableLight::getCurrentColorValue() {
48
+ // return (*(uint16_t *)esp_zb_zcl_get_attribute(
49
+ // _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_COLOR_TEMPERATURE_ID
50
+ // )
51
+ // ->data_p);
52
+ // }
53
+
68
54
// set attribute method -> method overridden in child class
69
55
void ZigbeeColorDimmableLight::zbAttributeSet (const esp_zb_zcl_set_attr_value_message_t *message) {
70
56
// check the data and call right method
@@ -94,25 +80,32 @@ void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_me
94
80
uint16_t light_color_x = (*(uint16_t *)message->attribute .data .value );
95
81
uint16_t light_color_y = getCurrentColorY ();
96
82
// calculate RGB from XY and call setColor()
97
- uint8_t red, green, blue;
98
- calculateRGB (light_color_x, light_color_y, red, green, blue);
99
- _current_blue = blue;
100
- _current_green = green;
101
- _current_red = red;
83
+ _current_color = espXYToRgbColor (255 , light_color_x, light_color_y); // TODO: Check if level is correct
102
84
lightChanged ();
103
85
return ;
104
86
105
87
} else if (message->attribute .id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_Y_ID && message->attribute .data .type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
106
88
uint16_t light_color_x = getCurrentColorX ();
107
89
uint16_t light_color_y = (*(uint16_t *)message->attribute .data .value );
108
90
// calculate RGB from XY and call setColor()
109
- uint8_t red, green, blue;
110
- calculateRGB (light_color_x, light_color_y, red, green, blue);
111
- _current_blue = blue;
112
- _current_green = green;
113
- _current_red = red;
91
+ _current_color = espXYToRgbColor (255 , light_color_x, light_color_y); // TODO: Check if level is correct
114
92
lightChanged ();
115
93
return ;
94
+ } else if (message->attribute .id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID && message->attribute .data .type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
95
+ uint16_t light_color_hue = (*(uint16_t *)message->attribute .data .value );
96
+ _current_color = espHsvToRgbColor (light_color_hue, getCurrentColorSaturation (), 255 );
97
+ lightChanged ();
98
+ return ;
99
+ } else if (message->attribute .id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_SATURATION_ID && message->attribute .data .type == ESP_ZB_ZCL_ATTR_TYPE_U8) {
100
+ uint8_t light_color_saturation = (*(uint8_t *)message->attribute .data .value );
101
+ _current_color = espHsvToRgbColor (getCurrentColorHue (), light_color_saturation, 255 );
102
+ lightChanged ();
103
+ return ;
104
+ // } else if (message->attribute.id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_COLOR_TEMPERATURE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
105
+ // uint16_t light_color_temperature = (*(uint16_t *)message->attribute.data.value);
106
+ // _current_color = espHsvToRgbColor(getCurrentColorHue(), getCurrentColorSaturation(), light_color_temperature);
107
+ // lightChanged();
108
+ // return;
116
109
} else {
117
110
log_w (" Received message ignored. Attribute ID: %d not supported for Color Control" , message->attribute .id );
118
111
}
@@ -123,20 +116,21 @@ void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_me
123
116
124
117
void ZigbeeColorDimmableLight::lightChanged () {
125
118
if (_on_light_change) {
126
- _on_light_change (_current_state, _current_red, _current_green, _current_blue , _current_level);
119
+ _on_light_change (_current_state, _current_color. r , _current_color. g , _current_color. b , _current_level);
127
120
}
128
121
}
129
122
130
123
void ZigbeeColorDimmableLight::setLight (bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue) {
131
124
// Update all attributes
132
125
_current_state = state;
133
126
_current_level = level;
134
- _current_red = red;
135
- _current_green = green;
136
- _current_blue = blue;
127
+ _current_color = {red, green, blue};
137
128
lightChanged ();
138
129
139
- log_v (" Updating on/off light state to %d" , state);
130
+ espXyColor_t xy_color = espRgbColorToXYColor (_current_color);
131
+ espHsvColor_t hsv_color = espRgbColorToHsvColor (_current_color);
132
+
133
+ log_v (" Updating light state: %d, level: %d, color: %d, %d, %d" , state, level, red, green, blue);
140
134
/* Update light clusters */
141
135
esp_zb_lock_acquire (portMAX_DELAY);
142
136
// set on/off state
@@ -147,28 +141,45 @@ void ZigbeeColorDimmableLight::setLight(bool state, uint8_t level, uint8_t red,
147
141
esp_zb_zcl_set_attribute_val (
148
142
_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
143
);
150
- // set color
151
- uint16_t color_x, color_y;
152
- calculateXY (red, green, blue, color_x, color_y);
144
+ // set xy color
145
+ esp_zb_zcl_set_attribute_val (
146
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_X_ID, &xy_color.x , false
147
+ );
153
148
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
149
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_Y_ID , &xy_color. y , false
155
150
);
151
+ // set hsv color
156
152
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
153
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID , &hsv_color. h , false
158
154
);
155
+ esp_zb_zcl_set_attribute_val (
156
+ _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_SATURATION_ID, &hsv_color.s , false
157
+ );
158
+ // esp_zb_zcl_set_attribute_val(
159
+ // _endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_VALUE_ID, &hsv_color.v, false
160
+ // );
159
161
esp_zb_lock_release ();
160
162
}
161
163
162
164
void ZigbeeColorDimmableLight::setLightState (bool state) {
163
- setLight (state, _current_level, _current_red, _current_green, _current_blue );
165
+ setLight (state, _current_level, _current_color. r , _current_color. g , _current_color. b );
164
166
}
165
167
166
168
void ZigbeeColorDimmableLight::setLightLevel (uint8_t level) {
167
- setLight (_current_state, level, _current_red, _current_green, _current_blue );
169
+ setLight (_current_state, level, _current_color. r , _current_color. g , _current_color. b );
168
170
}
169
171
170
172
void ZigbeeColorDimmableLight::setLightColor (uint8_t red, uint8_t green, uint8_t blue) {
171
173
setLight (_current_state, _current_level, red, green, blue);
172
174
}
173
175
176
+ void ZigbeeColorDimmableLight::setLightColor (espRgbColor_t rgb_color) {
177
+ setLight (_current_state, _current_level, rgb_color.r , rgb_color.g , rgb_color.b );
178
+ }
179
+
180
+ void ZigbeeColorDimmableLight::setLightColor (espHsvColor_t hsv_color) {
181
+ espRgbColor_t rgb_color = espHsvColorToRgbColor (hsv_color);
182
+ setLight (_current_state, _current_level, rgb_color.r , rgb_color.g , rgb_color.b );
183
+ }
184
+
174
185
#endif // SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
0 commit comments