RPC_Serial/main.cpp

#include "mbed.h"
#include "mbed_rpc.h"

Serial pc(USBTX, USBRX);

int i1;
float f2;

void func1(Arguments* arg, Reply* r) {
    pc.printf("obj_name: %s\n", arg->obj_name);
    pc.printf("method_name: %s\n", arg->method_name);
    pc.printf("argc: %d\n", arg->argc);
    for(int n = 0; n < arg->argc; n++) {
        pc.printf("argv[%d]: %s\n", n, arg->argv[n]);
    }
}

void func2(Arguments* arg, Reply* r) {
    r->putData<float>(f2 * i1);
}

void func3(Arguments* arg, Reply* r) {
    i1 = arg->getArg<int>();
    f2 = arg->getArg<float>();
}

int main() {
    // setup the classes that can be created dynamically
    RPC::add_rpc_class<RpcDigitalOut>();
    RPC::add_rpc_class<RpcDigitalIn>();
    RPC::add_rpc_class<RpcDigitalInOut>();
    //RPC::add_rpc_class<RpcAnalogIn>(); // need Reply::putData<unsigned short>()
    //RPC::add_rpc_class<RpcAnalogOut>(); // need Arguments::getArg<unsigned short>()
    RPC::add_rpc_class<RpcPwmOut>();
    RPC::add_rpc_class<RpcSPI>();
    RPC::add_rpc_class<RpcSerial>();
    RPC::add_rpc_class<RpcTimer>();

    RPCVariable<int> _i1(&i1, "i1");    // /i1/write 123  /i1/read
    RPCVariable<float> _f2(&f2, "f2");  // /f2/write 45.6 /f2/read

    RPCFunction _func1(func1, "func1"); // /func1/run
    RPCFunction _func2(func2, "func2"); // /func2/run 
    RPCFunction _func3(func3, "func3"); // /func3/run 123 45.6
    
    // receive commands, and send back the responses
    char buf[RPC_MAX_STRING], result[RPC_MAX_STRING];
    while(1) {
        pc.gets(buf, sizeof(buf));
        RPC::call(buf, result); 
        pc.printf("%s\n", result);
    }
}