FadeLed
A simple Arduino library to fade leds on hardware PWM
FadeLed Class Reference

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.
 

Detailed Description

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.

See also
set(), update()

Constructor & Destructor Documentation

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().

Note
Make all objects global (or static if you really must) because a destruct will lead to errors!
Warning
Don't make two objects for the same pin, they will conflict!
Parameters
[in]pinThe PWM pin to fade with this object
See also
update(), set(), on(), off()

Member Function Documentation

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.

Note
You can also use this to set a brightness without fading
See also
beginOn()
Parameters
[in]valThe brightness to start at.
void FadeLed::beginOn ( )

Sets the start brightness directly to full.

Short for calling begin(255)

See also
begin()
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

Returns
If the led is fading or not
bool FadeLed::falling ( )

Returns if the led is still fading down.

true if fading down.

false otherwise

Returns
if the led is fading down
flvar_t FadeLed::get ( )

Returns the last set brightness.

Returns the last set brightness via set() (or begin())

See also
set(), begin()
Returns
The last set brightness
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.

Returns
Biggest brightness step
flvar_t FadeLed::getCurrent ( )

Returns the current brightness.

Returns the current brightness of the output

Note
It does not return the last set value. For that, use get()
See also
get()
Returns
Current brightness of the led.
flvar_t FadeLed::getGamma ( flvar_t  step)
inlineprotected

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.

Returns
Corresponding level for the given step. Returns in if no table is in use.
Note
Does not check for out of range! That's up to the caller. For that, use getGammaValue().
See also
getGammaValue()
Parameters
[in]stepThe step to get the gamma corrected output level for.
Returns
The gamma corrected output level if a gamma table is uses, otherwise it returns in.
flvar_t FadeLed::getGammaValue ( flvar_t  step)

Get gamma corrected value.

Gives the gamma corrected output level. Checks for the biggest possible step.

See also
getGamma()
Parameters
[in]stepThe step to get the gamma corrected output level for. Limited to the biggest possible value
Returns
The gamma corrected output level if a gamma table is uses, otherwise it returns in.
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)

See also
setGammaTable(), FADE_LED_RESOLUTION
void FadeLed::off ( )

Fade to off.

Sets the led to fade to off. Same as calling set(0)

See also
set()
void FadeLed::on ( )

Fade to max brightness.

Sets the led to fade to max brightness. Same as calling set(255)

See also
set()
bool FadeLed::rising ( )

Returns if the led is still fading up.

true if fading up.

false otherwise

Returns
if the led is fading up
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.

Note
To make all the fading work you need to call FadeLed::update() often in the loop()!
See also
done(), setTime()
Parameters
[in]valThe 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

void FadeLed::setGammaTable ( const flvar_t table,
flvar_t  biggestStep = 100 
)

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.

{C++}
//put gamma table in PROGMEM
const flvar_t myGammaTable[20] PROGMEM = {....};
//link it to an object
//biggestStep is size - 1!
led.setGammaTable(myGammaTable, 19)

Pointing to a NULL pointer will result in no gamma correction. biggestStep will then limit the brightness. But easier to use noGammaTable().

{C++}
//No gamma table, 8-bit PWM
led.setGammaTable(NULL, 255);

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.

Note
It stops and resets but does not change the PWM output. This only gets changed after a new call to set(), on(), off(), begin() or beginOn(). If no action is taken a abrupt jump will happen if not at zero brightness.
Parameters
[in]tableThe gamma table in PROGMEM
[in]biggestStepThe biggest step of that gamma table (aka size -1) If no parameter is used 100 is assumed to be the top value possible.
void FadeLed::setInterval ( unsigned int  interval)
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

Warning
Call this before setting a fading time (via setTime()). Changing the interval will change the fading time or each FadeLed object.
See also
setTime()
Parameters
[in]intervalInterval 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.

Note
If you want to change the update interval (setInterval()) do that before calling setTime(). The fade time is calculated using the interval.
See also
setInterval()
Parameters
[in]timeThe 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

void FadeLed::update ( )
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 is once for all objects of FadeLed. You can call it using the class name like:

{C++}
loop(){
}
Note
Call this function often in order not to skip steps. Make the code non-blocking aka don't use delay() anywhere! See Blink Without Delay()
void FadeLed::updateThis ( )
protected

Updates fading of this object only.

Can't be called directly (it's protected). Instead call update() to update all FadeLed objects.

See also
update()

The documentation for this class was generated from the following files: