Serial terminal, mainly for debugging purposes
Revision 0:07da1704cfd8, committed 2011-05-23
- Comitter:
- Tobias
- Date:
- Mon May 23 08:02:59 2011 +0000
- Commit message:
Changed in this revision
SerialTerm.cpp | Show annotated file Show diff for this revision Revisions of this file |
SerialTerm.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 07da1704cfd8 SerialTerm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialTerm.cpp Mon May 23 08:02:59 2011 +0000 @@ -0,0 +1,100 @@ +#include "mbed.h" +#include "SerialTerm.h" + +SerialTerm::SerialTerm(Serial *s) +{ + First_Command = NULL; + serial = s; + New_Row = false; + Command_Len = 0; +} + +void SerialTerm::Tick(void) +{ + if (New_Row) + { + serial->printf("> "); + New_Row = false; + } + + if (serial->readable()) + { + unsigned char c = serial->getc(); + if (c != '\n') + { + serial->putc(c); + Command_Buffer[Command_Len++] = c; + } + else + { + serial->printf("\n\r"); + Command_Buffer[Command_Len++] = 0; + + New_Row = true; + HandleCommand(); + + Command_Len = 0; + } + } +} + +void SerialTerm::HandleCommand(void) +{ + + int pos = 0; + int num_args = 0; + char cmd_split[16][16]; + 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 + for (int i = 0; i < 16; i++) + split_entrys[i] = cmd_split[i]; + while (num_args < 16) + { + bool found = false; + int a = 0; + while (Command_Buffer[pos] == ' ' && Command_Buffer[pos] != 0) + pos++; + + while (Command_Buffer[pos] != ' ' && Command_Buffer[pos] != 0) + { + found = true; + + cmd_split[num_args][a] = Command_Buffer[pos]; + + a++; + pos++; + } + cmd_split[num_args][a] = 0; + if (found) + num_args++; + else + break; + pos++; + } + + SerialTerm_Command *next = First_Command; + + if (num_args < 1) + return; + + while (next) + { + if (strcmp(cmd_split[0], next->cmd) == 0) + { + return (*next->func)(num_args, split_entrys, serial); + } + + next = next->next; + } + + serial->printf("Unknown command '%s'\n\r", cmd_split[0]); +} + +void SerialTerm::Add_Command(char *cmd, void (*func)(int argc, char **argv, Serial *s)) +{ + SerialTerm_Command *command = new SerialTerm_Command; + command->cmd = cmd; + command->func = func; + command->next = First_Command; + + First_Command = command; +} \ No newline at end of file
diff -r 000000000000 -r 07da1704cfd8 SerialTerm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialTerm.h Mon May 23 08:02:59 2011 +0000 @@ -0,0 +1,25 @@ +class SerialTerm; + +struct SerialTerm_Command +{ + char *cmd; + void (*func)(int argc, char **argv, Serial *s); + SerialTerm_Command *next; +}; + +class SerialTerm +{ +public: + SerialTerm(Serial *s); + + void Tick(void); + void HandleCommand(void); + void Add_Command(char *cmd, void (*func)(int argc, char **argv, Serial *s)); + + SerialTerm_Command *First_Command; + + Serial *serial; + bool New_Row; + char Command_Buffer[256]; + int Command_Len; +}; \ No newline at end of file