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: USBDevice max32630fthr
Fork of MAXREFDES220# by
DSInterface.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 **************************************************************************** 00032 */ 00033 00034 #include <ctype.h> 00035 #include <string.h> 00036 #include "DSInterface.h" 00037 #include "../version.h" 00038 #include "Peripherals.h" 00039 #include "assert.h" 00040 00041 static const char *glbl_cmds[] = { 00042 "stop", 00043 "get_device_info", 00044 "silent_mode 0", 00045 "silent_mode 1", 00046 "pause 0", 00047 "pause 1", 00048 "enable console", 00049 "disable console", 00050 }; 00051 00052 typedef enum { 00053 stop=0, 00054 get_device_info, 00055 silent0_mode, 00056 silent1_mode, 00057 pause0_mode, 00058 pause1_mode, 00059 enable_console, 00060 disable_console, 00061 NUM_CMDS, 00062 } glbl_cmd_state; 00063 00064 00065 DSInterface::DSInterface(USBSerial* USB) 00066 { 00067 cmd_idx = 0; 00068 silent_mode = false; 00069 pause_mode = false; 00070 00071 memset(sensor_list, 0, DS_MAX_NUM_SENSORCOMMS * sizeof(SensorComm*)); 00072 num_sensors = 0; 00073 00074 m_USB = USB; 00075 00076 data_report_mode = 0; 00077 console_interface_exists = false; 00078 } 00079 00080 DSInterface::~DSInterface() 00081 { 00082 } 00083 00084 void DSInterface::add_sensor_comm(SensorComm *s) 00085 { 00086 assert_msg(num_sensors < DS_MAX_NUM_SENSORCOMMS, "Too many sensors added to DSInterface. Increase DS_MAX_NUM_SENSORCOMMS."); 00087 sensor_list[num_sensors++] = s; 00088 } 00089 00090 void DSInterface::enable_console_interface() 00091 { 00092 console_interface_exists = true; 00093 } 00094 00095 void DSInterface::build_command(char ch) 00096 { 00097 if (!this->silent_mode) /* BUG: POTENTIAL BUG, what uart port to echo, not only console */ 00098 m_USB->printf("%c", ch); 00099 00100 if (ch == 0x00) { 00101 pr_err("Ignored char 0x00"); 00102 return; 00103 } 00104 00105 if ((ch == '\n') || (ch == '\r')) { 00106 if (cmd_idx < (int) SENSOR_STR_BUF_SZ) 00107 cmd_str[cmd_idx++] = '\0'; 00108 m_USB->printf("\r\n"); 00109 parse_command(); 00110 00111 //Clear cmd_str 00112 while (cmd_idx > 0) /* BUG: POTENTIAL BUG for multiple port access */ 00113 cmd_str[--cmd_idx] = '\0'; 00114 00115 } else if ((ch == 0x08 || ch == 0x7F) && cmd_idx > 0) { 00116 //Backspace character 00117 if (cmd_idx > 0) 00118 cmd_str[--cmd_idx] = '\0'; 00119 } else { 00120 /* BUG: POTENTIAL BUG for multiple port access */ 00121 if (cmd_idx < (int) SENSOR_STR_BUF_SZ) 00122 cmd_str[cmd_idx++] = ch; 00123 } 00124 00125 } 00126 00127 void DSInterface::parse_command() 00128 { 00129 char *ptr_ch; 00130 int i; 00131 glbl_cmd_state cmd; 00132 char charbuf[512]; 00133 int data_len = 0; 00134 int ret; 00135 00136 00137 00138 //If top level command, then handle it 00139 for (i = 0; i < NUM_CMDS; i++) { 00140 ptr_ch = strstr(cmd_str, glbl_cmds[i]); 00141 if (*ptr_ch) { 00142 cmd = (glbl_cmd_state)i; 00143 00144 switch (cmd) { 00145 case (stop): { 00146 for (int i = 0; i < num_sensors; i++) { 00147 sensor_list[i]->stop(); 00148 } 00149 00150 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00151 } break; 00152 case (get_device_info): { 00153 data_len = snprintf(charbuf, sizeof(charbuf), 00154 "\r\n%s platform=%s firmware_ver=%s sensors=", ptr_ch, platform_name, firmware_version); 00155 00156 //Add list of sensors 00157 for (int i = 0; i < num_sensors; i++) { 00158 if (sensor_list[i]->is_visible()) { 00159 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, 00160 "%s", sensor_list[i]->get_type()); 00161 if (i < (num_sensors - 1)) 00162 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, ","); 00163 } 00164 } 00165 00166 00167 for (int i = 0; i < num_sensors; i++) { 00168 SensorComm *s = sensor_list[i]; 00169 if (!s->is_visible()) 00170 continue; 00171 00172 //Add part name 00173 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, 00174 " part_name_%s=%s", s->get_type(), s->get_part_name()); 00175 00176 uint8_t part_id, part_rev; 00177 ret = s->get_part_info(&part_id, &part_rev); 00178 00179 if (ret == 0) { 00180 //Add part id 00181 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, 00182 " part_id_%s=%d", s->get_type(), part_id); 00183 00184 //Add rev id 00185 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, 00186 " part_rev_%s=%d", s->get_type(), part_rev); 00187 } 00188 } 00189 00190 00191 snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " err=0\r\n"); 00192 m_USB->printf(charbuf); 00193 00194 } break; 00195 case (silent0_mode): { 00196 silent_mode = false; 00197 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00198 } break; 00199 case (silent1_mode): { 00200 silent_mode = true; 00201 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00202 } break; 00203 case (pause0_mode): { 00204 pause_mode = false; 00205 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00206 } break; 00207 case (pause1_mode): { 00208 pause_mode = true; 00209 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00210 } break; 00211 case (enable_console): { 00212 console_interface_exists = true; 00213 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00214 } break; 00215 case (disable_console): { 00216 console_interface_exists = false; 00217 m_USB->printf("\r\n%s err=0\r\n", ptr_ch); 00218 } break; 00219 default: 00220 break; 00221 } 00222 00223 return; 00224 } 00225 } 00226 00227 //Loop through each sensor in sensorList 00228 //If sensor.get_type() is contained in cmd_str, pass cmd_str to that sensor's parser 00229 for (int i = 0; i < num_sensors; i++) { 00230 if (strstr(&cmd_str[0], sensor_list[i]->get_type())) { 00231 if (sensor_list[i]->parse_command(cmd_str)) 00232 return; 00233 break; 00234 } 00235 } 00236 00237 //If we still haven't found a way to parse the command, 00238 //send it to every sensor until one handles it 00239 for (int i = 0; i < num_sensors; i++) { 00240 if (sensor_list[i]->parse_command(cmd_str)) 00241 return; 00242 } 00243 00244 00245 //No one could handle the command, print cmd not recognized string 00246 m_USB->printf("\r\n%s err=-255\r\n", cmd_str); 00247 } 00248 00249 void DSInterface::data_report_execute() 00250 { 00251 char buffer[256]; 00252 int data_len = 0; 00253 00254 buffer[0] = '\0'; 00255 00256 for (int i = 0; i < num_sensors; i++) { 00257 //if ((*it)->is_enabled()) { 00258 data_len = sensor_list[i]->data_report_execute(buffer, sizeof(buffer)); 00259 if (!this->pause_mode && (data_len > 0)) { 00260 if(console_interface_exists){ 00261 #ifdef ASCII_COMM 00262 m_USB->printf(buffer); 00263 #else 00264 m_USB->writeBlock((uint8_t*)buffer, data_len); 00265 #endif 00266 } 00267 data_len = 0; 00268 } 00269 //We only support streaming from one device at a time, so break here 00270 // break; 00271 //} 00272 } 00273 } 00274 00275 void DSInterface::set_fw_version(const char* version) 00276 { 00277 if (version && *version) 00278 firmware_version = version; 00279 } 00280 00281 void DSInterface::set_fw_platform(const char* platform) 00282 { 00283 if (platform && *platform) 00284 platform_name = platform; 00285 }
Generated on Mon Jul 18 2022 23:37:28 by
