Varun Nambiar / Mbed 2 deprecated RPC_Function_Example

Dependencies:   RPCInterface mbed

Fork of RPC_Tutorial by Varun Nambiar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbed_rpc.h"
00003 
00004 /**
00005  *  This example program has been updated to use the RPC implementation in the new mbed libraries.
00006  *  This example demonstrates using RPC over serial
00007  */
00008 
00009 Serial pc(USBTX, USBRX);
00010 void moveTo(Arguments *in, Reply *out);
00011 RPCFunction rpcMove(&moveTo, "moveTo");
00012 double xLoc, yLoc;
00013 
00014 int main() {
00015     //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
00016     
00017     // receive commands, and send back the responses
00018     char buf[256], outbuf[256];
00019     while(1) {
00020         pc.gets(buf, 256);
00021         //Call the static call method on the RPC class
00022         RPC::call(buf, outbuf); 
00023         pc.printf("%s\n", outbuf);
00024     }
00025 }
00026 
00027 // Make sure the method takes in Arguments and Reply objects.
00028 void moveTo (Arguments *in, Reply *out)   {
00029     bool success = true;
00030     
00031     // In this scenario, when using RPC delimit the two arguments with a space.
00032     xLoc = in->getArg<double>();
00033     yLoc = in->getArg<double>();
00034  
00035     // Have code here to move robot to location (xLoc, yLoc) and update success.
00036 
00037     char buffer[200];
00038     sprintf(buffer, "Successfully moved to location (%f, %f)", xLoc, yLoc);
00039     if (success) {
00040         out->putData(buffer);
00041     } else {
00042         out->putData("Failed to move to location.");
00043     }
00044 }