Refactoring
This commit is contained in:
37
LiveData.cpp
Normal file
37
LiveData.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
#ifndef LIVEDATA_CPP
|
||||
#define LIVEDATA_CPP
|
||||
|
||||
#include "LiveData.h"
|
||||
|
||||
/**
|
||||
Hex to dec (1-2 byte values, signed/unsigned)
|
||||
For 4 byte change int to long and add part for signed numbers
|
||||
*/
|
||||
float LiveData::hexToDec(String hexString, byte bytes, bool signedNum) {
|
||||
|
||||
unsigned int decValue = 0;
|
||||
unsigned int nextInt;
|
||||
|
||||
for (int i = 0; i < hexString.length(); i++) {
|
||||
nextInt = int(hexString.charAt(i));
|
||||
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
|
||||
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
|
||||
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
|
||||
nextInt = constrain(nextInt, 0, 15);
|
||||
decValue = (decValue * 16) + nextInt;
|
||||
}
|
||||
|
||||
// Unsigned - do nothing
|
||||
if (!signedNum) {
|
||||
return decValue;
|
||||
}
|
||||
// Signed for 1, 2 bytes
|
||||
if (bytes == 1) {
|
||||
return (decValue > 127 ? (float)decValue - 256.0 : decValue);
|
||||
}
|
||||
return (decValue > 32767 ? (float)decValue - 65536.0 : decValue);
|
||||
}
|
||||
|
||||
//
|
||||
#endif // LIVEDATA_CPP
|
||||
Reference in New Issue
Block a user