Parse status and grab data

This commit is contained in:
Lucas Peter 2024-09-23 18:06:55 +02:00
parent 5f126e2b55
commit 59ea084ff4
No known key found for this signature in database
2 changed files with 63 additions and 9 deletions

View file

@ -145,9 +145,9 @@ bool USkyPortalSubsystem::OpenPortalHandle() {
void USkyPortalSubsystem::ChangePortalColor(const FLinearColor& Color) void USkyPortalSubsystem::ChangePortalColor(const FLinearColor& Color)
{ {
unsigned char r = FMath::Clamp(Color.R * 100, 0.0f, 100.0f); unsigned char r = FMath::Clamp(Color.R * 100, 0.0f, 255.0f);
unsigned char g = FMath::Clamp(Color.G * 100, 0.0f, 100.0f); unsigned char g = FMath::Clamp(Color.G * 100, 0.0f, 255.0f);
unsigned char b = FMath::Clamp(Color.B * 100, 0.0f, 100.0f); unsigned char b = FMath::Clamp(Color.B * 100, 0.0f, 255.0f);
RWBlock req; RWBlock req;
@ -277,6 +277,56 @@ void USkyPortalSubsystem::Write(RWBlock* pb) {
#endif #endif
FPortalStatusData USkyPortalSubsystem::ParsePortalStatus(const RWBlock& ResponseBlock)
{
FPortalStatusData PortalStatusData;
// Parse the figure status array (little-endian 32-bit integer)
uint32 FigureStatusArray = 0;
// Reading the 32-bit integer (character status array) from the buffer
FigureStatusArray |= ResponseBlock.buf[1]; // 1st byte
FigureStatusArray |= (ResponseBlock.buf[2] << 8); // 2nd byte
FigureStatusArray |= (ResponseBlock.buf[3] << 16); // 3rd byte
FigureStatusArray |= (ResponseBlock.buf[4] << 24); // 4th byte
// For each of the 16 entries, extract the 2-bit status and map it to EFigureStatus
for (int32 i = 0; i < 16; ++i)
{
uint8 StatusBits = (FigureStatusArray >> (i * 2)) & 0b11; // Extract 2 bits
EFigureStatus FigureStatus;
switch (StatusBits)
{
case 0b00:
FigureStatus = EFigureStatus::NOT_PRESENT;
break;
case 0b01:
FigureStatus = EFigureStatus::PRESENT;
break;
case 0b11:
FigureStatus = EFigureStatus::ADDED;
break;
case 0b10:
FigureStatus = EFigureStatus::REMOVED;
break;
default:
FigureStatus = EFigureStatus::NOT_PRESENT; // Default case
break;
}
// Add to the array of figure statuses
PortalStatusData.StatusArray.Add(FigureStatus);
}
// The next byte is the response counter
PortalStatusData.Counter = ResponseBlock.buf[5];
// The last byte is the boolean indicating whether the portal is ready
PortalStatusData.bIsReady = ResponseBlock.buf[6] != 0; // 0 means not ready, non-zero means ready
return PortalStatusData;
}
void USkyPortalSubsystem::Sleep(int sleepMs) { void USkyPortalSubsystem::Sleep(int sleepMs) {
FPlatformProcess::Sleep(sleepMs * 0.0001); FPlatformProcess::Sleep(sleepMs * 0.0001);
} }
@ -298,7 +348,7 @@ void USkyPortalSubsystem::CheckComplexResponse() {
UE_LOG(LogSkyportalIO, Error, TEXT("Error.\n %s"), hid_error(PortalDevice)); UE_LOG(LogSkyportalIO, Error, TEXT("Error.\n %s"), hid_error(PortalDevice));
return; return;
} }
EPortalCommand CommandResponse = GetPortalCommandFromChar(res.buf[1]); EPortalCommand CommandResponse = GetPortalCommandFromChar(res.buf[0]);
switch (CommandResponse) switch (CommandResponse)
{ {
case A: case A:
@ -316,7 +366,8 @@ void USkyPortalSubsystem::CheckComplexResponse() {
case R: case R:
break; break;
case S: case S:
CurrentStatusData = ParsePortalStatus(res);
//Send delegate when new informations are received
break; break;
default: default:
break; break;
@ -501,7 +552,7 @@ bool USkyPortalSubsystem::ConnectPortal()
ActivatePortal(1); ActivatePortal(1);
Sleep(500); Sleep(500);
ChangePortalColor(FLinearColor(0xC8, 0xC8, 0xC8)); ChangePortalColor(FLinearColor(0.5, 0.5, 0.5));
UE_LOG(LogSkyportalIO, Log, TEXT("Portal connected: ")); UE_LOG(LogSkyportalIO, Log, TEXT("Portal connected: "));
} }

View file

@ -40,9 +40,9 @@ enum class EFigureStatus : uint8
}; };
USTRUCT(BlueprintType) USTRUCT(BlueprintType)
struct FPortalStatusResponse struct FPortalStatusData
{ {
GENERATED_BODY() GENERATED_USTRUCT_BODY()
// Array of statuses // Array of statuses
UPROPERTY(BlueprintReadOnly, Category = "SkyPortal|Figure") UPROPERTY(BlueprintReadOnly, Category = "SkyPortal|Figure")
@ -170,6 +170,8 @@ public:
UPROPERTY(BlueprintAssignable, Category = "SkyPortal|Skylander") UPROPERTY(BlueprintAssignable, Category = "SkyPortal|Skylander")
FOnSkylanderRemovedDelegate OnSkylanderRemoved; FOnSkylanderRemovedDelegate OnSkylanderRemoved;
UPROPERTY(BlueprintReadOnly)
FPortalStatusData CurrentStatusData;
EPortalCommand GetPortalCommandFromChar(unsigned char Char); EPortalCommand GetPortalCommandFromChar(unsigned char Char);
@ -186,8 +188,9 @@ public:
private:
private:
FPortalStatusData ParsePortalStatus(const RWBlock& ResponseBlock);