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
- Committer:
- uswickra
- Date:
- 2014-08-31
- Revision:
- 0:ece62a42511f
- Child:
- 2:04d8e2ad8cff
File content as of revision 0:ece62a42511f:
#include "mbed.h" #include "Command.h" #include <vector> extern Serial pc; using namespace std; class SerialConsole { const char* prompt_str; char buffer[100]; vector<Cmd*> cmds; int param; //available commands Led_On_Cmd cmd1; Led_Off_Cmd cmd2; Cmd null_cmd; public: SerialConsole() { prompt_str = "p542% "; init(); }; SerialConsole(char* pr) { prompt_str = pr; init(); }; void start() { //start with prompt prompt(); // int count = 0 ; while(1) { char ch = pc.getc(); buffer[count++] = ch ; //check for carriage return or new line if(ch == 0x0a || ch == 0x0d ) { //for each new line parse command then execute command Cmd* in_cmd = parse_command(count); in_cmd->execute(param); //reset count/buffer count = 0; //go to a new command line newline(); prompt(); continue ; } else if (ch == 'q') { //exit newline(); prompt(); return; } pc.putc(ch); } }; private: void init() { //default param param = 1 ; //default commands cmds.push_back(&cmd1); cmds.push_back(&cmd2); } Cmd* parse_command(int count) { //parse command from buffer int curr_pos = 0; //loop until a space is encounterred //discard any leading spaces bool check_leading_spaces = true; bool check_cmd_str = true; bool check_arg_str = false; char cmd_str[100]; char arg_str[100]; int cmd_str_idx = 0; int arg_str_idx = 0; while (curr_pos < count) { if(buffer[curr_pos] == '\0') { break; } if (check_leading_spaces && buffer[curr_pos] == ' ') { curr_pos++; continue; } else if (check_cmd_str && buffer[curr_pos] != ' ') { //we have come to the command string start check_leading_spaces = false; cmd_str[cmd_str_idx++] = buffer[curr_pos]; } else if (check_arg_str && buffer[curr_pos] != ' ') { //we have come to the arg string start check_leading_spaces = false; arg_str[arg_str_idx++] = buffer[curr_pos]; } else if (buffer[curr_pos] == ' ') { if(check_arg_str) break; // we have come to the spaces after cmd string check_cmd_str = false; check_leading_spaces = true; check_arg_str = true; } curr_pos++; }; //make valid C string for comparison cmd_str[cmd_str_idx]= '\0'; arg_str[arg_str_idx]= '\0'; //find any command matching input for(vector<Cmd*>::iterator it = cmds.begin(); it != cmds.end(); it++){ Cmd* curr_cmd = *it; if(strcmp(curr_cmd->get_name(), cmd_str) == 0){ //success : command found for given input param = atoi(arg_str); //todo remove newline(); pc.puts("CMD SUCCESS param : "); pc.printf("%d", param); newline(); return curr_cmd; } } return &null_cmd; } void prompt() { pc.puts(prompt_str); }; void newline() { pc.putc(0x0d); pc.putc(0x0a); }; }; /* newline(); pc.puts("DEBUG CMD : "); pc.putc(char(cmd_str_idx+'0')); newline(); for(int i = 0 ; i < cmd_str_idx ; i++ ){ pc.putc(cmd_str[i]); } newline(); pc.puts("DEBUG ARG: "); pc.putc(char(arg_str_idx+ '0')); newline(); for(int i = 0 ; i < arg_str_idx ; i++ ){ pc.putc(arg_str[i]); } newline();*/