Maxim Integrated / Mbed OS MAXREFDES220_HR_SPO2_MONITOR

Dependencies:   max32630fthr USBDevice

Fork of MAXREFDES220_HEART_RATE_MONITOR by Maxim Integrated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSInterface.cpp Source File

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 #include "utils.h"
00041 
00042 #ifdef ENABLE_BLE
00043 #include "BLE_ICARUS.h"
00044 #endif
00045 
00046 static const char *glbl_cmds[] = {
00047     "stop",
00048     "get_device_info",
00049     "silent_mode 0",
00050     "silent_mode 1",
00051     "pause 0",
00052     "pause 1",
00053     "enable console",
00054     "disable console",
00055 };
00056 
00057 typedef enum {
00058     stop=0,
00059     get_device_info,
00060     silent0_mode,
00061     silent1_mode,
00062     pause0_mode,
00063     pause1_mode,
00064     enable_console,
00065     disable_console,
00066     NUM_CMDS,
00067 } glbl_cmd_state;
00068 
00069 
00070 DSInterface::DSInterface(USBSerial* USB)
00071 {
00072     cmd_idx = 0;
00073     silent_mode = false;
00074     pause_mode = false;
00075 
00076     memset(sensor_list, 0, DS_MAX_NUM_SENSORCOMMS * sizeof(SensorComm*));
00077     num_sensors = 0;
00078 
00079     m_USB = USB;
00080 
00081     ds_console_interface_exists_ = false;
00082 }
00083 
00084 DSInterface::~DSInterface()
00085 {
00086 }
00087 
00088 void DSInterface::add_sensor_comm(SensorComm *s)
00089 {
00090     assert_msg(num_sensors < DS_MAX_NUM_SENSORCOMMS, "Too many sensors added to DSInterface. Increase DS_MAX_NUM_SENSORCOMMS.");
00091     sensor_list[num_sensors++] = s;
00092 }
00093 
00094 void DSInterface::enable_console_interface()
00095 {
00096     ds_console_interface_exists_ = true;
00097 }
00098 
00099 void DSInterface::build_command(char ch)
00100 {
00101     if (!this->silent_mode) /* BUG: POTENTIAL BUG, what uart port to echo, not only console */
00102         m_USB->printf("%c", ch);
00103 
00104     if (ch == 0x00) {
00105         pr_err("Ignored char 0x00");
00106         return;
00107     }
00108 
00109     if ((ch == '\n') || (ch == '\r')) {
00110         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00111         cmd_str[cmd_idx++] = '\0';
00112         m_USB->printf("\r\n");
00113         parse_command();
00114 
00115         //Clear cmd_str
00116         while (cmd_idx > 0) /* BUG: POTENTIAL BUG for multiple port access */
00117             cmd_str[--cmd_idx] = '\0';
00118 
00119     } else if ((ch == 0x08 || ch == 0x7F) && cmd_idx > 0) {
00120         //Backspace character
00121         if (cmd_idx > 0)
00122             cmd_str[--cmd_idx] = '\0';
00123     } else {
00124         /* BUG: POTENTIAL BUG for multiple port access */
00125         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00126             cmd_str[cmd_idx++] = ch;
00127     }
00128 
00129 }
00130 
00131 void DSInterface::parse_command()
00132 {
00133     int i;
00134     glbl_cmd_state cmd;
00135     char charbuf[512];
00136     int data_len = 0;
00137     int ret;
00138     bool parsed_cmd = true;
00139 
00140     //If top level command, then handle it
00141     for (i = 0; i < NUM_CMDS; i++) {
00142         if (starts_with(&cmd_str[0], glbl_cmds[i])) {
00143             cmd = (glbl_cmd_state)i;
00144 
00145             switch (cmd) {
00146                 case (stop): {
00147                     for (int i = 0; i < num_sensors; i++) {
00148                         sensor_list[i]->stop();
00149                     }
00150                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00151                             "\r\n%s err=0\r\n", cmd_str);
00152                 } break;
00153                 case (get_device_info): {
00154                     data_len = snprintf(charbuf, sizeof(charbuf),
00155                         "\r\n%s platform=%s firmware_ver=%s sensors=", cmd_str, platform_name, firmware_version);
00156 
00157                     //Add list of sensors
00158                     for (int i = 0; i < num_sensors; i++) {
00159                         if (sensor_list[i]->is_visible()) {
00160                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00161                                             "%s", sensor_list[i]->get_type());
00162                             if (i < (num_sensors - 1))
00163                                 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, ",");
00164                         }
00165                     }
00166 
00167 
00168                     for (int i = 0; i < num_sensors; i++) {
00169                         SensorComm *s = sensor_list[i];
00170                         if (!s->is_visible())
00171                             continue;
00172 
00173                         //Add algo_ver
00174                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00175                                             " algo_ver_%s=%s", s->get_type(), s->get_algo_ver());
00176 
00177                         //Add part name
00178                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00179                                             " part_name_%s=%s", s->get_type(), s->get_part_name());
00180 
00181                         uint8_t part_id, part_rev;
00182                         ret = s->get_part_info(&part_id, &part_rev);
00183 
00184                         if (ret == 0) {
00185                             //Add part id
00186                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00187                                             " part_id_%s=%d", s->get_type(), part_id);
00188 
00189                             //Add rev id
00190                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00191                                             " part_rev_%s=%d", s->get_type(), part_rev);
00192                         }
00193                     }
00194 
00195 
00196                     data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " err=0\r\n");
00197 
00198                 } break;
00199                 case (silent0_mode): {
00200                     silent_mode = false;
00201                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00202                             "\r\n%s err=0\r\n", cmd_str);
00203                 } break;
00204                 case (silent1_mode): {
00205                     silent_mode = true;
00206                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00207                             "\r\n%s err=0\r\n", cmd_str);
00208                 } break;
00209                 case (pause0_mode): {
00210                     pause_mode = false;
00211                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00212                             "\r\n%s err=0\r\n", cmd_str);
00213                 } break;
00214                 case (pause1_mode): {
00215                     pause_mode = true;
00216                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00217                             "\r\n%s err=0\r\n", cmd_str);
00218                 } break;
00219                 case (enable_console): {
00220                     ds_console_interface_exists_ = true;
00221                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00222                             "\r\n%s err=0\r\n", cmd_str);
00223                 } break;
00224                 case (disable_console): {
00225                     ds_console_interface_exists_ = false;
00226                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00227                             "\r\n%s err=0\r\n", cmd_str);
00228                 } break;
00229                 default:
00230                     parsed_cmd = false;
00231                     break;
00232             }
00233 
00234             if (parsed_cmd) {
00235                 m_USB->printf(charbuf);
00236 #if ENABLE_BLE
00237                 if (BLE_Icarus_Interface_Exists()) {
00238                     BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00239                     }
00240 #endif
00241             }
00242 
00243             return;
00244         }
00245     }
00246 
00247     //Loop through each sensor in sensorList
00248     //If sensor.get_type() is contained in cmd_str, pass cmd_str to that sensor's parser
00249     for (int i = 0; i < num_sensors; i++) {
00250         if (strstr(&cmd_str[0], sensor_list[i]->get_type())) {
00251             if (sensor_list[i]->parse_command(cmd_str))
00252                 return;
00253             break;
00254         }
00255     }
00256 
00257     //If we still haven't found a way to parse the command,
00258     //send it to every sensor until one handles it
00259     for (int i = 0; i < num_sensors; i++) {
00260         if (sensor_list[i]->parse_command(cmd_str))
00261             return;
00262     }
00263     
00264 
00265     //No one could handle the command, print cmd not recognized string
00266     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00267             "\r\n%s err=-255\r\n", cmd_str);
00268     m_USB->printf(charbuf);
00269 #if ENABLE_BLE
00270     if (BLE_Icarus_Interface_Exists()) {
00271         BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00272     }
00273 #endif
00274 }
00275 
00276 void DSInterface::data_report_execute()
00277 {
00278     char buffer[256];
00279     int data_len = 0;
00280 
00281     buffer[0] = '\0';
00282 
00283     for (int i = 0; i < num_sensors; i++) {
00284         //if ((*it)->is_enabled()) {
00285         data_len = sensor_list[i]->data_report_execute(buffer, sizeof(buffer));
00286         if (!this->pause_mode && (data_len > 0)) {
00287 #ifdef ENABLE_BLE
00288             if (BLE_Icarus_Interface_Exists()) {
00289                 BLE_Icarus_AddtoQueue((uint8_t *)buffer, (int32_t)sizeof(buffer), data_len);
00290             }
00291 #endif
00292             if (ds_console_interface_exists_){
00293 #ifdef ASCII_COMM
00294                 m_USB->printf(buffer);
00295 #else
00296                 m_USB->writeBlock((uint8_t*)buffer, data_len);
00297 #endif
00298             }
00299             data_len = 0;
00300         }
00301         //We only support streaming from one device at a time, so break here
00302         //  break;
00303         //}
00304     }
00305 }
00306 
00307 void DSInterface::set_fw_version(const char* version)
00308 {
00309     if (version && *version)
00310         firmware_version = version;
00311 }
00312 
00313 void DSInterface::set_fw_platform(const char* platform)
00314 {
00315     if (platform && *platform)
00316         platform_name = platform;
00317 }