FadeLed
A simple Arduino library to fade leds on hardware PWM
|
Main class of the FadeLed-library. More...
#include <FadeLed.h>
Public Member Functions | |
FadeLed (byte pin) | |
Constructor of a FadeLed object. More... | |
void | begin (flvar_t val) |
Set a direct begin value to start at without fade. More... | |
void | set (flvar_t val) |
Set the brightness to fade to. More... | |
flvar_t | get () |
Returns the last set brightness. More... | |
flvar_t | getCurrent () |
Returns the current brightness. More... | |
bool | done () |
Returns if the LED is done fading. More... | |
void | on () |
Fade to max brightness. More... | |
void | off () |
Fade to off. More... | |
void | beginOn () |
Sets the start brightness directly to full. More... | |
void | setTime (unsigned long time, bool constTime=false) |
Set the time a (full) fade will take. More... | |
bool | rising () |
Returns if the LED is still fading up. More... | |
bool | falling () |
Returns if the LED is still fading down. More... | |
void | stop () |
Stops the current fading. More... | |
void | setGammaTable (const flvar_t *table, flvar_t biggestStep=100) |
Sets a gamma table to use. More... | |
void | noGammaTable () |
Use no gamma correction for full range. More... | |
flvar_t | getGammaValue (flvar_t step) |
Get gamma corrected value. More... | |
flvar_t | getBiggestStep () |
Get the biggest brightness step. More... | |
Static Public Member Functions | |
static void | update () |
Updates all FadeLed objects. More... | |
static void | setInterval (unsigned int interval) |
Sets the interval at which to update the fading. More... | |
Protected Member Functions | |
void | updateThis () |
Updates fading of this object only. More... | |
flvar_t | getGamma (flvar_t step) |
Protected Attributes | |
byte | _pin |
PWM pin to control. | |
flvar_t | _setVal |
The brightness to which last set to fade to. | |
flvar_t | _startVal |
The brightness at which the new fade needs to start. | |
flvar_t | _curVal |
Current brightness. | |
bool | _constTime |
Constant time fade or just constant speed fade. | |
unsigned long | _countMax |
The number of _interval's a fade should take. | |
unsigned long | _count |
The number of _interval's passed. | |
const flvar_t * | _gammaLookup |
Pointer to the Gamma table in PROGMEM. | |
flvar_t | _biggestStep |
The biggest input step possible. | |
Static Protected Attributes | |
static FadeLed * | _ledList [FADE_LED_MAX_LED] |
array of pointers to all FadeLed objects | |
static byte | _ledCount = 0 |
Next number of FadeLed object. | |
static unsigned int | _interval = 50 |
Interval (in ms) between updates. | |
static unsigned int | _millisLast = 0 |
Last time all FadeLed objects where updated. | |
Main class of the FadeLed-library.
The FadeLed class includes all the functions to easily fade a LED including a static function to update all constructed FadeLed objects.
FadeLed::FadeLed | ( | byte | pin | ) |
Constructor of a FadeLed object.
Makes a object to fade a specific pin. You can make a object for each PWM pin you want to fade. Updating all objects is done by simply calling update()
When created the default brightness is 0. You can start at a different brightness by calling begin().
[in] | pin | The PWM pin to fade with this object |
void FadeLed::begin | ( | flvar_t | val | ) |
Set a direct begin value to start at without fade.
If you want to directly start the LED at a certain brightness you can set it with this. This will directly set the brightness without fading.
[in] | val | The brightness to start at. |
void FadeLed::beginOn | ( | ) |
bool FadeLed::done | ( | ) |
Returns if the LED is done fading.
true if the LED is done fading and reached the set() value.
false if it's still fading
bool FadeLed::falling | ( | ) |
Returns if the LED is still fading down.
true if fading down.
false otherwise
flvar_t FadeLed::get | ( | ) |
flvar_t FadeLed::getBiggestStep | ( | ) |
Get the biggest brightness step.
Gives the biggest brightness step for the gamma table in use. If no table is in use, it returns the biggest output level.
flvar_t FadeLed::getCurrent | ( | ) |
Gives the output level for a given gamma step
Looks it up in the PROGMEM gamma table for this object if table is assigned. Deals with the variable size used.
Can't be called directly (it's protected) but it's inline for speed. If you want to get a gamma value for a given step, use getGammaValue() instead.
[in] | step | The step to get the gamma corrected output level for. |
Get gamma corrected value.
Gives the gamma corrected output level. Checks for the biggest possible step.
[in] | step | The step to get the gamma corrected output level for. Limited to the biggest possible value |
void FadeLed::noGammaTable | ( | ) |
Use no gamma correction for full range.
Let this object use no gamma correction and just use the full PWM range.
It's short for setGammaTable(NULL, FADE_LED_RESOLUTION)
void FadeLed::off | ( | ) |
void FadeLed::on | ( | ) |
Fade to max brightness.
Sets the LED to fade to max brightness. Same as calling set(255)
bool FadeLed::rising | ( | ) |
Returns if the LED is still fading up.
true if fading up.
false otherwise
void FadeLed::set | ( | flvar_t | val | ) |
Set the brightness to fade to.
Set the brightness to which the LED should fade to.
In constant fade speed if the new value is in the same fading direction as were started and the value is not yet passed the fade just continues to the new value
In constant fade time a new value is ignored if the LED is still fading
Otherwise the fade is just reset and the LED will start fading to the new brightness.
[in] | val | The brightness to fade to. |
edit 2016-11-17 Fix so you can set it to a new value while fading in constant speed. And impossible to set a new value in constant time.
edit 2016-11-30 Fixed out of range possibility
Sets a gamma table to use.
Let this FadeLed object use a specific gamma table. This table must be put in PROGMEM to work. flvar_t can be used as variable type to get a variable type that matches the (set) bit resolution of the PWM.
Pointing to a NULL pointer will result in no gamma correction. biggestStep will then limit the brightness. But easier to use noGammaTable().
It will stop the current fading. It also resets it to start from 0 for the next fade.
If you want to fade from the current brightness with the new gamma table you have to find the starting value yourself and set it via begin().
By default a 101 steps (0-100 aka percentage) table is used with a gamma of 2,3. To generate a table with a different gamma you can use the provided Python script. (You need to install Python for it to work!) Call it like: python gamma.py Gamma Steps PWMbits [VariableName]
. VariableName is optional. For example python gamma.py 2.5 50 10
will result in a table with 50 steps (0 - 49) with gamma = 2,5 for a 10-bit PWM. This will be stored in gamma.h and can be copy pasted into your code.
[in] | table | The gamma table in PROGMEM |
[in] | biggestStep | The biggest step of that gamma table (aka size -1) If no parameter is used 100 is assumed to be the top value possible. |
|
static |
Sets the interval at which to update the fading.
Only every interval when calling update() it's checked to see if the brightness of the LEDs needs to change (fade) to leave time for other stuff.
default: 50ms
[in] | interval | Interval in ms |
void FadeLed::setTime | ( | unsigned long | time, |
bool | constTime = false |
||
) |
Set the time a (full) fade will take.
This will set how much time a fade will take.
The real fade time will be a whole multiple of the set interval (rounded down). For example, if interval is set to 50ms and you specify a fade time of 1025ms the fading time is actually set to 1000ms.
Constant fade speed (default)
In constant fade speed this is the time a fade from off to full brightness (or vice versa) will take. A fade of less will go with the same speed but will take less time
Constant fade time
In constant fade time this is the time each fade will take. No matter how much change in brightness. Each call to set() will reset the fading time and it will fade from it's current brightness to the new brightness in this time. Useful for example if you want to fade a RGB LED from one color to another in a set time. No matter the level of the individual RGB colors.
[in] | time | The time (ms) a fade will take |
[in] | constTime | [optional] true to use constant fade time. Default constant fading speed |
void FadeLed::stop | ( | ) |
Stops the current fading.
Makes the current brightness the set brightness
Useful if you fade to find the desired brightness
|
static |
Updates all FadeLed objects.
This is the core function of FadeLed. Calling this function will check each object of FadeLed to see if the brightness needs changing (fade).
It's a static function, you only need to call it once for all objects of FadeLed. You can call it using the class name like:
|
protected |