DataConversion.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "DataConversion.h"
  2. void DataConversion::floatToFixed(float value, uint8_t* buffer)
  3. {
  4. // Convert float to 4 byte hex
  5. int32_t fixedval = (value * ((int32_t)1 << 23));
  6. // Store the 4 bytes in the passed buffer
  7. buffer[0] = 0x00; // First must be empty
  8. buffer[1] = (fixedval >> 24) & 0xFF;
  9. buffer[2] = (fixedval >> 16) & 0xFF;
  10. buffer[3] = (fixedval >> 8) & 0xFF;
  11. buffer[4] = fixedval & 0xFF;
  12. }
  13. /***************************************
  14. Function: intToFixed()
  15. Purpose: Converts a 28.0 integer value to 5-byte HEX and stores it to a buffer
  16. Inputs: int32_t value; Value to convert
  17. uint8_t *buffer; Buffer to store the converted data to
  18. Returns: None
  19. ***************************************/
  20. void DataConversion::intToFixed(int32_t value, uint8_t* buffer)
  21. {
  22. // Store the 4 bytes in the passed buffer
  23. buffer[0] = 0x00; // First must be empty
  24. buffer[1] = (value >> 24) & 0xFF;
  25. buffer[2] = (value >> 16) & 0xFF;
  26. buffer[3] = (value >> 8) & 0xFF;
  27. buffer[4] = value & 0xFF;
  28. }
  29. /***************************************
  30. Function: floatToInt()
  31. Purpose: Converts a 5.23 float value to int 28.0
  32. Inputs: float value; Value to convert
  33. Returns: int32_t; Converted value
  34. ***************************************/
  35. int32_t DataConversion::floatToInt(float value)
  36. {
  37. // Convert float 5.23 to int 28.0
  38. return (value * ((int32_t)1 << 23));
  39. }