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
00001 // 00002 // cmd_io.cpp : library of routines to communicate with virtual COM port 00003 // ========== 00004 // 00005 #include "mbed.h" 00006 #include "cmd_io.h" 00007 #include "globals.h" 00008 00009 //************************************************************************ 00010 // 00011 // get_cmd - read a command string from the serial port 00012 // ======= 00013 // 00014 // Method 00015 // 1. Read string of data until a Newline character 00016 // string is passed as the parameter 00017 // 2. return a sting through the pointer and a count of the number of characters 00018 // 00019 // Notes 00020 // 00021 uint32_t get_cmd(CMD_STRUCT *command) 00022 { 00023 char ch; 00024 uint8_t i; 00025 // 00026 // read string and use newline as terminator 00027 // 00028 for (i= 0 ; i < CMD_STR_BUFFER_SZ ; i++) { 00029 ch = pc.getc(); 00030 if (ch == '\r') { 00031 continue; 00032 } 00033 command->cmd_str[i] = ch; 00034 if (ch == '\n') { 00035 command->cmd_str[i] = '\0'; 00036 break; 00037 } 00038 } 00039 command->char_cnt = i; // record character count 00040 command->result_status = OK; 00041 return 0; 00042 } 00043 00044 //************************************************************************ 00045 // 00046 // parse_cmd - split command into its component parts 00047 // ========= 00048 // 00049 // Method 00050 // 1. first character is the command code 00051 // 2. subsequent character define a set of parameters 00052 // 00053 // String is located in the CMD_STRUCT and the resulting 00054 // data is returned to the same structure. 00055 // 00056 // Notes 00057 // 00058 uint32_t parse_cmd(CMD_STRUCT *command) 00059 { 00060 uint8_t param_cnt, i, state; 00061 uint16_t tmp; 00062 00063 command->cmd_code = command->cmd_str[0]; 00064 param_cnt = 0; 00065 tmp = 0; 00066 state = 0; 00067 for (i=1 ; i < CMD_STR_BUFFER_SZ ; i++) { // step through characters 00068 if (command->cmd_str[i] == '\0') { 00069 if (state == 1) { // count paramter that is terminated by NEWLINE 00070 command->param[param_cnt] = tmp; 00071 param_cnt++; 00072 } 00073 command->nos_params = param_cnt; 00074 return OK; 00075 } 00076 if ((command->cmd_str[i] == ' ') && (state == 0)) { // skip spaces 00077 continue; 00078 } 00079 if ((command->cmd_str[i] == ' ') && (state == 1)) { // skip spaces 00080 state = 0; 00081 command->param[param_cnt] = tmp; 00082 param_cnt++; 00083 tmp = 0; 00084 continue; 00085 } 00086 state = 1; 00087 if ((command->cmd_str[i] >= '0') && (command->cmd_str[i] <= '9')) { 00088 tmp = (tmp * 10) + (command->cmd_str[i] - '0'); 00089 } else { 00090 command->param[param_cnt] = tmp; 00091 return CMD_BAD_CHARACTER; 00092 } 00093 } 00094 return CMD_NO_TERMINATOR; 00095 } 00096 00097 //************************************************************************ 00098 // 00099 // reply_to_cmd - return data and status info 00100 // ============ 00101 // 00102 // Method 00103 // 00104 // Notes 00105 // 00106 void reply_to_cmd(CMD_STRUCT *command) 00107 { 00108 send_status(command->result_status); 00109 if ((command->result_status == OK) && (command->nos_data > 0)) { 00110 send_data(command); 00111 } 00112 } 00113 00114 //************************************************************************ 00115 // 00116 // send_status - command status 00117 // =========== 00118 // 00119 // Notes 00120 // return status value as a positive interger in a string format 00121 // with a terminating newline character. 00122 // 00123 void send_status(uint32_t value) 00124 { 00125 pc.printf("%d\n", value); 00126 return; 00127 } 00128 00129 //************************************************************************ 00130 // 00131 // send_data - send data appropriate to the command 00132 // ========= 00133 // 00134 // Method 00135 // 00136 // Notes 00137 // 00138 void send_data(CMD_STRUCT *command) 00139 { 00140 char buffer[80]; 00141 uint8_t i; 00142 // 00143 // create data string 00144 // 00145 if (command->nos_data == 1) { 00146 sprintf(buffer, "%u\n", (int)command->result_data[0]); 00147 } 00148 if (command->nos_data == 2) { 00149 sprintf(buffer, "%u %u\n", (int)command->result_data[0], (int)command->result_data[1]); 00150 } 00151 /* if (command->nos_data == 7) { 00152 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], 00153 (int)command->result_data[4], (int)command->result_data[5], (int)command->result_data[6]); 00154 }*/ 00155 // 00156 // send string 00157 // 00158 for (i = 0 ; i < 40 ; i++) { 00159 if (buffer[i] == '\0') { // do not send NULL character 00160 return; 00161 } 00162 pc.putc(buffer[i]); 00163 } 00164 return; 00165 }
Generated on Mon Aug 8 2022 11:43:31 by
1.7.2