Minor fixes on HidDevice.Corrected print on error, added leading zeros and fixed nonblocking write option

This commit is contained in:
DANDO\Aless
2025-03-27 22:36:29 -07:00
committed by teejusb
parent 35714a0a04
commit db3543b507
2 changed files with 18 additions and 11 deletions
+14 -8
View File
@@ -2,24 +2,22 @@
#include "HidDevice.h"
#include "RageLog.h"
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection) :
HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) :
vid{ vid },
pid{ pid },
interfaceNum{ interfaceNum },
autoReconnection{autoReconnection}
autoReconnection{autoReconnection},
nonBlockingWrite{nonBlockingWrite}
{
bool result = TryConnect();
if (!result) {
LOG->Warn("HID device with VID/PID %x/%x not found.", vid, pid);
LOG->Warn("HID device with VID/PID %04x/%04x not found.", vid, pid);
return;
}
else
{
foundOnce = true;
hid_set_nonblocking(handle, 1);
}
}
HidDevice::~HidDevice()
@@ -38,6 +36,9 @@ bool HidDevice::Open(const char* path)
{
handle = hid_open_path(path);
if(nonBlockingWrite)
hid_set_nonblocking(handle, 1);
return handle != nullptr;
}
@@ -71,6 +72,11 @@ bool HidDevice::IsConnected() {
return handle != nullptr;
}
bool HidDevice::FoundOnce()
{
return foundOnce;
}
char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
{
struct hid_device_info* devs, * cur_dev;
@@ -113,7 +119,7 @@ void HidDevice::Read(unsigned char* data, size_t length)
if (result == -1)
{
LOG->Warn("HID device with VID/PID %x/%x read failed. Fail reason %s", vid, pid, GetError());
LOG->Warn("HID device with VID/PID %04x/%04x read failed. Fail reason %ls", vid, pid, GetError());
}
}
@@ -126,7 +132,7 @@ void HidDevice::Write(const unsigned char* data, size_t length)
if (result != length)
{
LOG->Warn("HID device with VID/PID %x/%x write failed. Fail reason %s", vid, pid, GetError());
LOG->Warn("HID device with VID/PID %04x/%04x write failed. Fail reason %ls", vid, pid, GetError());
Close();
}
}
+4 -3
View File
@@ -9,7 +9,7 @@ private:
hid_device* handle{nullptr};
bool autoReconnection = true;
bool nonBlockingWrite;
int vid;
int pid;
int interfaceNum = -1;
@@ -23,12 +23,13 @@ private:
public:
static char* GetPath(int vid, int pid, int interfaceNum = -1);
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true);
HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false);
virtual ~HidDevice();
bool IsConnected();
bool FoundOnce();
void Read(unsigned char* data, size_t length);
void Write(const unsigned char* data, size_t length);
};