FadeLed
A simple Arduino library to fade leds on hardware PWM
FadeLed.h
Go to the documentation of this file.
1 
8 #ifndef _FADE_LED_H
9 #define _FADE_LED_H
10 
11 #if defined(ARDUINO) && ARDUINO >= 100
12 #include "Arduino.h"
13 #else
14 #include "WProgram.h"
15 #endif
16 
17 #if defined ( ESP8266 )
18  #include <pgmspace.h>
19 #else
20  #include <avr/pgmspace.h>
21 #endif
22 
32  #if defined ( ESP8266 )
33  //ESP8266 has 10-bit PWM
34  #define FADE_LED_PWM_BITS 10
35 #else
36  //Change this to match the number of PWM bit on other devices
37  #define FADE_LED_PWM_BITS 8
38 #endif
39 
40 
41 
49 #if FADE_LED_PWM_BITS <= 8
50 typedef uint8_t flvar_t;
51 #else
52 typedef uint16_t flvar_t;
53 #endif
54 
55 
61 #ifndef FADE_LED_MAX_LED
62 #define FADE_LED_MAX_LED 6
63 #endif
64 
74 #ifndef FADE_LED_RESOLUTION
75 #define FADE_LED_RESOLUTION ((1 <<FADE_LED_PWM_BITS) -1)
76 #endif
77 
78 #include "FadeLedGamma.h"
79 
87 class FadeLed{
88  public:
104  FadeLed(byte pin);
105 
132  FadeLed(byte pin, const flvar_t* gammaLookup, flvar_t biggestStep);
133 
144  FadeLed(byte pin, bool hasGammaTable);
145 
151  ~FadeLed();
152 
164  void begin(flvar_t val);
165 
183  void set(flvar_t val);
184 
194  flvar_t get();
195 
208 
218  bool done();
219 
228  void on();
229 
238  void off();
239 
247  void beginOn();
248 
269  void setTime(unsigned long time, bool constTime = false);
270 
280  bool rising();
281 
291  bool falling();
292 
300  void stop();
301 
334  void setGammaTable(const flvar_t* table, flvar_t biggestStep = 100);
335 
345  void noGammaTable();
346 
358 
367 
368 
385  static void update();
386 
400  static void setInterval(unsigned int interval);
401 
402  protected:
403  const byte _pin;
407  bool _constTime;
408  unsigned long _countMax;
409  unsigned long _count;
412 
413 
414 
423  void updateThis();
424 
441  flvar_t getGamma(flvar_t step);
442 
444  static byte _ledCount;
445  static unsigned int _interval;
446  static unsigned int _millisLast;
447 };
448 
450  if(_gammaLookup == nullptr){
451  return step;
452  }
453  else{
454  #if FADE_LED_PWM_BITS <= 8
455  return pgm_read_byte_near(_gammaLookup + step);
456  #else
457  return pgm_read_word_near(_gammaLookup + step);
458  #endif
459  }
460 }
461 
462 #endif
flvar_t getCurrent()
Returns the current brightness.
Definition: FadeLed.cpp:123
bool falling()
Returns if the LED is still fading down.
Definition: FadeLed.cpp:153
void updateThis()
Updates fading of this object only.
Definition: FadeLed.cpp:190
flvar_t _biggestStep
The biggest input step possible.
Definition: FadeLed.h:411
Main class of the FadeLed-library.
Definition: FadeLed.h:87
FadeLed(byte pin)
Simple constructor of a FadeLed object with gamma correction.
Definition: FadeLed.cpp:9
void on()
Fade to max brightness.
Definition: FadeLed.cpp:131
unsigned long _count
The number of _interval&#39;s passed.
Definition: FadeLed.h:409
const byte _pin
PWM pin to control.
Definition: FadeLed.h:403
static void setInterval(unsigned int interval)
Sets the interval at which to update the fading.
Definition: FadeLed.cpp:260
flvar_t getBiggestStep()
Get the biggest brightness step.
Definition: FadeLed.cpp:186
flvar_t _curVal
Current brightness.
Definition: FadeLed.h:406
#define FADE_LED_MAX_LED
Maximum number of FadeLed objects.
Definition: FadeLed.h:62
void noGammaTable()
Use no gamma correction for full range.
Definition: FadeLed.cpp:175
void off()
Fade to off.
Definition: FadeLed.cpp:135
void setTime(unsigned long time, bool constTime=false)
Set the time a (full) fade will take.
Definition: FadeLed.cpp:143
flvar_t _setVal
The brightness to which last set to fade to.
Definition: FadeLed.h:404
const flvar_t * _gammaLookup
Pointer to the Gamma table in PROGMEM.
Definition: FadeLed.h:410
flvar_t getGammaValue(flvar_t step)
Get gamma corrected value.
Definition: FadeLed.cpp:179
void begin(flvar_t val)
Set a direct begin value to start at without fade.
Definition: FadeLed.cpp:58
static unsigned int _interval
Interval (in ms) between updates.
Definition: FadeLed.h:445
static FadeLed * _ledList[FADE_LED_MAX_LED]
array of pointers to all FadeLed objects
Definition: FadeLed.h:443
static unsigned int _millisLast
Last time all FadeLed objects where updated.
Definition: FadeLed.h:446
flvar_t _startVal
The brightness at which the new fade needs to start.
Definition: FadeLed.h:405
bool _constTime
Constant time fade or just constant speed fade.
Definition: FadeLed.h:407
unsigned long _countMax
The number of _interval&#39;s a fade should take.
Definition: FadeLed.h:408
flvar_t getGamma(flvar_t step)
Definition: FadeLed.h:449
static byte _ledCount
Next number of FadeLed object.
Definition: FadeLed.h:444
bool done()
Returns if the LED is done fading.
Definition: FadeLed.cpp:127
bool rising()
Returns if the LED is still fading up.
Definition: FadeLed.cpp:149
void stop()
Stops the current fading.
Definition: FadeLed.cpp:157
~FadeLed()
Simple destructor of a FadeLed object.
Definition: FadeLed.cpp:39
static void update()
Updates all FadeLed objects.
Definition: FadeLed.cpp:264
void setGammaTable(const flvar_t *table, flvar_t biggestStep=100)
Sets a gamma table to use.
Definition: FadeLed.cpp:161
uint8_t flvar_t
Sets the variable type used for the brightness.
Definition: FadeLed.h:50
void beginOn()
Sets the start brightness directly to full.
Definition: FadeLed.cpp:139