Minor fixes on HidDevice.Corrected print on error, added leading zeros and fixed nonblocking write option
This commit is contained in:
@@ -2,24 +2,22 @@
|
|||||||
#include "HidDevice.h"
|
#include "HidDevice.h"
|
||||||
#include "RageLog.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 },
|
vid{ vid },
|
||||||
pid{ pid },
|
pid{ pid },
|
||||||
interfaceNum{ interfaceNum },
|
interfaceNum{ interfaceNum },
|
||||||
autoReconnection{autoReconnection}
|
autoReconnection{autoReconnection},
|
||||||
|
nonBlockingWrite{nonBlockingWrite}
|
||||||
{
|
{
|
||||||
bool result = TryConnect();
|
bool result = TryConnect();
|
||||||
|
|
||||||
if (!result) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
foundOnce = true;
|
foundOnce = true;
|
||||||
hid_set_nonblocking(handle, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HidDevice::~HidDevice()
|
HidDevice::~HidDevice()
|
||||||
@@ -38,6 +36,9 @@ bool HidDevice::Open(const char* path)
|
|||||||
{
|
{
|
||||||
handle = hid_open_path(path);
|
handle = hid_open_path(path);
|
||||||
|
|
||||||
|
if(nonBlockingWrite)
|
||||||
|
hid_set_nonblocking(handle, 1);
|
||||||
|
|
||||||
return handle != nullptr;
|
return handle != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +72,11 @@ bool HidDevice::IsConnected() {
|
|||||||
return handle != nullptr;
|
return handle != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HidDevice::FoundOnce()
|
||||||
|
{
|
||||||
|
return foundOnce;
|
||||||
|
}
|
||||||
|
|
||||||
char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
|
char* HidDevice::GetPath(int vid, int pid, int interfaceNumber)
|
||||||
{
|
{
|
||||||
struct hid_device_info* devs, * cur_dev;
|
struct hid_device_info* devs, * cur_dev;
|
||||||
@@ -113,7 +119,7 @@ void HidDevice::Read(unsigned char* data, size_t length)
|
|||||||
|
|
||||||
if (result == -1)
|
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)
|
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();
|
Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ private:
|
|||||||
hid_device* handle{nullptr};
|
hid_device* handle{nullptr};
|
||||||
|
|
||||||
bool autoReconnection = true;
|
bool autoReconnection = true;
|
||||||
|
bool nonBlockingWrite;
|
||||||
int vid;
|
int vid;
|
||||||
int pid;
|
int pid;
|
||||||
int interfaceNum = -1;
|
int interfaceNum = -1;
|
||||||
@@ -23,11 +23,12 @@ private:
|
|||||||
public:
|
public:
|
||||||
static char* GetPath(int vid, int pid, int interfaceNum = -1);
|
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();
|
virtual ~HidDevice();
|
||||||
|
|
||||||
bool IsConnected();
|
bool IsConnected();
|
||||||
|
bool FoundOnce();
|
||||||
|
|
||||||
void Read(unsigned char* data, size_t length);
|
void Read(unsigned char* data, size_t length);
|
||||||
void Write(const unsigned char* data, size_t length);
|
void Write(const unsigned char* data, size_t length);
|
||||||
|
|||||||
Reference in New Issue
Block a user