Let the on board RGB led run thru the color circle

Dependencies:   mbed

Test program for LPC 1549 and LPC 812 board. It use the on board RGB led to scroll thru the colors.

Committer:
dreschpe
Date:
Sat Oct 17 16:47:09 2015 +0000
Revision:
1:c92fa914546d
Parent:
0:5a281c0ca6ae
change RGB names. It will work for LPC812 and LPC 1549 board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:5a281c0ca6ae 1 #include "mbed.h"
dreschpe 0:5a281c0ca6ae 2
dreschpe 0:5a281c0ca6ae 3
dreschpe 0:5a281c0ca6ae 4 /* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
dreschpe 1:c92fa914546d 5 PwmOut r (LED_RED);
dreschpe 1:c92fa914546d 6 PwmOut g (LED_GREEN);
dreschpe 1:c92fa914546d 7 PwmOut b (LED_BLUE);
dreschpe 0:5a281c0ca6ae 8
dreschpe 0:5a281c0ca6ae 9 // function to convert hue , saturation and value to RGB
dreschpe 0:5a281c0ca6ae 10 // see http://en.wikipedia.org/wiki/HSL_and_HSV
dreschpe 0:5a281c0ca6ae 11 void hsv2rgb(float H,float S, float V)
dreschpe 0:5a281c0ca6ae 12 {
dreschpe 0:5a281c0ca6ae 13 float f,h,p,q,t;
dreschpe 0:5a281c0ca6ae 14 int i;
dreschpe 0:5a281c0ca6ae 15 if( S == 0.0) {
dreschpe 0:5a281c0ca6ae 16 r = 1.0 - V; // invert pwm !
dreschpe 0:5a281c0ca6ae 17 g = 1.0 - V;
dreschpe 0:5a281c0ca6ae 18 b = 1.0 - V;
dreschpe 0:5a281c0ca6ae 19 return;
dreschpe 0:5a281c0ca6ae 20 }
dreschpe 0:5a281c0ca6ae 21 if(H > 360.0) H = 0.0; // check values
dreschpe 0:5a281c0ca6ae 22 if(S > 1.0) S = 1.0;
dreschpe 0:5a281c0ca6ae 23 if(S < 0.0) S = 0.0;
dreschpe 0:5a281c0ca6ae 24 if(V > 1.0) V = 1.0;
dreschpe 0:5a281c0ca6ae 25 if(V < 0.0) V = 0.0;
dreschpe 0:5a281c0ca6ae 26 h = H / 60.0;
dreschpe 0:5a281c0ca6ae 27 i = (int) h;
dreschpe 0:5a281c0ca6ae 28 f = h - i;
dreschpe 0:5a281c0ca6ae 29 p = V * (1.0 - S);
dreschpe 0:5a281c0ca6ae 30 q = V * (1.0 - (S * f));
dreschpe 0:5a281c0ca6ae 31 t = V * (1.0 - (S * (1.0 - f)));
dreschpe 0:5a281c0ca6ae 32
dreschpe 0:5a281c0ca6ae 33 switch(i) {
dreschpe 0:5a281c0ca6ae 34 case 0:
dreschpe 0:5a281c0ca6ae 35 r = 1.0 - V; // invert pwm !
dreschpe 0:5a281c0ca6ae 36 g = 1.0 - t;
dreschpe 0:5a281c0ca6ae 37 b = 1.0 - p;
dreschpe 0:5a281c0ca6ae 38 break;
dreschpe 0:5a281c0ca6ae 39 case 1:
dreschpe 0:5a281c0ca6ae 40 r = 1.0 - q;
dreschpe 0:5a281c0ca6ae 41 g = 1.0 - V;
dreschpe 0:5a281c0ca6ae 42 b = 1.0 - p;
dreschpe 0:5a281c0ca6ae 43 break;
dreschpe 0:5a281c0ca6ae 44 case 2:
dreschpe 0:5a281c0ca6ae 45 r = 1.0 - p;
dreschpe 0:5a281c0ca6ae 46 g = 1.0 - V;
dreschpe 0:5a281c0ca6ae 47 b = 1.0 - t;
dreschpe 0:5a281c0ca6ae 48 break;
dreschpe 0:5a281c0ca6ae 49 case 3:
dreschpe 0:5a281c0ca6ae 50 r = 1.0 - p;
dreschpe 0:5a281c0ca6ae 51 g = 1.0 - q;
dreschpe 0:5a281c0ca6ae 52 b = 1.0 - V;
dreschpe 0:5a281c0ca6ae 53 break;
dreschpe 0:5a281c0ca6ae 54 case 4:
dreschpe 0:5a281c0ca6ae 55 r = 1.0 - t;
dreschpe 0:5a281c0ca6ae 56 g = 1.0 - p;
dreschpe 0:5a281c0ca6ae 57 b = 1.0 - V;
dreschpe 0:5a281c0ca6ae 58 break;
dreschpe 0:5a281c0ca6ae 59 case 5:
dreschpe 0:5a281c0ca6ae 60 default:
dreschpe 0:5a281c0ca6ae 61 r = 1.0 - V;
dreschpe 0:5a281c0ca6ae 62 g = 1.0 - p;
dreschpe 0:5a281c0ca6ae 63 b = 1.0 - q;
dreschpe 0:5a281c0ca6ae 64 break;
dreschpe 0:5a281c0ca6ae 65 }
dreschpe 0:5a281c0ca6ae 66 }
dreschpe 0:5a281c0ca6ae 67
dreschpe 0:5a281c0ca6ae 68
dreschpe 0:5a281c0ca6ae 69
dreschpe 0:5a281c0ca6ae 70 int main() {
dreschpe 0:5a281c0ca6ae 71 float h; // hue
dreschpe 0:5a281c0ca6ae 72 float s,v; // saturation and value;
dreschpe 0:5a281c0ca6ae 73 r.period(0.001); // set pwm period
dreschpe 0:5a281c0ca6ae 74 s = 1.0;
dreschpe 0:5a281c0ca6ae 75 v = 1.0;
dreschpe 0:5a281c0ca6ae 76 h = 0.0;
dreschpe 0:5a281c0ca6ae 77 for(;;){ // run thru colors
dreschpe 0:5a281c0ca6ae 78 hsv2rgb(h,s,v);
dreschpe 0:5a281c0ca6ae 79 wait_ms(50);
dreschpe 0:5a281c0ca6ae 80 if(h<360) h++;
dreschpe 0:5a281c0ca6ae 81 else h = 0;
dreschpe 0:5a281c0ca6ae 82 }
dreschpe 0:5a281c0ca6ae 83 }