Tricolor LED driver for any 3 pin tricolor LED.
Revision 0:80ebb233e295, committed 2013-05-24
- Comitter:
- fossum_13
- Date:
- Fri May 24 19:09:49 2013 +0000
- Commit message:
- Initial commit
Changed in this revision
tricolor.cpp | Show annotated file Show diff for this revision Revisions of this file |
tricolor.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 80ebb233e295 tricolor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tricolor.cpp Fri May 24 19:09:49 2013 +0000 @@ -0,0 +1,50 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2012 ARM Limited + */ +#include "tricolor.h" + +#define ON 255 +#define OFF 0 + +void Tricolor::Blue(void) { + SetLEDColor(OFF, OFF, ON); +} + +void Tricolor::Green(void) { + SetLEDColor(OFF, ON, OFF); +} + +void Tricolor::LEDOff(void) { + // Off == 1.0 + _Red = 1.0; + _Green = 1.0; + _Blue = 1.0; + _on = false; +} + +void Tricolor::LEDOn(void) { + _Red = _RedPwm; + _Green = _GreenPwm; + _Blue = _BluePwm; + _on = true; +} + +void Tricolor::Red(void) { + SetLEDColor(ON, OFF, OFF); +} + +void Tricolor::SetLEDColor(uint8_t red, uint8_t green, uint8_t blue) { + _RedPwm = 1.0 - ((float)red / 255); + _GreenPwm = 1.0 - ((float)green / 255); + _BluePwm = 1.0 - ((float)blue / 255); + + LEDOn(); +} + +void Tricolor::Toggle(void) { + if (_on) { + LEDOff(); + } else { + LEDOn(); + } +}
diff -r 000000000000 -r 80ebb233e295 tricolor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tricolor.h Fri May 24 19:09:49 2013 +0000 @@ -0,0 +1,75 @@ +#ifndef TRICOLOR_H +#define TRICOLOR_H + +#include "mbed.h" + +/** Tricolor LED driver + * + * This is a reentrant LED controller + * + * @code +#include "mbed.h" +#include "tricolor.h" + +Tricolor led(p23, p24, p25); // red, green, blue + +int main() { + led.SetLEDColor(0, 100, 0); // Green @ about 40% + + while(1) { + wait(0.5); + led.Toggle(); + } +} + * @endcode + */ +class Tricolor +{ + public: + /** Basic Constructor + */ + Tricolor(PinName r, PinName g, PinName b) : _Red(r), _Green(g), _Blue(b) { + SetLEDColor(0, 0, 0); + } + /** Color Constructor + * Adds color values to constructor. + */ + Tricolor(PinName r, uint8_t r_color, + PinName g, uint8_t g_color, + PinName b, uint8_t b_color) : + _Red(r), _Green(g), _Blue(b) { + SetLEDColor(r_color, g_color, b_color); + } + /// Sets LED to blue (LED will be on afterwards) + void Blue(void); + /// Sets LED to green (LED will be on afterwards) + void Green(void); + /// Turns the LED off (saves color) + void LEDOff(void); + /// Restores LED to set color (color MUST be set beforehand) + void LEDOn(void); + /// Sets LED to red (LED will be on afterwards) + void Red(void); + /** Sets custom color for LED (LED will be on afterwards) + * @param red Amount of red color (0-255) + * @param green Amount of green color (0-255) + * @param blue Amount of blue color (0-255) + */ + void SetLEDColor(uint8_t, uint8_t, uint8_t); + /// Toggles the LED on and off + void Toggle(void); + + private: + // LED Output + bool _on; // Is the LED on + PwmOut _Red; // LED red pin + PwmOut _Green; // LED green pin + PwmOut _Blue; // LED blue pin + + // LED Color + float _RedPwm; // Amount of red + float _GreenPwm;// Amount of green + float _BluePwm; // Amount of blue +}; + +#endif \ No newline at end of file