Utils
namespace Utils

Contains many commonly used utility functions.  Note: Communication with our devices (wireless and inertial) is done in Big Endian.

Functions
valueTypeSize
uint32 valueTypeSize(
ValueType type
)

Gets the size of the ValueType

Parameters
type

The ValueType to get the size of

Returns

The size of the given ValueType

Exceptions
msb
uint8 msb(
uint16 value
)

Gets the Most Significant Byte from the 2-Byte WORD

Parameters
value
uint16

the 2-Byte unsigned integer to find the MSB from

Returns

The Most Significant Byte of the passed in value

lsb
uint8 lsb(
uint16 value
)

Gets the Least Significant Byte from the 2-Byte WORD

Parameters
value
uint16

the 2-Byte unsigned integer to find the LSB from

Returns

The Least Significant Byte of the passed in value

msNibble
uint8 msNibble(
uint8 value
)

Gets the Most Significant Nibble (4-bits) from the 1-Byte value

Parameters
value
uint8

the 1-Byte value to find the Most Significant Nibble from

Returns

The Most Significant Nibble of the passed in value

lsNibble
uint8 lsNibble(
uint8 value
)

Gets the Least Significant Nibble (4-bits) from the 1-Byte value

Parameters
value
uint8

the 1-Byte value to find the Least Significant Nibble from

Returns

The Least Significant Nibble of the passed in value

make_int16
int16 make_int16(
uint8 msb,
uint8 lsb
)

Gets the 2-byte nsigned integer from 2 individual MSB and LSB bytes

Parameters
msb
uint8

the Most Significant Byte

lsb
uint8

the Lease Significant Byte

Returns

The 2-byte int16 built from the MSB and LSB bytes

split_uint16
void split_uint16(
uint16 value,
uint8 &msb,
uint8 &lsb
)

Converts the 2-byte unsigned integer into its individual 2 bytes

Parameters
value
uint16

the 2 byte uint16 to convert into bytes

msb
uint8&

holds the Most Significant Byte result

lsb
uint8&

holds the Least Significant Byte result

make_uint16
uint16 make_uint16(
uint8 msb,
uint8 lsb
)

Gets the 2-byte unsigned integer from 2 individual MSB and LSB bytes

Parameters
msb
uint8

the Most Significant Byte

lsb
uint8

the Lease Significant Byte

Returns

The 2-byte uint16 built from the MSB and LSB bytes

split_uint32
void split_uint32(
uint32 value,
uint8 &msb,
uint8 &byte2,
uint8 &byte3,
uint8 &lsb
)

Converts the 4-byte unsigned integer into its 4 individual bytes

Parameters
value
uint32

the 4 byte uint32 to convert into bytes

msb
uint8&

holds the msb result

byte2
uint8&

holds the second byte result

byte3
uint8&

holds the third byte result

lsb
uint8&

holds the lsb result

split_uint32
void split_uint32(
uint32 value,
uint16 &msw,
uint16 &lsw
)

Converts the 4-byte unsigned integer into its 2 uint16 values.

Parameters
value
uint32

the 4 byte uint32 to convert into bytes.

msw
uint16&

holds the msw result.

lsw
uint16&

holds the lsw result.

make_uint32
uint32 make_uint32(
uint8 msb,
uint8 byte2,
uint8 byte3,
uint8 lsb
)

gets the 4-byte unsigned integer from the 4 individual bytes

Parameters
msb
uint8

the msb to be used

byte2
uint8

the second byte to be used

byte3
uint8

the third byte to be used

lsb
uint8

the lsb to be used

Returns

the 4-byte uint32 built from the 4 bytes

make_uint32
uint32 make_uint32(
uint16 msw,
uint16 lsw
)

gets the 4-byte unsigned integer from the 2 uint16 words.

Parameters
msw
uint16

the most significant word to be used.

lsw
uint16

the lease significant word to be used.

Returns

the 4-byte uint32 built from the 2 uint16 words.

split_float_big_endian
void split_float_big_endian(
float value,
uint8 &msb,
uint8 &byte2,
uint8 &byte3,
uint8 &lsb
)

Converts the 4-byte float (in system endian) into its 4 individual bytes in big endian

Parameters
value
float

the 4 byte float (in system endian) to convert into bytes

msb
uint8&

holds the msb result

byte2
uint8&

holds the second byte result

