Nate Chandler / Mbed 2 deprecated RPC_Function_Example

Dependencies:   RPCInterface mbed

Fork of RPC_Function_Example by Varun Nambiar

Committer:
nambvarun
Date:
Fri Mar 11 23:42:52 2016 +0000
Revision:
0:74f2764b636b
Child:
1:50a40baa79bf
RPC Tutorial Program;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nambvarun 0:74f2764b636b 1 #include "mbed.h"
nambvarun 0:74f2764b636b 2 #include "mbed_rpc.h"
nambvarun 0:74f2764b636b 3
nambvarun 0:74f2764b636b 4 /**
nambvarun 0:74f2764b636b 5 * This example program has been updated to use the RPC implementation in the new mbed libraries.
nambvarun 0:74f2764b636b 6 * This example demonstrates using RPC over serial
nambvarun 0:74f2764b636b 7 */
nambvarun 0:74f2764b636b 8
nambvarun 0:74f2764b636b 9
nambvarun 0:74f2764b636b 10 // These are examples of some variable types that can be modified through RPC.
nambvarun 0:74f2764b636b 11 int wheelsOn;
nambvarun 0:74f2764b636b 12 char lcdBannerMessage;
nambvarun 0:74f2764b636b 13 float speed;
nambvarun 0:74f2764b636b 14
nambvarun 0:74f2764b636b 15 RPCVariable<int> rpcLights(&wheelsOn, "wheels");
nambvarun 0:74f2764b636b 16 RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner");
nambvarun 0:74f2764b636b 17 RPCVariable<float> rpcSpeed(&speed, "speed");
nambvarun 0:74f2764b636b 18
nambvarun 0:74f2764b636b 19 Serial pc(USBTX, USBRX);
nambvarun 0:74f2764b636b 20 void moveTo(Arguments *in, Reply *out);
nambvarun 0:74f2764b636b 21 RPCFunction rpcMove(&moveTo, "moveTo");
nambvarun 0:74f2764b636b 22 double xLoc, yLoc;
nambvarun 0:74f2764b636b 23
nambvarun 0:74f2764b636b 24 int main() {
nambvarun 0:74f2764b636b 25 //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
nambvarun 0:74f2764b636b 26
nambvarun 0:74f2764b636b 27 // receive commands, and send back the responses
nambvarun 0:74f2764b636b 28 char buf[256], outbuf[256];
nambvarun 0:74f2764b636b 29 while(1) {
nambvarun 0:74f2764b636b 30 pc.gets(buf, 256);
nambvarun 0:74f2764b636b 31 //Call the static call method on the RPC class
nambvarun 0:74f2764b636b 32 RPC::call(buf, outbuf);
nambvarun 0:74f2764b636b 33 pc.printf("%s\n", outbuf);
nambvarun 0:74f2764b636b 34 }
nambvarun 0:74f2764b636b 35 }
nambvarun 0:74f2764b636b 36
nambvarun 0:74f2764b636b 37 // Make sure the method takes in Arguments and Reply objects.
nambvarun 0:74f2764b636b 38 void moveTo (Arguments *in, Reply *out) {
nambvarun 0:74f2764b636b 39 bool success = true;
nambvarun 0:74f2764b636b 40
nambvarun 0:74f2764b636b 41 // In this scenario, when using RPC delimit the two arguments with a space.
nambvarun 0:74f2764b636b 42 xLoc = in->getArg<double>();
nambvarun 0:74f2764b636b 43 yLoc = in->getArg<double>();
nambvarun 0:74f2764b636b 44
nambvarun 0:74f2764b636b 45 // Have code here to move robot to location (xLoc, yLoc) and update success.
nambvarun 0:74f2764b636b 46
nambvarun 0:74f2764b636b 47 char buffer[200];
nambvarun 0:74f2764b636b 48 sprintf(buffer, "Successfully moved to location (%f, %f)", xLoc, yLoc);
nambvarun 0:74f2764b636b 49 if (success) {
nambvarun 0:74f2764b636b 50 out->putData(buffer);
nambvarun 0:74f2764b636b 51 } else {
nambvarun 0:74f2764b636b 52 out->putData("Failed to move to location.");
nambvarun 0:74f2764b636b 53 }
nambvarun 0:74f2764b636b 54 }