Arun Raj / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

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 /// TODO: additional for float
00108 /*
00109 int parse_cmd_data(const char* str, const char* cmd, float *vals, int vals_sz, bool hex)
00110 {
00111     const char* sptr = str + strlen(cmd);
00112     int found = 0;
00113     int ssfound;
00114     unsigned int val32;
00115 
00116     while (found < vals_sz) {
00117         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00118         if (*sptr == '\0')
00119             break;
00120         sptr++;
00121 
00122         if (hex)
00123             ssfound = sscanf(sptr, "%x", &val32);
00124         else
00125             ssfound = sscanf(sptr, "%d", &val32);
00126         if (ssfound != 1)
00127             break;
00128         *(vals + found) = (uint8_t)val32;
00129         found++;
00130     }
00131 
00132     return found;
00133 }
00134 */
00135 int parse_cmd_data(const char* str, const char* cmd, uint8_t *vals, int vals_sz, bool hex)
00136 {
00137     const char* sptr = str + strlen(cmd);
00138     int found = 0;
00139     int ssfound;
00140     unsigned int val32;
00141 
00142     while (found < vals_sz) {
00143         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00144         if (*sptr == '\0')
00145             break;
00146         sptr++;
00147 
00148         if (hex)
00149             ssfound = sscanf(sptr, "%x", &val32);
00150         else
00151             ssfound = sscanf(sptr, "%d", &val32);
00152         if (ssfound != 1)
00153             break;
00154         *(vals + found) = (uint8_t)val32;
00155         found++;
00156     }
00157 
00158     return found;
00159 }
00160 
00161 int parse_cmd_data(const char* str, const char* cmd, uint16_t *vals, int vals_sz, bool hex)
00162 {
00163     const char* sptr = str + strlen(cmd);
00164     int found = 0;
00165     int ssfound;
00166     unsigned int val32;
00167 
00168     while (found < vals_sz) {
00169         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00170         if (*sptr == '\0')
00171             break;
00172         sptr++;
00173 
00174         if (hex)
00175             ssfound = sscanf(sptr, "%x", &val32);
00176         else
00177             ssfound = sscanf(sptr, "%d", &val32);
00178         if (ssfound != 1)
00179             break;
00180         *(vals + found) = (uint16_t)val32;
00181         found++;
00182     }
00183 
00184     return found;
00185 }
00186 
00187 int parse_cmd_data(const char* str, const char* cmd, uint32_t *vals, int vals_sz, bool hex)
00188 {
00189     const char* sptr = str + strlen(cmd);
00190     int found = 0;
00191     int ssfound;
00192 
00193     while (found < vals_sz) {
00194         while (*sptr != ' ' && *sptr != '\0') { sptr++; }
00195         if (*sptr == '\0')
00196             break;
00197         sptr++;
00198 
00199         if (hex)
00200             ssfound = sscanf(sptr, "%x", vals + found);
00201         else
00202             ssfound = sscanf(sptr, "%d", vals + found);
00203         if (ssfound != 1)
00204             break;
00205         found++;
00206     }
00207 
00208     return found;
00209 }
00210 
00211 bool starts_with(const char* str1, const char* str2)
00212 {
00213     while (*str1 && *str2) {
00214         if (*str1 != *str2)
00215             return false;
00216         str1++;
00217         str2++;
00218     }
00219 
00220     if (*str2)
00221         return false;
00222 
00223     return true;
00224 }