Skip to content

Commit 9be0d08

Browse files
feat(lightbulb): Add online documentation for the lightbulb component
1 parent a4fdf46 commit 9be0d08

File tree

10 files changed

+661
-95
lines changed

10 files changed

+661
-95
lines changed

components/led/lightbulb_driver/include/lightbulb.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ typedef struct {
156156
float rgbcw_100[5]; /**< The RGBCW components required when saturation is 100 at a specific hue. */
157157
float rgbcw_50[5]; /**< The RGBCW components required when saturation is 50 at a specific hue. */
158158
float rgbcw_0[5]; /**< The RGBCW components required when saturation is 10 at a specific hue. */
159-
uint16_t hue;
159+
uint16_t hue; /**< hue. */
160160
} lightbulb_color_mapping_data_t;
161161

162162
/**
@@ -331,40 +331,44 @@ typedef struct {
331331
*/
332332
union {
333333
struct {
334-
uint16_t kelvin_min;
335-
uint16_t kelvin_max;
336-
} standard;
334+
uint16_t kelvin_min; /**< Minimum Kelvin value. */
335+
uint16_t kelvin_max; /**< Maximum Kelvin value. */
336+
} standard; /**< Standard Mode */
337337
struct {
338-
lightbulb_cct_mapping_data_t *table;
339-
int table_size;
340-
} precise;
338+
lightbulb_cct_mapping_data_t *table; /**< Mixed Color table */
339+
int table_size; /**< Table size */
340+
} precise; /**< Precise Mode */
341341
} cct_mix_mode;
342342

343+
/**
344+
* This configuration is used to set up the color calibration scheme.
345+
* Measure certain hue and saturation values as calibration points, and use a linear interpolation method for color calibration.
346+
*/
343347
union {
344348
struct {
345-
lightbulb_color_mapping_data_t *table;
346-
int table_size;
347-
} precise;
349+
lightbulb_color_mapping_data_t *table; /**< Mixed Color table */
350+
int table_size; /**< Table size */
351+
} precise; /**< Precise Mode */
348352
} color_mix_mode;
349353

350354
lightbulb_gamma_config_t *gamma_conf; /**< Pointer to the gamma configuration data. */
351355
lightbulb_power_limit_t *external_limit; /**< Pointer to the external power limit configuration. */
352356

353357
union {
354358
struct {
355-
gpio_num_t red;
356-
gpio_num_t green;
357-
gpio_num_t blue;
358-
gpio_num_t cold_cct;
359-
gpio_num_t warm_brightness;
359+
gpio_num_t red; /**< GPIO Pin for the red LED */
360+
gpio_num_t green; /**< GPIO Pin for the green LED */
361+
gpio_num_t blue; /**< GPIO Pin for the blue LED */
362+
gpio_num_t cold_cct; /**< GPIO Pin for the cold or cct LED */
363+
gpio_num_t warm_brightness; /**< GPIO Pin for the warm or brightness LED */
360364
} pwm_io; /**< Configuration for PWM driver I/O pins. */
361365

362366
struct {
363-
lightbulb_iic_out_pin_t red;
364-
lightbulb_iic_out_pin_t green;
365-
lightbulb_iic_out_pin_t blue;
366-
lightbulb_iic_out_pin_t cold_white;
367-
lightbulb_iic_out_pin_t warm_yellow;
367+
lightbulb_iic_out_pin_t red; /**< Port of the IIC dimming chip for red output */
368+
lightbulb_iic_out_pin_t green; /**< Port of the IIC dimming chip for green output */
369+
lightbulb_iic_out_pin_t blue; /**< Port of the IIC dimming chip for blue output */
370+
lightbulb_iic_out_pin_t cold_white; /**< Port of the IIC dimming chip for cold or white output */
371+
lightbulb_iic_out_pin_t warm_yellow; /**< Port of the IIC dimming chip for warm or yellow output */
368372
} iic_io; /**< Configuration for IIC driver I/O pins. */
369373
} io_conf; /**< Union for I/O configuration based on the selected driver type. */
370374

