Compare commits
3 commits
652d62ed96
...
ea75a1d07e
Author | SHA1 | Date | |
---|---|---|---|
ea75a1d07e | |||
|
efdfea54ba | ||
|
5496613f14 |
2 changed files with 130 additions and 5 deletions
|
@ -1,9 +1,14 @@
|
||||||
|
// A lot of this code is made because of the work of https://github.com/capull0/SkyDumper
|
||||||
|
|
||||||
#include "SkyPortalSubsystem.h"
|
#include "SkyPortalSubsystem.h"
|
||||||
#include "Engine/Engine.h"
|
#include "Engine/Engine.h"
|
||||||
#include "hidapi.h"
|
|
||||||
|
DEFINE_LOG_CATEGORY(LogHIDApi);
|
||||||
|
|
||||||
|
|
||||||
void USkyPortalSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
void USkyPortalSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
||||||
{
|
{
|
||||||
|
|
||||||
Super::Initialize(Collection);
|
Super::Initialize(Collection);
|
||||||
// Custom initialization logic
|
// Custom initialization logic
|
||||||
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Initialized"));
|
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Initialized"));
|
||||||
|
@ -12,19 +17,104 @@ void USkyPortalSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
||||||
int res = hid_init();
|
int res = hid_init();
|
||||||
if (res == 0)
|
if (res == 0)
|
||||||
{
|
{
|
||||||
UE_LOG(LogTemp, Log, TEXT("HIDAPI initialized successfully."));
|
UE_LOG(LogHIDApi, Log, TEXT("HIDAPI initialized successfully."));
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
UE_LOG(LogTemp, Error, TEXT("Failed to initialize HIDAPI."));
|
UE_LOG(LogHIDApi, Error, TEXT("Failed to initialize HIDAPI."));
|
||||||
|
HidError = hid_error(NULL);
|
||||||
|
UE_LOG(LogHIDApi, Error, TEXT("%s"), *HidError);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void USkyPortalSubsystem::Deinitialize()
|
void USkyPortalSubsystem::Deinitialize()
|
||||||
{
|
{
|
||||||
// Custom cleanup logic
|
// Disconnect portal
|
||||||
|
if (PortalDevice) {
|
||||||
|
hid_close(PortalDevice);
|
||||||
|
}
|
||||||
|
hid_exit();
|
||||||
|
|
||||||
|
|
||||||
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Deinitialized"));
|
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Deinitialized"));
|
||||||
Super::Deinitialize();
|
Super::Deinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Number of endpoints : 2
|
||||||
|
found an IN End Point 0 with attributes interrupt and address 0x1
|
||||||
|
found an OUT End Point 1 with attributes interrupt and address 0x1
|
||||||
|
*/
|
||||||
|
bool USkyPortalSubsystem::ConnectPortal() {
|
||||||
|
/*
|
||||||
|
Declare two pointers to hold information about HID devices.
|
||||||
|
"list" will point to the head of the linked list of devices,
|
||||||
|
"attributes" will be used to iterate through the list.
|
||||||
|
*/
|
||||||
|
struct hid_device_info* list, * attributes;
|
||||||
|
|
||||||
|
list = hid_enumerate(0x0, 0x0);
|
||||||
|
// If `list` is NULL, that means no devices were found or there was an error.
|
||||||
|
// In this case, print an error message and terminate the program.
|
||||||
|
if (!list) {
|
||||||
|
UE_LOG(LogHIDApi, Error, TEXT("No devices found"));
|
||||||
|
// Get the error message from the HIDAPI
|
||||||
|
HidError = hid_error(NULL);
|
||||||
|
UE_LOG(LogHIDApi, Error, TEXT("%s"), *HidError);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
attributes = list;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Iterate through the linked list of devices
|
||||||
|
int vendorCount = sizeof(VendorIds) / sizeof(VendorIds[0]);
|
||||||
|
int productCount = sizeof(ProductIds) / sizeof(ProductIds[0]);
|
||||||
|
|
||||||
|
while (attributes) {
|
||||||
|
// Check if the devices match any of vendor_id and product_id
|
||||||
|
for (int i = 0; i < vendorCount; i++) {
|
||||||
|
for (int j = 0; j < productCount; j++) {
|
||||||
|
if (attributes->vendor_id == VendorIds[i] && attributes->product_id == ProductIds[j]) {
|
||||||
|
UE_LOG(LogHIDApi, Display, TEXT("Portal found"));
|
||||||
|
UE_LOG(LogHIDApi, Log, TEXT("Vendor ID: 0x%x, Product ID: 0x%x"), attributes->vendor_id, attributes->product_id);
|
||||||
|
|
||||||
|
PortalDevice = hid_open(attributes->vendor_id, attributes->product_id, NULL);
|
||||||
|
if (PortalDevice) {
|
||||||
|
UE_LOG(LogHIDApi, Display, TEXT("Successful connection to Portal."));
|
||||||
|
|
||||||
|
// Free the device list
|
||||||
|
hid_free_enumeration(list);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move to the next device in the list
|
||||||
|
attributes = attributes->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free the device list
|
||||||
|
hid_free_enumeration(list);
|
||||||
|
UE_LOG(LogHIDApi, Error, TEXT("No Portals found"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void USkyPortalSubsystem::ReadPortal() {
|
||||||
|
if (PortalDevice) {
|
||||||
|
hid_read(PortalDevice, PortalRawData, 16);
|
||||||
|
//RawDataArray.Append(PortalRawData, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,17 @@
|
||||||
|
|
||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "Subsystems/EngineSubsystem.h"
|
#include "Subsystems/EngineSubsystem.h"
|
||||||
|
#include "hidapi.h"
|
||||||
|
|
||||||
|
|
||||||
#include "SkyPortalSubsystem.generated.h"
|
#include "SkyPortalSubsystem.generated.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* Macro Definitions */
|
||||||
|
#define TIMEOUT 30000
|
||||||
|
DECLARE_LOG_CATEGORY_EXTERN(LogHIDApi, Log, All);
|
||||||
|
|
||||||
|
/* Subsystem */
|
||||||
UCLASS()
|
UCLASS()
|
||||||
class SKYPORTAL_API USkyPortalSubsystem : public UEngineSubsystem
|
class SKYPORTAL_API USkyPortalSubsystem : public UEngineSubsystem
|
||||||
{
|
{
|
||||||
|
@ -13,4 +22,30 @@ public:
|
||||||
// Override initialization and deinitialization methods
|
// Override initialization and deinitialization methods
|
||||||
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
|
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
|
||||||
virtual void Deinitialize() override;
|
virtual void Deinitialize() override;
|
||||||
|
|
||||||
|
// Connect to Portal, return false if portal is not found
|
||||||
|
UFUNCTION(BlueprintCallable, CallInEditor)
|
||||||
|
bool ConnectPortal();
|
||||||
|
|
||||||
|
FString HidError;
|
||||||
|
|
||||||
|
private:
|
||||||
|
//Portal ref used in the subsystem
|
||||||
|
hid_device* PortalDevice;
|
||||||
|
|
||||||
|
unsigned char* PortalRawData;
|
||||||
|
|
||||||
|
FString RawData();
|
||||||
|
void ReadPortal();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
//Constants
|
||||||
|
const int VendorIds[4] = { 0x12ba, 0x54c, 0x1430, 0x1430 };
|
||||||
|
const int ProductIds[4] = { 0x150, 0x967, 0x1f17 };
|
||||||
|
|
||||||
|
/////Defaults values, should not be used
|
||||||
|
const int VendorId = 5168;
|
||||||
|
const int ProductId = 336;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue