zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.7k stars 6.54k forks source link

LED driver for RGB leds + device tree support #51858

Open koffes opened 1 year ago

koffes commented 1 year ago

Zephyr has a LED driver https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/led/led_gpio.c which is suitable for single LEDs. There is however (IMHO) no good way to handle RGB leds.

Describe the solution you'd like It would be great to have native support for RGB leds. That is, having a common API for both RGB and single LEDs. Something on this form:

// Turns on a single color LED or turns on  R, G and B channel
int led_gpio_on(const struct device *dev, uint32_t led_group);

// Turns off single color LED or turns off R, G and B channel
int led_gpio_off(const struct device *dev, uint32_t led_group); 

// Vararg function. Takes a single uint8_t for single_leds or three uint8_t for RGB leds
int led_gpio_set_brightness(const struct device *dev, uint32_t led_group, ...) 

// Will return error if supplied with single-led-group. color_define can be e.g. COLOR_RED, COLOR_WHITE...
int led_gpio_set_color(const struct device *dev, uint32_t led_group, uint8_t color_define) 

This would also mean that the devicetree must have some mechanism to group three GPIOs to form an RGB LED group.

koffes commented 1 year ago

FYI @alexsven @erikrobstad. I discovered this https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/led.h which I need to investigate.

nordicjm commented 1 year ago

There is LED strip already, which can be used (with a size of 1) https://docs.zephyrproject.org/latest/hardware/peripherals/led.html#led-strip

koffes commented 1 year ago

I have had a look at this one, as well as others, but it seems to me that in order to use them you need an external driver IC? In the use case we want to solve, the LED channels are connected directly to host GPIOs.

zephyrbot commented 8 months ago

Hi @mbolivar-ampere, @simonguinot, @Mani-Sadhasivam,

This issue, marked as an Enhancement, was opened a while ago and did not get any traction. It was just assigned to you based on the labels. If you don't consider yourself the right person to address this issue, please re-assing it to the right person.

Please take a moment to review if the issue is still relevant to the project. If it is, please provide feedback and direction on how to move forward. If it is not, has already been addressed, is a duplicate, or is no longer relevant, please close it with a short comment explaining the reason.

@koffes you are also encouraged to help moving this issue forward by providing additional information and confirming this request/issue is still relevant to you.

Thanks!

simonguinot commented 8 months ago

The LED DT API already allows to define multi-color (i.e. multi-channel) LEDs. Here is an example:

leds {
        compatible = "gpio-leds";

        /* Multi channel GPIO LED */
        led_0 {
                label = "RGB LED";
                gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>,
                        <&gpio0 2 GPIO_ACTIVE_LOW>,
                        <&gpio0 3 GPIO_ACTIVE_HIGH>;
                color-mapping =
                        <LED_COLOR_ID_RED>,
                        <LED_COLOR_ID_GREEN>,
                        <LED_COLOR_ID_BLUE>;
        };
        /* Single channel GPIO LED */
        led_1 {
                label = "Green LED";
                gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>;
                color-mapping = <LED_COLOR_ID_GREEN>,
        };
};

On the led_gpio driver side, we simply need to implement support for the color-mapping and gpios array properties through the ->set_color() method of the LED API. And probably we will need to disable the ->set_brightness() method for a multi-color LED. And that's it.

Note that the same comment applies to PWM LEDs.

Grouping LEDs is an interesting idea. Some LED controllers provides this feature and it would be nice for the LED API to support it. But I don't think it is the right way to support RGB GPIO LEDs.

simonguinot commented 8 months ago

Related with #63387