/* * Colours - library for managing the colours on RGB LEDs * Copyright 2009, 2013 Elena Grandi * * This file is part of Colours. * * Colours is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * Colours is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Colours. If not, see . */ #include "Arduino.h" #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; pinMode(_rPin,OUTPUT); pinMode(_gPin,OUTPUT); pinMode(_bPin,OUTPUT); } void Colours::writeRGB(unsigned char r,unsigned char g,unsigned char 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) { h = h % 360; if (s==0) { writeRGB(v,v,v); } else { unsigned int f,p,q,t; f = 256*(h%60)/60; p = v*(256-s)/256; q = v*(256-f*s/256)/256; t = v*(256-s*(256-f)/256)/256; switch( (h/60) % 6 ) { case 0: writeRGB(v,t,p); break; case 1: writeRGB(q,v,p); break; case 2: writeRGB(p,v,t); break; case 3: writeRGB(p,q,v); break; case 4: writeRGB(t,p,v); break; case 5: writeRGB(v,p,q); break; } } }