Serial terminal, mainly for debugging purposes
SerialTerm.cpp@0:07da1704cfd8, 2011-05-23 (annotated)
- Committer:
- Tobias
- Date:
- Mon May 23 08:02:59 2011 +0000
- Revision:
- 0:07da1704cfd8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tobias | 0:07da1704cfd8 | 1 | #include "mbed.h" |
Tobias | 0:07da1704cfd8 | 2 | #include "SerialTerm.h" |
Tobias | 0:07da1704cfd8 | 3 | |
Tobias | 0:07da1704cfd8 | 4 | SerialTerm::SerialTerm(Serial *s) |
Tobias | 0:07da1704cfd8 | 5 | { |
Tobias | 0:07da1704cfd8 | 6 | First_Command = NULL; |
Tobias | 0:07da1704cfd8 | 7 | serial = s; |
Tobias | 0:07da1704cfd8 | 8 | New_Row = false; |
Tobias | 0:07da1704cfd8 | 9 | Command_Len = 0; |
Tobias | 0:07da1704cfd8 | 10 | } |
Tobias | 0:07da1704cfd8 | 11 | |
Tobias | 0:07da1704cfd8 | 12 | void SerialTerm::Tick(void) |
Tobias | 0:07da1704cfd8 | 13 | { |
Tobias | 0:07da1704cfd8 | 14 | if (New_Row) |
Tobias | 0:07da1704cfd8 | 15 | { |
Tobias | 0:07da1704cfd8 | 16 | serial->printf("> "); |
Tobias | 0:07da1704cfd8 | 17 | New_Row = false; |
Tobias | 0:07da1704cfd8 | 18 | } |
Tobias | 0:07da1704cfd8 | 19 | |
Tobias | 0:07da1704cfd8 | 20 | if (serial->readable()) |
Tobias | 0:07da1704cfd8 | 21 | { |
Tobias | 0:07da1704cfd8 | 22 | unsigned char c = serial->getc(); |
Tobias | 0:07da1704cfd8 | 23 | if (c != '\n') |
Tobias | 0:07da1704cfd8 | 24 | { |
Tobias | 0:07da1704cfd8 | 25 | serial->putc(c); |
Tobias | 0:07da1704cfd8 | 26 | Command_Buffer[Command_Len++] = c; |
Tobias | 0:07da1704cfd8 | 27 | } |
Tobias | 0:07da1704cfd8 | 28 | else |
Tobias | 0:07da1704cfd8 | 29 | { |
Tobias | 0:07da1704cfd8 | 30 | serial->printf("\n\r"); |
Tobias | 0:07da1704cfd8 | 31 | Command_Buffer[Command_Len++] = 0; |
Tobias | 0:07da1704cfd8 | 32 | |
Tobias | 0:07da1704cfd8 | 33 | New_Row = true; |
Tobias | 0:07da1704cfd8 | 34 | HandleCommand(); |
Tobias | 0:07da1704cfd8 | 35 | |
Tobias | 0:07da1704cfd8 | 36 | Command_Len = 0; |
Tobias | 0:07da1704cfd8 | 37 | } |
Tobias | 0:07da1704cfd8 | 38 | } |
Tobias | 0:07da1704cfd8 | 39 | } |
Tobias | 0:07da1704cfd8 | 40 | |
Tobias | 0:07da1704cfd8 | 41 | void SerialTerm::HandleCommand(void) |
Tobias | 0:07da1704cfd8 | 42 | { |
Tobias | 0:07da1704cfd8 | 43 | |
Tobias | 0:07da1704cfd8 | 44 | int pos = 0; |
Tobias | 0:07da1704cfd8 | 45 | int num_args = 0; |
Tobias | 0:07da1704cfd8 | 46 | char cmd_split[16][16]; |
Tobias | 0:07da1704cfd8 | 47 | 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 |
Tobias | 0:07da1704cfd8 | 48 | for (int i = 0; i < 16; i++) |
Tobias | 0:07da1704cfd8 | 49 | split_entrys[i] = cmd_split[i]; |
Tobias | 0:07da1704cfd8 | 50 | while (num_args < 16) |
Tobias | 0:07da1704cfd8 | 51 | { |
Tobias | 0:07da1704cfd8 | 52 | bool found = false; |
Tobias | 0:07da1704cfd8 | 53 | int a = 0; |
Tobias | 0:07da1704cfd8 | 54 | while (Command_Buffer[pos] == ' ' && Command_Buffer[pos] != 0) |
Tobias | 0:07da1704cfd8 | 55 | pos++; |
Tobias | 0:07da1704cfd8 | 56 | |
Tobias | 0:07da1704cfd8 | 57 | while (Command_Buffer[pos] != ' ' && Command_Buffer[pos] != 0) |
Tobias | 0:07da1704cfd8 | 58 | { |
Tobias | 0:07da1704cfd8 | 59 | found = true; |
Tobias | 0:07da1704cfd8 | 60 | |
Tobias | 0:07da1704cfd8 | 61 | cmd_split[num_args][a] = Command_Buffer[pos]; |
Tobias | 0:07da1704cfd8 | 62 | |
Tobias | 0:07da1704cfd8 | 63 | a++; |
Tobias | 0:07da1704cfd8 | 64 | pos++; |
Tobias | 0:07da1704cfd8 | 65 | } |
Tobias | 0:07da1704cfd8 | 66 | cmd_split[num_args][a] = 0; |
Tobias | 0:07da1704cfd8 | 67 | if (found) |
Tobias | 0:07da1704cfd8 | 68 | num_args++; |
Tobias | 0:07da1704cfd8 | 69 | else |
Tobias | 0:07da1704cfd8 | 70 | break; |
Tobias | 0:07da1704cfd8 | 71 | pos++; |
Tobias | 0:07da1704cfd8 | 72 | } |
Tobias | 0:07da1704cfd8 | 73 | |
Tobias | 0:07da1704cfd8 | 74 | SerialTerm_Command *next = First_Command; |
Tobias | 0:07da1704cfd8 | 75 | |
Tobias | 0:07da1704cfd8 | 76 | if (num_args < 1) |
Tobias | 0:07da1704cfd8 | 77 | return; |
Tobias | 0:07da1704cfd8 | 78 | |
Tobias | 0:07da1704cfd8 | 79 | while (next) |
Tobias | 0:07da1704cfd8 | 80 | { |
Tobias | 0:07da1704cfd8 | 81 | if (strcmp(cmd_split[0], next->cmd) == 0) |
Tobias | 0:07da1704cfd8 | 82 | { |
Tobias | 0:07da1704cfd8 | 83 | return (*next->func)(num_args, split_entrys, serial); |
Tobias | 0:07da1704cfd8 | 84 | } |
Tobias | 0:07da1704cfd8 | 85 | |
Tobias | 0:07da1704cfd8 | 86 | next = next->next; |
Tobias | 0:07da1704cfd8 | 87 | } |
Tobias | 0:07da1704cfd8 | 88 | |
Tobias | 0:07da1704cfd8 | 89 | serial->printf("Unknown command '%s'\n\r", cmd_split[0]); |
Tobias | 0:07da1704cfd8 | 90 | } |
Tobias | 0:07da1704cfd8 | 91 | |
Tobias | 0:07da1704cfd8 | 92 | void SerialTerm::Add_Command(char *cmd, void (*func)(int argc, char **argv, Serial *s)) |
Tobias | 0:07da1704cfd8 | 93 | { |
Tobias | 0:07da1704cfd8 | 94 | SerialTerm_Command *command = new SerialTerm_Command; |
Tobias | 0:07da1704cfd8 | 95 | command->cmd = cmd; |
Tobias | 0:07da1704cfd8 | 96 | command->func = func; |
Tobias | 0:07da1704cfd8 | 97 | command->next = First_Command; |
Tobias | 0:07da1704cfd8 | 98 | |
Tobias | 0:07da1704cfd8 | 99 | First_Command = command; |
Tobias | 0:07da1704cfd8 | 100 | } |