![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
utils.cpp@117:e9facba9db27, 2015-04-23 (annotated)
- Committer:
- klauss
- Date:
- Thu Apr 23 21:53:28 2015 +0000
- Revision:
- 117:e9facba9db27
- Parent:
- 114:472502b31a12
- Child:
- 119:ee6a53069455
merge dos fw contendo as funcionalidade de bootloader e o fw que estava em campo na ultima vistoria
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "utils.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 117:e9facba9db27 | 3 | static char itoh[16]; |
klauss | 117:e9facba9db27 | 4 | char hexbuf[10]; |
klauss | 117:e9facba9db27 | 5 | |
klauss | 117:e9facba9db27 | 6 | void hex_init(void) { |
klauss | 117:e9facba9db27 | 7 | itoh[0] = '0'; itoh[1] = '1'; itoh[2] = '2'; itoh[3] = '3'; |
klauss | 117:e9facba9db27 | 8 | itoh[4] = '4'; itoh[5] = '5'; itoh[6] = '6'; itoh[7] = '7'; |
klauss | 117:e9facba9db27 | 9 | itoh[8] = '8'; itoh[9] = '9'; itoh[10] = 'a'; itoh[11] = 'b'; |
klauss | 117:e9facba9db27 | 10 | itoh[12] = 'c'; itoh[13] = 'd'; itoh[14] = 'e'; itoh[15] = 'f'; |
klauss | 117:e9facba9db27 | 11 | } |
klauss | 117:e9facba9db27 | 12 | |
klauss | 117:e9facba9db27 | 13 | char * hex8 (uint8_t i) { |
klauss | 117:e9facba9db27 | 14 | hexbuf[0] = itoh[(i>>4) & 0x0f]; |
klauss | 117:e9facba9db27 | 15 | hexbuf[1] = itoh[i & 0x0f]; |
klauss | 117:e9facba9db27 | 16 | hexbuf[2] = 0; |
klauss | 117:e9facba9db27 | 17 | return hexbuf; |
klauss | 117:e9facba9db27 | 18 | } |
klauss | 117:e9facba9db27 | 19 | |
klauss | 117:e9facba9db27 | 20 | char * hex16 (uint16_t i) { |
klauss | 117:e9facba9db27 | 21 | hexbuf[0] = itoh[(i>>12) & 0x0f]; |
klauss | 117:e9facba9db27 | 22 | hexbuf[1] = itoh[(i>>8) & 0x0f]; |
klauss | 117:e9facba9db27 | 23 | hexbuf[2] = itoh[(i>>4) & 0x0f]; |
klauss | 117:e9facba9db27 | 24 | hexbuf[3] = itoh[i & 0x0f]; |
klauss | 117:e9facba9db27 | 25 | hexbuf[4] = 0; |
klauss | 117:e9facba9db27 | 26 | return hexbuf; |
klauss | 117:e9facba9db27 | 27 | } |
klauss | 117:e9facba9db27 | 28 | |
klauss | 74:81c47fff88a5 | 29 | int ls_comp( const void * a, const void * b ){ return( *( int * )a - *( int * )b ); } |
klauss | 68:b54993674190 | 30 | |
klauss | 0:4d17cd9c8f9d | 31 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 32 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 33 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 34 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 35 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 36 | start++; |
klauss | 0:4d17cd9c8f9d | 37 | end--; |
klauss | 0:4d17cd9c8f9d | 38 | } |
klauss | 0:4d17cd9c8f9d | 39 | } |
klauss | 0:4d17cd9c8f9d | 40 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 41 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 42 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 43 | |
klauss | 0:4d17cd9c8f9d | 44 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 45 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 46 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 47 | return str; |
klauss | 0:4d17cd9c8f9d | 48 | } |
klauss | 114:472502b31a12 | 49 | if( num < 0 //&& base == 10 |
klauss | 114:472502b31a12 | 50 | ){ |
klauss | 0:4d17cd9c8f9d | 51 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 52 | num = -num; |
klauss | 0:4d17cd9c8f9d | 53 | } |
klauss | 0:4d17cd9c8f9d | 54 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 55 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 56 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 57 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 58 | } |
klauss | 0:4d17cd9c8f9d | 59 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 60 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 61 | |
klauss | 0:4d17cd9c8f9d | 62 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 63 | |
klauss | 0:4d17cd9c8f9d | 64 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 65 | return( str ); |
klauss | 0:4d17cd9c8f9d | 66 | } |
klauss | 0:4d17cd9c8f9d | 67 | |
klauss | 74:81c47fff88a5 | 68 | int convert_ext_to_port( int ext ){ |
klauss | 74:81c47fff88a5 | 69 | if( ext < 1000 ) return ext; |
klauss | 74:81c47fff88a5 | 70 | |
klauss | 74:81c47fff88a5 | 71 | else{ |
klauss | 74:81c47fff88a5 | 72 | int aux = 0; |
klauss | 74:81c47fff88a5 | 73 | aux = ( ext / 1000 ) * 1000; |
klauss | 74:81c47fff88a5 | 74 | ext -= aux; |
klauss | 74:81c47fff88a5 | 75 | aux += ext % 100; |
klauss | 74:81c47fff88a5 | 76 | return( aux ); |
klauss | 74:81c47fff88a5 | 77 | } |
klauss | 0:4d17cd9c8f9d | 78 | } |
klauss | 0:4d17cd9c8f9d | 79 | |
klauss | 74:81c47fff88a5 | 80 | void xstrcpy(uint8_t * dest, const uint8_t * src ){ |
klauss | 74:81c47fff88a5 | 81 | while (*src) *dest++ = *src++; |
klauss | 74:81c47fff88a5 | 82 | *dest = 0; |
klauss | 0:4d17cd9c8f9d | 83 | } |
klauss | 0:4d17cd9c8f9d | 84 | |
klauss | 78:1353744f01e1 | 85 | void xmemcpy(uint8_t * dest, const uint8_t * src, uint16_t size ){ |
klauss | 74:81c47fff88a5 | 86 | while (size--) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 87 | } |
klauss | 0:4d17cd9c8f9d | 88 | |
klauss | 74:81c47fff88a5 | 89 | void xmemcpy32(uint32_t * dest, uint32_t * src, uint16_t size){ |
klauss | 74:81c47fff88a5 | 90 | size >>= 2; |
klauss | 74:81c47fff88a5 | 91 | while (size--) *dest++ = *src++; |
klauss | 78:1353744f01e1 | 92 | } |
klauss | 78:1353744f01e1 | 93 | |
klauss | 78:1353744f01e1 | 94 | uint8_t xstrmatch(const uint8_t * s1, const uint8_t * s2){ |
klauss | 78:1353744f01e1 | 95 | while (*s1) { |
klauss | 78:1353744f01e1 | 96 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 78:1353744f01e1 | 97 | } |
klauss | 78:1353744f01e1 | 98 | if ( *s2 == 0 ) |
klauss | 78:1353744f01e1 | 99 | return 1; /* matches */ |
klauss | 78:1353744f01e1 | 100 | else |
klauss | 78:1353744f01e1 | 101 | return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 102 | } |
klauss | 81:3656f00ab3db | 103 | |
klauss | 81:3656f00ab3db | 104 | uint8_t xmemmatch(const uint8_t * s1, const uint8_t * s2, uint16_t size){ |
klauss | 81:3656f00ab3db | 105 | while (size--) { |
klauss | 81:3656f00ab3db | 106 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 107 | } |
klauss | 81:3656f00ab3db | 108 | return 1; /* matches */ |
klauss | 74:81c47fff88a5 | 109 | } |