From 2692c4107384fc680f1df32a12b821d79a060e00 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Thu, 12 Nov 2009 22:50:11 +0100 Subject: WriteHSV function from the mood_lamp sketch --- Colours.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Colours.cpp b/Colours.cpp index b2ae38e..c121391 100644 --- a/Colours.cpp +++ b/Colours.cpp @@ -15,7 +15,41 @@ 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); } 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; + } + } + } -- cgit v1.2.3