fix writing!!

This commit is contained in:
Lucas 2024-09-21 17:12:02 +02:00
parent a148964f31
commit 57bbaaa033

View file

@ -64,11 +64,6 @@ void USkyPortalSubsystem::RestartPortal()
memset(req.buf, 0, rw_buf_size);
req.buf[1] = 'R';
// Wait until the device is ready
while (PortalStatus() == 0) {
FPlatformProcess::Sleep(0.05f); // Sleep for 50ms and try again
}
do { Write(&req); } while (CheckResponse(&res, 'R'));
}
@ -148,9 +143,9 @@ bool USkyPortalSubsystem::OpenPortalHandle() {
void USkyPortalSubsystem::ChangePortalColor(const FLinearColor& Color)
{
unsigned char r = Color.R;
unsigned char g = Color.G;
unsigned char b = Color.B;
unsigned char r = FMath::Clamp(Color.R * 100, 0.0f, 100.0f);
unsigned char g = FMath::Clamp(Color.G * 100, 0.0f, 100.0f);
unsigned char b = FMath::Clamp(Color.B * 100, 0.0f, 100.0f):
RWBlock req;
@ -164,6 +159,51 @@ void USkyPortalSubsystem::ChangePortalColor(const FLinearColor& Color)
Write(&req);
}
#if PLATFORM_WINDOWS
#include <Windows.h>
#define HID_CTL_CODE(id) \
CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_NEITHER, FILE_ANY_ACCESS)
#define HID_IN_CTL_CODE(id) \
CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_IN_DIRECT, FILE_ANY_ACCESS)
#define IOCTL_HID_SET_OUTPUT_REPORT HID_IN_CTL_CODE(101)
struct hid_device_ {
HANDLE device_handle;
BOOL blocking;
USHORT output_report_length;
unsigned char* write_buf;
size_t input_report_length;
USHORT feature_report_length;
unsigned char* feature_buf;
wchar_t* last_error_str;
BOOL read_pending;
char* read_buf;
OVERLAPPED ol;
OVERLAPPED write_ol;
struct hid_device_info* device_info;
};
void USkyPortalSubsystem::Write(RWBlock* pb)
{
BOOL res;
OVERLAPPED ol;
memset(&ol, 0, sizeof(ol));
DWORD bytes_returned;
res = DeviceIoControl(PortalDevice->device_handle,
IOCTL_HID_SET_OUTPUT_REPORT,
(unsigned char*)pb->buf, 0x21,
(unsigned char*)pb->buf, 0x21,
&bytes_returned, &ol);
ensureMsgf(res, TEXT("Unable to write to Portal, %s"), hid_error(PortalDevice));
}
#else
void USkyPortalSubsystem::Write(RWBlock *pb) {
if (!ensure(PortalDevice)) {
@ -174,19 +214,10 @@ void USkyPortalSubsystem::Write(RWBlock *pb) {
pb->buf[0] = 0; // Use report 0
for (int attempt = 0; attempt < 10; attempt++) {
if (hid_write(PortalDevice, pb->buf, 0x21) == -1) {
UE_LOG(LogSkyportalIO, Warning, TEXT("Attempt %d: Failed to write to Portal, retrying..."), attempt + 1);
FPlatformProcess::Sleep(0.1f);
}
else {
UE_LOG(LogSkyportalIO, Verbose, TEXT("Successfully wrote to the Portal on attempt %d."), attempt + 1);
return;
}
}
//ensureMsgf(hid_write(PortalDevice, pb->buf, 0x21) != -1, TEXT("Unable to write to Portal, %s"), hid_error(PortalDevice));
ensureMsgf(hid_write(PortalDevice, pb->buf, 0x21) != -1, TEXT("Unable to write to Portal, %s"), hid_error(PortalDevice));
UE_LOG(LogSkyportalIO, Error, TEXT("Unable to write to Portal after multiple attempts. error:\n %s"), hid_error(PortalDevice));
}
#endif
void USkyPortalSubsystem::Sleep(int sleepMs) {
@ -330,6 +361,8 @@ bool USkyPortalSubsystem::ConnectPortal()
if (bPortalConnected) {
RestartPortal();
ActivatePortal(1);
Sleep(500);
ChangePortalColor(FLinearColor(0xC8, 0xC8, 0xC8));
UE_LOG(LogSkyportalIO, Log, TEXT("Portal Status: %d\n"), PortalStatus());