Skip to content

Commit d9c8245

Browse files
committed
feat(zigbee): Use contexpr, cb typedefs, cb simplification
1 parent 59de367 commit d9c8245

File tree

3 files changed

+82
-35
lines changed

3 files changed

+82
-35
lines changed

docs/en/zigbee/ep_color_dimmable_light.rst

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ This function will return ``true`` if successful, ``false`` otherwise.
8989
Callback Functions
9090
******************
9191

92+
Callback Type Definitions
93+
^^^^^^^^^^^^^^^^^^^^^^^^^
94+
95+
For better type safety and readability, typedefs are provided for all callback functions:
96+
97+
.. code-block:: arduino
98+
99+
typedef void (*ZigbeeColorLightRgbCallback)(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level);
100+
typedef void (*ZigbeeColorLightHsvCallback)(bool state, uint8_t hue, uint8_t saturation, uint8_t value);
101+
typedef void (*ZigbeeColorLightTempCallback)(bool state, uint8_t level, uint16_t color_temperature);
102+
103+
These typedefs can be used instead of raw function pointer syntax for better code clarity.
104+
92105
onLightChange
93106
^^^^^^^^^^^^^
94107

@@ -102,6 +115,12 @@ Sets the legacy callback function for light state changes (RGB mode).
102115
103116
* ``callback`` - Function pointer to the light change callback (state, red, green, blue, level)
104117

118+
* ``state`` - Light state (true = on, false = off)
119+
* ``red`` - Red component (0-255)
120+
* ``green`` - Green component (0-255)
121+
* ``blue`` - Blue component (0-255)
122+
* ``level`` - Brightness level (0-255)
123+
105124
.. note::
106125
This method is deprecated. Please use ``onLightChangeRgb()`` for RGB/XY mode callbacks.
107126

@@ -112,9 +131,17 @@ Sets the callback function for RGB/XY color mode changes.
112131

113132
.. code-block:: arduino
114133
134+
void onLightChangeRgb(ZigbeeColorLightRgbCallback callback);
135+
// or using raw function pointer syntax:
115136
void onLightChangeRgb(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t));
116137
117138
* ``callback`` - Function pointer to the RGB light change callback (state, red, green, blue, level)
139+
140+
* ``state`` - Light state (true = on, false = off)
141+
* ``red`` - Red component (0-255)
142+
* ``green`` - Green component (0-255)
143+
* ``blue`` - Blue component (0-255)
144+
* ``level`` - Brightness level (0-255)
118145

119146
onLightChangeHsv
120147
^^^^^^^^^^^^^^^^
@@ -123,9 +150,16 @@ Sets the callback function for HSV (Hue/Saturation) color mode changes.
123150

124151
.. code-block:: arduino
125152
126-
void onLightChangeHsv(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t));
153+
void onLightChangeHsv(ZigbeeColorLightHsvCallback callback);
154+
// or using raw function pointer syntax:
155+
void onLightChangeHsv(void (*callback)(bool, uint8_t, uint8_t, uint8_t));
127156
128-
* ``callback`` - Function pointer to the HSV light change callback (state, hue, saturation, value, level)
157+
* ``callback`` - Function pointer to the HSV light change callback (state, hue, saturation, value)
158+
159+
* ``state`` - Light state (true = on, false = off)
160+
* ``hue`` - Hue component (0-254)
161+
* ``saturation`` - Saturation component (0-254)
162+
* ``value`` - Value/brightness component (0-255)
129163

130164
onLightChangeTemp
131165
^^^^^^^^^^^^^^^^^
@@ -134,9 +168,15 @@ Sets the callback function for color temperature mode changes.
134168

135169
.. code-block:: arduino
136170
171+
void onLightChangeTemp(ZigbeeColorLightTempCallback callback);
172+
// or using raw function pointer syntax:
137173
void onLightChangeTemp(void (*callback)(bool, uint8_t, uint16_t));
138174
139175
* ``callback`` - Function pointer to the temperature light change callback (state, level, temperature_mireds)
176+
177+
* ``state`` - Light state (true = on, false = off)
178+
* ``level`` - Brightness level (0-255)
179+
* ``temperature_mireds`` - Color temperature in mireds (inverse of Kelvin)
140180

141181
Control Methods
142182
***************

libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ ZigbeeColorDimmableLight::ZigbeeColorDimmableLight(uint8_t endpoint) : ZigbeeEP(
5454
_color_capabilities = ZIGBEE_COLOR_CAPABILITY_X_Y; //default XY color supported only
5555

5656
// Initialize callbacks to nullptr
57-
_on_light_change = nullptr;
5857
_on_light_change_rgb = nullptr;
5958
_on_light_change_hsv = nullptr;
6059
_on_light_change_temp = nullptr;
@@ -245,24 +244,15 @@ void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_me
245244
}
246245
}
247246

