![](/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@119:ee6a53069455, 2015-04-30 (annotated)
- Committer:
- klauss
- Date:
- Thu Apr 30 15:42:41 2015 +0000
- Revision:
- 119:ee6a53069455
- Parent:
- 117:e9facba9db27
- Child:
- 121:ee02790d00b7
funcionalidade - clock "ogro" - implementada
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 | 119:ee6a53069455 | 6 | int 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 | 119:ee6a53069455 | 11 | return ( 0 ); |
klauss | 117:e9facba9db27 | 12 | } |
klauss | 117:e9facba9db27 | 13 | |
klauss | 117:e9facba9db27 | 14 | char * hex8 (uint8_t i) { |
klauss | 117:e9facba9db27 | 15 | hexbuf[0] = itoh[(i>>4) & 0x0f]; |
klauss | 117:e9facba9db27 | 16 | hexbuf[1] = itoh[i & 0x0f]; |
klauss | 117:e9facba9db27 | 17 | hexbuf[2] = 0; |
klauss | 117:e9facba9db27 | 18 | return hexbuf; |
klauss | 117:e9facba9db27 | 19 | } |
klauss | 117:e9facba9db27 | 20 | |
klauss | 117:e9facba9db27 | 21 | char * hex16 (uint16_t i) { |
klauss | 117:e9facba9db27 | 22 | hexbuf[0] = itoh[(i>>12) & 0x0f]; |
klauss | 117:e9facba9db27 | 23 | hexbuf[1] = itoh[(i>>8) & 0x0f]; |
klauss | 117:e9facba9db27 | 24 | hexbuf[2] = itoh[(i>>4) & 0x0f]; |
klauss | 117:e9facba9db27 | 25 | hexbuf[3] = itoh[i & 0x0f]; |
klauss | 117:e9facba9db27 | 26 | hexbuf[4] = 0; |
klauss | 117:e9facba9db27 | 27 | return hexbuf; |
klauss | 117:e9facba9db27 | 28 | } |
klauss | 117:e9facba9db27 | 29 | |
klauss | 74:81c47fff88a5 | 30 | int ls_comp( const void * a, const void * b ){ return( *( int * )a - *( int * )b ); } |
klauss | 68:b54993674190 | 31 | |
klauss | 0:4d17cd9c8f9d | 32 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 33 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 34 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 35 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 36 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 37 | start++; |
klauss | 0:4d17cd9c8f9d | 38 | end--; |
klauss | 0:4d17cd9c8f9d | 39 | } |
klauss | 0:4d17cd9c8f9d | 40 | } |
klauss | 0:4d17cd9c8f9d | 41 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 42 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 43 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 44 | |
klauss | 0:4d17cd9c8f9d | 45 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 46 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 47 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 48 | return str; |
klauss | 0:4d17cd9c8f9d | 49 | } |
klauss | 114:472502b31a12 | 50 | if( num < 0 //&& base == 10 |
klauss | 114:472502b31a12 | 51 | ){ |
klauss | 0:4d17cd9c8f9d | 52 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 53 | num = -num; |
klauss | 0:4d17cd9c8f9d | 54 | } |
klauss | 0:4d17cd9c8f9d | 55 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 56 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 57 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 58 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 59 | } |
klauss | 0:4d17cd9c8f9d | 60 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 61 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 62 | |
klauss | 0:4d17cd9c8f9d | 63 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 64 | |
klauss | 0:4d17cd9c8f9d | 65 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 66 | return( str ); |
klauss | 0:4d17cd9c8f9d | 67 | } |
klauss | 0:4d17cd9c8f9d | 68 | |
klauss | 74:81c47fff88a5 | 69 | int convert_ext_to_port( int ext ){ |
klauss | 74:81c47fff88a5 | 70 | if( ext < 1000 ) return ext; |
klauss | 74:81c47fff88a5 | 71 | |
klauss | 74:81c47fff88a5 | 72 | else{ |
klauss | 74:81c47fff88a5 | 73 | int aux = 0; |
klauss | 74:81c47fff88a5 | 74 | aux = ( ext / 1000 ) * 1000; |
klauss | 74:81c47fff88a5 | 75 | ext -= aux; |
klauss | 74:81c47fff88a5 | 76 | aux += ext % 100; |
klauss | 74:81c47fff88a5 | 77 | return( aux ); |
klauss | 74:81c47fff88a5 | 78 | } |
klauss | 0:4d17cd9c8f9d | 79 | } |
klauss | 0:4d17cd9c8f9d | 80 | |
klauss | 74:81c47fff88a5 | 81 | void xstrcpy(uint8_t * dest, const uint8_t * src ){ |
klauss | 74:81c47fff88a5 | 82 | while (*src) *dest++ = *src++; |
klauss | 74:81c47fff88a5 | 83 | *dest = 0; |
klauss | 0:4d17cd9c8f9d | 84 | } |
klauss | 0:4d17cd9c8f9d | 85 | |
klauss | 78:1353744f01e1 | 86 | void xmemcpy(uint8_t * dest, const uint8_t * src, uint16_t size ){ |
klauss | 74:81c47fff88a5 | 87 | while (size--) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 88 | } |
klauss | 0:4d17cd9c8f9d | 89 | |
klauss | 74:81c47fff88a5 | 90 | void xmemcpy32(uint32_t * dest, uint32_t * src, uint16_t size){ |
klauss | 74:81c47fff88a5 | 91 | size >>= 2; |
klauss | 74:81c47fff88a5 | 92 | while (size--) *dest++ = *src++; |
klauss | 78:1353744f01e1 | 93 | } |
klauss | 78:1353744f01e1 | 94 | |
klauss | 78:1353744f01e1 | 95 | uint8_t xstrmatch(const uint8_t * s1, const uint8_t * s2){ |
klauss | 78:1353744f01e1 | 96 | while (*s1) { |
klauss | 78:1353744f01e1 | 97 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 78:1353744f01e1 | 98 | } |
klauss | 78:1353744f01e1 | 99 | if ( *s2 == 0 ) |
klauss | 78:1353744f01e1 | 100 | return 1; /* matches */ |
klauss | 78:1353744f01e1 | 101 | else |
klauss | 78:1353744f01e1 | 102 | return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 103 | } |
klauss | 81:3656f00ab3db | 104 | |
klauss | 81:3656f00ab3db | 105 | uint8_t xmemmatch(const uint8_t * s1, const uint8_t * s2, uint16_t size){ |
klauss | 81:3656f00ab3db | 106 | while (size--) { |
klauss | 81:3656f00ab3db | 107 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 108 | } |
klauss | 81:3656f00ab3db | 109 | return 1; /* matches */ |
klauss | 74:81c47fff88a5 | 110 | } |