Skip to content

Commit

Permalink
Add static utility functions to convert float and integer to/from byt…
Browse files Browse the repository at this point in the history
…e arrays: BytesToInt, BytesToFloat, IntToBytes, FloatToBytes.
  • Loading branch information
RVillani committed Jun 7, 2017
1 parent b02c192 commit 0d0a681
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
UE4Duino/Binaries/
UE4Duino/Intermediate/
/*.zip
34 changes: 34 additions & 0 deletions UE4Duino/Source/UE4Duino/Private/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ USerial* USerial::OpenComPort(bool &bOpened, int32 Port, int32 BaudRate)
return Serial;
}

int32 USerial::BytesToInt(TArray<uint8> Bytes)
{
if (Bytes.Num() != 4)
{
return 0;
}

return *reinterpret_cast<int32*>(Bytes.GetData());
}

TArray<uint8> USerial::IntToBytes(const int32 &Int)
{
TArray<uint8> Bytes;
Bytes.Append(reinterpret_cast<const uint8*>(&Int), 4);
return Bytes;
}

float USerial::BytesToFloat(TArray<uint8> Bytes)
{
if (Bytes.Num() != 4)
{
return 0;
}

return *reinterpret_cast<float*>(Bytes.GetData());
}

TArray<uint8> USerial::FloatToBytes(const float &Float)
{
TArray<uint8> Bytes;
Bytes.Append(reinterpret_cast<const uint8*>(&Float), 4);
return Bytes;
}

USerial::USerial()
: m_hIDComDev(NULL)
, m_Port(-1)
Expand Down
36 changes: 36 additions & 0 deletions UE4Duino/Source/UE4Duino/Private/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,42 @@ class USerial : public UObject
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Open Serial Port"), Category = "UE4Duino", meta = (Keywords = "com arduino serial start"))
static USerial* OpenComPort(bool &bOpened, int32 Port = 1, int32 BaudRate = 9600);

/**
* Utility function to convert 4 bytes into an Integer. If the input array's length is not 4, returns 0.
*
* @param Bytes A byte array with 4 values representing the integer in little-endian format.
* @return The final integer value or 0 for an invalid array.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Bytes to Int"), Category = "UE4Duino", meta = (Keywords = "cast concatenate group bit bitwise"))
static int32 BytesToInt(TArray<uint8> Bytes);

/**
* Utility function to get the 4 bytes that make an integer.
*
* @param Int The integer value to be converted.
* @return A byte array containing the 4 bytes that make the integer, starting from the least significant one (little endian).
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Int to Bytes"), Category = "UE4Duino", meta = (Keywords = "cast separate bit bitwise"))
static TArray<uint8> IntToBytes(const int32 &Int);

/**
* Utility function to convert 4 bytes into a float. If the input array's length is not 4, returns 0.0.
*
* @param Bytes A byte array with 4 values representing the float in IEEE 754 standard format.
* @return The final float value or 0.0 for an invalid array.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Bytes to Float"), Category = "UE4Duino", meta = (Keywords = "cast concatenate group bit bitwise"))
static float BytesToFloat(TArray<uint8> Bytes);

/**
* Utility function to get the 4 bytes that make a float.
*
* @param Float The float value to be converted.
* @return A byte array containing the 4 bytes that make the float, in IEEE 754 standard format.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Float to Bytes"), Category = "UE4Duino", meta = (Keywords = "cast separate bit bitwise"))
static TArray<uint8> FloatToBytes(const float &Float);

/**
* Open a serial port. Don't forget to close the port before exiting the game.
* If this Serial instance has already an opened port,
Expand Down

0 comments on commit 0d0a681

Please sign in to comment.