Tobias Haegermarck / SerialTerm

Dependents:   RobotArmControl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialTerm.cpp Source File

SerialTerm.cpp

00001 #include "mbed.h"
00002 #include "SerialTerm.h"
00003 
00004 SerialTerm::SerialTerm(Serial *s)
00005 {
00006     First_Command = NULL;
00007     serial = s;
00008     New_Row = false;
00009     Command_Len = 0;
00010 }
00011 
00012 void SerialTerm::Tick(void)
00013 {
00014     if (New_Row)
00015     {
00016         serial->printf("> ");
00017         New_Row = false;
00018     }
00019 
00020     if (serial->readable())
00021     {
00022         unsigned char c = serial->getc();
00023         if (c != '\n')
00024         {
00025             serial->putc(c);
00026             Command_Buffer[Command_Len++] = c;
00027         }
00028         else
00029         {
00030             serial->printf("\n\r");
00031             Command_Buffer[Command_Len++] = 0;
00032                 
00033             New_Row = true;
00034             HandleCommand();
00035                 
00036             Command_Len = 0;
00037         }
00038     }
00039 }
00040 
00041 void SerialTerm::HandleCommand(void)
00042 {
00043 
00044     int pos = 0;
00045     int num_args = 0;
00046     char cmd_split[16][16];
00047     char *split_entrys[16]; // HACKHACK: Couldent get it to properly send a tidy pointer to the callback that didnt need casting, made it use this instead, FIX ASAP
00048     for (int i = 0; i < 16; i++)
00049         split_entrys[i] = cmd_split[i];
00050     while (num_args < 16)
00051     {
00052         bool found = false;
00053         int a = 0;
00054         while (Command_Buffer[pos] == ' ' && Command_Buffer[pos] != 0)
00055             pos++;
00056         
00057         while (Command_Buffer[pos] != ' ' && Command_Buffer[pos] != 0)
00058         {
00059             found = true;
00060             
00061             cmd_split[num_args][a] = Command_Buffer[pos];
00062             
00063             a++;
00064             pos++;
00065         }
00066         cmd_split[num_args][a] = 0;
00067         if (found)
00068             num_args++;
00069         else
00070             break;
00071         pos++;
00072     }
00073 
00074     SerialTerm_Command *next = First_Command;
00075     
00076     if (num_args < 1)
00077         return;
00078     
00079     while (next)
00080     {
00081         if (strcmp(cmd_split[0], next->cmd) == 0)
00082         {
00083             return (*next->func)(num_args, split_entrys, serial);
00084         }
00085     
00086         next = next->next;
00087     }
00088     
00089     serial->printf("Unknown command '%s'\n\r", cmd_split[0]);
00090 }
00091 
00092 void SerialTerm::Add_Command(char *cmd, void (*func)(int argc, char **argv, Serial *s))
00093 {
00094     SerialTerm_Command *command = new SerialTerm_Command;
00095     command->cmd = cmd;
00096     command->func = func;
00097     command->next = First_Command;
00098     
00099     First_Command = command;
00100 }