Demo Team / Mbed 2 deprecated DataVizButtonCode

Dependencies:   EthernetInterface mbed-rtos mbed

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 #include <string> 
00004 
00005 
00006 const char* ECHO_SERVER_ADDRESS = "10.2.134.30";
00007 const int ECHO_SERVER_PORT = 7;
00008 
00009 DigitalOut led1(D7);
00010 DigitalOut led2(D6);
00011 DigitalOut led3(D5);
00012 DigitalOut led4(D4);
00013 
00014 InterruptIn DataSwitch1(D3);
00015 InterruptIn DataSwitch2(D2);
00016 InterruptIn DataSwitch3(D1);
00017 InterruptIn DataSwitch4(D0);
00018 
00019 
00020 int prev_dataset = 0;
00021 char json_str[100];
00022 int button = 0;
00023 string channel = ("channel");
00024 char dataset = '1';
00025 
00026 
00027 void dataset1_cb() 
00028 {   
00029     
00030     button = 1; 
00031 }
00032 void dataset2_cb() 
00033 {
00034     
00035     button = 2;  
00036 }
00037 void dataset3_cb() 
00038 {
00039     
00040     button = 3;    
00041 }
00042 void dataset4_cb() 
00043 {
00044     
00045     button = 4; 
00046 }
00047 
00048 int main() 
00049 {
00050     
00051     DataSwitch1.rise(&dataset1_cb);
00052     DataSwitch2.rise(&dataset2_cb);
00053     DataSwitch3.rise(&dataset3_cb);
00054     DataSwitch4.rise(&dataset4_cb);
00055 
00056     EthernetInterface eth;
00057     eth.init(); //Use DHCP
00058     eth.connect();
00059     printf("\nClient IP Address is %s\n", eth.getIPAddress());
00060     
00061     // Connect to Server
00062     TCPSocketConnection socket;
00063     while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) 
00064     {
00065         printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00066         wait(1);
00067     }
00068     printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
00069     
00070     while(1)
00071     {   if (dataset != prev_dataset)
00072         {
00073             switch(dataset)
00074             {   
00075                 case(0):
00076                     break;
00077                 case(1):
00078                     channel = "";
00079                     dataset = '1';
00080                     sprintf (json_str, "{\"channel\":%s,\"dataset\":%c\"}", channel, dataset);
00081                     //printf("Sending  message to Server : '%s' \n",json_str);
00082                     socket.send_all(json_str, sizeof(json_str) - 1);
00083                     led1=1;
00084                     led2=0;
00085                     led3=0;
00086                     led4=0;
00087                     break;
00088                 case(2):
00089                     channel = "";
00090                     dataset = '2';
00091                     sprintf (json_str, "{\"channel\":%s,\"dataset\":%c\"}", channel, dataset);
00092                     //printf("Sending  message to Server : '%s' \n",json_str);
00093                     socket.send_all(json_str, sizeof(json_str) - 1);
00094                     led1=0;
00095                     led2=1;
00096                     led3=0;
00097                     led4=0;
00098                     break;
00099                 case(3):
00100                     channel = "";
00101                     dataset = '3';
00102                     sprintf (json_str, "{\"channel\":%s,\"dataset\":%c\"}", channel, dataset);
00103                     //printf("Sending  message to Server : '%s' \n",json_str);
00104                     socket.send_all(json_str, sizeof(json_str) - 1);
00105                     led1=0;
00106                     led2=0;
00107                     led3=1;
00108                     led4=0;
00109                     break;
00110                 case(4):
00111                     channel = "";
00112                     dataset = '4';
00113                     sprintf (json_str, "{\"channel\":%s,\"dataset\":%c\"}", channel, dataset);
00114                     //printf("Sending  message to Server : '%s' \n",json_str);
00115                     socket.send_all(json_str, sizeof(json_str) - 1);
00116                     led1=0;
00117                     led2=0;
00118                     led3=0;
00119                     led4=1;       
00120                     break;
00121                 default:
00122                     break;  
00123             }
00124             prev_dataset = dataset;
00125                     // Receive message from server
00126             char buf[256];
00127             int n = socket.receive(buf, 256);
00128             buf[n] = '\0';
00129             printf("Received message from server: '%s'\n", buf);
00130         }
00131         
00132     
00133 
00134     }
00135     
00136     // Clean up
00137     socket.close();
00138     eth.disconnect();
00139     
00140     while(true) {}
00141 }
00142 
00143