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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Derived from Simon Ford's RPC demo program,
00002 // this adds line editing and help to the RPC over serial over USB operation.
00003 #include "mbed.h"
00004 #include "rpc.h"
00005 #include "getline.h"
00006 
00007 Serial pc(USBTX, USBRX);    // 9600/n/8/1
00008 
00009 char command[256] = {0};
00010 char response[256] = {0};
00011 
00012 // print a summary of classes and objects and their methods
00013 void usage()
00014 {
00015     pc.puts("/<class>/new pin [pin [...]] [objname] => objname\n\r");
00016     pc.puts("/<objname>/<method> [arg [arg ...]]\n\r");
00017     char resp2[256];
00018     rpc("/", resp2);
00019     char *p = strtok(resp2, " ");
00020     while (*p)
00021     {
00022         command[0] = '/';
00023         strcpy(command+1, p);
00024         strcat(command, "/");
00025         rpc(command, response);
00026         pc.puts(command);
00027         pc.puts(" : ");
00028         pc.puts(response);
00029         pc.puts("\n\r");
00030         p = strtok(NULL, " ");
00031     }
00032 }
00033 
00034 int main()
00035 {
00036     // setup the classes that can be created dynamically
00037     Base::add_rpc_class<AnalogIn>();
00038     Base::add_rpc_class<AnalogOut>();
00039     Base::add_rpc_class<DigitalIn>();
00040     Base::add_rpc_class<DigitalOut>();
00041     Base::add_rpc_class<PwmOut>();
00042     Base::add_rpc_class<Timer>();
00043     Base::add_rpc_class<SPI>();
00044     Base::add_rpc_class<BusOut>();
00045     Base::add_rpc_class<BusIn>();
00046 
00047     pc.puts("type help for help\n\r");
00048    
00049     // receive commands, and send back the responses
00050     for (;;)
00051     {
00052         if (getline(pc, command, 256) > 0)
00053         {
00054             if (!strcmp(command, "help"))
00055                 usage();
00056             else
00057             {
00058                 rpc(command, response);
00059                 if (strlen(response) > 0)
00060                 {
00061                     pc.putc('[');
00062                     pc.puts(response);
00063                     pc.puts("]\n\r");
00064                 }
00065             }
00066             pc.puts("> ");
00067         }
00068         else
00069         {
00070             pc.puts("?\n\r> ");
00071         }
00072     }
00073 }