set led to common anode

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 libxively mbed-rtos mbed

Fork of xively-dreamforce by Ilya Dmitrichenko

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #define XI_FEED_ID 60544303 // set Xively Feed ID (numerical, no quoutes)
00005 #define XI_API_KEY "l3JC4iQeiw7k5LGvoKAGydQnJ0ekmIDr7z8mh6fBh5j130hP" // set Xively API key (double-quoted string)
00006 
00007 #include "app_board_io.h"
00008 
00009 #include "xively.h"
00010 #include "xi_err.h"
00011 
00012 #include "MMA7660.h"
00013 #include "LM75B.h"
00014 #include "C12832_lcd.h"
00015 
00016 LM75B tmp(p28, p27);
00017 DigitalOut fan(p22);
00018 PwmOut progress_led(LED1);
00019 C12832_LCD lcd;
00020 
00021 DigitalOut r (p23);
00022 DigitalOut g (p24);
00023 DigitalOut b (p25);
00024 
00025 #include "logo.h"
00026 
00027 int main()
00028 {
00029     lcd_print_xively_logo();
00030 
00031     EthernetInterface eth;
00032 
00033     int s = eth.init(); //Use DHCP
00034 
00035     if( s != NULL ) {
00036         lcd_printf( "Could not initialise. Will halt!\n" );
00037         exit( 0 );
00038     }
00039 
00040     s = eth.connect();
00041 
00042     if( s != NULL ) {
00043         lcd_printf( "Could not connect. Will halt!\n" );
00044         exit( 0 );
00045     } else {
00046         lcd_printf( "IP: %s\n", eth.getIPAddress() );
00047     }
00048 
00049     xi_feed_t output_channels;
00050     memset( &output_channels, NULL, sizeof( xi_feed_t ) );
00051 
00052     output_channels.feed_id = XI_FEED_ID;
00053     output_channels.datastream_count = 1;
00054 
00055     output_channels.datastreams[0].datapoint_count = 1;
00056     xi_datastream_t* temperature_datastream = &output_channels.datastreams[0];
00057     strcpy( temperature_datastream->datastream_id, "temp" );
00058     xi_datapoint_t* temp = &temperature_datastream->datapoints[0];
00059 
00060     xi_feed_t input_channels;
00061     memset( &input_channels, NULL, sizeof( xi_feed_t ) );
00062 
00063     input_channels.feed_id = XI_FEED_ID;
00064     input_channels.datastream_count = 2;
00065 
00066     input_channels.datastreams[0].datapoint_count = 1;
00067     xi_datastream_t* fan_ctl_datastream = &input_channels.datastreams[0];
00068     strcpy( fan_ctl_datastream->datastream_id, "fan" );
00069     xi_datapoint_t* fan_ctl = &fan_ctl_datastream->datapoints[0];
00070 
00071     input_channels.datastreams[1].datapoint_count = 1;
00072     xi_datastream_t* led_ctl_datastream = &input_channels.datastreams[1];
00073     strcpy( led_ctl_datastream->datastream_id, "led" );
00074     xi_datapoint_t* led_ctl = &led_ctl_datastream->datapoints[0];
00075 
00076     // create the xively library context
00077     xi_context_t* xi_context
00078     = xi_create_context( XI_HTTP, XI_API_KEY, output_channels.feed_id );
00079 
00080     // check if everything works
00081     if( xi_context == NULL ) {
00082         return -1;
00083     }
00084 
00085     while( true ) {
00086 
00087         xi_set_value_f32( temp, tmp.read() );
00088 
00089         printf( "Updating output channels...\r\n" );
00090         progress_led = 0.6;
00091         lcd_printf( "updating...\n" );
00092         xi_feed_update( xi_context, &output_channels );
00093         progress_led = 0.1;
00094         printf( "   [ec:%s]\r\n", xi_get_last_error() );
00095 
00096         printf( "Reading input channels...\r\n" );
00097         progress_led = 0.8;
00098         lcd_printf( "reading...\n" );
00099         xi_feed_get( xi_context, &input_channels );
00100         progress_led = 0.1;
00101         printf( "   [ec:%s]\r\n", xi_get_last_error() );
00102 
00103         lcd_printf("Now updating ...");
00104         wait( 4.0 );
00105         lcd_printf( "led: %s\nfan: %d\n", xi_value_pointer_str( led_ctl ), xi_get_value_i32( fan_ctl ) );
00106 
00107         switch( xi_get_value_i32( fan_ctl ) ) {
00108             case 0:
00109                 fan = 0;
00110                 break;
00111             case 1:
00112                 fan = 1;
00113                 break;
00114         }
00115 
00116         if( strcmp( xi_value_pointer_str( led_ctl ), "RED" ) == 0 ) {
00117             r = 0;
00118             g = 1;
00119             b = 1;
00120         } else if( strcmp( xi_value_pointer_str( led_ctl ), "GREEN" ) == 0 ) {
00121             r = 1;
00122             g = 0;
00123             b = 1;
00124         } else if( strcmp( xi_value_pointer_str( led_ctl ), "BLUE" ) == 0 ) {
00125             r = 1;
00126             g = 1;
00127             b = 0;
00128         } else { /* OFF */
00129             r = 1;
00130             g = 1;
00131             b = 1;
00132         }
00133 
00134         wait( 5.0 );
00135         progress_led = 0;
00136     }
00137 }