Added hid device class as an arch utils

This commit is contained in:
dando92
2025-03-18 17:40:39 +01:00
committed by teejusb
parent 1e62bec650
commit 3cbae013b8
3 changed files with 86 additions and 0 deletions
+3
View File
@@ -1,3 +1,6 @@
list(APPEND SMDATA_OS_SRC "archutils/Common/HidDevice.cpp")
list(APPEND SMDATA_OS_HPP "archutils/Common/HidDevice.h")
if(APPLE)
list(APPEND SMDATA_OS_DARWIN_SRC
"archutils/Darwin/Crash.mm"
+58
View File
@@ -0,0 +1,58 @@
#include "global.h"
#include "HidDevice.h"
#include "RageLog.h"
HidDevice::HidDevice(int vid, int pid) :vid(vid), pid(pid)
{
bool result = TryConnect();
if (!result) {
LOG->Warn("HID device with VID/PID %x/%x not found.", vid, pid);
hid_exit();
return;
}
else
{
hid_set_nonblocking(handle, 1);
foundOnce = true;
}
}
HidDevice::~HidDevice()
{
if (handle)
hid_close(handle);
hid_exit();
}
bool HidDevice::TryConnect()
{
handle = hid_open(vid, pid, NULL);
return handle != NULL;
}
bool HidDevice::IsConnected() {
if (!handle && foundOnce)
return TryConnect();
return handle != NULL;
}
void HidDevice::Read(unsigned char* data, size_t length)
{
if (!IsConnected())
return;
hid_read(handle, data, length);
}
void HidDevice::Write(const unsigned char* data, size_t length)
{
if (!IsConnected())
return;
hid_write(handle, data, length);
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef HidDevice_H
#define HidDevice_H
#include "hidapi.h"
class HidDevice
{
private:
hid_device* handle;
bool foundOnce = false;
int vid;
int pid;
bool TryConnect();
bool IsConnected();
public:
HidDevice(int vid, int pid);
virtual ~HidDevice();
void Read(unsigned char* data, size_t length);
void Write(const unsigned char* data, size_t length);
};
#endif