Enhanced version of Simon Ford's RPC demo program allowing command-line editing and providing interactive help. Includes Stream (Serial, etc.) line editing routine in separate files.

Dependencies:   mbed

Committer:
bikeNomad
Date:
Wed Feb 10 15:34:58 2010 +0000
Revision:
0:ec7de5c0199f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 0:ec7de5c0199f 1 // Derived from Simon Ford's RPC demo program,
bikeNomad 0:ec7de5c0199f 2 // this adds line editing and help to the RPC over serial over USB operation.
bikeNomad 0:ec7de5c0199f 3 #include "mbed.h"
bikeNomad 0:ec7de5c0199f 4 #include "rpc.h"
bikeNomad 0:ec7de5c0199f 5 #include "getline.h"
bikeNomad 0:ec7de5c0199f 6
bikeNomad 0:ec7de5c0199f 7 Serial pc(USBTX, USBRX); // 9600/n/8/1
bikeNomad 0:ec7de5c0199f 8
bikeNomad 0:ec7de5c0199f 9 char command[256] = {0};
bikeNomad 0:ec7de5c0199f 10 char response[256] = {0};
bikeNomad 0:ec7de5c0199f 11
bikeNomad 0:ec7de5c0199f 12 // print a summary of classes and objects and their methods
bikeNomad 0:ec7de5c0199f 13 void usage()
bikeNomad 0:ec7de5c0199f 14 {
bikeNomad 0:ec7de5c0199f 15 pc.puts("/<class>/new pin [pin [...]] [objname] => objname\n\r");
bikeNomad 0:ec7de5c0199f 16 pc.puts("/<objname>/<method> [arg [arg ...]]\n\r");
bikeNomad 0:ec7de5c0199f 17 char resp2[256];
bikeNomad 0:ec7de5c0199f 18 rpc("/", resp2);
bikeNomad 0:ec7de5c0199f 19 char *p = strtok(resp2, " ");
bikeNomad 0:ec7de5c0199f 20 while (*p)
bikeNomad 0:ec7de5c0199f 21 {
bikeNomad 0:ec7de5c0199f 22 command[0] = '/';
bikeNomad 0:ec7de5c0199f 23 strcpy(command+1, p);
bikeNomad 0:ec7de5c0199f 24 strcat(command, "/");
bikeNomad 0:ec7de5c0199f 25 rpc(command, response);
bikeNomad 0:ec7de5c0199f 26 pc.puts(command);
bikeNomad 0:ec7de5c0199f 27 pc.puts(" : ");
bikeNomad 0:ec7de5c0199f 28 pc.puts(response);
bikeNomad 0:ec7de5c0199f 29 pc.puts("\n\r");
bikeNomad 0:ec7de5c0199f 30 p = strtok(NULL, " ");
bikeNomad 0:ec7de5c0199f 31 }
bikeNomad 0:ec7de5c0199f 32 }
bikeNomad 0:ec7de5c0199f 33
bikeNomad 0:ec7de5c0199f 34 int main()
bikeNomad 0:ec7de5c0199f 35 {
bikeNomad 0:ec7de5c0199f 36 // setup the classes that can be created dynamically
bikeNomad 0:ec7de5c0199f 37 Base::add_rpc_class<AnalogIn>();
bikeNomad 0:ec7de5c0199f 38 Base::add_rpc_class<AnalogOut>();
bikeNomad 0:ec7de5c0199f 39 Base::add_rpc_class<DigitalIn>();
bikeNomad 0:ec7de5c0199f 40 Base::add_rpc_class<DigitalOut>();
bikeNomad 0:ec7de5c0199f 41 Base::add_rpc_class<PwmOut>();
bikeNomad 0:ec7de5c0199f 42 Base::add_rpc_class<Timer>();
bikeNomad 0:ec7de5c0199f 43 Base::add_rpc_class<SPI>();
bikeNomad 0:ec7de5c0199f 44 Base::add_rpc_class<BusOut>();
bikeNomad 0:ec7de5c0199f 45 Base::add_rpc_class<BusIn>();
bikeNomad 0:ec7de5c0199f 46
bikeNomad 0:ec7de5c0199f 47 pc.puts("type help for help\n\r");
bikeNomad 0:ec7de5c0199f 48
bikeNomad 0:ec7de5c0199f 49 // receive commands, and send back the responses
bikeNomad 0:ec7de5c0199f 50 for (;;)
bikeNomad 0:ec7de5c0199f 51 {
bikeNomad 0:ec7de5c0199f 52 if (getline(pc, command, 256) > 0)
bikeNomad 0:ec7de5c0199f 53 {
bikeNomad 0:ec7de5c0199f 54 if (!strcmp(command, "help"))
bikeNomad 0:ec7de5c0199f 55 usage();
bikeNomad 0:ec7de5c0199f 56 else
bikeNomad 0:ec7de5c0199f 57 {
bikeNomad 0:ec7de5c0199f 58 rpc(command, response);
bikeNomad 0:ec7de5c0199f 59 if (strlen(response) > 0)
bikeNomad 0:ec7de5c0199f 60 {
bikeNomad 0:ec7de5c0199f 61 pc.putc('[');
bikeNomad 0:ec7de5c0199f 62 pc.puts(response);
bikeNomad 0:ec7de5c0199f 63 pc.puts("]\n\r");
bikeNomad 0:ec7de5c0199f 64 }
bikeNomad 0:ec7de5c0199f 65 }
bikeNomad 0:ec7de5c0199f 66 pc.puts("> ");
bikeNomad 0:ec7de5c0199f 67 }
bikeNomad 0:ec7de5c0199f 68 else
bikeNomad 0:ec7de5c0199f 69 {
bikeNomad 0:ec7de5c0199f 70 pc.puts("?\n\r> ");
bikeNomad 0:ec7de5c0199f 71 }
bikeNomad 0:ec7de5c0199f 72 }
bikeNomad 0:ec7de5c0199f 73 }