TEST
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Utilities/utils.h@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 | |
gmehmet | 1:f60eafbf009a | 35 | #ifndef __UTILS__H_ |
gmehmet | 1:f60eafbf009a | 36 | #include <mbed.h> |
gmehmet | 1:f60eafbf009a | 37 | |
gmehmet | 1:f60eafbf009a | 38 | #ifndef ARRAY_SIZE |
gmehmet | 1:f60eafbf009a | 39 | #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) |
gmehmet | 1:f60eafbf009a | 40 | #endif |
gmehmet | 1:f60eafbf009a | 41 | |
gmehmet | 1:f60eafbf009a | 42 | /* |
gmehmet | 1:f60eafbf009a | 43 | * @brief Parse DeviceStudio get_reg command |
gmehmet | 1:f60eafbf009a | 44 | * @details format is "get_reg <type> <addr>" |
gmehmet | 1:f60eafbf009a | 45 | * |
gmehmet | 1:f60eafbf009a | 46 | * @param[in] str - Pointer to start of command |
gmehmet | 1:f60eafbf009a | 47 | * @param[in] dev_type - device type, ie "ppg" |
gmehmet | 1:f60eafbf009a | 48 | * @param[in] addr - Parsed address |
gmehmet | 1:f60eafbf009a | 49 | * |
gmehmet | 1:f60eafbf009a | 50 | * @return 0 on success, -1 on failure |
gmehmet | 1:f60eafbf009a | 51 | */ |
gmehmet | 1:f60eafbf009a | 52 | int parse_get_reg_cmd(const char* str, const char* dev_type, uint8_t* addr); |
gmehmet | 1:f60eafbf009a | 53 | |
gmehmet | 1:f60eafbf009a | 54 | /* |
gmehmet | 1:f60eafbf009a | 55 | * @brief Parse DeviceStudio set_reg command |
gmehmet | 1:f60eafbf009a | 56 | * @details format is "set_reg <type> <addr> <val>" |
gmehmet | 1:f60eafbf009a | 57 | * |
gmehmet | 1:f60eafbf009a | 58 | * @param[in] str - Pointer to start of command |
gmehmet | 1:f60eafbf009a | 59 | * @param[in] dev_type - device type, ie "ppg" |
gmehmet | 1:f60eafbf009a | 60 | * @param[in] addr - Parsed address |
gmehmet | 1:f60eafbf009a | 61 | * @param[in] val - Parsed value |
gmehmet | 1:f60eafbf009a | 62 | * |
gmehmet | 1:f60eafbf009a | 63 | * @return 0 on success, -1 on failure |
gmehmet | 1:f60eafbf009a | 64 | */ |
gmehmet | 1:f60eafbf009a | 65 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint8_t* val); |
gmehmet | 1:f60eafbf009a | 66 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint16_t* val); |
gmehmet | 1:f60eafbf009a | 67 | int parse_set_reg_cmd(const char* str, const char* dev_type, uint8_t* addr, uint32_t* val); |
gmehmet | 1:f60eafbf009a | 68 | |
gmehmet | 1:f60eafbf009a | 69 | /* |
gmehmet | 1:f60eafbf009a | 70 | * @brief Parse data values sent by DeviceStudio command |
gmehmet | 1:f60eafbf009a | 71 | * @details For a command format of "<cmd> <val1> <val2> ... <valN>", |
gmehmet | 1:f60eafbf009a | 72 | * This command will parse val1 - valN into vals array |
gmehmet | 1:f60eafbf009a | 73 | * |
gmehmet | 1:f60eafbf009a | 74 | * @param[in] str - The full string received |
gmehmet | 1:f60eafbf009a | 75 | * @param[in] cmd - The command only |
gmehmet | 1:f60eafbf009a | 76 | * @param[out] vals - The output array of values |
gmehmet | 1:f60eafbf009a | 77 | * @param[in] vals_sz - The maximum number of values the output array vals can hold |
gmehmet | 1:f60eafbf009a | 78 | * @param[in] hex - Set to true for hexidecimal values, false for decimal |
gmehmet | 1:f60eafbf009a | 79 | * |
gmehmet | 1:f60eafbf009a | 80 | * @return -1 on error, otherwise the number of values parsed |
gmehmet | 1:f60eafbf009a | 81 | */ |
gmehmet | 1:f60eafbf009a | 82 | int parse_cmd_data(const char* str, const char* cmd, uint8_t *vals, int vals_sz, bool hex); |
gmehmet | 1:f60eafbf009a | 83 | int parse_cmd_data(const char* str, const char* cmd, uint16_t *vals, int vals_sz, bool hex); |
gmehmet | 1:f60eafbf009a | 84 | int parse_cmd_data(const char* str, const char* cmd, uint32_t *vals, int vals_sz, bool hex); |
gmehmet | 1:f60eafbf009a | 85 | |
gmehmet | 1:f60eafbf009a | 86 | /* |
gmehmet | 1:f60eafbf009a | 87 | * @brief Determine if str2 is a substring of str1 beginning at idx 0 |
gmehmet | 1:f60eafbf009a | 88 | * |
gmehmet | 1:f60eafbf009a | 89 | * @param[in] str1 - The parent string |
gmehmet | 1:f60eafbf009a | 90 | * @param[in] str2 - The substring which should exist starting at index 0 of str1 |
gmehmet | 1:f60eafbf009a | 91 | * |
gmehmet | 1:f60eafbf009a | 92 | * @return true if str1 starts with str2 |
gmehmet | 1:f60eafbf009a | 93 | * |
gmehmet | 1:f60eafbf009a | 94 | * Examples: |
gmehmet | 1:f60eafbf009a | 95 | * str1 = "An apple", str2 = "An a", returns true |
gmehmet | 1:f60eafbf009a | 96 | * str1 = "A dog", str2 = "A a", returns false |
gmehmet | 1:f60eafbf009a | 97 | * str1 = "An apple", str2 = "An apple tree", returns false |
gmehmet | 1:f60eafbf009a | 98 | */ |
gmehmet | 1:f60eafbf009a | 99 | bool starts_with(const char* str1, const char* str2); |
gmehmet | 1:f60eafbf009a | 100 | |
gmehmet | 1:f60eafbf009a | 101 | #endif |