leds: core: add support for RGB LED's
PATCH v7 5/5 leds: core: add support for RGB LED's
Export a function to convert HSV color values to RGB. It's intended to be called by drivers for RGB LEDs.
Signed-off-by Heiner Kallweit <[email protected]>
---
v2:
- move hsv -> rgb conversion to separate file
- remove flag LED_DEV_CAP_RGB
v3:
- call led_hsv_to_rgb only if LED_DEV_CAP_HSV is set This is needed in cases when we have monochrome and color LEDs as well in a system.
v4:
- Export led_hsv_to_rgb and let the device driver call it instead of doing the conversion in the core
v5:
- don't ignore led_cdev->brightness_get silently if LED_DEV_CAP_RGB is set but warn
v6:
- no changes
v7:
- remove "for now" in WARN_ON comment
- move position of WARN_ON
--- drivers/leds/led-class.c | 6 ++++++ drivers/leds/led-rgb-core.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/leds.h | 8 ++++++++ 3 files changed, 50 insertions(+)
drivers/leds/led-class.c +6 -0
@@ -205,6 +205,12 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)if (ret)dev_warn (parent,"Led %s renamed to %s due to name collision" , led_cdev->name,dev_name (led_cdev->dev)); +/* +* Reading back the color is not supported as multiple +* HSV -> RGB -> HSV conversions may distort the color due to +* rounding issues in the conversion algorithm +*/ +WARN_ON (led_cdev->flags &LED_DEV_CAP_RGB && led_cdev->brightness_get);#ifdef CONFIG_LEDS_TRIGGERS init_rwsem (&led_cdev->trigger_lock);
drivers/leds/led-rgb-core.c +36 -0
@@ -37,3 +37,39 @@ enum led_brightness led_confine_brightness(struct led_classdev *led_cdev,return brightness |min (value &LED_BRIGHTNESS_MASK , led_cdev->max_brightness); } + +enum led_brightness led_hsv_to_rgb (enum led_brightness hsv) +{ +int h =min_t (int, (hsv >>16 ) &0xff ,251 ); +int s = (hsv >>8 ) &0xff ; +int v = hsv &0xff ; +int f, p, q, t, r, g, b; + +if (!v) +return 0 ; +if (!s) +return (v <<16 ) + (v <<8 ) + v; + + f =DIV_ROUND_CLOSEST ((h %42 ) *255 ,42 ); + p = v -DIV_ROUND_CLOSEST (s * v,255 ); + q = v -DIV_ROUND_CLOSEST (f * s * v,255 *255 ); + t = v -DIV_ROUND_CLOSEST ((255 - f) * s * v,255 *255 ); + +switch (h /42 ) { +case 0 : + r = v; g = t; b = p;break ; +case 1 : + r = q; g = v; b = p;break ; +case 2 : + r = p; g = v; b = t;break ; +case 3 : + r = p; g = q; b = v;break ; +case 4 : + r = t; g = p; b = v;break ; +case 5 : + r = v; g = p; b = q;break ; + } + +return (r <<16 ) + (g <<8 ) + b; +} +EXPORT_SYMBOL_GPL (led_hsv_to_rgb);
include/linux/leds.h +8 -0
@@ -226,6 +226,14 @@ static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)return led_cdev->flags &LED_SYSFS_DISABLE ; } +/** +* led_hsv_to_rgb - convert a hsv color value to rgb color model +* @hsv: the hsv value to convert +* +* Returns: the resulting rgb value +*/ +enum led_brightness led_hsv_to_rgb (enum led_brightness hsv); +/* * LED Triggers */
Signature · 1 line
2.7.1