Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: KL25Z_HSI2RGBW_PWM KL25Z_HSI2RGBW_PWM_local KL25Z_FFT_Demo FFT_BUENA ... more
You are viewing an older revision! See the latest version
Homepage
HSI to RGB / RGBW with optional PWM output¶
Library for converting HSI color space values to RGB or RGBW and, optionally, directly send the converted values to PWM outputs.
Code ported from http://saikoled.com - Copyright 2013, Brian Neltner
http://blog.saikoled.com/post/44677718712/how-to-convert-from-hsi-to-rgb-white
Info on the HSI color space can be found here and here.
Demo code using the KL25Z-FRDM board:
Import programKL25Z_HSI2RGBW_PWM
SaikoLED fade demo using the HSI2RGBW_PWM libary
Constructor & conversion function
depending on the way you want to use this library, you can call the conversion function in different ways
// Assign 4 PWM outputs and also allow to return the converted RGBW value. hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); // Call to the conversion function float H, S, I; float rgbw[4]; led.hsi2rgbw(H, S, I, rgbw); // Assign 4 PWM outputs, no converted RGBW value will be returned. hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); // Call to the conversion function float H, S, I; led.hsi2rgbw(H, S, I); // No PWM outputs are assigned, only the converted RGBW value will be returned. hsi2rgbw_pwm led; // Call to the conversion function float H, S, I; float rgbw[4]; led.hsi2rgbw(H, S, I, rgbw); // Assign 3 PWM outputs and also allow to return the converted RGB value. hsi2rgbw_pwm led(PTD4, PTA12, PTA4); // Call to the conversion function float H, S, I; float rgb[4]; led.hsi2rgbw(H, S, I, rgbw); // Assign 3 PWM outputs, no converted RGB value will be returned. hsi2rgbw_pwm led(PTD4, PTA12, PTA4); // Call to the conversion function float H, S, I; led.hsi2rgbw(H, S, I); // No PWM outputs are assigned, only the converted RGB value will be returned. hsi2rgbw_pwm led; // Call to the conversion function float H, S, I; float rgb[4]; led.hsi2rgbw(H, S, I, rgb);
Hue, Saturation and Intesity range
H ue | 0...360 | Color value : 0 or 360 = red, 120 = green, 240 = blue, 60 = yellow, 300 = magenta, ... |
S aturation | 0...1 | Color saturation : 0 = white, 1 = max. color, 0.7 is a nice pastel |
I ntensity | 0...1 | Color intensity : 0 = off, 1 = full intensity |
RGB(W) return value
In your main program, declare an array of 4 float values and pass the pointer to this array to the function:
Even when RGB is used, do declare an array of 4 float values.
float rgbw[4];
Upon exit, the function will return the values in rgbw[]
, representing the red, green, blue and white component that corresponds to the chosen HSI values. Range : 0...1 where 0 = minimum and 1 = maximum.
rgbw[0] | RED | |
rgbw[1] | GREEN | |
rgbw[2] | BLUE | |
rgbw[3] | WHITE | Note : this value has no significance when RGB mode is used. |
parabolic mode
By default, each component of the RGB(W) return value is mapped to a parabolic scale.
Call led.parabolic(0);
to disable the parabolic mapping.
Choose RGB or RGBW mode
By default, the conversion return value is set to RGBW.
Call led.colorMode(RGB);
to change to RGB mode.
Call led.colorMode(RGBW);
to go back to RGBW mode.
Change PWM period
By default, the PWM period is set to 4ms (250Hz).
Call led.period(n);
to change the PWM period (replace n with a value in ms).
Change PWM values
We can alter the PWM outputs directly, without passing through the HSI to RGB(W) conversion.
declare float rgbw[4];
and set each value of this array.
Call led.pwm(rgbw);
to write the values directly to the PWM outputs.
NOTES
period() and pwm() will not change any value if no PWM pins are declared.
Do not forget to replace led.
with the declared function name.