Connect a Spacemouse Pro on UE4

Hi,

I create a class to link the spacemouse Pro on UE4. But I think that I didn’t used the best way. Is it possible to critic my work please ?

  1. I link the library HIDAPI

    using UnrealBuildTool;

    public class IronMan : ModuleRules
    {
    public IronMan(TargetInfo Target)
    {
    PublicDependencyModuleNames.AddRange(new string[] { “Core”, “CoreUObject”, “Engine”, “InputCore” });

     	PrivateDependencyModuleNames.AddRange(new string[] {  });
    
         PublicAdditionalLibraries.Add("C:/LIBS/hidapi-0.7.0/windows/x64/Release/hidapi.lib");
         PublicIncludePaths.Add("C:/LIBS/hidapi-0.7.0/hidapi");
     }
    

    }

  2. I create my header class

    #pragma once

    #include “hidapi.h”
    #include “AllowWindowsPlatformTypes.h”
    #include
    #include “HideWindowsPlatformTypes.h”

    #include “GameFramework/PlayerController.h”
    #include “IronManController.generated.h”

    UCLASS()
    class IRONMAN_API AIronManController : public APlayerController
    {
    GENERATED_BODY()

    public:
    float x;
    float y;
    float z;
    float Rx;
    float Ry;
    float Rz;
    hid_device *handle;

    public:
    AIronManController();

     virtual void BeginPlay() override;
     virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
    
     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "SpacemousePro")
     	FVector GetLocationSpacemousePro();
    
     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "SpacemousePro")
     	FVector GetRotationSpacemousePro();
    

    };

  3. I thread the device

    #include “IronMan.h”
    #include “IronManController.h”

    float ParseValue(unsigned char value, unsigned char signe)
    {
    if (signe == 0xff)
    return -(255 - static_cast(value));
    if (signe == 0xfe)
    return -(2 * 255 - static_cast(value));
    if (signe == 0x00)
    return static_cast(value);
    if (signe == 0x01)
    return 255 + static_cast(value);
    return 0.0f;
    }

    void CatchEvent(float* x, float* y, float* z, float* Rx, floatRy, float Rz, hid_device** handle)
    {
    unsigned char buf[7];

     memset(buf, 0x00, sizeof(buf));
     buf[0] = 0x01;
     buf[1] = 0x81;
    
     *handle = hid_open(0x046d, 0xc62b, NULL);
     if (*handle == NULL)
     	return;
    
     hid_set_nonblocking(*handle, 1);
    
     while (*handle != NULL)
     {
     	if (hid_read(*handle, buf, sizeof(buf)) == 7)
     	{
     		if (buf[0] == 1)
     		{
     			*x = ParseValue(buf[3], buf[4]);
     			*y = ParseValue(buf[1], buf[2]);
     			*z = ParseValue(buf[5], buf[6]);
     		}
     		if (buf[0] == 2)
     		{
     			*Rx = ParseValue(buf[3], buf[4]);
     			*Ry = ParseValue(buf[1], buf[2]);
     			*Rz = ParseValue(buf[5], buf[6]);
     		}
     	}
     }
    

    }

    AIronManController::AIronManController()
    {
    }

    void AIronManController::BeginPlay()
    {
    std::thread mouse3D;
    this->x = this->y = this->z = this->Rx = this->Ry = this->Rz = 0.0f;
    mouse3D = std::thread(CatchEvent, &this->x, &this->y, &this->z, &this->Rx, &this->Ry, &this->Rz, &this->handle);
    mouse3D.detach();
    }

    void AIronManController::EndPlay(const EEndPlayReason::Type EndPlayReason)
    {
    hid_close(handle);
    this->handle = NULL;
    Super::EndPlay(EndPlayReason);
    }

    FVector AIronManController::GetLocationSpacemousePro()
    {
    return FVector(-x, y, -z);
    }

    FVector AIronManController::GetRotationSpacemousePro()
    {
    return FVector(-Rx, Ry, Rz);
    }

My work works so it’s possible that will help somebody. But if you think you can make better I’m enjoy to follow you’re answer.

Regard,