MMEx with SPI Slave to allow legacy devices to communicate with modern media such as USB, SD cards, the internet and all of the mbed\'s other interfaces
Dependencies: NetServices MSCUsbHost mbed TMP102 SDFileSystem
helper.cpp@0:67a55a82ce06, 2011-02-27 (annotated)
- Committer:
- DeMein
- Date:
- Sun Feb 27 18:54:40 2011 +0000
- Revision:
- 0:67a55a82ce06
Version as submitted to the NXP Design Challenge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DeMein | 0:67a55a82ce06 | 1 | /* MMEx for MBED - Support functions |
DeMein | 0:67a55a82ce06 | 2 | * Copyright (c) 2011 MK |
DeMein | 0:67a55a82ce06 | 3 | * |
DeMein | 0:67a55a82ce06 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
DeMein | 0:67a55a82ce06 | 5 | * of this software and associated documentation files (the "Software"), to deal |
DeMein | 0:67a55a82ce06 | 6 | * in the Software without restriction, including without limitation the rights |
DeMein | 0:67a55a82ce06 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
DeMein | 0:67a55a82ce06 | 8 | * copies of the Software, and to permit persons to whom the Software is |
DeMein | 0:67a55a82ce06 | 9 | * furnished to do so, subject to the following conditions: |
DeMein | 0:67a55a82ce06 | 10 | * |
DeMein | 0:67a55a82ce06 | 11 | * The above copyright notice and this permission notice shall be included in |
DeMein | 0:67a55a82ce06 | 12 | * all copies or substantial portions of the Software. |
DeMein | 0:67a55a82ce06 | 13 | * |
DeMein | 0:67a55a82ce06 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
DeMein | 0:67a55a82ce06 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
DeMein | 0:67a55a82ce06 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
DeMein | 0:67a55a82ce06 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
DeMein | 0:67a55a82ce06 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DeMein | 0:67a55a82ce06 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
DeMein | 0:67a55a82ce06 | 20 | * THE SOFTWARE. |
DeMein | 0:67a55a82ce06 | 21 | */ |
DeMein | 0:67a55a82ce06 | 22 | |
DeMein | 0:67a55a82ce06 | 23 | /** |
DeMein | 0:67a55a82ce06 | 24 | \file helper.cpp |
DeMein | 0:67a55a82ce06 | 25 | \brief various support functions for the MMEx |
DeMein | 0:67a55a82ce06 | 26 | */ |
DeMein | 0:67a55a82ce06 | 27 | |
DeMein | 0:67a55a82ce06 | 28 | #include "helper.h" |
DeMein | 0:67a55a82ce06 | 29 | |
DeMein | 0:67a55a82ce06 | 30 | /** remove all spaces from a string |
DeMein | 0:67a55a82ce06 | 31 | * |
DeMein | 0:67a55a82ce06 | 32 | * @param src the string to be modified |
DeMein | 0:67a55a82ce06 | 33 | * |
DeMein | 0:67a55a82ce06 | 34 | */ |
DeMein | 0:67a55a82ce06 | 35 | void wipesp(char *src) { |
DeMein | 0:67a55a82ce06 | 36 | // remove all spaces from a string |
DeMein | 0:67a55a82ce06 | 37 | char *dst = src; |
DeMein | 0:67a55a82ce06 | 38 | |
DeMein | 0:67a55a82ce06 | 39 | while (*src != 0) { |
DeMein | 0:67a55a82ce06 | 40 | if (*src != c_space) { |
DeMein | 0:67a55a82ce06 | 41 | *dst++ = *src; // copy |
DeMein | 0:67a55a82ce06 | 42 | } |
DeMein | 0:67a55a82ce06 | 43 | src++; |
DeMein | 0:67a55a82ce06 | 44 | } |
DeMein | 0:67a55a82ce06 | 45 | *dst = 0; |
DeMein | 0:67a55a82ce06 | 46 | } |
DeMein | 0:67a55a82ce06 | 47 | |
DeMein | 0:67a55a82ce06 | 48 | /** remove part of a string |
DeMein | 0:67a55a82ce06 | 49 | * |
DeMein | 0:67a55a82ce06 | 50 | * @param src the string to be modified |
DeMein | 0:67a55a82ce06 | 51 | * pos start position for removing characters |
DeMein | 0:67a55a82ce06 | 52 | * len number of characters to remove |
DeMein | 0:67a55a82ce06 | 53 | * |
DeMein | 0:67a55a82ce06 | 54 | */ |
DeMein | 0:67a55a82ce06 | 55 | void remchar(char *src, int pos, int len) { |
DeMein | 0:67a55a82ce06 | 56 | char *dst = src; |
DeMein | 0:67a55a82ce06 | 57 | int i = 0; |
DeMein | 0:67a55a82ce06 | 58 | |
DeMein | 0:67a55a82ce06 | 59 | if (pos < 0) return; |
DeMein | 0:67a55a82ce06 | 60 | if (len < 0) return; |
DeMein | 0:67a55a82ce06 | 61 | |
DeMein | 0:67a55a82ce06 | 62 | src += pos; |
DeMein | 0:67a55a82ce06 | 63 | dst = src; |
DeMein | 0:67a55a82ce06 | 64 | src += len; |
DeMein | 0:67a55a82ce06 | 65 | for ( i = pos + 1; *src != 0; i++ ) *dst++ = *src++; |
DeMein | 0:67a55a82ce06 | 66 | *dst = 0; // Ensure the string is null-terminated. |
DeMein | 0:67a55a82ce06 | 67 | } |
DeMein | 0:67a55a82ce06 | 68 | |
DeMein | 0:67a55a82ce06 | 69 | /** convert a hex char '0' to 'F' in its decimal equivalent |
DeMein | 0:67a55a82ce06 | 70 | * |
DeMein | 0:67a55a82ce06 | 71 | * @param C character |
DeMein | 0:67a55a82ce06 | 72 | * @return a value 0 to 15, 0 when C was not in '0'..'F' |
DeMein | 0:67a55a82ce06 | 73 | * |
DeMein | 0:67a55a82ce06 | 74 | */ |
DeMein | 0:67a55a82ce06 | 75 | int hex2int(char C) { |
DeMein | 0:67a55a82ce06 | 76 | if ((C >= '0') && (C <= '9')) return(C - '0'); |
DeMein | 0:67a55a82ce06 | 77 | if ((C >= 'A') && (C <= 'F')) return(C - 'A' + 10); |
DeMein | 0:67a55a82ce06 | 78 | return(0); |
DeMein | 0:67a55a82ce06 | 79 | } |
DeMein | 0:67a55a82ce06 | 80 | |
DeMein | 0:67a55a82ce06 | 81 | /** insert a string in another string |
DeMein | 0:67a55a82ce06 | 82 | * |
DeMein | 0:67a55a82ce06 | 83 | * @param str1 target string where insert takes place in |
DeMein | 0:67a55a82ce06 | 84 | * @param str2 string to be inserted |
DeMein | 0:67a55a82ce06 | 85 | * @param pos position of insertion |
DeMein | 0:67a55a82ce06 | 86 | * |
DeMein | 0:67a55a82ce06 | 87 | */ |
DeMein | 0:67a55a82ce06 | 88 | void insertstr(char *str1, char *str2, int pos) { |
DeMein | 0:67a55a82ce06 | 89 | int i; |
DeMein | 0:67a55a82ce06 | 90 | int sz1 = 0; // size of str1 |
DeMein | 0:67a55a82ce06 | 91 | int sz2 = 0; // size of str2 |
DeMein | 0:67a55a82ce06 | 92 | char *dst = str1; // save start of str1 |
DeMein | 0:67a55a82ce06 | 93 | char *end = str1; // end of str1 |
DeMein | 0:67a55a82ce06 | 94 | |
DeMein | 0:67a55a82ce06 | 95 | for (i = 0; str1[i] != 0; i++) sz1++; // length of string1 |
DeMein | 0:67a55a82ce06 | 96 | for (i = 0; str2[i] != 0; i++) sz2++; // length of string2 |
DeMein | 0:67a55a82ce06 | 97 | |
DeMein | 0:67a55a82ce06 | 98 | str1 += pos; // insert point |
DeMein | 0:67a55a82ce06 | 99 | end += sz1; // end of string 1 |
DeMein | 0:67a55a82ce06 | 100 | dst += sz1 + sz2; // end of new string |
DeMein | 0:67a55a82ce06 | 101 | |
DeMein | 0:67a55a82ce06 | 102 | // now move the end of str1 to make room |
DeMein | 0:67a55a82ce06 | 103 | for (i = 0; i <= sz2; i++) *dst-- = *end--; |
DeMein | 0:67a55a82ce06 | 104 | |
DeMein | 0:67a55a82ce06 | 105 | // and move str2 in the free space inside str1 |
DeMein | 0:67a55a82ce06 | 106 | for (i = 0; i < sz2; i++) *str1++ = *str2++; |
DeMein | 0:67a55a82ce06 | 107 | } |
DeMein | 0:67a55a82ce06 | 108 | |
DeMein | 0:67a55a82ce06 | 109 | /** convert a string to upper case if global variable upcase is true |
DeMein | 0:67a55a82ce06 | 110 | * |
DeMein | 0:67a55a82ce06 | 111 | * @param str string to modify |
DeMein | 0:67a55a82ce06 | 112 | */ |
DeMein | 0:67a55a82ce06 | 113 | void upstring(char * str) { |
DeMein | 0:67a55a82ce06 | 114 | if (upcase) { |
DeMein | 0:67a55a82ce06 | 115 | while (*str != NULL) { |
DeMein | 0:67a55a82ce06 | 116 | if ((*str >= 'a') && (*str <= 'z')) *str = *str - 0x20; |
DeMein | 0:67a55a82ce06 | 117 | str++; |
DeMein | 0:67a55a82ce06 | 118 | } |
DeMein | 0:67a55a82ce06 | 119 | } |
DeMein | 0:67a55a82ce06 | 120 | } |
DeMein | 0:67a55a82ce06 | 121 | |
DeMein | 0:67a55a82ce06 | 122 |