248-
void ZigbeeColorDimmableLight::lightChanged() {
249-
if (_on_light_change) {
250-
_on_light_change(_current_state, _current_color.r, _current_color.g, _current_color.b, _current_level);
251-
}
252-
}
253-
254247
void ZigbeeColorDimmableLight::lightChangedRgb() {
255248
if (_on_light_change_rgb) {
256249
_on_light_change_rgb(_current_state, _current_color.r, _current_color.g, _current_color.b, _current_level);
257-
} else if (_on_light_change) {
258-
// Fallback to legacy callback (deprecated) to maintain backward compatibility.
259-
_on_light_change(_current_state, _current_color.r, _current_color.g, _current_color.b, _current_level);
260250
}
261251
}
262252

263253
void ZigbeeColorDimmableLight::lightChangedHsv() {
264254
if (_on_light_change_hsv) {
265-
_on_light_change_hsv(_current_state, _current_hsv.h, _current_hsv.s, _current_hsv.v, _current_level);
255+
_on_light_change_hsv(_current_state, _current_hsv.h, _current_hsv.s, _current_hsv.v);
266256
}
267257
}
268258

@@ -309,8 +299,9 @@ bool ZigbeeColorDimmableLight::setLight(bool state, uint8_t level, uint8_t red,
309299

310300
espXyColor_t xy_color = espRgbColorToXYColor(_current_color);
311301
espHsvColor_t hsv_color = espRgbColorToHsvColor(_current_color);
312-
uint8_t hue = std::min((uint8_t)hsv_color.h, (uint8_t)254); // Clamp to 0-254
313-
uint8_t saturation = std::min((uint8_t)hsv_color.s, (uint8_t)254); // Clamp to 0-254
302+
// Clamp hue and saturation to valid Zigbee range (0-254, where 254 = 0xFE is max per ZCL spec)
303+
uint8_t hue = std::min(std::max((uint8_t)hsv_color.h, (uint8_t)0), (uint8_t)254);
304+
uint8_t saturation = std::min(std::max((uint8_t)hsv_color.s, (uint8_t)0), (uint8_t)254);
314305
// Update HSV state
315306
_current_hsv = hsv_color;
316307

@@ -398,16 +389,26 @@ bool ZigbeeColorDimmableLight::setLightColor(espHsvColor_t hsv_color) {
398389
log_e("Failed to set light color mode: %d", ZIGBEE_COLOR_MODE_HUE_SATURATION);
399390
return false;
400391
}
401-
// Update HSV state
392+
// Update HSV state and level from value component
402393
_current_hsv = hsv_color;
394+
_current_level = hsv_color.v; // Use HSV value component to update brightness level
403395
lightChangedHsv();
404396

405-
uint8_t hue = std::min((uint8_t)hsv_color.h, (uint8_t)254); // Clamp to 0-254
406-
uint8_t saturation = std::min((uint8_t)hsv_color.s, (uint8_t)254); // Clamp to 0-254
397+
// Clamp hue and saturation to valid Zigbee range (0-254, where 254 = 0xFE is max per ZCL spec)
398+
uint8_t hue = std::min(std::max((uint8_t)hsv_color.h, (uint8_t)0), (uint8_t)254);
399+
uint8_t saturation = std::min(std::max((uint8_t)hsv_color.s, (uint8_t)0), (uint8_t)254);
407400

408-
log_v("Updating light HSV: H=%d, S=%d, V=%d", hue, saturation, hsv_color.v);
401+
log_v("Updating light HSV: H=%d, S=%d, V=%d (level=%d)", hue, saturation, hsv_color.v, _current_level);
409402
/* Update light clusters */
410403
esp_zb_lock_acquire(portMAX_DELAY);
404+
//set level (brightness from HSV value component)
405+
ret = esp_zb_zcl_set_attribute_val(
406+
_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
407+
);
408+
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
409+
log_e("Failed to set light level: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret));
410+
goto unlock_and_return;
411+
}
411412
//set hue
412413
ret = esp_zb_zcl_set_attribute_val(
413414
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID, &hue, false

libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565
}
6666

6767
// Color capabilities bit flags (matching ZCL spec) - can be combined with bitwise OR
68-
#define ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION (1 << 0) // Bit 0: Hue/saturation supported
69-
#define ZIGBEE_COLOR_CAPABILITY_ENHANCED_HUE (1 << 1) // Bit 1: Enhanced hue supported
70-
#define ZIGBEE_COLOR_CAPABILITY_COLOR_LOOP (1 << 2) // Bit 2: Color loop supported
71-
#define ZIGBEE_COLOR_CAPABILITY_X_Y (1 << 3) // Bit 3: X/Y supported
72-
#define ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP (1 << 4) // Bit 4: Color temperature supported
68+
static constexpr uint16_t ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION = (1 << 0); // Bit 0: Hue/saturation supported
69+
static constexpr uint16_t ZIGBEE_COLOR_CAPABILITY_ENHANCED_HUE = (1 << 1); // Bit 1: Enhanced hue supported
70+
static constexpr uint16_t ZIGBEE_COLOR_CAPABILITY_COLOR_LOOP = (1 << 2); // Bit 2: Color loop supported
71+
static constexpr uint16_t ZIGBEE_COLOR_CAPABILITY_X_Y = (1 << 3); // Bit 3: X/Y supported
72+
static constexpr uint16_t ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP = (1 << 4); // Bit 4: Color temperature supported
7373

7474
// Color mode enum values (matching ZCL spec)
7575
enum ZigbeeColorMode {
@@ -78,6 +78,14 @@ enum ZigbeeColorMode {
7878
ZIGBEE_COLOR_MODE_TEMPERATURE = 0x02, // ColorTemperature
7979
};
8080

81+
// Callback function type definitions for better readability and type safety
82+
// RGB callback: (state, red, green, blue, level)
83+
typedef void (*ZigbeeColorLightRgbCallback)(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level);
84+
// HSV callback: (state, hue, saturation, value) - value represents brightness (0-255)
85+
typedef void (*ZigbeeColorLightHsvCallback)(bool state, uint8_t hue, uint8_t saturation, uint8_t value);
86+
// Temperature callback: (state, level, color_temperature_in_mireds)
87+
typedef void (*ZigbeeColorLightTempCallback)(bool state, uint8_t level, uint16_t color_temperature);
88+
8189
class ZigbeeColorDimmableLight : public ZigbeeEP {
8290
public:
8391
ZigbeeColorDimmableLight(uint8_t endpoint);
@@ -87,16 +95,16 @@ class ZigbeeColorDimmableLight : public ZigbeeEP {
8795
bool setLightColorCapabilities(uint16_t capabilities);
8896

8997
[[deprecated("Use onLightChangeRgb() instead. This will be removed in a future major version.")]]
90-
void onLightChange(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t)) {
91-
_on_light_change = callback;
98+
void onLightChange(ZigbeeColorLightRgbCallback callback) {
99+
_on_light_change_rgb = callback;
92100
}
93-
void onLightChangeRgb(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t)) {
101+
void onLightChangeRgb(ZigbeeColorLightRgbCallback callback) {
94102
_on_light_change_rgb = callback;
95103
}
96-
void onLightChangeHsv(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t)) {
104+
void onLightChangeHsv(ZigbeeColorLightHsvCallback callback) {
97105
_on_light_change_hsv = callback;
98106
}
99-
void onLightChangeTemp(void (*callback)(bool, uint8_t, uint16_t)) {
107+
void onLightChangeTemp(ZigbeeColorLightTempCallback callback) {
100108
_on_light_change_temp = callback;
101109
}
102110
void restoreLight() {
@@ -156,19 +164,17 @@ class ZigbeeColorDimmableLight : public ZigbeeEP {
156164
uint8_t getCurrentColorSaturation();
157165
uint16_t getCurrentColorTemperature();
158166

159-
void lightChanged();
160167
void lightChangedRgb();
161168
void lightChangedHsv();
162169
void lightChangedTemp();
163170
void lightChangedByMode();
164-
//callback function to be called on light change (State, R, G, B, Level) - legacy
165-
void (*_on_light_change)(bool, uint8_t, uint8_t, uint8_t, uint8_t);
171+
166172
//callback function to be called on light change for RGB (State, R, G, B, Level)
167-
void (*_on_light_change_rgb)(bool, uint8_t, uint8_t, uint8_t, uint8_t);
173+
ZigbeeColorLightRgbCallback _on_light_change_rgb;
168174
//callback function to be called on light change for HSV (State, H, S, V, Level)
169-
void (*_on_light_change_hsv)(bool, uint8_t, uint8_t, uint8_t, uint8_t);
175+
ZigbeeColorLightHsvCallback _on_light_change_hsv;
170176
//callback function to be called on light change for TEMP (State, Level, Temperature)
171-
void (*_on_light_change_temp)(bool, uint8_t, uint16_t);
177+
ZigbeeColorLightTempCallback _on_light_change_temp;
172178

173179
bool _current_state;
174180
uint8_t _current_level;

0 commit comments

Comments
 (0)