Library for RGB LED
Revision 0:8f07ffe423ce, committed 2015-10-29
- Comitter:
- wqz9822
- Date:
- Thu Oct 29 16:17:50 2015 +0000
- Commit message:
- RGB LED Library;
Changed in this revision
diff -r 000000000000 -r 8f07ffe423ce color.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/color.cpp Thu Oct 29 16:17:50 2015 +0000 @@ -0,0 +1,17 @@ +#include "color.h" + +LEDColor:: LEDColor(int r, int g, int b) + : red(r/255.0), green(g/255.0), blue(b/255.0) +{ +} +//Operator overload to adjust brightness with no color change +LEDColor operator * (const LEDColor& x, const float& b) +{ + return LEDColor(x.red*b,x.green*b,x.blue*b); +} +//Operator overload to add colors +LEDColor operator + (const LEDColor& x, const LEDColor& y) +{ + return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue); +} + \ No newline at end of file
diff -r 000000000000 -r 8f07ffe423ce color.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/color.h Thu Oct 29 16:17:50 2015 +0000 @@ -0,0 +1,15 @@ + +#ifndef COLOR_H +#define COLOR_H + +//class for 3 PWM color values for RGBLED +class LEDColor +{ +public: + LEDColor(int r, int g, int b); + float red; + float green; + float blue; +}; + +#endif
diff -r 000000000000 -r 8f07ffe423ce rgbLed.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgbLed.cpp Thu Oct 29 16:17:50 2015 +0000 @@ -0,0 +1,35 @@ +#include "rgbLed.h" + +#define COMMON_ANODE + +RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) + : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) +{ + //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) + _redpin.period(0.0005); +} + +void RGBLed::write(float red,float green, float blue) +{ + #ifdef COMMON_ANODE + red = 1 - red; + green = 1 - green; + blue = 1 - blue; + #endif + _redpin = red; + _greenpin = green; + _bluepin = blue; +} + +void RGBLed::write(LEDColor c) +{ + #ifdef COMMON_ANODE + _redpin = 1 - c.red; + _greenpin = 1 - c.green; + _bluepin = 1 - c.blue; + #else + _redpin = c.red; + _greenpin = c.green; + _bluepin = c.blue; + #endif +}
diff -r 000000000000 -r 8f07ffe423ce rgbLed.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgbLed.h Thu Oct 29 16:17:50 2015 +0000 @@ -0,0 +1,22 @@ +#include "mbed.h" +#include "color.h" + +#ifndef RGB_H +#define RGB_H +//Class to control an RGB LED using three PWM pins +class RGBLed +{ +public: + RGBLed(PinName redpin, PinName greenpin, PinName bluepin); + void write(float red,float green, float blue); + void write(LEDColor c); + RGBLed operator = (LEDColor c) { + write(c); + return *this; + }; +private: + PwmOut _redpin; + PwmOut _greenpin; + PwmOut _bluepin; +}; +#endif \ No newline at end of file