Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Committer:
modtronix-com
Date:
Fri Aug 19 15:52:51 2016 +1000
Revision:
19:42ae82a8f571
Parent:
3:99cb87ee1792
Added tag v1.1 for changeset 37e7c8fac8c7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 3:99cb87ee1792 1 /**
modtronix 3:99cb87ee1792 2 * File: mx_helpers.cpp
modtronix 3:99cb87ee1792 3 *
modtronix 3:99cb87ee1792 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 3:99cb87ee1792 5 *
modtronix 3:99cb87ee1792 6 * Description:
modtronix 3:99cb87ee1792 7 *
modtronix 3:99cb87ee1792 8 * Software License Agreement:
modtronix 3:99cb87ee1792 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 3:99cb87ee1792 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 3:99cb87ee1792 11 * applications. If this is modified software, any license conditions from original
modtronix 3:99cb87ee1792 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 3:99cb87ee1792 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 3:99cb87ee1792 14 *
modtronix 3:99cb87ee1792 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 3:99cb87ee1792 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 3:99cb87ee1792 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 3:99cb87ee1792 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 3:99cb87ee1792 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 3:99cb87ee1792 20 */
modtronix 3:99cb87ee1792 21 #include "mbed.h"
modtronix 3:99cb87ee1792 22
modtronix 3:99cb87ee1792 23 #define DEBUG_ENABLE 1
modtronix 3:99cb87ee1792 24 #include "mx_default_debug.h"
modtronix 3:99cb87ee1792 25
modtronix 3:99cb87ee1792 26 //Includes that use debugging - Include AFTER debug defines/includes above! It uses them for debugging!
modtronix 3:99cb87ee1792 27 #include "mx_helpers.h"
modtronix 3:99cb87ee1792 28
modtronix 3:99cb87ee1792 29 uint8_t MxHelpers::ascii_hex_to_byte(WORD_VAL asciiChars) {
modtronix 3:99cb87ee1792 30 // Convert lowercase to uppercase
modtronix 3:99cb87ee1792 31 if (asciiChars.v[1] > 'F')
modtronix 3:99cb87ee1792 32 asciiChars.v[1] -= 'a' - 'A';
modtronix 3:99cb87ee1792 33 if (asciiChars.v[0] > 'F')
modtronix 3:99cb87ee1792 34 asciiChars.v[0] -= 'a' - 'A';
modtronix 3:99cb87ee1792 35
modtronix 3:99cb87ee1792 36 // Convert 0-9, A-F to 0x0-0xF
modtronix 3:99cb87ee1792 37 if (asciiChars.v[1] > '9')
modtronix 3:99cb87ee1792 38 asciiChars.v[1] -= 'A' - 10;
modtronix 3:99cb87ee1792 39 else
modtronix 3:99cb87ee1792 40 asciiChars.v[1] -= '0';
modtronix 3:99cb87ee1792 41
modtronix 3:99cb87ee1792 42 if (asciiChars.v[0] > '9')
modtronix 3:99cb87ee1792 43 asciiChars.v[0] -= 'A' - 10;
modtronix 3:99cb87ee1792 44 else
modtronix 3:99cb87ee1792 45 asciiChars.v[0] -= '0';
modtronix 3:99cb87ee1792 46
modtronix 3:99cb87ee1792 47 // Concatenate
modtronix 3:99cb87ee1792 48 return (asciiChars.v[1] << 4) | asciiChars.v[0];
modtronix 3:99cb87ee1792 49 }
modtronix 3:99cb87ee1792 50
modtronix 3:99cb87ee1792 51 uint8_t MxHelpers::ascii_hex_nibble_to_byte(uint8_t c) {
modtronix 3:99cb87ee1792 52 if ((c <= '9') && (c >= '0'))
modtronix 3:99cb87ee1792 53 return (c - '0');
modtronix 3:99cb87ee1792 54 else if ((c <= 'F') && (c >= 'A'))
modtronix 3:99cb87ee1792 55 return (c - 55);
modtronix 3:99cb87ee1792 56 else if ((c <= 'f') && (c >= 'a'))
modtronix 3:99cb87ee1792 57 return (c - 87);
modtronix 3:99cb87ee1792 58
modtronix 3:99cb87ee1792 59 return 0xff; //Indicate given byte was NOT alpha numerical
modtronix 3:99cb87ee1792 60 }
modtronix 3:99cb87ee1792 61
modtronix 3:99cb87ee1792 62
modtronix 3:99cb87ee1792 63 uint16_t MxHelpers::cvt_ascii_dec_to_uint16(const char* str, uint8_t* retFlags) {
modtronix 3:99cb87ee1792 64 uint16_t val = 0; //Returned value
modtronix 3:99cb87ee1792 65 uint8_t ret = 0; //Return retFlags value
modtronix 3:99cb87ee1792 66 bool isHex = 0;
modtronix 3:99cb87ee1792 67 const char* savedStrPtr;
modtronix 3:99cb87ee1792 68
modtronix 3:99cb87ee1792 69 savedStrPtr = str;
modtronix 3:99cb87ee1792 70
modtronix 3:99cb87ee1792 71 //MX_DEBUG("\nparseHexDecWord()");
modtronix 3:99cb87ee1792 72
modtronix 3:99cb87ee1792 73 //Does the string contain a Upper Case Hex value
modtronix 3:99cb87ee1792 74 if (*str == 'x') {
modtronix 3:99cb87ee1792 75 str++;
modtronix 3:99cb87ee1792 76 isHex = true;
modtronix 3:99cb87ee1792 77 //MX_DEBUG(" -d");
modtronix 3:99cb87ee1792 78 }
modtronix 3:99cb87ee1792 79
modtronix 3:99cb87ee1792 80 //Parse all '0'-'9', and 'A'-'F' bytes
modtronix 3:99cb87ee1792 81 while ((((*str >= '0') && (*str <= '9'))
modtronix 3:99cb87ee1792 82 || ((*str >= 'A') && (*str <= 'F')))) {
modtronix 3:99cb87ee1792 83 if (isHex) {
modtronix 3:99cb87ee1792 84 val = (val << 4)
modtronix 3:99cb87ee1792 85 + ((*str <= '9') ? (*str - '0') : (*str - 'A' + 10));
modtronix 3:99cb87ee1792 86 } else {
modtronix 3:99cb87ee1792 87 val = (val * 10) + ((*str) - '0');
modtronix 3:99cb87ee1792 88 }
modtronix 3:99cb87ee1792 89 str++;
modtronix 3:99cb87ee1792 90 }
modtronix 3:99cb87ee1792 91
modtronix 3:99cb87ee1792 92 //Get the number of bytes parsed
modtronix 3:99cb87ee1792 93 ret = str - savedStrPtr;
modtronix 3:99cb87ee1792 94 if (retFlags != NULL) {
modtronix 3:99cb87ee1792 95 *retFlags = ret;
modtronix 3:99cb87ee1792 96 }
modtronix 3:99cb87ee1792 97
modtronix 3:99cb87ee1792 98 return val;
modtronix 3:99cb87ee1792 99 }
modtronix 3:99cb87ee1792 100
modtronix 3:99cb87ee1792 101
modtronix 3:99cb87ee1792 102 uint16_t MxHelpers::cvt_ascii_hex_to_uint16(const char* str, uint8_t* retFlags) {
modtronix 3:99cb87ee1792 103 uint16_t retVal = 0; //Returned value
modtronix 3:99cb87ee1792 104 uint8_t ret = 0; //Return retFlags value
modtronix 3:99cb87ee1792 105 bool isDecimal = 0;
modtronix 3:99cb87ee1792 106 const char* savedStrPtr;
modtronix 3:99cb87ee1792 107
modtronix 3:99cb87ee1792 108 savedStrPtr = str;
modtronix 3:99cb87ee1792 109
modtronix 3:99cb87ee1792 110 //MX_DEBUG("\nparseHexDecWord()");
modtronix 3:99cb87ee1792 111
modtronix 3:99cb87ee1792 112 //Does the string contain a decimal value
modtronix 3:99cb87ee1792 113 if (*str == 'd') {
modtronix 3:99cb87ee1792 114 str++;
modtronix 3:99cb87ee1792 115 isDecimal = true;
modtronix 3:99cb87ee1792 116 //MX_DEBUG(" -d");
modtronix 3:99cb87ee1792 117 }
modtronix 3:99cb87ee1792 118
modtronix 3:99cb87ee1792 119 //Parse all '0'-'9', and 'A'-'F' bytes
modtronix 3:99cb87ee1792 120 while ((((*str >= '0') && (*str <= '9'))
modtronix 3:99cb87ee1792 121 || ((*str >= 'A') && (*str <= 'F')))) {
modtronix 3:99cb87ee1792 122 if (isDecimal) {
modtronix 3:99cb87ee1792 123 retVal = (retVal * 10) + ((*str) - '0');
modtronix 3:99cb87ee1792 124 } else {
modtronix 3:99cb87ee1792 125 retVal = (retVal << 4)
modtronix 3:99cb87ee1792 126 + ((*str <= '9') ? (*str - '0') : (*str - 'A' + 10));
modtronix 3:99cb87ee1792 127 }
modtronix 3:99cb87ee1792 128 str++;
modtronix 3:99cb87ee1792 129 }
modtronix 3:99cb87ee1792 130
modtronix 3:99cb87ee1792 131 //Get the number of bytes parsed
modtronix 3:99cb87ee1792 132 ret = str - savedStrPtr;
modtronix 3:99cb87ee1792 133 if (retFlags != NULL) {
modtronix 3:99cb87ee1792 134 *retFlags = ret;
modtronix 3:99cb87ee1792 135 }
modtronix 3:99cb87ee1792 136
modtronix 3:99cb87ee1792 137 return retVal;
modtronix 3:99cb87ee1792 138 }
modtronix 3:99cb87ee1792 139
modtronix 3:99cb87ee1792 140
modtronix 3:99cb87ee1792 141 uint8_t MxHelpers::cvt_uint16_to_ascii_str(uint16_t val, uint8_t* buf) {
modtronix 3:99cb87ee1792 142 uint8_t i;
modtronix 3:99cb87ee1792 143 uint8_t retVal = 0;
modtronix 3:99cb87ee1792 144 uint16_t digit;
modtronix 3:99cb87ee1792 145 uint16_t divisor;
modtronix 3:99cb87ee1792 146 bool printed = false;
modtronix 3:99cb87ee1792 147 //const uint16_t NZ_UITOA_DIV[4] = { 1, 10, 100, 1000 };
modtronix 3:99cb87ee1792 148 //static const uint16_t NZ_UITOA_DIV[4] = { 1, 10, 100, 1000 };
modtronix 3:99cb87ee1792 149
modtronix 3:99cb87ee1792 150 if (val) {
modtronix 3:99cb87ee1792 151 divisor = 10000;
modtronix 3:99cb87ee1792 152 for (i = 5; i != 0; i--) {
modtronix 3:99cb87ee1792 153 digit = val / divisor;
modtronix 3:99cb87ee1792 154 if ((digit!=0) || printed) {
modtronix 3:99cb87ee1792 155 *buf++ = '0' + digit;
modtronix 3:99cb87ee1792 156 val -= digit * divisor;
modtronix 3:99cb87ee1792 157 printed = true;
modtronix 3:99cb87ee1792 158 retVal++;
modtronix 3:99cb87ee1792 159 }
modtronix 3:99cb87ee1792 160 divisor = divisor / 10;
modtronix 3:99cb87ee1792 161
modtronix 3:99cb87ee1792 162 //Alternative method
modtronix 3:99cb87ee1792 163 //divisor = NZ_UITOA_DIV[i - 2];
modtronix 3:99cb87ee1792 164
modtronix 3:99cb87ee1792 165 //Alternative method
modtronix 3:99cb87ee1792 166 //if (i==5)
modtronix 3:99cb87ee1792 167 // divisor = 1000;
modtronix 3:99cb87ee1792 168 //else if (i==4)
modtronix 3:99cb87ee1792 169 // divisor = 100;
modtronix 3:99cb87ee1792 170 //else if (i==3)
modtronix 3:99cb87ee1792 171 // divisor = 10;
modtronix 3:99cb87ee1792 172 //else
modtronix 3:99cb87ee1792 173 // divisor = 1;
modtronix 3:99cb87ee1792 174 }
modtronix 3:99cb87ee1792 175 } else {
modtronix 3:99cb87ee1792 176 *buf++ = '0';
modtronix 3:99cb87ee1792 177 retVal++;
modtronix 3:99cb87ee1792 178 }
modtronix 3:99cb87ee1792 179
modtronix 3:99cb87ee1792 180 *buf = '\0';
modtronix 3:99cb87ee1792 181
modtronix 3:99cb87ee1792 182 return retVal;
modtronix 3:99cb87ee1792 183 }
modtronix 3:99cb87ee1792 184
modtronix 3:99cb87ee1792 185
modtronix 3:99cb87ee1792 186
modtronix 3:99cb87ee1792 187