set led to common anode

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 libxively mbed-rtos mbed

Fork of xively-dreamforce by Ilya Dmitrichenko

Files at this revision

API Documentation at this revision

Comitter:
errordeveloper
Date:
Mon Nov 11 17:33:56 2013 +0000
Parent:
14:1bb008e28de8
Child:
16:878226cefdb1
Commit message:
Control RGB LED from a thread using pots, control fan using GPIO via an NPN transistor

Changed in this revision

app_board_io.cpp Show annotated file Show diff for this revision Revisions of this file
app_board_io.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/app_board_io.cpp	Sat Nov 09 10:15:50 2013 +0000
+++ b/app_board_io.cpp	Mon Nov 11 17:33:56 2013 +0000
@@ -1,4 +1,6 @@
+#include "mbed.h"
 #include "app_board_io.h"
+
 #include "C12832_lcd.h"
 
 extern C12832_LCD lcd;
@@ -16,4 +18,102 @@
     lcd.locate( 0, 3 );
     lcd.printf( buffer );
     //wait( 1.0 );
+}
+
+/**
+  * Demo for the RGB Led on the mbed Lab Board
+  * Pot 2 changes the color 
+  * Pot 1 changes the value
+  * the saturation is set to maximum
+  * 
+  * Based on:
+  * https://mbed.org/users/dreschpe/code/app-board-RGB2/
+  *
+  * Copyright (c) 2012 Peter Drescher - DC2PD
+  */
+
+/* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
+PwmOut r (p23);
+PwmOut g (p24);
+PwmOut b (p25);
+
+AnalogIn pot1(p19);
+AnalogIn pot2(p20);
+
+// function to convert hue , saturation and  value to RGB
+// see http://en.wikipedia.org/wiki/HSL_and_HSV
+void hsv2rgb(float H,float S, float V)
+{
+    float f,h,p,q,t;
+    int i;
+    if( S == 0.0) {
+        r = 1.0 - V;  // invert pwm !
+        g = 1.0 - V;
+        b = 1.0 - V;
+        return;
+    }
+    if(H > 360.0) H = 0.0;   // check values
+    if(S > 1.0) S = 1.0; 
+    if(S < 0.0) S = 0.0;
+    if(V > 1.0) V = 1.0;
+    if(V < 0.0) V = 0.0;
+    h = H / 60.0;
+    i = (int) h;
+    f = h - i;
+    p = V * (1.0 - S);
+    q = V * (1.0 - (S * f));
+    t = V * (1.0 - (S * (1.0 - f)));
+
+    switch(i) {
+        case 0:
+            r = 1.0 - V;  // invert pwm !
+            g = 1.0 - t;
+            b = 1.0 - p;
+            break;
+        case 1:
+            r = 1.0 - q;
+            g = 1.0 - V;
+            b = 1.0 - p;
+            break;
+        case 2:
+            r = 1.0 - p;
+            g = 1.0 - V;
+            b = 1.0 - t;
+            break;
+        case 3:
+            r = 1.0 - p;
+            g = 1.0 - q;
+            b = 1.0 - V;
+            break;
+        case 4:
+            r = 1.0 - t;
+            g = 1.0 - p;
+            b = 1.0 - V;
+            break;
+        case 5:
+        default:
+            r = 1.0 - V;
+            g = 1.0 - p;
+            b = 1.0 - q;
+            break;
+    }
+}
+
+void rgbpwm_thread(void const *args)
+{
+    float h, s, v;
+    
+    r.period(0.001);  // set pwm period
+    s = 1.0;
+    while(true){
+
+        h = (pot1.read_u16() >> 6) * 0.3515625;  // truncate to 10 bits and scale to 0 - 360;
+
+
+        v = (pot2.read_u16() >> 6) * 0.0009765624; // truncate and scale to 0 - 1;
+
+        hsv2rgb(h,s,v);
+        
+        wait_ms(500);
+    }
 }
\ No newline at end of file
--- a/app_board_io.h	Sat Nov 09 10:15:50 2013 +0000
+++ b/app_board_io.h	Mon Nov 11 17:33:56 2013 +0000
@@ -4,4 +4,6 @@
 #ifdef __cplusplus
 extern "C"
 #endif
-void lcd_printf( const char* fmt, ...  );
\ No newline at end of file
+void lcd_printf( const char* fmt, ...  );
+
+void rgbpwm_thread(void const *args);
\ No newline at end of file
--- a/main.cpp	Sat Nov 09 10:15:50 2013 +0000
+++ b/main.cpp	Mon Nov 11 17:33:56 2013 +0000
@@ -14,14 +14,20 @@
 #include "C12832_lcd.h"
 
 LM75B tmp(p28, p27);
+DigitalOut fan(p22);
 C12832_LCD lcd;
 
 #include "logo.h"
 
 int main() {
     lcd_print_xively_logo();
+    //r.period( 0.001 );
+    //g.period( 0.001 );
+    //b.period( 0.001 );
     EthernetInterface eth;
     
+    Thread _rgbpwm(rgbpwm_thread);
+    
     int s = eth.init(); //Use DHCP
     
     if( s != NULL )
@@ -79,7 +85,7 @@
         return -1;
     }
 
-    while(1) {
+    while( true ) {
       
       xi_set_value_f32( temp, tmp.read() );
         
@@ -91,23 +97,29 @@
       xi_feed_get( xi_context, &input_channels );
       printf( "   [%s]\r\n", xi_get_error_string( xi_get_last_error() ) );
       
+      lcd_printf( "led: %s\nfan: %d\n", xi_value_pointer_str( led_ctl ), xi_get_value_i32( fan_ctl ) );
+      
+      switch( xi_get_value_i32( fan_ctl ) ) {
+        case 0: fan = 0; break;
+        case 1: fan = 1; break;
+      }
       if( strcmp( xi_value_pointer_str( led_ctl ), "RED" ) == 0 )
       {
-        lcd_printf( "Turning LED red\n" );
+ 
       }
       else if( strcmp( xi_value_pointer_str( led_ctl ), "GREEN" ) == 0 )
       {
-        lcd_printf( "Turning LED green\n" );
+   
       }
       else if( strcmp( xi_value_pointer_str( led_ctl ), "BLUE" ) == 0 )
       {
-        lcd_printf( "Turning LED blue\n" );
+      
       }
       else /* OFF */
       {
-        lcd_printf( "Turning off the LED\n" );
+      
       }
       
-      wait( 15.0 );
+      wait( 5.0 );
     }
 }