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

Files at this revision

API Documentation at this revision

Comitter:
bikeNomad
Date:
Wed Feb 10 15:34:58 2010 +0000
Commit message:

Changed in this revision

getline.cpp Show annotated file Show diff for this revision Revisions of this file
getline.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /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
--- /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
--- /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> ");
+        }
+    }
+}
--- /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