The LED module controls the LEDs on the device using PWM (Pulse Width Modulation) and timers to created blinking patterns. For devices with RGB LEDs, the module can set specific colors. For devices without RGB LEDs, the colors will map to regular LEDs instead.
The LED module uses the zbus channel led_chan to receive control commands. Other modules can publish messages to this channel to control the LED behavior.
The module accepts messages with the following parameters:
-
Color Values
red: Red component (0-255)green: Green component (0-255)blue: Blue component (0-255)
-
Timing Parameters
duration_on_msec: How long the LED stays on in milliseconds.duration_off_msec: How long the LED stays off in milliseconds.repetitions: Number of blink cycles (-1 for infinite blinking).
The message structure is defined in led_module.h:
struct led_msg {
enum led_msg_type type;
/** RGB values (0 to 255) */
uint8_t red;
uint8_t green;
uint8_t blue;
/** Duration of the RGB on/off cycle */
uint32_t duration_on_msec;
uint32_t duration_off_msec;
/** Number of on/off cycles (-1 indicates forever) */
int repetitions;
};Instead of using a state machine, the LED module operates as follows:
-
When a message is received on the
led_chanchannel:- Any existing blink pattern is canceled.
- The new LED state (colors and timing) is saved.
- The LED is turned on with the specified color.
-
If a blinking pattern is specified (repetitions != 0):
- A timer is started with
duration_on_msec. - When the timer expires, the LED toggles between on and off states.
- The timer alternates between
duration_on_msecandduration_off_msec. - This continues until the specified number of repetitions is reached.
- If repetitions is -1, the blinking continues indefinitely or until a new message is received.
- A timer is started with
The module handles error cases gracefully and reports issues through logging.
The LED module uses the following configuration options:
-
CONFIG_APP_LED_LOG_LEVEL: Controls logging level for the LED module.
-
Devicetree Configuration: The module requires three PWM LED aliases in the devicetree:
pwm-led0: Red channelpwm-led1: Green channelpwm-led2: Blue channel
You can control the LED colors through the LED module using zbus messages. The following is an example of how to set different LED patterns:
/* Set yellow blinking pattern */
struct led_msg led_msg = {
.type = LED_RGB_SET,
.red = 255,
.green = 255,
.blue = 0,
.duration_on_msec = 250,
.duration_off_msec = 2000,
.repetitions = 10,
};
/* Publish the message to led_chan */
int err = zbus_chan_pub(&led_chan, &led_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
return;
}