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.
Revision 0:ec7de5c0199f, committed 2010-02-10
- Comitter:
- bikeNomad
- Date:
- Wed Feb 10 15:34:58 2010 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r ec7de5c0199f getline.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getline.cpp Wed Feb 10 15:34:58 2010 +0000 @@ -0,0 +1,32 @@ +#include "mbed.h" +// receive a line from a stream, allowing backspace editing, +// and checking for buffer overflow. Terminates on either a \n or \r. +size_t getline(Stream &s, char *buf, size_t bufsize) +{ + char c; + size_t receivedChars = 0; + for(;;) + { + c = s.getc(); + if (c == '\r' || c == '\n') + break; + s.putc(c); + if (c == '\b') + { + if (receivedChars > 0) + { + buf--; + receivedChars--; + } + } + else if (receivedChars < bufsize - 1) + { + *buf++ = c; + receivedChars++; + } + } + *buf++ = 0; + s.putc('\n'); + s.putc('\r'); + return receivedChars; +} \ No newline at end of file
diff -r 000000000000 -r ec7de5c0199f getline.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getline.h Wed Feb 10 15:34:58 2010 +0000 @@ -0,0 +1,5 @@ + mbed_save_all(callback); +} +// receive a line from a stream, allowing backspace editing, +// and checking for buffer overflow. Terminates on either a \n or \r. +extern size_t getline(Stream &s, char *buf, size_t bufsize); \ No newline at end of file
diff -r 000000000000 -r ec7de5c0199f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Feb 10 15:34:58 2010 +0000 @@ -0,0 +1,73 @@ +// Derived from Simon Ford's RPC demo program, +// this adds line editing and help to the RPC over serial over USB operation. +#include "mbed.h" +#include "rpc.h" +#include "getline.h" + +Serial pc(USBTX, USBRX); // 9600/n/8/1 + +char command[256] = {0}; +char response[256] = {0}; + +// print a summary of classes and objects and their methods +void usage() +{ + pc.puts("/<class>/new pin [pin [...]] [objname] => objname\n\r"); + pc.puts("/<objname>/<method> [arg [arg ...]]\n\r"); + char resp2[256]; + rpc("/", resp2); + char *p = strtok(resp2, " "); + while (*p) + { + command[0] = '/'; + strcpy(command+1, p); + strcat(command, "/"); + rpc(command, response); + pc.puts(command); + pc.puts(" : "); + pc.puts(response); + pc.puts("\n\r"); + p = strtok(NULL, " "); + } +} + +int main() +{ + // setup the classes that can be created dynamically + Base::add_rpc_class<AnalogIn>(); + Base::add_rpc_class<AnalogOut>(); + Base::add_rpc_class<DigitalIn>(); + Base::add_rpc_class<DigitalOut>(); + Base::add_rpc_class<PwmOut>(); + Base::add_rpc_class<Timer>(); + Base::add_rpc_class<SPI>(); + Base::add_rpc_class<BusOut>(); + Base::add_rpc_class<BusIn>(); + + pc.puts("type help for help\n\r"); + + // receive commands, and send back the responses + for (;;) + { + if (getline(pc, command, 256) > 0) + { + if (!strcmp(command, "help")) + usage(); + else + { + rpc(command, response); + if (strlen(response) > 0) + { + pc.putc('['); + pc.puts(response); + pc.puts("]\n\r"); + } + } + pc.puts("> "); + } + else + { + pc.puts("?\n\r> "); + } + } +}
diff -r 000000000000 -r ec7de5c0199f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Feb 10 15:34:58 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0