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@2:52c13333401e, 2012-10-20 (annotated)
- Committer:
- dreschpe
- Date:
- Sat Oct 20 09:14:13 2012 +0000
- Revision:
- 2:52c13333401e
- Parent:
- 1:670665e77763
- Child:
- 3:139f52e5e15c
change ADC scaling
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 | 2:52c13333401e | 8 | * This Demo run only on LPC1768 ! |
dreschpe | 2:52c13333401e | 9 | * The LPC11U24 has different PWM pins ! |
dreschpe | 1:670665e77763 | 10 | */ |
dreschpe | 1:670665e77763 | 11 | |
dreschpe | 1:670665e77763 | 12 | |
chris | 0:f86c572491c3 | 13 | #include "mbed.h" |
dreschpe | 1:670665e77763 | 14 | #include "Small_7.h" |
dreschpe | 1:670665e77763 | 15 | #include "Arial_9.h" |
dreschpe | 1:670665e77763 | 16 | #include "stdio.h" |
dreschpe | 1:670665e77763 | 17 | #include "C12832_lcd.h" |
chris | 0:f86c572491c3 | 18 | |
dreschpe | 1:670665e77763 | 19 | /* 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 | 20 | PwmOut r (p23); |
chris | 0:f86c572491c3 | 21 | PwmOut g (p24); |
chris | 0:f86c572491c3 | 22 | PwmOut b (p25); |
chris | 0:f86c572491c3 | 23 | |
dreschpe | 1:670665e77763 | 24 | // LCD object |
dreschpe | 1:670665e77763 | 25 | C12832_LCD LCD("LCD"); |
dreschpe | 1:670665e77763 | 26 | |
dreschpe | 1:670665e77763 | 27 | AnalogIn Pot1(p19); |
dreschpe | 1:670665e77763 | 28 | AnalogIn Pot2(p20); |
dreschpe | 1:670665e77763 | 29 | |
dreschpe | 1:670665e77763 | 30 | // function to convert hue , saturation and value to RGB |
dreschpe | 1:670665e77763 | 31 | // see http://en.wikipedia.org/wiki/HSL_and_HSV |
dreschpe | 1:670665e77763 | 32 | void hsv2rgb(float H,float S, float V) |
dreschpe | 1:670665e77763 | 33 | { |
dreschpe | 1:670665e77763 | 34 | float f,h,p,q,t; |
dreschpe | 1:670665e77763 | 35 | int i; |
dreschpe | 1:670665e77763 | 36 | if( S == 0.0) { |
dreschpe | 1:670665e77763 | 37 | r = 1.0 - V; // invert pwm ! |
dreschpe | 1:670665e77763 | 38 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 39 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 40 | return; |
dreschpe | 1:670665e77763 | 41 | } |
dreschpe | 1:670665e77763 | 42 | if(H > 360.0) H = 0.0; // check values |
dreschpe | 1:670665e77763 | 43 | if(S > 1.0) S = 1.0; |
dreschpe | 1:670665e77763 | 44 | if(S < 0.0) S = 0.0; |
dreschpe | 1:670665e77763 | 45 | if(V > 1.0) V = 1.0; |
dreschpe | 1:670665e77763 | 46 | if(V < 0.0) V = 0.0; |
dreschpe | 1:670665e77763 | 47 | h = H / 60.0; |
dreschpe | 1:670665e77763 | 48 | i = (int) h; |
dreschpe | 1:670665e77763 | 49 | f = h - i; |
dreschpe | 1:670665e77763 | 50 | p = V * (1.0 - S); |
dreschpe | 1:670665e77763 | 51 | q = V * (1.0 - (S * f)); |
dreschpe | 1:670665e77763 | 52 | t = V * (1.0 - (S * (1.0 - f))); |
dreschpe | 1:670665e77763 | 53 | |
dreschpe | 1:670665e77763 | 54 | switch(i) { |
dreschpe | 1:670665e77763 | 55 | case 0: |
dreschpe | 1:670665e77763 | 56 | r = 1.0 - V; // invert pwm ! |
dreschpe | 1:670665e77763 | 57 | g = 1.0 - t; |
dreschpe | 1:670665e77763 | 58 | b = 1.0 - p; |
dreschpe | 1:670665e77763 | 59 | break; |
dreschpe | 1:670665e77763 | 60 | case 1: |
dreschpe | 1:670665e77763 | 61 | r = 1.0 - q; |
dreschpe | 1:670665e77763 | 62 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 63 | b = 1.0 - p; |
dreschpe | 1:670665e77763 | 64 | break; |
dreschpe | 1:670665e77763 | 65 | case 2: |
dreschpe | 1:670665e77763 | 66 | r = 1.0 - p; |
dreschpe | 1:670665e77763 | 67 | g = 1.0 - V; |
dreschpe | 1:670665e77763 | 68 | b = 1.0 - t; |
dreschpe | 1:670665e77763 | 69 | break; |
dreschpe | 1:670665e77763 | 70 | case 3: |
dreschpe | 1:670665e77763 | 71 | r = 1.0 - p; |
dreschpe | 1:670665e77763 | 72 | g = 1.0 - q; |
dreschpe | 1:670665e77763 | 73 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 74 | break; |
dreschpe | 1:670665e77763 | 75 | case 4: |
dreschpe | 1:670665e77763 | 76 | r = 1.0 - t; |
dreschpe | 1:670665e77763 | 77 | g = 1.0 - p; |
dreschpe | 1:670665e77763 | 78 | b = 1.0 - V; |
dreschpe | 1:670665e77763 | 79 | break; |
dreschpe | 1:670665e77763 | 80 | case 5: |
dreschpe | 1:670665e77763 | 81 | default: |
dreschpe | 1:670665e77763 | 82 | r = 1.0 - V; |
dreschpe | 1:670665e77763 | 83 | g = 1.0 - p; |
dreschpe | 1:670665e77763 | 84 | b = 1.0 - q; |
dreschpe | 1:670665e77763 | 85 | break; |
dreschpe | 1:670665e77763 | 86 | } |
dreschpe | 1:670665e77763 | 87 | } |
dreschpe | 1:670665e77763 | 88 | |
dreschpe | 1:670665e77763 | 89 | |
chris | 0:f86c572491c3 | 90 | int main() |
chris | 0:f86c572491c3 | 91 | { |
dreschpe | 1:670665e77763 | 92 | float h; // hue |
dreschpe | 1:670665e77763 | 93 | float s,v; // saturation and value; |
dreschpe | 2:52c13333401e | 94 | unsigned short temp; |
dreschpe | 1:670665e77763 | 95 | r.period(0.001); // set pwm period |
dreschpe | 1:670665e77763 | 96 | |
dreschpe | 1:670665e77763 | 97 | LCD.claim(stdout); // send stdout to the LCD display |
dreschpe | 1:670665e77763 | 98 | LCD.cls(); |
dreschpe | 1:670665e77763 | 99 | LCD.locate(10,0); |
dreschpe | 1:670665e77763 | 100 | LCD.set_font((unsigned char*) Arial_9); |
dreschpe | 1:670665e77763 | 101 | printf("RGB Led Demo"); |
dreschpe | 2:52c13333401e | 102 | LCD.copy_to_lcd(); |
dreschpe | 1:670665e77763 | 103 | LCD.set_font((unsigned char*) Small_7); |
dreschpe | 1:670665e77763 | 104 | s = 1.0; |
dreschpe | 1:670665e77763 | 105 | for(;;){ |
dreschpe | 1:670665e77763 | 106 | // get Poti 1 for color |
dreschpe | 2:52c13333401e | 107 | temp = Pot1.read_u16(); |
dreschpe | 2:52c13333401e | 108 | temp = temp >> 6; // truncate to 10 Bit |
dreschpe | 2:52c13333401e | 109 | h = temp * 0.3515625; // scale to 0 - 360; |
dreschpe | 1:670665e77763 | 110 | LCD.locate(0,13); |
dreschpe | 2:52c13333401e | 111 | printf("Colour = %3.2f degree",h); |
dreschpe | 1:670665e77763 | 112 | // get Poti 2 fo value |
dreschpe | 2:52c13333401e | 113 | temp = Pot2.read_u16(); |
dreschpe | 2:52c13333401e | 114 | temp = temp >> 6; |
dreschpe | 2:52c13333401e | 115 | v = temp * 0.0009765624; // scale to 0 - 1; |
dreschpe | 1:670665e77763 | 116 | LCD.locate(0,23); |
dreschpe | 1:670665e77763 | 117 | printf("Val = %01.3f ",v); |
dreschpe | 1:670665e77763 | 118 | LCD.copy_to_lcd(); |
dreschpe | 1:670665e77763 | 119 | hsv2rgb(h,s,v); |
dreschpe | 1:670665e77763 | 120 | wait_ms(500); |
chris | 0:f86c572491c3 | 121 | } |
chris | 0:f86c572491c3 | 122 | } |