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 utils.cpp Source File

utils.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 "utils.h"
00035 #include <ctype.h>
00036 #include "Peripherals.h"
00037 
00038 /*
00039  * @brief Parse DeviceStudio get_reg command 
00040  * @details format is "get_reg <type> <addr>"
00041  *
00042  * @return 0 on success, -1 on failure
00043  */
00044 int parse_get_reg_cmd(const char* str, const char* dev_type, uint8_t* addr)
00045 {
00046     const char* num_start = str + strlen("get_reg") + strlen(dev_type) + 2;
00047     unsigned int addr32;
00048 
00049     int num_found = sscanf(num_start, "%x", &addr32);
00050     if (num_found == 1) {
00051         *addr = (uint8_t)addr32;
00052         return 0;
00053     } else {
00054         return -1;
00055     }
00056 }
00057 
00058 /*
00059  * @brief Parse DeviceStudio set_reg command 
00060  * @details format is "set_reg <type> <addr> <val>"
00061  *
00062  * @return 0 on success, -1 on failure
00063  */
00064 int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint8_t* val)
00065 {
00066     const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2;
00067     unsigned int addr32, val32;
00068 
00069     int num_found = sscanf(num_start, "%x %x", &addr32, &val32);
00070     if (num_found == 2) {
00071         *addr = (uint8_t)addr32;
00072         *val = (uint8_t)val32;
00073         return 0;
00074     } else {
00075         return -1;
00076     }
00077 }
00078 int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint16_t* val)
00079 {
00080     const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2;
00081     unsigned int addr32, val32;
00082 
00083     int num_found = sscanf(num_start, "%x %x", &addr32, &val32);
00084     if (num_found == 2) {
00085         *addr = (uint8_t)addr32;
00086         *val = (uint16_t)val32;
00087         return 0;
00088     } else {
00089         return -1;
00090     }
00091 }
00092 int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint32_t* val)
00093 {
00094     const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2;
00095     unsigned int addr32, val32;
00096 
00097     int num_found = sscanf(num_start, "%x %x", &addr32, &val32);
00098     if (num_found == 2) {
00099         *addr = (uint8_t)addr32;
00100         *val = val32;
00101         return 0;
00102     } else {
00103         return -1;
00104     }
00105 }
00106 
00107 int parse_cmd_data(const char* str, const char* cmd, uint8_t *vals, int vals_sz, bool hex)
00108 {
00109     const char* sptr = str + strlen(cmd);
00110     int found = 0;
00111     int ssfound;
00112     unsigned int val32;
00113 
00114     while (found < vals_sz) {
00115         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00116         if (*sptr == '\0')
00117             break;
00118         sptr++;
00119 
00120         if (hex)
00121             ssfound = sscanf(sptr, "%x", &val32);
00122         else
00123             ssfound = sscanf(sptr, "%d", &val32);
00124         if (ssfound != 1)
00125             break;
00126         *(vals + found) = (uint8_t)val32;
00127         found++;
00128     }
00129 
00130     return found;
00131 }
00132 
00133 int parse_cmd_data(const char* str, const char* cmd, uint16_t *vals, int vals_sz, bool hex)
00134 {
00135     const char* sptr = str + strlen(cmd);
00136     int found = 0;
00137     int ssfound;
00138     unsigned int val32;
00139 
00140     while (found < vals_sz) {
00141         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00142         if (*sptr == '\0')
00143             break;
00144         sptr++;
00145 
00146         if (hex)
00147             ssfound = sscanf(sptr, "%x", &val32);
00148         else
00149             ssfound = sscanf(sptr, "%d", &val32);
00150         if (ssfound != 1)
00151             break;
00152         *(vals + found) = (uint16_t)val32;
00153         found++;
00154     }
00155 
00156     return found;
00157 }
00158 
00159 int parse_cmd_data(const char* str, const char* cmd, uint32_t *vals, int vals_sz, bool hex)
00160 {
00161     const char* sptr = str + strlen(cmd);
00162     int found = 0;
00163     int ssfound;
00164 
00165     while (found < vals_sz) {
00166         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00167         if (*sptr == '\0')
00168             break;
00169         sptr++;
00170 
00171         if (hex)
00172             ssfound = sscanf(sptr, "%x", vals + found);
00173         else
00174             ssfound = sscanf(sptr, "%d", vals + found);
00175         if (ssfound != 1)
00176             break;
00177         found++;
00178     }
00179 
00180     return found;
00181 }
00182 
00183 bool starts_with(const char* str1, const char* str2)
00184 {
00185     while (*str1 && *str2) {
00186         if (*str1 != *str2)
00187             return false;
00188         str1++;
00189         str2++;
00190     }
00191 
00192     if (*str2)
00193         return false;
00194 
00195     return true;
00196 }