Varun Nambiar / Mbed 2 deprecated RPC_Tutorial

Dependencies:   RPCInterface mbed

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 
00010 // These are examples of some variable types that can be modified through RPC.
00011 int wheelsOn;
00012 char lcdBannerMessage;
00013 float speed;
00014 
00015 RPCVariable<int> rpcLights(&wheelsOn, "wheels");
00016 RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner");
00017 RPCVariable<float> rpcSpeed(&speed, "speed");
00018 
00019 Serial pc(USBTX, USBRX);
00020 void moveTo(Arguments *in, Reply *out);
00021 RPCFunction rpcMove(&moveTo, "moveTo");
00022 double xLoc, yLoc;
00023 
00024 int main() {
00025     //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
00026     
00027     // receive commands, and send back the responses
00028     char buf[256], outbuf[256];
00029     while(1) {
00030         pc.gets(buf, 256);
00031         //Call the static call method on the RPC class
00032         RPC::call(buf, outbuf); 
00033         pc.printf("%s\n", outbuf);
00034     }
00035 }
00036 
00037 // Make sure the method takes in Arguments and Reply objects.
00038 void moveTo (Arguments *in, Reply *out)   {
00039     bool success = true;
00040     
00041     // In this scenario, when using RPC delimit the two arguments with a space.
00042     xLoc = in->getArg<double>();
00043     yLoc = in->getArg<double>();
00044  
00045     // Have code here to move robot to location (xLoc, yLoc) and update success.
00046 
00047     char buffer[200];
00048     sprintf(buffer, "Successfully moved to location (%f, %f)", xLoc, yLoc);
00049     if (success) {
00050         out->putData(buffer);
00051     } else {
00052         out->putData("Failed to move to location.");
00053     }
00054 }