Version FC
Dependencies: DmTftLibrary eeprom SX1280Lib filesystem mbed
Fork of MSNV2-Terminal_V1-5 by
util.cpp@41:5a436163dddf, 2018-10-22 (annotated)
- Committer:
- FCH_31
- Date:
- Mon Oct 22 09:37:50 2018 +0000
- Revision:
- 41:5a436163dddf
- Parent:
- 12:e9ec35413787
avec radio;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
FCH_31 | 8:cd489b7c49a0 | 1 | /* |
FCH_31 | 8:cd489b7c49a0 | 2 | * MISNet |
FCH_31 | 8:cd489b7c49a0 | 3 | * |
FCH_31 | 8:cd489b7c49a0 | 4 | * Util: Utilities |
FCH_31 | 8:cd489b7c49a0 | 5 | * |
FCH_31 | 8:cd489b7c49a0 | 6 | * Created on: August 17, 2018 Author: Francis CHATAIN |
FCH_31 | 8:cd489b7c49a0 | 7 | * |
FCH_31 | 8:cd489b7c49a0 | 8 | */ |
FCH_31 | 8:cd489b7c49a0 | 9 | |
FCH_31 | 8:cd489b7c49a0 | 10 | #include "mbed.h" |
FCH_31 | 8:cd489b7c49a0 | 11 | #include <string> |
FCH_31 | 8:cd489b7c49a0 | 12 | |
FCH_31 | 12:e9ec35413787 | 13 | |
FCH_31 | 12:e9ec35413787 | 14 | /* Helper function for printing floats & doubles */ |
FCH_31 | 12:e9ec35413787 | 15 | static char *print_double(char* str, double v, int decimalDigits) |
FCH_31 | 12:e9ec35413787 | 16 | { |
FCH_31 | 12:e9ec35413787 | 17 | int i = 1; |
FCH_31 | 12:e9ec35413787 | 18 | int intPart, fractPart; |
FCH_31 | 12:e9ec35413787 | 19 | int len; |
FCH_31 | 12:e9ec35413787 | 20 | char *ptr; |
FCH_31 | 12:e9ec35413787 | 21 | |
FCH_31 | 12:e9ec35413787 | 22 | /* prepare decimal digits multiplicator */ |
FCH_31 | 12:e9ec35413787 | 23 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
FCH_31 | 12:e9ec35413787 | 24 | |
FCH_31 | 12:e9ec35413787 | 25 | /* calculate integer & fractinal parts */ |
FCH_31 | 12:e9ec35413787 | 26 | intPart = (int)v; |
FCH_31 | 12:e9ec35413787 | 27 | fractPart = (int)((v-(double)(int)v)*i); |
FCH_31 | 12:e9ec35413787 | 28 | |
FCH_31 | 12:e9ec35413787 | 29 | /* fill in integer part */ |
FCH_31 | 12:e9ec35413787 | 30 | sprintf(str, "%i.", intPart); |
FCH_31 | 12:e9ec35413787 | 31 | |
FCH_31 | 12:e9ec35413787 | 32 | /* prepare fill in of fractional part */ |
FCH_31 | 12:e9ec35413787 | 33 | len = strlen(str); |
FCH_31 | 12:e9ec35413787 | 34 | ptr = &str[len]; |
FCH_31 | 12:e9ec35413787 | 35 | |
FCH_31 | 12:e9ec35413787 | 36 | /* fill in leading fractional zeros */ |
FCH_31 | 12:e9ec35413787 | 37 | for (i/=10;i>1; i/=10, ptr++) { |
FCH_31 | 12:e9ec35413787 | 38 | if (fractPart >= i) { |
FCH_31 | 12:e9ec35413787 | 39 | break; |
FCH_31 | 12:e9ec35413787 | 40 | } |
FCH_31 | 12:e9ec35413787 | 41 | *ptr = '0'; |
FCH_31 | 12:e9ec35413787 | 42 | } |
FCH_31 | 12:e9ec35413787 | 43 | |
FCH_31 | 12:e9ec35413787 | 44 | /* fill in (rest of) fractional part */ |
FCH_31 | 12:e9ec35413787 | 45 | sprintf(ptr, "%i", fractPart); |
FCH_31 | 12:e9ec35413787 | 46 | |
FCH_31 | 12:e9ec35413787 | 47 | return str; |
FCH_31 | 12:e9ec35413787 | 48 | } |
FCH_31 | 12:e9ec35413787 | 49 | |
FCH_31 | 12:e9ec35413787 | 50 | |
FCH_31 | 8:cd489b7c49a0 | 51 | // Convert a byte to string |
FCH_31 | 8:cd489b7c49a0 | 52 | string binToString (unsigned char *inputData, int dataLength) |
FCH_31 | 8:cd489b7c49a0 | 53 | { |
FCH_31 | 8:cd489b7c49a0 | 54 | char asciiString[dataLength*2 +1]; // 2 characters per byte plus a null at the end. |
FCH_31 | 8:cd489b7c49a0 | 55 | for (int i = 0; i<dataLength; i++) sprintf (asciiString+2*i,"%02X",*(inputData+i)) ; |
FCH_31 | 8:cd489b7c49a0 | 56 | asciiString[dataLength*2] = 0 ; // in theory redundant, the last sprintf should have done this but just to be sure... |
FCH_31 | 8:cd489b7c49a0 | 57 | return string (asciiString) ; |
FCH_31 | 8:cd489b7c49a0 | 58 | } |
FCH_31 | 8:cd489b7c49a0 | 59 | |
FCH_31 | 8:cd489b7c49a0 | 60 | void printBytes ( unsigned char *buf, size_t len) |
FCH_31 | 8:cd489b7c49a0 | 61 | { |
FCH_31 | 8:cd489b7c49a0 | 62 | int i; |
FCH_31 | 8:cd489b7c49a0 | 63 | for (i = 0; i < len; i++) |
FCH_31 | 8:cd489b7c49a0 | 64 | printf("%02x", buf[i]); printf("\n"); |
FCH_31 | 8:cd489b7c49a0 | 65 | } |
FCH_31 | 12:e9ec35413787 | 66 | |
FCH_31 | 12:e9ec35413787 | 67 | |
FCH_31 | 12:e9ec35413787 | 68 | // LITTLE ENDIAN! |
FCH_31 | 12:e9ec35413787 | 69 | uint16_t read16(uint8_t *src) |
FCH_31 | 12:e9ec35413787 | 70 | { |
FCH_31 | 12:e9ec35413787 | 71 | uint16_t d; |
FCH_31 | 12:e9ec35413787 | 72 | uint8_t b; |
FCH_31 | 12:e9ec35413787 | 73 | b = *src; |
FCH_31 | 12:e9ec35413787 | 74 | d = *(src+1); |
FCH_31 | 12:e9ec35413787 | 75 | d <<= 8; |
FCH_31 | 12:e9ec35413787 | 76 | d |= b; |
FCH_31 | 12:e9ec35413787 | 77 | return d; |
FCH_31 | 12:e9ec35413787 | 78 | } |
FCH_31 | 12:e9ec35413787 | 79 | |
FCH_31 | 12:e9ec35413787 | 80 | // LITTLE ENDIAN! |
FCH_31 | 12:e9ec35413787 | 81 | uint32_t read32(uint8_t *src) |
FCH_31 | 12:e9ec35413787 | 82 | { |
FCH_31 | 12:e9ec35413787 | 83 | uint32_t d; |
FCH_31 | 12:e9ec35413787 | 84 | uint16_t b; |
FCH_31 | 12:e9ec35413787 | 85 | |
FCH_31 | 12:e9ec35413787 | 86 | b = read16(src); |
FCH_31 | 12:e9ec35413787 | 87 | d = read16(src+2); |
FCH_31 | 12:e9ec35413787 | 88 | d <<= 16; |
FCH_31 | 12:e9ec35413787 | 89 | d |= b; |
FCH_31 | 12:e9ec35413787 | 90 | return d; |
FCH_31 | 12:e9ec35413787 | 91 | } |