TEST
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Utilities/utils.cpp@1:f60eafbf009a, 2019-04-10 (annotated)
- Committer:
- gmehmet
- Date:
- Wed Apr 10 14:56:25 2019 +0300
- Revision:
- 1:f60eafbf009a
upload from local
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmehmet | 1:f60eafbf009a | 1 | /*************************************************************************** |
gmehmet | 1:f60eafbf009a | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
gmehmet | 1:f60eafbf009a | 3 | * |
gmehmet | 1:f60eafbf009a | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
gmehmet | 1:f60eafbf009a | 5 | * copy of this software and associated documentation files (the "Software"), |
gmehmet | 1:f60eafbf009a | 6 | * to deal in the Software without restriction, including without limitation |
gmehmet | 1:f60eafbf009a | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
gmehmet | 1:f60eafbf009a | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
gmehmet | 1:f60eafbf009a | 9 | * Software is furnished to do so, subject to the following conditions: |
gmehmet | 1:f60eafbf009a | 10 | * |
gmehmet | 1:f60eafbf009a | 11 | * The above copyright notice and this permission notice shall be included |
gmehmet | 1:f60eafbf009a | 12 | * in all copies or substantial portions of the Software. |
gmehmet | 1:f60eafbf009a | 13 | * |
gmehmet | 1:f60eafbf009a | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
gmehmet | 1:f60eafbf009a | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
gmehmet | 1:f60eafbf009a | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
gmehmet | 1:f60eafbf009a | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
gmehmet | 1:f60eafbf009a | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
gmehmet | 1:f60eafbf009a | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
gmehmet | 1:f60eafbf009a | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
gmehmet | 1:f60eafbf009a | 21 | * |
gmehmet | 1:f60eafbf009a | 22 | * Except as contained in this notice, the name of Maxim Integrated |
gmehmet | 1:f60eafbf009a | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
gmehmet | 1:f60eafbf009a | 24 | * Products, Inc. Branding Policy. |
gmehmet | 1:f60eafbf009a | 25 | * |
gmehmet | 1:f60eafbf009a | 26 | * The mere transfer of this software does not imply any licenses |
gmehmet | 1:f60eafbf009a | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
gmehmet | 1:f60eafbf009a | 28 | * trademarks, maskwork rights, or any other form of intellectual |
gmehmet | 1:f60eafbf009a | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
gmehmet | 1:f60eafbf009a | 30 | * ownership rights. |
gmehmet | 1:f60eafbf009a | 31 | **************************************************************************** |
gmehmet | 1:f60eafbf009a | 32 | */ |
gmehmet | 1:f60eafbf009a | 33 | |
gmehmet | 1:f60eafbf009a | 34 | #include "utils.h" |
gmehmet | 1:f60eafbf009a | 35 | #include <ctype.h> |
gmehmet | 1:f60eafbf009a | 36 | #include "Peripherals.h" |
gmehmet | 1:f60eafbf009a | 37 | |
gmehmet | 1:f60eafbf009a | 38 | /* |
gmehmet | 1:f60eafbf009a | 39 | * @brief Parse DeviceStudio get_reg command |
gmehmet | 1:f60eafbf009a | 40 | * @details format is "get_reg <type> <addr>" |
gmehmet | 1:f60eafbf009a | 41 | * |
gmehmet | 1:f60eafbf009a | 42 | * @return 0 on success, -1 on failure |
gmehmet | 1:f60eafbf009a | 43 | */ |
gmehmet | 1:f60eafbf009a | 44 | int parse_get_reg_cmd(const char* str, const char* dev_type, uint8_t* addr) |
gmehmet | 1:f60eafbf009a | 45 | { |
gmehmet | 1:f60eafbf009a | 46 | const char* num_start = str + strlen("get_reg") + strlen(dev_type) + 2; |
gmehmet | 1:f60eafbf009a | 47 | unsigned int addr32; |
gmehmet | 1:f60eafbf009a | 48 | |
gmehmet | 1:f60eafbf009a | 49 | int num_found = sscanf(num_start, "%x", &addr32); |
gmehmet | 1:f60eafbf009a | 50 | if (num_found == 1) { |
gmehmet | 1:f60eafbf009a | 51 | *addr = (uint8_t)addr32; |
gmehmet | 1:f60eafbf009a | 52 | return 0; |
gmehmet | 1:f60eafbf009a | 53 | } else { |
gmehmet | 1:f60eafbf009a | 54 | return -1; |
gmehmet | 1:f60eafbf009a | 55 | } |
gmehmet | 1:f60eafbf009a | 56 | } |
gmehmet | 1:f60eafbf009a | 57 | |
gmehmet | 1:f60eafbf009a | 58 | /* |
gmehmet | 1:f60eafbf009a | 59 | * @brief Parse DeviceStudio set_reg command |
gmehmet | 1:f60eafbf009a | 60 | * @details format is "set_reg <type> <addr> <val>" |
gmehmet | 1:f60eafbf009a | 61 | * |
gmehmet | 1:f60eafbf009a | 62 | * @return 0 on success, -1 on failure |
gmehmet | 1:f60eafbf009a | 63 | */ |
gmehmet | 1:f60eafbf009a | 64 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint8_t* val) |
gmehmet | 1:f60eafbf009a | 65 | { |
gmehmet | 1:f60eafbf009a | 66 | const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2; |
gmehmet | 1:f60eafbf009a | 67 | unsigned int addr32, val32; |
gmehmet | 1:f60eafbf009a | 68 | |
gmehmet | 1:f60eafbf009a | 69 | int num_found = sscanf(num_start, "%x %x", &addr32, &val32); |
gmehmet | 1:f60eafbf009a | 70 | if (num_found == 2) { |
gmehmet | 1:f60eafbf009a | 71 | *addr = (uint8_t)addr32; |
gmehmet | 1:f60eafbf009a | 72 | *val = (uint8_t)val32; |
gmehmet | 1:f60eafbf009a | 73 | return 0; |
gmehmet | 1:f60eafbf009a | 74 | } else { |
gmehmet | 1:f60eafbf009a | 75 | return -1; |
gmehmet | 1:f60eafbf009a | 76 | } |
gmehmet | 1:f60eafbf009a | 77 | } |
gmehmet | 1:f60eafbf009a | 78 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint16_t* val) |
gmehmet | 1:f60eafbf009a | 79 | { |
gmehmet | 1:f60eafbf009a | 80 | const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2; |
gmehmet | 1:f60eafbf009a | 81 | unsigned int addr32, val32; |
gmehmet | 1:f60eafbf009a | 82 | |
gmehmet | 1:f60eafbf009a | 83 | int num_found = sscanf(num_start, "%x %x", &addr32, &val32); |
gmehmet | 1:f60eafbf009a | 84 | if (num_found == 2) { |
gmehmet | 1:f60eafbf009a | 85 | *addr = (uint8_t)addr32; |
gmehmet | 1:f60eafbf009a | 86 | *val = (uint16_t)val32; |
gmehmet | 1:f60eafbf009a | 87 | return 0; |
gmehmet | 1:f60eafbf009a | 88 | } else { |
gmehmet | 1:f60eafbf009a | 89 | return -1; |
gmehmet | 1:f60eafbf009a | 90 | } |
gmehmet | 1:f60eafbf009a | 91 | } |
gmehmet | 1:f60eafbf009a | 92 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint32_t* val) |
gmehmet | 1:f60eafbf009a | 93 | { |
gmehmet | 1:f60eafbf009a | 94 | const char* num_start = str + strlen("set_reg") + strlen(dev_type) + 2; |
gmehmet | 1:f60eafbf009a | 95 | unsigned int addr32, val32; |
gmehmet | 1:f60eafbf009a | 96 | |
gmehmet | 1:f60eafbf009a | 97 | int num_found = sscanf(num_start, "%x %x", &addr32, &val32); |
gmehmet | 1:f60eafbf009a | 98 | if (num_found == 2) { |
gmehmet | 1:f60eafbf009a | 99 | *addr = (uint8_t)addr32; |
gmehmet | 1:f60eafbf009a | 100 | *val = val32; |
gmehmet | 1:f60eafbf009a | 101 | return 0; |
gmehmet | 1:f60eafbf009a | 102 | } else { |
gmehmet | 1:f60eafbf009a | 103 | return -1; |
gmehmet | 1:f60eafbf009a | 104 | } |
gmehmet | 1:f60eafbf009a | 105 | } |
gmehmet | 1:f60eafbf009a | 106 | |
gmehmet | 1:f60eafbf009a | 107 | /// TODO: additional for float |
gmehmet | 1:f60eafbf009a | 108 | /* |
gmehmet | 1:f60eafbf009a | 109 | int parse_cmd_data(const char* str, const char* cmd, float *vals, int vals_sz, bool hex) |
gmehmet | 1:f60eafbf009a | 110 | { |
gmehmet | 1:f60eafbf009a | 111 | const char* sptr = str + strlen(cmd); |
gmehmet | 1:f60eafbf009a | 112 | int found = 0; |
gmehmet | 1:f60eafbf009a | 113 | int ssfound; |
gmehmet | 1:f60eafbf009a | 114 | unsigned int val32; |
gmehmet | 1:f60eafbf009a | 115 | |
gmehmet | 1:f60eafbf009a | 116 | while (found < vals_sz) { |
gmehmet | 1:f60eafbf009a | 117 | while (*sptr != ' ' && *sptr != '\0') { sptr++; } |
gmehmet | 1:f60eafbf009a | 118 | if (*sptr == '\0') |
gmehmet | 1:f60eafbf009a | 119 | break; |
gmehmet | 1:f60eafbf009a | 120 | sptr++; |
gmehmet | 1:f60eafbf009a | 121 | |
gmehmet | 1:f60eafbf009a | 122 | if (hex) |
gmehmet | 1:f60eafbf009a | 123 | ssfound = sscanf(sptr, "%x", &val32); |
gmehmet | 1:f60eafbf009a | 124 | else |
gmehmet | 1:f60eafbf009a | 125 | ssfound = sscanf(sptr, "%d", &val32); |
gmehmet | 1:f60eafbf009a | 126 | if (ssfound != 1) |
gmehmet | 1:f60eafbf009a | 127 | break; |
gmehmet | 1:f60eafbf009a | 128 | *(vals + found) = (uint8_t)val32; |
gmehmet | 1:f60eafbf009a | 129 | found++; |
gmehmet | 1:f60eafbf009a | 130 | } |
gmehmet | 1:f60eafbf009a | 131 | |
gmehmet | 1:f60eafbf009a | 132 | return found; |
gmehmet | 1:f60eafbf009a | 133 | } |
gmehmet | 1:f60eafbf009a | 134 | */ |
gmehmet | 1:f60eafbf009a | 135 | int parse_cmd_data(const char* str, const char* cmd, uint8_t *vals, int vals_sz, bool hex) |
gmehmet | 1:f60eafbf009a | 136 | { |
gmehmet | 1:f60eafbf009a | 137 | const char* sptr = str + strlen(cmd); |
gmehmet | 1:f60eafbf009a | 138 | int found = 0; |
gmehmet | 1:f60eafbf009a | 139 | int ssfound; |
gmehmet | 1:f60eafbf009a | 140 | unsigned int val32; |
gmehmet | 1:f60eafbf009a | 141 | |
gmehmet | 1:f60eafbf009a | 142 | while (found < vals_sz) { |
gmehmet | 1:f60eafbf009a | 143 | while (*sptr != ' ' && *sptr != '\0') { sptr++; } |
gmehmet | 1:f60eafbf009a | 144 | if (*sptr == '\0') |
gmehmet | 1:f60eafbf009a | 145 | break; |
gmehmet | 1:f60eafbf009a | 146 | sptr++; |
gmehmet | 1:f60eafbf009a | 147 | |
gmehmet | 1:f60eafbf009a | 148 | if (hex) |
gmehmet | 1:f60eafbf009a | 149 | ssfound = sscanf(sptr, "%x", &val32); |
gmehmet | 1:f60eafbf009a | 150 | else |
gmehmet | 1:f60eafbf009a | 151 | ssfound = sscanf(sptr, "%d", &val32); |
gmehmet | 1:f60eafbf009a | 152 | if (ssfound != 1) |
gmehmet | 1:f60eafbf009a | 153 | break; |
gmehmet | 1:f60eafbf009a | 154 | *(vals + found) = (uint8_t)val32; |
gmehmet | 1:f60eafbf009a | 155 | found++; |
gmehmet | 1:f60eafbf009a | 156 | } |
gmehmet | 1:f60eafbf009a | 157 | |
gmehmet | 1:f60eafbf009a | 158 | return found; |
gmehmet | 1:f60eafbf009a | 159 | } |
gmehmet | 1:f60eafbf009a | 160 | |
gmehmet | 1:f60eafbf009a | 161 | int parse_cmd_data(const char* str, const char* cmd, uint16_t *vals, int vals_sz, bool hex) |
gmehmet | 1:f60eafbf009a | 162 | { |
gmehmet | 1:f60eafbf009a | 163 | const char* sptr = str + strlen(cmd); |
gmehmet | 1:f60eafbf009a | 164 | int found = 0; |
gmehmet | 1:f60eafbf009a | 165 | int ssfound; |
gmehmet | 1:f60eafbf009a | 166 | unsigned int val32; |
gmehmet | 1:f60eafbf009a | 167 | |
gmehmet | 1:f60eafbf009a | 168 | while (found < vals_sz) { |
gmehmet | 1:f60eafbf009a | 169 | while (*sptr != ' ' && *sptr != '\0') { sptr++; } |
gmehmet | 1:f60eafbf009a | 170 | if (*sptr == '\0') |
gmehmet | 1:f60eafbf009a | 171 | break; |
gmehmet | 1:f60eafbf009a | 172 | sptr++; |
gmehmet | 1:f60eafbf009a | 173 | |
gmehmet | 1:f60eafbf009a | 174 | if (hex) |
gmehmet | 1:f60eafbf009a | 175 | ssfound = sscanf(sptr, "%x", &val32); |
gmehmet | 1:f60eafbf009a | 176 | else |
gmehmet | 1:f60eafbf009a | 177 | ssfound = sscanf(sptr, "%d", &val32); |
gmehmet | 1:f60eafbf009a | 178 | if (ssfound != 1) |
gmehmet | 1:f60eafbf009a | 179 | break; |
gmehmet | 1:f60eafbf009a | 180 | *(vals + found) = (uint16_t)val32; |
gmehmet | 1:f60eafbf009a | 181 | found++; |
gmehmet | 1:f60eafbf009a | 182 | } |
gmehmet | 1:f60eafbf009a | 183 | |
gmehmet | 1:f60eafbf009a | 184 | return found; |
gmehmet | 1:f60eafbf009a | 185 | } |
gmehmet | 1:f60eafbf009a | 186 | |
gmehmet | 1:f60eafbf009a | 187 | int parse_cmd_data(const char* str, const char* cmd, uint32_t *vals, int vals_sz, bool hex) |
gmehmet | 1:f60eafbf009a | 188 | { |
gmehmet | 1:f60eafbf009a | 189 | const char* sptr = str + strlen(cmd); |
gmehmet | 1:f60eafbf009a | 190 | int found = 0; |
gmehmet | 1:f60eafbf009a | 191 | int ssfound; |
gmehmet | 1:f60eafbf009a | 192 | |
gmehmet | 1:f60eafbf009a | 193 | while (found < vals_sz) { |
gmehmet | 1:f60eafbf009a | 194 | while (*sptr != ' ' && *sptr != '\0') { sptr++; } |
gmehmet | 1:f60eafbf009a | 195 | if (*sptr == '\0') |
gmehmet | 1:f60eafbf009a | 196 | break; |
gmehmet | 1:f60eafbf009a | 197 | sptr++; |
gmehmet | 1:f60eafbf009a | 198 | |
gmehmet | 1:f60eafbf009a | 199 | if (hex) |
gmehmet | 1:f60eafbf009a | 200 | ssfound = sscanf(sptr, "%x", vals + found); |
gmehmet | 1:f60eafbf009a | 201 | else |
gmehmet | 1:f60eafbf009a | 202 | ssfound = sscanf(sptr, "%d", vals + found); |
gmehmet | 1:f60eafbf009a | 203 | if (ssfound != 1) |
gmehmet | 1:f60eafbf009a | 204 | break; |
gmehmet | 1:f60eafbf009a | 205 | found++; |
gmehmet | 1:f60eafbf009a | 206 | } |
gmehmet | 1:f60eafbf009a | 207 | |
gmehmet | 1:f60eafbf009a | 208 | return found; |
gmehmet | 1:f60eafbf009a | 209 | } |
gmehmet | 1:f60eafbf009a | 210 | |
gmehmet | 1:f60eafbf009a | 211 | bool starts_with(const char* str1, const char* str2) |
gmehmet | 1:f60eafbf009a | 212 | { |
gmehmet | 1:f60eafbf009a | 213 | while (*str1 && *str2) { |
gmehmet | 1:f60eafbf009a | 214 | if (*str1 != *str2) |
gmehmet | 1:f60eafbf009a | 215 | return false; |
gmehmet | 1:f60eafbf009a | 216 | str1++; |
gmehmet | 1:f60eafbf009a | 217 | str2++; |
gmehmet | 1:f60eafbf009a | 218 | } |
gmehmet | 1:f60eafbf009a | 219 | |
gmehmet | 1:f60eafbf009a | 220 | if (*str2) |
gmehmet | 1:f60eafbf009a | 221 | return false; |
gmehmet | 1:f60eafbf009a | 222 | |
gmehmet | 1:f60eafbf009a | 223 | return true; |
gmehmet | 1:f60eafbf009a | 224 | } |