docs/Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ INPUT = \
4747
$(PROJECT_PATH)/components/keyboard_button/include/keyboard_button.h \
4848
$(PROJECT_PATH)/components/knob/include/iot_knob.h \
4949
$(PROJECT_PATH)/components/led/led_indicator/include/led_indicator.h \
50+
$(PROJECT_PATH)/components/led/lightbulb_driver/include/lightbulb.h \
5051
$(PROJECT_PATH)/components/motor/esp_sensorless_bldc_control/control/include/bldc_control_param.h \
5152
$(PROJECT_PATH)/components/motor/esp_sensorless_bldc_control/include/bldc_control.h \
5253
$(PROJECT_PATH)/components/motor/esp_sensorless_bldc_control/user_cfg/bldc_user_cfg.h \
30.1 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Electrical&Lighting Solution
2+
=============================
3+
4+
:link_to_translation:`zh_CN:[中文]`
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
Lightbulb Driver <lightbulb_driver>
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
Lightbulb Driver
2+
=================
3+
:link_to_translation:`zh_CN:[中文]`
4+
5+
The Lightbulb driver component encapsulates several commonly used dimming schemes for bulb lights,
6+
using an abstraction layer to manage these schemes, making it easier for developers to integrate into their applications.
7+
Currently, all ESP32 series chips are fully supported
8+
9+
Dimming scheme
10+
---------------
11+
12+
PWM Dimming scheme
13+
^^^^^^^^^^^^^^^^^^^
14+
15+
PWM dimming is a technique that controls LED brightness by adjusting the pulse width.
16+
The core idea is to vary the duty cycle of the current pulse (i.e., the proportion of high-level time within the entire cycle time).
17+
When the duty cycle is high, the LED receives current for a longer duration, resulting in higher brightness; conversely, a low duty cycle shortens the current duration, producing lower brightness.
18+
19+
All ESP chips support PWM output using hardware `LEDC driver <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html?highlight=ledc>`__ or `MCPWM <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html?highlight=mcpwm>`__ driver.
20+
It is recommended to use the LEDC driver as it supports hardware fading, with configurable frequency and duty cycle, and a maximum resolution of 20 bits.
21+
The following types of PWM dimming drivers are currently supported:
22+
23+
- RGB + C/W
24+
- RGB + CCT/Brightnes
25+
26+
PWM scheme use cases
27+
~~~~~~~~~~~~~~~~~~~~~~
28+
29+
Use the PWM scheme to light up a 3-channel RGB bulb with PWM frequency set to 4000Hz. Use software color mixing, enable the fade function, and set the power-on color to red.
30+
31+
.. code:: c
32+
33+
lightbulb_config_t config = {
34+
// 1. Select PWM output and configure parameters
35+
.type = DRIVER_ESP_PWM,
36+
.driver_conf.pwm.freq_hz = 4000,
37+
38+
// 2. capability selection: enable/disable according to your needs
39+
.capability.enable_fade = true,
40+
.capability.fade_time_ms = 800,
41+
.capability.enable_lowpower = false,
42+
/* Enable this feature if your white light output is hardware-controlled independently rather than software color-mixed */
43+
.capability.enable_hardware_cct = true,
44+
.capability.enable_status_storage = true,
45+
/* Configure the LED combination based on your LED aluminum substrate */
46+
.capability.led_beads = LED_BEADS_3CH_RGB,
47+
.capability.storage_cb = NULL,
48+
.capability.sync_change_brightness_value = true,
49+
50+
// 3. Configure hardware pins for PWM output
51+
.io_conf.pwm_io.red = 25,
52+
.io_conf.pwm_io.green = 26,
53+
.io_conf.pwm_io.blue = 27,
54+
55+
// 4. Limits parameter
56+
.external_limit = NULL,
57+
58+
// 5. Color calibration parameters
59+
.gamma_conf = NULL,
60+
61+
// 6. Initialize lighting parameters; if 'on' is set, the lightbulb will light up during driver initialization
62+
.init_status.mode = WORK_COLOR,
63+
.init_status.on = true,
64+
.init_status.hue = 0,
65+
.init_status.saturation = 100,
66+
.init_status.value = 100,
67+
};
68+
lightbulb_init(&config);
69+
70+
I2C Dimming Scheme
71+
^^^^^^^^^^^^^^^^^^^
72+
73+
`I2C Dimming <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2c.html>`__ :
74+
Control commands are sent via the I2C bus to the dimming chip, adjusting the LED brightness by changing the output current of the dimming chip.
75+
The I2C bus consists of two lines: the data line (SDA) and the clock line (SCL). All ESP chips support dimming chips using hardware I2C communication.
76+
The following dimming chips are currently supported:
77+
78+
- SM2135EH
79+
- SM2X35EGH (SM2235EGH/SM2335EGH)
80+
- BP57x8D (BP5758/BP5758D/BP5768)
81+
- BP1658CJ
82+
- KP18058
83+
84+
I2C Dimming Use Case
85+
~~~~~~~~~~~~~~~~~~~~~~
86+
87+
Use the I2C driver to control the BP5758D chip to light up a 5-channel RGBCW bulb. Set the I2C communication rate to 300KHz, with RGB LEDs driven at 50mA and CW LEDs at 30mA.
88+
Use software color mixing, enable the fade function, and set the power-on color to red.
89+
90+
.. code:: c
91+
92+
lightbulb_config_t config = {
93+
// 1. Select the required chip and configure parameters. Each chip has different configuration parameters, so please refer carefully to the chip manual.
94+
.type = DRIVER_BP57x8D,
95+
.driver_conf.bp57x8d.freq_khz = 300,
96+
.driver_conf.bp57x8d.enable_iic_queue = true,
97+
.driver_conf.bp57x8d.iic_clk = 4,
98+
.driver_conf.bp57x8d.iic_sda = 5,
99+
.driver_conf.bp57x8d.current = {50, 50, 50, 30, 30},
100+
101+
// 2. capability selection: enable/disable according to your needs
102+
.capability.enable_fade = true,
103+
.capability.fade_time_ms = 800,
104+
.capability.enable_lowpower = false,
105+
106+
.capability.enable_status_storage = true,
107+
.capability.led_beads = LED_BEADS_5CH_RGBCW,
108+
.capability.storage_cb = NULL,
109+
.capability.sync_change_brightness_value = true,
110+
111+
// 3. Configure the hardware pins for the IIC chip
112+
.io_conf.iic_io.red = OUT3,
113+
.io_conf.iic_io.green = OUT2,
114+
.io_conf.iic_io.blue = OUT1,
115+
.io_conf.iic_io.cold_white = OUT5,
116+
.io_conf.iic_io.warm_yellow = OUT4,
117+
118+
// 4. Limits parameter
119+
.external_limit = NULL,
120+
121+
// 5. Color calibration parameters
122+
.gamma_conf = NULL,
123+
124+
// 6. Initialize lighting parameters; if 'on' is set, the lightbulb will light up during driver initialization
125+
.init_status.mode = WORK_COLOR,
126+
.init_status.on = true,
127+
.init_status.hue = 0,
128+
.init_status.saturation = 100,
129+
.init_status.value = 100,
130+
};
131+
lightbulb_init(&config);
132+
133+
Single-wire dimming scheme
134+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135+
136+
A single-wire dimming scheme is a method of controlling LED brightness through a single communication line.
137+
The core idea is to adjust LED brightness by sending control signals through a specific communication protocol.
138+
This scheme can be implemented on ESP chips using the `RMT peripheral <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html>`__ or `SPI peripheral <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html>`__.
139+
SPI is recommended for controlling LED communication. The following LED types are currently supported:
140+
141+
- WS2812
142+
143+
WS2812 Use Case
144+
~~~~~~~~~~~~~~~~
145+
146+
Use the SPI driver to light up 10 WS2812 LEDs, enable the fade function, and set the power-on color to red.
147+
148+
.. code:: c
149+
150+
lightbulb_config_t config = {
151+
// 1. Select WS2812 output and configure parameters
152+
.type = DRIVER_WS2812,
153+
.driver_conf.ws2812.led_num = 10,
154+
.driver_conf.ws2812.ctrl_io = 4,
155+
156+
// 2. capability selection: enable/disable according to your needs
157+
.capability.enable_fade = true,
158+
.capability.fade_time_ms = 800,
159+
.capability.enable_status_storage = true,
160+
161+
/* For WS2812, only LED_BEADS_3CH_RGB can be selected */
162+
.capability.led_beads = LED_BEADS_3CH_RGB,
163+
.capability.storage_cb = NULL,
164+
165+
// 4. Limits parameter
166+
.external_limit = NULL,
167+
168+
// 5. Color calibration parameters
169+
.gamma_conf = NULL,
170+
171+
// 6. Initialize lighting parameters; if 'on' is set, the lightbulb will light up during driver initialization
172+
.init_status.mode = WORK_COLOR,
173+
.init_status.on = true,
174+
.init_status.hue = 0,
175+
.init_status.saturation = 100,
176+
.init_status.value = 100,
177+
};
178+
lightbulb_init(&config);
179+
180+
Fade principle
181+
---------------
182+
183+
The fade effect in the bulb light component is implemented through software. Each channel records the current output value, target value, step size, and remaining steps.
184+
When the API is used to set a color, it updates the target value, step size, step count, and enables a fade timer.
185+
This timer triggers a callback every 12ms, where each channel's steps are checked.
186+
As long as there are steps remaining, the current value is adjusted by the step size and updated in the underlying driver.
187+
When all channels have reached zero steps, the fade is complete, and the timer is stopped.
188+
189+
Low-power implementation process
190+
-----------------------------------
191+
192+
To pass certifications like T20 for low power consumption, after optimizing the light board's power supply, some low-power configurations are needed in the software.
193+
Apart from the settings mentioned in the `Low-Power Mode Usage Guide <https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-guides/low-power-mode.html>`__ the driver logic also requires adjustments.
194+
In the lightbulb component, low-power adjustments have been added for both PWM and I2C dimming schemes. The specific logic involves the I2C scheme using the dimming chip's low-power command to exit or enter low power when switching the light on or off.
195+
For the PWM scheme, ESP32 requires power lock management with the APB clock source to prevent flickering—locking power and disabling dynamic frequency scaling when the light is on, and releasing the lock when off, other chips use the XTAL clock source, so no further measures are necessary.
196+
197+
Color calibration scheme
198+
---------------------------
199+
200+
CCT Mode
201+
^^^^^^^^^^^^
202+
203+
The calibration for color temperature mode requires configuring the following structure.
204+
205+
.. code:: c
206+
207+
union {
208+
struct {
209+
uint16_t kelvin_min;
210+
uint16_t kelvin_max;
211+
} standard;
212+
struct {
213+
lightbulb_cct_mapping_data_t *table;
214+
int table_size;
215+
} precise;
216+
} cct_mix_mode;
217+
218+
219+
- Standard mode: calibrate the maximum and minimum Kelvin values, use linear interpolation to fill in intermediate values, and then adjust the output ratio of cool and warm LEDs according to the target color temperature.
220+
221+
- Precision mode: calibrate the required output ratios of red, green, blue, cool, and warm LEDs at different color temperatures. Use these calibration points to directly output the corresponding ratios. The more calibration points, the more accurate the color temperature.
222+
223+
Color Mode
224+
^^^^^^^^^^^^
225+
226+
The calibration for color mode requires configuring the following structure.
227+
228+
.. code:: c
229+
230+
union {
231+
struct {
232+
lightbulb_color_mapping_data_t *table;
233+
int table_size;
234+
} precise;
235+
} color_mix_mode;
236+
237+
- Standard Mode: No parameter configuration is required. Internal theoretical algorithms will convert HSV, XYY, and other color models into RGB ratios, and LEDs will light up directly based on this ratio.
238+
239+
- Precision Mode: Calibrate colors using the HSV model. Measure the required output ratios of red, green, blue, cool, and warm LEDs for various hues and saturation levels as calibration points. Use linear interpolation to fill in intermediate values and light up the LEDs based on these calibrated ratios.
240+
241+
242+
Power limiting scheme
243+
----------------------
244+
245+
Power limiting is used to balance and fine-tune the output current of a specific channel or the overall system to meet power requirements.
246+
247+
To limit the overall power, configure the following structure.
248+
249+
.. code:: c
250+
251+
typedef struct {
252+
/* Scale the incoming value
253+
* range: 10% <= value <= 100%
254+
* step: 1%
255+
* default min: 10%
256+
* default max: 100%
257+
*/
258+
uint8_t white_max_brightness; /**< Maximum brightness limit for white light output. */
259+
uint8_t white_min_brightness; /**< Minimum brightness limit for white light output. */
260+
uint8_t color_max_value; /**< Maximum value limit for color light output. */
261+
uint8_t color_min_value; /**< Minimum value limit for color light output. */
262+
263+
/* Dynamically adjust the final power
264+
* range: 100% <= value <= 500%
265+
* step: 10%
266+
*/
267+
uint16_t white_max_power; /**< Maximum power limit for white light output. */
268+
uint16_t color_max_power; /**< Maximum power limit for color light output. */
269+
} lightbulb_power_limit_t;
270+
271+
- ``white_max_brightness`` and ``white_min_brightness`` are used in color temperature mode to constrain the ``brightness`` parameter set by the color temperature API within these maximum and minimum values.
272+
- ``color_max_value`` and ``color_min_value`` are used in color mode to constrain the ``value`` parameter set by the color API within these maximum and minimum values.
273+
- ``white_max_power`` is used to limit power in color temperature mode. The default value is 100, meaning the maximum output power is half of the full power; if set to 200, it can reach the maximum power of the cool and warm LEDs.
274+
- ``color_max_power`` is used to limit power in color mode. The default value is 100, meaning the maximum output power is one-third of full power; if set to 300, it can reach the maximum power of the red, green, and blue LEDs.
275+
276+
To limit the power of individual LEDs, configure the following structure:
277+
278+
.. code:: c
279+
280+
typedef struct {
281+
float balance_coefficient[5]; /**< Array of float coefficients for adjusting the intensity of each color channel (R, G, B, C, W).
282+
These coefficients help in achieving the desired color balance for the light output. */
283+
284+
float curve_coefficient; /**< Coefficient for gamma correction. This value is used to modify the luminance levels
285+
to suit the non-linear characteristics of human vision, thus improving the overall
286+
visual appearance of the light. */
287+
} lightbulb_gamma_config_t;
288+
289+
290+
- ``balance_coefficient`` is used to fine-tune the output current of each LED. The final output of the driver will be reduced according to this ratio, with a default value of 1.0, meaning no reduction.
291+
- ``curve_coefficient`` is used to convert linear changes during fading into curve-based changes.
292+
293+
.. Note::
294+
295+
Modifying ``balance_coefficient`` will affect the accuracy of color calibration, so this parameter should be adjusted before performing color calibration.
296+
This parameter is especially useful for I2C dimming chips that output current in multiples of 5 or 10; if specific currents are needed, this parameter can be used for adjustment.
297+
298+
299+
API Reference
300+
-----------------
301+
302+
.. include-build-file:: inc/lightbulb.inc

0 commit comments

Comments
 (0)