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 WattBob_TextLCD globals
cmd_io.cpp@0:af612d827a23, 2011-11-30 (annotated)
- Committer:
- Nurbol
- Date:
- Wed Nov 30 00:53:17 2011 +0000
- Revision:
- 0:af612d827a23
- Child:
- 2:1549900755ba
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nurbol | 0:af612d827a23 | 1 | // |
Nurbol | 0:af612d827a23 | 2 | // cmd_io.cpp : library of routines to communicate with virtual COM port |
Nurbol | 0:af612d827a23 | 3 | // ========== |
Nurbol | 0:af612d827a23 | 4 | // |
Nurbol | 0:af612d827a23 | 5 | #include "mbed.h" |
Nurbol | 0:af612d827a23 | 6 | #include "cmd_io.h" |
Nurbol | 0:af612d827a23 | 7 | #include "globals.h" |
Nurbol | 0:af612d827a23 | 8 | |
Nurbol | 0:af612d827a23 | 9 | //************************************************************************ |
Nurbol | 0:af612d827a23 | 10 | // |
Nurbol | 0:af612d827a23 | 11 | // get_cmd - read a command string from the serial port |
Nurbol | 0:af612d827a23 | 12 | // ======= |
Nurbol | 0:af612d827a23 | 13 | // |
Nurbol | 0:af612d827a23 | 14 | // Method |
Nurbol | 0:af612d827a23 | 15 | // 1. Read string of data until a Newline character |
Nurbol | 0:af612d827a23 | 16 | // string is passed as the parameter |
Nurbol | 0:af612d827a23 | 17 | // 2. return a sting through the pointer and a count of the number of characters |
Nurbol | 0:af612d827a23 | 18 | // |
Nurbol | 0:af612d827a23 | 19 | // Notes |
Nurbol | 0:af612d827a23 | 20 | // |
Nurbol | 0:af612d827a23 | 21 | uint32_t get_cmd(CMD_STRUCT *command) |
Nurbol | 0:af612d827a23 | 22 | { |
Nurbol | 0:af612d827a23 | 23 | char ch; |
Nurbol | 0:af612d827a23 | 24 | uint8_t i; |
Nurbol | 0:af612d827a23 | 25 | // |
Nurbol | 0:af612d827a23 | 26 | // read string and use newline as terminator |
Nurbol | 0:af612d827a23 | 27 | // |
Nurbol | 0:af612d827a23 | 28 | for (i= 0 ; i < CMD_STR_BUFFER_SZ ; i++) { |
Nurbol | 0:af612d827a23 | 29 | ch = pc.getc(); |
Nurbol | 0:af612d827a23 | 30 | if (ch == '\r') { |
Nurbol | 0:af612d827a23 | 31 | continue; |
Nurbol | 0:af612d827a23 | 32 | } |
Nurbol | 0:af612d827a23 | 33 | command->cmd_str[i] = ch; |
Nurbol | 0:af612d827a23 | 34 | if (ch == '\n') { |
Nurbol | 0:af612d827a23 | 35 | command->cmd_str[i] = '\0'; |
Nurbol | 0:af612d827a23 | 36 | break; |
Nurbol | 0:af612d827a23 | 37 | } |
Nurbol | 0:af612d827a23 | 38 | } |
Nurbol | 0:af612d827a23 | 39 | command->char_cnt = i; // record character count |
Nurbol | 0:af612d827a23 | 40 | command->result_status = OK; |
Nurbol | 0:af612d827a23 | 41 | return 0; |
Nurbol | 0:af612d827a23 | 42 | } |
Nurbol | 0:af612d827a23 | 43 | |
Nurbol | 0:af612d827a23 | 44 | //************************************************************************ |
Nurbol | 0:af612d827a23 | 45 | // |
Nurbol | 0:af612d827a23 | 46 | // parse_cmd - split command into its component parts |
Nurbol | 0:af612d827a23 | 47 | // ========= |
Nurbol | 0:af612d827a23 | 48 | // |
Nurbol | 0:af612d827a23 | 49 | // Method |
Nurbol | 0:af612d827a23 | 50 | // 1. first character is the command code |
Nurbol | 0:af612d827a23 | 51 | // 2. subsequent character define a set of parameters |
Nurbol | 0:af612d827a23 | 52 | // |
Nurbol | 0:af612d827a23 | 53 | // String is located in the CMD_STRUCT and the resulting |
Nurbol | 0:af612d827a23 | 54 | // data is returned to the same structure. |
Nurbol | 0:af612d827a23 | 55 | // |
Nurbol | 0:af612d827a23 | 56 | // Notes |
Nurbol | 0:af612d827a23 | 57 | // |
Nurbol | 0:af612d827a23 | 58 | uint32_t parse_cmd(CMD_STRUCT *command) |
Nurbol | 0:af612d827a23 | 59 | { |
Nurbol | 0:af612d827a23 | 60 | uint8_t param_cnt, i, state; |
Nurbol | 0:af612d827a23 | 61 | uint16_t tmp; |
Nurbol | 0:af612d827a23 | 62 | |
Nurbol | 0:af612d827a23 | 63 | command->cmd_code = command->cmd_str[0]; |
Nurbol | 0:af612d827a23 | 64 | param_cnt = 0; |
Nurbol | 0:af612d827a23 | 65 | tmp = 0; |
Nurbol | 0:af612d827a23 | 66 | state = 0; |
Nurbol | 0:af612d827a23 | 67 | for (i=1 ; i < CMD_STR_BUFFER_SZ ; i++) { // step through characters |
Nurbol | 0:af612d827a23 | 68 | if (command->cmd_str[i] == '\0') { |
Nurbol | 0:af612d827a23 | 69 | if (state == 1) { // count paramter that is terminated by NEWLINE |
Nurbol | 0:af612d827a23 | 70 | command->param[param_cnt] = tmp; |
Nurbol | 0:af612d827a23 | 71 | param_cnt++; |
Nurbol | 0:af612d827a23 | 72 | } |
Nurbol | 0:af612d827a23 | 73 | command->nos_params = param_cnt; |
Nurbol | 0:af612d827a23 | 74 | return OK; |
Nurbol | 0:af612d827a23 | 75 | } |
Nurbol | 0:af612d827a23 | 76 | if ((command->cmd_str[i] == ' ') && (state == 0)) { // skip spaces |
Nurbol | 0:af612d827a23 | 77 | continue; |
Nurbol | 0:af612d827a23 | 78 | } |
Nurbol | 0:af612d827a23 | 79 | if ((command->cmd_str[i] == ' ') && (state == 1)) { // skip spaces |
Nurbol | 0:af612d827a23 | 80 | state = 0; |
Nurbol | 0:af612d827a23 | 81 | command->param[param_cnt] = tmp; |
Nurbol | 0:af612d827a23 | 82 | param_cnt++; |
Nurbol | 0:af612d827a23 | 83 | tmp = 0; |
Nurbol | 0:af612d827a23 | 84 | continue; |
Nurbol | 0:af612d827a23 | 85 | } |
Nurbol | 0:af612d827a23 | 86 | state = 1; |
Nurbol | 0:af612d827a23 | 87 | if ((command->cmd_str[i] >= '0') && (command->cmd_str[i] <= '9')) { |
Nurbol | 0:af612d827a23 | 88 | tmp = (tmp * 10) + (command->cmd_str[i] - '0'); |
Nurbol | 0:af612d827a23 | 89 | } else { |
Nurbol | 0:af612d827a23 | 90 | command->param[param_cnt] = tmp; |
Nurbol | 0:af612d827a23 | 91 | return CMD_BAD_CHARACTER; |
Nurbol | 0:af612d827a23 | 92 | } |
Nurbol | 0:af612d827a23 | 93 | } |
Nurbol | 0:af612d827a23 | 94 | return CMD_NO_TERMINATOR; |
Nurbol | 0:af612d827a23 | 95 | } |
Nurbol | 0:af612d827a23 | 96 | |
Nurbol | 0:af612d827a23 | 97 | //************************************************************************ |
Nurbol | 0:af612d827a23 | 98 | // |
Nurbol | 0:af612d827a23 | 99 | // reply_to_cmd - return data and status info |
Nurbol | 0:af612d827a23 | 100 | // ============ |
Nurbol | 0:af612d827a23 | 101 | // |
Nurbol | 0:af612d827a23 | 102 | // Method |
Nurbol | 0:af612d827a23 | 103 | // |
Nurbol | 0:af612d827a23 | 104 | // Notes |
Nurbol | 0:af612d827a23 | 105 | // |
Nurbol | 0:af612d827a23 | 106 | void reply_to_cmd(CMD_STRUCT *command) |
Nurbol | 0:af612d827a23 | 107 | { |
Nurbol | 0:af612d827a23 | 108 | send_status(command->result_status); |
Nurbol | 0:af612d827a23 | 109 | if ((command->result_status == OK) && (command->nos_data > 0)) { |
Nurbol | 0:af612d827a23 | 110 | send_data(command); |
Nurbol | 0:af612d827a23 | 111 | } |
Nurbol | 0:af612d827a23 | 112 | } |
Nurbol | 0:af612d827a23 | 113 | |
Nurbol | 0:af612d827a23 | 114 | //************************************************************************ |
Nurbol | 0:af612d827a23 | 115 | // |
Nurbol | 0:af612d827a23 | 116 | // send_status - command status |
Nurbol | 0:af612d827a23 | 117 | // =========== |
Nurbol | 0:af612d827a23 | 118 | // |
Nurbol | 0:af612d827a23 | 119 | // Notes |
Nurbol | 0:af612d827a23 | 120 | // return status value as a positive interger in a string format |
Nurbol | 0:af612d827a23 | 121 | // with a terminating newline character. |
Nurbol | 0:af612d827a23 | 122 | // |
Nurbol | 0:af612d827a23 | 123 | void send_status(uint32_t value) |
Nurbol | 0:af612d827a23 | 124 | { |
Nurbol | 0:af612d827a23 | 125 | pc.printf("%d\n", value); |
Nurbol | 0:af612d827a23 | 126 | return; |
Nurbol | 0:af612d827a23 | 127 | } |
Nurbol | 0:af612d827a23 | 128 | |
Nurbol | 0:af612d827a23 | 129 | //************************************************************************ |
Nurbol | 0:af612d827a23 | 130 | // |
Nurbol | 0:af612d827a23 | 131 | // send_data - send data appropriate to the command |
Nurbol | 0:af612d827a23 | 132 | // ========= |
Nurbol | 0:af612d827a23 | 133 | // |
Nurbol | 0:af612d827a23 | 134 | // Method |
Nurbol | 0:af612d827a23 | 135 | // |
Nurbol | 0:af612d827a23 | 136 | // Notes |
Nurbol | 0:af612d827a23 | 137 | // |
Nurbol | 0:af612d827a23 | 138 | void send_data(CMD_STRUCT *command) |
Nurbol | 0:af612d827a23 | 139 | { |
Nurbol | 0:af612d827a23 | 140 | char buffer[80]; |
Nurbol | 0:af612d827a23 | 141 | uint8_t i; |
Nurbol | 0:af612d827a23 | 142 | // |
Nurbol | 0:af612d827a23 | 143 | // create data string |
Nurbol | 0:af612d827a23 | 144 | // |
Nurbol | 0:af612d827a23 | 145 | if (command->nos_data == 1) { |
Nurbol | 0:af612d827a23 | 146 | sprintf(buffer, "%u\n", (int)command->result_data[0]); |
Nurbol | 0:af612d827a23 | 147 | } |
Nurbol | 0:af612d827a23 | 148 | if (command->nos_data == 2) { |
Nurbol | 0:af612d827a23 | 149 | sprintf(buffer, "%u %u\n", (int)command->result_data[0], (int)command->result_data[1]); |
Nurbol | 0:af612d827a23 | 150 | } |
Nurbol | 0:af612d827a23 | 151 | if (command->nos_data == 7) { |
Nurbol | 0:af612d827a23 | 152 | sprintf(buffer, "%u %u %u %u %u %u %u\n", (int)command->result_data[0], (int)command->result_data[1], (int)command->result_data[2], (int)command->result_data[3], |
Nurbol | 0:af612d827a23 | 153 | (int)command->result_data[4], (int)command->result_data[5], (int)command->result_data[6]); |
Nurbol | 0:af612d827a23 | 154 | } |
Nurbol | 0:af612d827a23 | 155 | // |
Nurbol | 0:af612d827a23 | 156 | // send string |
Nurbol | 0:af612d827a23 | 157 | // |
Nurbol | 0:af612d827a23 | 158 | for (i = 0 ; i < 40 ; i++) { |
Nurbol | 0:af612d827a23 | 159 | if (buffer[i] == '\0') { // do not send NULL character |
Nurbol | 0:af612d827a23 | 160 | return; |
Nurbol | 0:af612d827a23 | 161 | } |
Nurbol | 0:af612d827a23 | 162 | pc.putc(buffer[i]); |
Nurbol | 0:af612d827a23 | 163 | } |
Nurbol | 0:af612d827a23 | 164 | return; |
Nurbol | 0:af612d827a23 | 165 | } |