byte3
uint8&

holds the third byte result

lsb
uint8&

holds the lsb result

split_float_little_endian
void split_float_little_endian(
float value,
uint8 &lsb,
uint8 &byte2,
uint8 &byte3,
uint8 &msb
)

Converts the 4-byte float (in system endian) into its 4 individual bytes in little endian

Parameters
value
float

the 4 byte float (in system endian) to convert into bytes

lsb
uint8&

holds the lsb result

byte2
uint8&

holds the second byte result

byte3
uint8&

holds the third byte result

msb
uint8&

holds the msb result

make_float_big_endian
float make_float_big_endian(
uint8 msb,
uint8 byte2,
uint8 byte3,
uint8 lsb
)

Builds a 4-byte float (in system endian) from 4 bytes given in big endian

Parameters
msb
uint8

the most significant byte

byte2
uint8

the second byte

byte3
uint8

the third byte

lsb
uint8

the least significant byte

Returns

A 4-byte float in system-endianess built from the 4 given bytes

make_float_little_endian
float make_float_little_endian(
uint8 lsb,
uint8 byte2,
uint8 byte3,
uint8 msb
)

Builds a 4-byte float (in system endian) from 4 bytes given in little endian

Parameters
lsb
uint8

the least significant byte

byte2
uint8

the second byte

byte3
uint8

the third byte

msb
uint8

the most significant byte

Returns

A 4-byte float in system-endianess built from the 4 given bytes

split_double_big_endian
void split_double_big_endian(
double value,
uint8 &msb,
uint8 &byte2,
uint8 &byte3,
uint8 &byte4,
uint8 &byte5,
uint8 &byte6,
uint8 &byte7,
uint8 &lsb
)

Converts the 8-byte double (in system endian) into its 8 individual bytes in big endian

Parameters
value
double

the 8 byte double (in system endian) to convert into bytes

msb
uint8&

holds the msb result

byte2
uint8&

holds the second byte result

byte3
uint8&

holds the third byte result

byte4
uint8&

holds the fourth byte result

byte5
uint8&

holds the fifth byte result

byte6
uint8&

holds the sixth byte result

byte7
uint8&

holds the seventh byte result

lsb
uint8&

holds the lsb result

split_double_little_endian
void split_double_little_endian(
double value,
uint8 &lsb,
uint8 &byte2,
uint8 &byte3,
uint8 &byte4,
uint8 &byte5,
uint8 &byte6,
uint8 &byte7,
uint8 &msb
)

Converts the 8-byte double (in system endian) into its 8 individual bytes in little endian

Parameters
value
double

the 8 byte double (in system endian) to convert into bytes

lsb
uint8&

holds the lsb result

byte2
uint8&

holds the second byte result

byte3
uint8&

holds the third byte result

byte4
uint8&

holds the fourth byte result

byte5
uint8&

holds the fifth byte result

byte6
uint8&

holds the sixth byte result

byte7
uint8&

holds the seventh byte result

msb
uint8&

holds the msb result

make_double_big_endian
double make_double_big_endian(
uint8 msb,
uint8 byte2,
uint8 byte3,
uint8 byte4,
uint8 byte5,
uint8 byte6,
uint8 byte7,
uint8 lsb
)

Builds a 8-byte double (in system endian) from the bytes given in big endian

Parameters
msb
uint8

the msb to be used

byte2
uint8

the 2nd byte to be used

byte3
uint8

the 3rd byte to be used

byte4
uint8

the 4th byte to be used

byte5
uint8

the 5th byte to be used

byte6
uint8

the 6th byte to be used

byte7
uint8

the 7th byte to be used

lsb
uint8

the lsb to be used

Returns

the 8-byte double (in system endian) built from the 8 bytes

make_double_little_endian
double make_double_little_endian(
uint8 lsb,
uint8 byte2,
uint8 byte3,
uint8 byte4,
uint8 byte5,
uint8 byte6,
uint8 byte7,
uint8 msb
)

Builds a 8-byte double (in system endian) from the bytes given in little endian

Parameters
lsb
uint8

the lsb to be used

byte2
uint8

the 2nd byte to be used

byte3
uint8

the 3rd byte to be used

byte4
uint8

the 4th byte to be used

byte5
uint8

the 5th byte to be used

byte6
uint8

the 6th byte to be used

byte7
uint8

