Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
SerialConsole.h@0:ece62a42511f, 2014-08-31 (annotated)
- Committer:
- uswickra
- Date:
- Sun Aug 31 06:41:40 2014 +0000
- Revision:
- 0:ece62a42511f
- Child:
- 2:04d8e2ad8cff
commit programme;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uswickra | 0:ece62a42511f | 1 | #include "mbed.h" |
uswickra | 0:ece62a42511f | 2 | #include "Command.h" |
uswickra | 0:ece62a42511f | 3 | #include <vector> |
uswickra | 0:ece62a42511f | 4 | extern Serial pc; |
uswickra | 0:ece62a42511f | 5 | |
uswickra | 0:ece62a42511f | 6 | using namespace std; |
uswickra | 0:ece62a42511f | 7 | |
uswickra | 0:ece62a42511f | 8 | class SerialConsole |
uswickra | 0:ece62a42511f | 9 | { |
uswickra | 0:ece62a42511f | 10 | const char* prompt_str; |
uswickra | 0:ece62a42511f | 11 | char buffer[100]; |
uswickra | 0:ece62a42511f | 12 | vector<Cmd*> cmds; |
uswickra | 0:ece62a42511f | 13 | |
uswickra | 0:ece62a42511f | 14 | int param; |
uswickra | 0:ece62a42511f | 15 | //available commands |
uswickra | 0:ece62a42511f | 16 | Led_On_Cmd cmd1; |
uswickra | 0:ece62a42511f | 17 | Led_Off_Cmd cmd2; |
uswickra | 0:ece62a42511f | 18 | Cmd null_cmd; |
uswickra | 0:ece62a42511f | 19 | |
uswickra | 0:ece62a42511f | 20 | public: |
uswickra | 0:ece62a42511f | 21 | SerialConsole() { |
uswickra | 0:ece62a42511f | 22 | prompt_str = "p542% "; |
uswickra | 0:ece62a42511f | 23 | init(); |
uswickra | 0:ece62a42511f | 24 | }; |
uswickra | 0:ece62a42511f | 25 | |
uswickra | 0:ece62a42511f | 26 | SerialConsole(char* pr) { |
uswickra | 0:ece62a42511f | 27 | prompt_str = pr; |
uswickra | 0:ece62a42511f | 28 | init(); |
uswickra | 0:ece62a42511f | 29 | }; |
uswickra | 0:ece62a42511f | 30 | |
uswickra | 0:ece62a42511f | 31 | void start() { |
uswickra | 0:ece62a42511f | 32 | //start with prompt |
uswickra | 0:ece62a42511f | 33 | prompt(); |
uswickra | 0:ece62a42511f | 34 | // |
uswickra | 0:ece62a42511f | 35 | int count = 0 ; |
uswickra | 0:ece62a42511f | 36 | while(1) { |
uswickra | 0:ece62a42511f | 37 | char ch = pc.getc(); |
uswickra | 0:ece62a42511f | 38 | buffer[count++] = ch ; |
uswickra | 0:ece62a42511f | 39 | //check for carriage return or new line |
uswickra | 0:ece62a42511f | 40 | if(ch == 0x0a || ch == 0x0d ) { |
uswickra | 0:ece62a42511f | 41 | //for each new line parse command then execute command |
uswickra | 0:ece62a42511f | 42 | Cmd* in_cmd = parse_command(count); |
uswickra | 0:ece62a42511f | 43 | in_cmd->execute(param); |
uswickra | 0:ece62a42511f | 44 | //reset count/buffer |
uswickra | 0:ece62a42511f | 45 | count = 0; |
uswickra | 0:ece62a42511f | 46 | //go to a new command line |
uswickra | 0:ece62a42511f | 47 | newline(); |
uswickra | 0:ece62a42511f | 48 | prompt(); |
uswickra | 0:ece62a42511f | 49 | continue ; |
uswickra | 0:ece62a42511f | 50 | } else if (ch == 'q') { |
uswickra | 0:ece62a42511f | 51 | //exit |
uswickra | 0:ece62a42511f | 52 | newline(); |
uswickra | 0:ece62a42511f | 53 | prompt(); |
uswickra | 0:ece62a42511f | 54 | return; |
uswickra | 0:ece62a42511f | 55 | } |
uswickra | 0:ece62a42511f | 56 | pc.putc(ch); |
uswickra | 0:ece62a42511f | 57 | } |
uswickra | 0:ece62a42511f | 58 | }; |
uswickra | 0:ece62a42511f | 59 | |
uswickra | 0:ece62a42511f | 60 | private: |
uswickra | 0:ece62a42511f | 61 | void init() { |
uswickra | 0:ece62a42511f | 62 | //default param |
uswickra | 0:ece62a42511f | 63 | param = 1 ; |
uswickra | 0:ece62a42511f | 64 | //default commands |
uswickra | 0:ece62a42511f | 65 | cmds.push_back(&cmd1); |
uswickra | 0:ece62a42511f | 66 | cmds.push_back(&cmd2); |
uswickra | 0:ece62a42511f | 67 | } |
uswickra | 0:ece62a42511f | 68 | |
uswickra | 0:ece62a42511f | 69 | Cmd* parse_command(int count) { |
uswickra | 0:ece62a42511f | 70 | //parse command from buffer |
uswickra | 0:ece62a42511f | 71 | int curr_pos = 0; |
uswickra | 0:ece62a42511f | 72 | //loop until a space is encounterred |
uswickra | 0:ece62a42511f | 73 | //discard any leading spaces |
uswickra | 0:ece62a42511f | 74 | bool check_leading_spaces = true; |
uswickra | 0:ece62a42511f | 75 | bool check_cmd_str = true; |
uswickra | 0:ece62a42511f | 76 | bool check_arg_str = false; |
uswickra | 0:ece62a42511f | 77 | char cmd_str[100]; |
uswickra | 0:ece62a42511f | 78 | char arg_str[100]; |
uswickra | 0:ece62a42511f | 79 | int cmd_str_idx = 0; |
uswickra | 0:ece62a42511f | 80 | int arg_str_idx = 0; |
uswickra | 0:ece62a42511f | 81 | while (curr_pos < count) { |
uswickra | 0:ece62a42511f | 82 | if(buffer[curr_pos] == '\0') { |
uswickra | 0:ece62a42511f | 83 | break; |
uswickra | 0:ece62a42511f | 84 | } |
uswickra | 0:ece62a42511f | 85 | if (check_leading_spaces && buffer[curr_pos] == ' ') { |
uswickra | 0:ece62a42511f | 86 | curr_pos++; |
uswickra | 0:ece62a42511f | 87 | continue; |
uswickra | 0:ece62a42511f | 88 | } else if (check_cmd_str && buffer[curr_pos] != ' ') { |
uswickra | 0:ece62a42511f | 89 | //we have come to the command string start |
uswickra | 0:ece62a42511f | 90 | check_leading_spaces = false; |
uswickra | 0:ece62a42511f | 91 | cmd_str[cmd_str_idx++] = buffer[curr_pos]; |
uswickra | 0:ece62a42511f | 92 | } else if (check_arg_str && buffer[curr_pos] != ' ') { |
uswickra | 0:ece62a42511f | 93 | //we have come to the arg string start |
uswickra | 0:ece62a42511f | 94 | check_leading_spaces = false; |
uswickra | 0:ece62a42511f | 95 | arg_str[arg_str_idx++] = buffer[curr_pos]; |
uswickra | 0:ece62a42511f | 96 | } else if (buffer[curr_pos] == ' ') { |
uswickra | 0:ece62a42511f | 97 | if(check_arg_str) break; |
uswickra | 0:ece62a42511f | 98 | // we have come to the spaces after cmd string |
uswickra | 0:ece62a42511f | 99 | check_cmd_str = false; |
uswickra | 0:ece62a42511f | 100 | check_leading_spaces = true; |
uswickra | 0:ece62a42511f | 101 | check_arg_str = true; |
uswickra | 0:ece62a42511f | 102 | } |
uswickra | 0:ece62a42511f | 103 | curr_pos++; |
uswickra | 0:ece62a42511f | 104 | }; |
uswickra | 0:ece62a42511f | 105 | |
uswickra | 0:ece62a42511f | 106 | //make valid C string for comparison |
uswickra | 0:ece62a42511f | 107 | cmd_str[cmd_str_idx]= '\0'; |
uswickra | 0:ece62a42511f | 108 | arg_str[arg_str_idx]= '\0'; |
uswickra | 0:ece62a42511f | 109 | |
uswickra | 0:ece62a42511f | 110 | //find any command matching input |
uswickra | 0:ece62a42511f | 111 | for(vector<Cmd*>::iterator it = cmds.begin(); it != cmds.end(); it++){ |
uswickra | 0:ece62a42511f | 112 | Cmd* curr_cmd = *it; |
uswickra | 0:ece62a42511f | 113 | if(strcmp(curr_cmd->get_name(), cmd_str) == 0){ |
uswickra | 0:ece62a42511f | 114 | //success : command found for given input |
uswickra | 0:ece62a42511f | 115 | param = atoi(arg_str); |
uswickra | 0:ece62a42511f | 116 | //todo remove |
uswickra | 0:ece62a42511f | 117 | newline(); |
uswickra | 0:ece62a42511f | 118 | pc.puts("CMD SUCCESS param : "); |
uswickra | 0:ece62a42511f | 119 | pc.printf("%d", param); |
uswickra | 0:ece62a42511f | 120 | newline(); |
uswickra | 0:ece62a42511f | 121 | return curr_cmd; |
uswickra | 0:ece62a42511f | 122 | } |
uswickra | 0:ece62a42511f | 123 | } |
uswickra | 0:ece62a42511f | 124 | return &null_cmd; |
uswickra | 0:ece62a42511f | 125 | } |
uswickra | 0:ece62a42511f | 126 | |
uswickra | 0:ece62a42511f | 127 | void prompt() { |
uswickra | 0:ece62a42511f | 128 | pc.puts(prompt_str); |
uswickra | 0:ece62a42511f | 129 | }; |
uswickra | 0:ece62a42511f | 130 | |
uswickra | 0:ece62a42511f | 131 | void newline() { |
uswickra | 0:ece62a42511f | 132 | pc.putc(0x0d); |
uswickra | 0:ece62a42511f | 133 | pc.putc(0x0a); |
uswickra | 0:ece62a42511f | 134 | }; |
uswickra | 0:ece62a42511f | 135 | |
uswickra | 0:ece62a42511f | 136 | }; |
uswickra | 0:ece62a42511f | 137 | |
uswickra | 0:ece62a42511f | 138 | /* |
uswickra | 0:ece62a42511f | 139 | newline(); |
uswickra | 0:ece62a42511f | 140 | pc.puts("DEBUG CMD : "); |
uswickra | 0:ece62a42511f | 141 | pc.putc(char(cmd_str_idx+'0')); |
uswickra | 0:ece62a42511f | 142 | newline(); |
uswickra | 0:ece62a42511f | 143 | for(int i = 0 ; i < cmd_str_idx ; i++ ){ |
uswickra | 0:ece62a42511f | 144 | pc.putc(cmd_str[i]); |
uswickra | 0:ece62a42511f | 145 | } |
uswickra | 0:ece62a42511f | 146 | newline(); |
uswickra | 0:ece62a42511f | 147 | pc.puts("DEBUG ARG: "); |
uswickra | 0:ece62a42511f | 148 | pc.putc(char(arg_str_idx+ '0')); |
uswickra | 0:ece62a42511f | 149 | newline(); |
uswickra | 0:ece62a42511f | 150 | for(int i = 0 ; i < arg_str_idx ; i++ ){ |
uswickra | 0:ece62a42511f | 151 | pc.putc(arg_str[i]); |
uswickra | 0:ece62a42511f | 152 | } |
uswickra | 0:ece62a42511f | 153 | newline();*/ |