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 #define FADE_LED_PWM_BITS 8
33 
34 
42 #if FADE_LED_PWM_BITS <= 8
43 typedef byte flvar_t;
44 #else
45 typedef unsigned int flvar_t;
46 #endif
47 
48 
54 #ifndef FADE_LED_MAX_LED
55 #define FADE_LED_MAX_LED 6
56 #endif
57 
67 #ifndef FADE_LED_RESOLUTION
68 #define FADE_LED_RESOLUTION ((1 <<FADE_LED_PWM_BITS) -1)
69 #endif
70 
71 #include "FadeLedGamma.h"
72 
80 class FadeLed{
81  public:
97  FadeLed(byte pin);
98 
110  void begin(flvar_t val);
111 
129  void set(flvar_t val);
130 
140  flvar_t get();
141 
154 
164  bool done();
165 
174  void on();
175 
184  void off();
185 
193  void beginOn();
194 
215  void setTime(unsigned long time, bool constTime = false);
216 
226  bool rising();
227 
237  bool falling();
238 
246  void stop();
247 
280  void setGammaTable(const flvar_t* table, flvar_t biggestStep = 100);
281 
291  void noGammaTable();
292 
304 
313 
314 
331  static void update();
332 
346  static void setInterval(unsigned int interval);
347 
348  protected:
349  byte _pin;
353  bool _constTime;
354  unsigned long _countMax;
355  unsigned long _count;
358 
359 
360 
369  void updateThis();
370 
387  flvar_t getGamma(flvar_t step);
388 
390  static byte _ledCount;
391  static unsigned int _interval;
392  static unsigned int _millisLast;
393 };
394 
396  if(_gammaLookup == NULL){
397  return step;
398  }
399  else{
400  #if FADE_LED_PWM_BITS <= 8
401  return pgm_read_byte_near(_gammaLookup + step);
402  #else
403  return pgm_read_word_near(_gammaLookup + step);
404  #endif
405  }
406 }
407 
408 #endif
flvar_t getCurrent()
Returns the current brightness.
Definition: FadeLed.cpp:87
bool falling()
Returns if the led is still fading down.
Definition: FadeLed.cpp:117
void updateThis()
Updates fading of this object only.
Definition: FadeLed.cpp:154
flvar_t _biggestStep
The biggest input step possible.
Definition: FadeLed.h:357
Main class of the FadeLed-library.
Definition: FadeLed.h:80
FadeLed(byte pin)
Constructor of a FadeLed object.
Definition: FadeLed.cpp:9
void on()
Fade to max brightness.
Definition: FadeLed.cpp:95
byte flvar_t
Sets the variable type used for the brightness.
Definition: FadeLed.h:43
unsigned long _count
The number of _interval&#39;s passed.
Definition: FadeLed.h:355
static void setInterval(unsigned int interval)
Sets the interval at which to update the fading.
Definition: FadeLed.cpp:224
flvar_t getBiggestStep()
Get the biggest brightness step.
Definition: FadeLed.cpp:150
flvar_t _curVal
Current brightness.
Definition: FadeLed.h:352
#define FADE_LED_MAX_LED
Maximum number of FadeLed objects.
Definition: FadeLed.h:55
void noGammaTable()
Use no gamma correction for full range.
Definition: FadeLed.cpp:139
void off()
Fade to off.
Definition: FadeLed.cpp:99
void setTime(unsigned long time, bool constTime=false)
Set the time a (full) fade will take.
Definition: FadeLed.cpp:107
flvar_t _setVal
The brightness to which last set to fade to.
Definition: FadeLed.h:350
const flvar_t * _gammaLookup
Pointer to the Gamma table in PROGMEM.
Definition: FadeLed.h:356
flvar_t getGammaValue(flvar_t step)
Get gamma corrected value.
Definition: FadeLed.cpp:143
void begin(flvar_t val)
Set a direct begin value to start at without fade.
Definition: FadeLed.cpp:22
static unsigned int _interval
Interval (in ms) between updates.
Definition: FadeLed.h:391
static FadeLed * _ledList[FADE_LED_MAX_LED]
array of pointers to all FadeLed objects
Definition: FadeLed.h:389
static unsigned int _millisLast
Last time all FadeLed objects where updated.
Definition: FadeLed.h:392
flvar_t _startVal
The brightness at which the new fade needs to start.
Definition: FadeLed.h:351
byte _pin
PWM pin to control.
Definition: FadeLed.h:349
bool _constTime
Constant time fade or just constant speed fade.
Definition: FadeLed.h:353
unsigned long _countMax
The number of _interval&#39;s a fade should take.
Definition: FadeLed.h:354
flvar_t getGamma(flvar_t step)
Definition: FadeLed.h:395
static byte _ledCount
Next number of FadeLed object.
Definition: FadeLed.h:390
bool done()
Returns if the led is done fading.
Definition: FadeLed.cpp:91
bool rising()
Returns if the led is still fading up.
Definition: FadeLed.cpp:113
void stop()
Stops the current fading.
Definition: FadeLed.cpp:121
static void update()
Updates all FadeLed objects.
Definition: FadeLed.cpp:228
void setGammaTable(const flvar_t *table, flvar_t biggestStep=100)
Sets a Gamma table to use.
Definition: FadeLed.cpp:125
void beginOn()
Sets the start brightness directly to full.
Definition: FadeLed.cpp:103