Example program to cycle the RGB LED on the mbed application board through all colours
Dependencies: C12832_lcd LCD_fonts mbed
Fork of app-board-RGB by
Test Program to show the RGB Led on the mbed Lab Board
The color is changed by Pot 2, the value by Pot 1.
The program use a function to convert hue , saturation and value to RGB.
With this parameters you can change the color thru the rainbow. see http://en.wikipedia.org/wiki/HSL_and_HSV
main.cpp@1:670665e77763, 2012-10-20 (annotated)
- Committer:
- dreschpe
- Date:
- Sat Oct 20 00:21:54 2012 +0000
- Revision:
- 1:670665e77763
- Parent:
- 0:f86c572491c3
- Child:
- 2:52c13333401e
Test program for the RGB led on the mbed Lab Board V1.0;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 1:670665e77763 | 1 | /** |
dreschpe | 1:670665e77763 | 2 | * Demo for the RGB Led on the mbed Lab Board |
dreschpe | 1:670665e77763 | 3 | * Pot 2 changes the color |
dreschpe | 1:670665e77763 | 4 | * Pot 1 changes the value |
dreschpe | 1:670665e77763 | 5 | * the saturation is set to maximum |
dreschpe | 1:670665e77763 | 6 | * |
dreschpe | 1:670665e77763 | 7 | * Copyright (c) 2012 Peter Drescher - DC2PD |
dreschpe | 1:670665e77763 | 8 | */ |
dreschpe | 1:670665e77763 | 9 | |
dreschpe | 1:670665e77763 | 10 | |
chris | 0:f86c572491c3 | 11 | #include "mbed.h" |
dreschpe | 1:670665e77763 | 12 | #include "Small_7.h" |
dreschpe | 1:670665e77763 | 13 | #include "Arial_9.h" |
dreschpe | 1:670665e77763 | 14 | #include "stdio.h" |
dreschpe | 1:670665e77763 | 15 | #include "C12832_lcd.h" |
chris | 0:f86c572491c3 | 16 | |
dreschpe | 1:670665e77763 | 17 | /* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */ |
chris | 0:f86c572491c3 | 18 | PwmOut r (p23); |
chris | 0:f86c572491c3 | 19 | PwmOut g (p24); |
chris | 0:f86c572491c3 | 20 | PwmOut b (p25); |
chris | 0:f86c572491c3 | 21 | |
dreschpe | 1:670665e77763 | 22 | // LCD object |
dreschpe | 1:670665e77763 | 23 | C12832_LCD LCD("LCD"); |
dreschpe | 1:670665e77763 | 24 | |
dreschpe | 1:670665e77763 | 25 | AnalogIn Pot1(p19); |
dreschpe | 1:670665e77763 | 26 | AnalogIn Pot2(p20); |
dreschpe | 1:670665e77763 | 27 | |
dreschpe | 1:670665e77763 | 28 | // function to convert hue , saturation and value to RGB |
dreschpe | 1:670665e77763 | 29 | // see http://en.wikipedia.org/wiki/HSL_and_HSV |
dreschpe | 1:670665e77763 | 30 | void hsv2rgb(float H,float S, float V) |
dreschpe | 1:670665e77763 | 31 | { |
dreschpe | 1:670665e77763 | 32 | float f,h,p,q,t; |
dreschpe | 1:670665e77763 | 33 | int i; |
dreschpe | 1:670665e77763 | 34 | if( S == 0.0) { |
dreschpe | 1:670665e77763 | 35 | r = 1.0 - V; // invert pwm ! |
dreschpe | 1:670665e77763 | 36 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 37 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 38 | return; |
dreschpe | 1:670665e77763 | 39 | } |
dreschpe | 1:670665e77763 | 40 | if(H > 360.0) H = 0.0; // check values |
dreschpe | 1:670665e77763 | 41 | if(S > 1.0) S = 1.0; |
dreschpe | 1:670665e77763 | 42 | if(S < 0.0) S = 0.0; |
dreschpe | 1:670665e77763 | 43 | if(V > 1.0) V = 1.0; |
dreschpe | 1:670665e77763 | 44 | if(V < 0.0) V = 0.0; |
dreschpe | 1:670665e77763 | 45 | h = H / 60.0; |
dreschpe | 1:670665e77763 | 46 | i = (int) h; |
dreschpe | 1:670665e77763 | 47 | f = h - i; |
dreschpe | 1:670665e77763 | 48 | p = V * (1.0 - S); |
dreschpe | 1:670665e77763 | 49 | q = V * (1.0 - (S * f)); |
dreschpe | 1:670665e77763 | 50 | t = V * (1.0 - (S * (1.0 - f))); |
dreschpe | 1:670665e77763 | 51 | |
dreschpe | 1:670665e77763 | 52 | switch(i) { |
dreschpe | 1:670665e77763 | 53 | case 0: |
dreschpe | 1:670665e77763 | 54 | r = 1.0 - V; // invert pwm ! |
dreschpe | 1:670665e77763 | 55 | g = 1.0 - t; |
dreschpe | 1:670665e77763 | 56 | b = 1.0 - p; |
dreschpe | 1:670665e77763 | 57 | break; |
dreschpe | 1:670665e77763 | 58 | case 1: |
dreschpe | 1:670665e77763 | 59 | r = 1.0 - q; |
dreschpe | 1:670665e77763 | 60 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 61 | b = 1.0 - p; |
dreschpe | 1:670665e77763 | 62 | break; |
dreschpe | 1:670665e77763 | 63 | case 2: |
dreschpe | 1:670665e77763 | 64 | r = 1.0 - p; |
dreschpe | 1:670665e77763 | 65 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 66 | b = 1.0 - t; |
dreschpe | 1:670665e77763 | 67 | break; |
dreschpe | 1:670665e77763 | 68 | case 3: |
dreschpe | 1:670665e77763 | 69 | r = 1.0 - p; |
dreschpe | 1:670665e77763 | 70 | g = 1.0 - q; |
dreschpe | 1:670665e77763 | 71 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 72 | break; |
dreschpe | 1:670665e77763 | 73 | case 4: |
dreschpe | 1:670665e77763 | 74 | r = 1.0 - t; |
dreschpe | 1:670665e77763 | 75 | g = 1.0 - p; |
dreschpe | 1:670665e77763 | 76 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 77 | break; |
dreschpe | 1:670665e77763 | 78 | case 5: |
dreschpe | 1:670665e77763 | 79 | default: |
dreschpe | 1:670665e77763 | 80 | r = 1.0 - V; |
dreschpe | 1:670665e77763 | 81 | g = 1.0 - p; |
dreschpe | 1:670665e77763 | 82 | b = 1.0 - q; |
dreschpe | 1:670665e77763 | 83 | break; |
dreschpe | 1:670665e77763 | 84 | } |
dreschpe | 1:670665e77763 | 85 | } |
dreschpe | 1:670665e77763 | 86 | |
dreschpe | 1:670665e77763 | 87 | |
chris | 0:f86c572491c3 | 88 | int main() |
chris | 0:f86c572491c3 | 89 | { |
dreschpe | 1:670665e77763 | 90 | float h; // hue |
dreschpe | 1:670665e77763 | 91 | float s,v; // saturation and value; |
dreschpe | 1:670665e77763 | 92 | float temp,temp2; |
dreschpe | 1:670665e77763 | 93 | r.period(0.001); // set pwm period |
dreschpe | 1:670665e77763 | 94 | |
dreschpe | 1:670665e77763 | 95 | LCD.claim(stdout); // send stdout to the LCD display |
dreschpe | 1:670665e77763 | 96 | LCD.cls(); |
dreschpe | 1:670665e77763 | 97 | LCD.locate(10,0); |
dreschpe | 1:670665e77763 | 98 | LCD.set_font((unsigned char*) Arial_9); |
dreschpe | 1:670665e77763 | 99 | printf("RGB Led Demo"); |
dreschpe | 1:670665e77763 | 100 | LCD.set_font((unsigned char*) Small_7); |
dreschpe | 1:670665e77763 | 101 | s = 1.0; |
dreschpe | 1:670665e77763 | 102 | for(;;){ |
dreschpe | 1:670665e77763 | 103 | // get Poti 1 for color |
dreschpe | 1:670665e77763 | 104 | temp = Pot1.read(); |
dreschpe | 1:670665e77763 | 105 | temp2 = Pot1.read(); |
dreschpe | 1:670665e77763 | 106 | h = (temp + temp2) * 180; |
dreschpe | 1:670665e77763 | 107 | LCD.locate(0,13); |
dreschpe | 1:670665e77763 | 108 | printf("Colour = %3.2f ",h); |
dreschpe | 1:670665e77763 | 109 | // get Poti 2 fo value |
dreschpe | 1:670665e77763 | 110 | temp = Pot2.read(); |
dreschpe | 1:670665e77763 | 111 | temp2 = Pot2.read(); |
dreschpe | 1:670665e77763 | 112 | v = (temp + temp2) / 2; |
dreschpe | 1:670665e77763 | 113 | LCD.locate(0,23); |
dreschpe | 1:670665e77763 | 114 | printf("Val = %01.3f ",v); |
dreschpe | 1:670665e77763 | 115 | LCD.copy_to_lcd(); |
dreschpe | 1:670665e77763 | 116 | hsv2rgb(h,s,v); |
dreschpe | 1:670665e77763 | 117 | wait_ms(500); |
chris | 0:f86c572491c3 | 118 | } |
chris | 0:f86c572491c3 | 119 | } |