the 7th byte to be used

msb
uint8

the msb to be used

Returns

the 8-byte double (in system endian) built from the 8 bytes

getCurrentSystemTime
uint64 getCurrentSystemTime()

Gets the current system time in nanoseconds since unix epoch

Returns

The nanoseconds representation of the current system time in UTC

threadSleep
void threadSleep(
uint64 milli
)

Sleeps for a specified amount of milliseconds on the current thread.

Parameters
milli
uint64

The number of milliseconds to sleep for.

logBase2
double logBase2(
double value
)

Gets the log base 2 of a value

Parameters
value
double

The value to get the log of

Returns

The log base 2 of the value

round
float round(
float value
)

Returns the value that is nearest to the provided value to round. (ie. round(2.5) == 3, round(2.4) == 2)

Parameters
value
float

The value to round

Returns

The rounded result.

roundDownToNearestBase2
uint32 roundDownToNearestBase2(
double value
)

Rounds down to the nearest base 2 value

Parameters
value
double

The value to round down to the nearest base 2

Returns

The value rounded down to the nearest base 2

roundUpToNearestBase2
uint32 roundUpToNearestBase2(
double value
)

Rounds up to the nearest base 2 value

Parameters
value
double

The value to round up to the nearest base 2

Returns

The value rounded up to the nearest base 2

radiansToDegrees
double radiansToDegrees(
float angle
)

Converts the radian angle to degrees.

Parameters
angle
float

The angle in radians.

Returns

The angle in degrees.

bitIsSet
bool bitIsSet(
uint32 value,
size_t bitPos
)

Checks if a specific bit is set in the given value.

Parameters
value
uint32

The value to check the bit of.

bitPos
size_t

The bit position to check if it is set. (0 = far right)

Returns

true if the bit is set, false if it is not set.

removeChar
void removeChar(
std::string &src,
char charToRemove
)

Removes all found instances of a char from a string.

Parameters
src
std::string&

The string to remove the character from. This will be changed to hold the result.

charToRemove
char

The character to remove all instances of from the string.

removeStr
void removeStr(
std::string &src,
const std::string &strToRemove
)

Removes all found instances of a string from another string.

Parameters
str

The string to remove the substring from. This will be changed to hold the result.

strtoRemove
const std::string&

The substring to remove all instances of from the string.

containsStr
bool containsStr(
const std::string &src,
const std::string &containsThis
)

Checks whether or not a string contains another string.

Parameters
src
const std::string&

The source string.

containsThis
const std::string&

The string to attempt to find in the source string.

Returns

true if the string was found in the source string, false if it was not found.

strTrimLeft
void strTrimLeft(
std::string &src
)

Trims all whitespace from the left of a string.

Parameters
src
std::string&

The string to trim whitespace from. This will be changed to hold the result.

strTrimRight
void strTrimRight(
std::string &src
)

Trims all whitespace from the right of a string.

Parameters
src
std::string&

The string to trim whitespace from. This will be changed to hold the result.

filterSensorcloudName
void filterSensorcloudName(
std::string &str
)

Replaces characters that are not supported by SensorCloud with a '-' character.

Parameters
str
std::string&

The string to filter. This will hold the result.

normalizeAngle
float normalizeAngle(
float angle
)

Normalizes an angle value to be between 0 and 360 degrees.

Parameters
angle
float

The angle to normalize.

Returns

The normalized angle within 0 and 360 degrees.

isNaN
template<typename T> bool isNaN(
value
)

Checks whether the given value is NaN or not.

Parameters
value
T

The value to check if it is a NaN.

Returns

true if the value is a NaN, false otherwise.

checkBounds_min
template<typename T> void checkBounds_min(
&value,
min
)

Checks that a value is not below a minimum value. If the value is less than the min, the value is changed to that min value.

Parameters
value
T&

The value to check

min
T

The minimum value to make sure the value is greater than or equal to

checkBounds_max
template<typename T> void checkBounds_max(
&value,
max
)

Checks that a value is not above a maximum value. If the value is greater than the max, the value is changed to that max value.

Parameters
value
T&

The value to check

max
T

The maximum value to make sure the value is less than or equal to.

toStr
template<typename T> std::string toStr(
value
)

Performs a boost::lexical_cast from a value to an std::string

Parameters
value
T

The value to convert to a string.

Returns

The string result of the lexical cast.