From bd7c712d5990548aff35accf2f996c59850924ac Mon Sep 17 00:00:00 2001 From: Elena of Valhalla'' Grandi Date: Wed, 14 Aug 2013 11:56:37 +0200 Subject: Support for common cathode RGB LEDs --- Colours.cpp | 22 +++++++++++++++++++--- Colours.h | 3 +++ examples/hue_rotation/hue_rotation.ino | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Colours.cpp b/Colours.cpp index bb77e85..50b782d 100644 --- a/Colours.cpp +++ b/Colours.cpp @@ -6,6 +6,16 @@ #include "Colours.h" Colours::Colours(int rPin,int gPin,int bPin) { + _initPINs(rPin,gPin,bPin); + _invert = false; +} + +Colours::Colours(int rPin,int gPin,int bPin,bool invert) { + _initPINs(rPin,gPin,bPin); + _invert = invert; +} + +void Colours::_initPINs(int rPin,int gPin,int bPin) { _rPin = rPin; _gPin = gPin; _bPin = bPin; @@ -15,9 +25,15 @@ Colours::Colours(int rPin,int gPin,int bPin) { } void Colours::writeRGB(unsigned char r,unsigned char g,unsigned char b) { - analogWrite(_rPin, r); - analogWrite(_gPin, g); - analogWrite(_bPin, b); + if (_invert) { + analogWrite(_rPin, 255-r); + analogWrite(_gPin, 255-g); + analogWrite(_bPin, 255-b); + } else { + analogWrite(_rPin, r); + analogWrite(_gPin, g); + analogWrite(_bPin, b); + } } void Colours::writeHSV(unsigned int h,unsigned char s,unsigned char v) { diff --git a/Colours.h b/Colours.h index cd08bfc..c91b30a 100644 --- a/Colours.h +++ b/Colours.h @@ -10,10 +10,13 @@ class Colours { public: Colours(int rPin,int gPin,int bPin); + Colours(int rPin,int gPin,int bPin,bool invert); void writeRGB(unsigned char r,unsigned char g,unsigned char b); void writeHSV(unsigned int h,unsigned char s,unsigned char v); private: int _rPin,_gPin,_bPin; + bool _invert; + void _initPINs(int rPin,int gPin,int bPin); }; #endif diff --git a/examples/hue_rotation/hue_rotation.ino b/examples/hue_rotation/hue_rotation.ino index 2151618..6e57069 100644 --- a/examples/hue_rotation/hue_rotation.ino +++ b/examples/hue_rotation/hue_rotation.ino @@ -1,6 +1,9 @@ #include Colours colours(6,5,3); +// For common cathode RGB LEDs comment the line above and uncomment +// the one below. +//Colours colours(6,5,3,true); void setup() { } -- cgit v1.2.3