Norimasa Okamoto / Mbed 2 deprecated app-board-websocket-rpc

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 WebSocketClient mbed-rpc mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // app-board-websocket-rpc/main.cpp
00002 #include "EthernetInterface.h"
00003 #include "Websocket.h"
00004 #include "mbed_rpc.h"
00005 #include "C12832_lcd.h"
00006 #include "LM75B.h"
00007 #include "MMA7660.h"
00008 #include <string>
00009 
00010 #define CHANNEL "appboard-rpc"
00011 #define URL "ws://sockets.mbed.org/ws/"CHANNEL"/rw"
00012 #define VIEWER "http://va009039-mbed.appspot.com/appboard/"CHANNEL
00013 
00014 Serial pc(USBTX, USBRX);
00015 C12832_LCD lcd;
00016 LM75B LM75B_tmp(p28, p27);
00017 MMA7660 MMA(p28, p27);
00018  
00019 int main() {
00020     pc.baud(921600);
00021     printf("%s\n", __FILE__);
00022 
00023     EthernetInterface eth;
00024     eth.init(); //Use DHCP
00025     eth.connect();
00026     pc.printf("IP Address is %s\n\r", eth.getIPAddress());
00027  
00028     Websocket ws(URL);
00029     ws.connect();
00030 
00031     RPC::add_rpc_class<RpcDigitalOut>();
00032     RPC::add_rpc_class<RpcDigitalIn>();
00033     RPC::add_rpc_class<RpcDigitalInOut>();
00034     RPC::add_rpc_class<RpcAnalogIn>();
00035     RPC::add_rpc_class<RpcAnalogOut>();
00036     RPC::add_rpc_class<RpcPwmOut>();
00037     RPC::add_rpc_class<RpcSPI>();
00038     RPC::add_rpc_class<RpcSerial>();
00039     RPC::add_rpc_class<RpcTimer>();
00040 
00041     RPC::construct<RpcDigitalOut, PinName, const char*>(LED1, "led1"); // mbed LED  
00042     RPC::construct<RpcDigitalOut, PinName, const char*>(LED2, "led2");  
00043     RPC::construct<RpcDigitalOut, PinName, const char*>(LED3, "led3");  
00044     RPC::construct<RpcDigitalOut, PinName, const char*>(LED4, "led4");  
00045 
00046     RPC::construct<RpcDigitalIn, PinName, const char*>(p12, "Down"); // Joystick  
00047     RPC::construct<RpcDigitalIn, PinName, const char*>(p13, "Left");
00048     RPC::construct<RpcDigitalIn, PinName, const char*>(p14, "Centre");
00049     RPC::construct<RpcDigitalIn, PinName, const char*>(p15, "Up");
00050     RPC::construct<RpcDigitalIn, PinName, const char*>(p16, "Right");
00051 
00052     RPC::construct<RpcAnalogIn, PinName, const char*>(p19, "pot1");  // Potentiometers left
00053     RPC::construct<RpcAnalogIn, PinName, const char*>(p20, "pot2");  // right
00054 
00055     RPC::construct<RpcPwmOut, PinName, const char*>(p23, "Red"); // RGB LED 
00056     RPC::construct<RpcPwmOut, PinName, const char*>(p24, "Green"); 
00057     RPC::construct<RpcPwmOut, PinName, const char*>(p25, "Blue"); 
00058 
00059     pc.printf("\n\n"VIEWER"\n\n");
00060 
00061     Timer t,MMA_t;
00062     t.reset();
00063     t.start();
00064     MMA_t.reset();
00065     MMA_t.start();
00066     float pre_x,pre_y,pre_z;
00067     float pre_pot[2] = {0.0, 0.0};
00068     const char* pot_name[] = {"pot1", "pot2", NULL};
00069     char buf[RPC_MAX_STRING], result[RPC_MAX_STRING];
00070     while(1) {
00071         if (ws.read(buf)) {
00072             pc.printf("ws.read: %s\n", buf);
00073             lcd.locate(0, 0);
00074             lcd.printf("rcv: %s", buf);
00075             if (buf[0] == '/') {
00076                 RPC::call(buf, result); 
00077                 pc.printf("result: %s\n", result);
00078                 ws.send(result);
00079             }
00080             t.reset();
00081         }
00082         
00083         for(int i = 0; pot_name[i] != NULL; i++) {
00084             RPC* rpc = RPC::lookup(pot_name[i]);
00085             if (rpc) {
00086                 float pot = reinterpret_cast<RpcAnalogIn*>(rpc)->read();
00087                 if (abs(pre_pot[i] - pot) > 0.05) {
00088                     pre_pot[i] = pot;
00089                     snprintf(buf, sizeof(buf), "{\"%s\":%.2f}", pot_name[i], pot);
00090                     pc.printf("ws.send: %s\n", buf);
00091                     lcd.locate(0, 16);
00092                     lcd.printf("snd: %s", buf);
00093                     ws.send(buf);
00094                     t.reset();
00095                 }
00096             }
00097         }
00098         if (t.read() > 60) {
00099             snprintf(buf, sizeof(buf), "{\"LM75B\":%.2f}", LM75B_tmp.read());
00100             pc.printf("ws.send: %s\n", buf);
00101             ws.send(buf);
00102             t.reset();
00103         }
00104         if (MMA_t.read_ms() > 100) {
00105             MMA_t.reset();
00106             float x = MMA.x();
00107             float y = MMA.y();
00108             float z = MMA.z();
00109             if (abs(pre_x - x) > 0.1 || abs(pre_y - y) > 0.1 || abs(pre_z - z) > 0.1) {
00110                 pre_x = x; pre_y = y; pre_z = z;
00111                 snprintf(buf, sizeof(buf), "{\"MMA7660\":{\"x\":%.2f,\"y\":%.2f,\"z\":%.2f}}", x, y, z);
00112                 lcd.locate(0, 0);
00113                 lcd.printf(buf);
00114                 pc.printf("ws.send: %s\n", buf);
00115             }
00116         }
00117         //Thread::wait(100);
00118     }
00119 }