sl@0
|
1 |
//
|
sl@0
|
2 |
//
|
sl@0
|
3 |
//
|
sl@0
|
4 |
|
sl@0
|
5 |
#include "HidDevice.h"
|
sl@0
|
6 |
|
sl@0
|
7 |
|
sl@0
|
8 |
|
sl@0
|
9 |
//
|
sl@0
|
10 |
// class HidDevice
|
sl@0
|
11 |
//
|
sl@0
|
12 |
|
sl@0
|
13 |
HidDevice::HidDevice():iHidDevice(NULL)
|
sl@0
|
14 |
{
|
sl@0
|
15 |
Close();
|
sl@0
|
16 |
}
|
sl@0
|
17 |
|
sl@0
|
18 |
/**
|
sl@0
|
19 |
*/
|
sl@0
|
20 |
int HidDevice::Open(const char* aPath)
|
sl@0
|
21 |
{
|
sl@0
|
22 |
Close();
|
sl@0
|
23 |
|
sl@0
|
24 |
iHidDevice = hid_open_path(aPath);
|
sl@0
|
25 |
|
sl@0
|
26 |
if (!iHidDevice)
|
sl@0
|
27 |
{
|
sl@0
|
28 |
//Fail to connect our device
|
sl@0
|
29 |
return 0;
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
FetchStrings();
|
sl@0
|
33 |
|
sl@0
|
34 |
return 1;
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
/**
|
sl@0
|
38 |
See hidapi documentation.
|
sl@0
|
39 |
*/
|
sl@0
|
40 |
int HidDevice::Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber)
|
sl@0
|
41 |
{
|
sl@0
|
42 |
Close();
|
sl@0
|
43 |
|
sl@0
|
44 |
iHidDevice = hid_open(aVendorId, aProductId, aSerialNumber);
|
sl@0
|
45 |
|
sl@0
|
46 |
if (!iHidDevice)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
//Fail to connect our device
|
sl@0
|
49 |
return 0;
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
FetchStrings();
|
sl@0
|
53 |
|
sl@0
|
54 |
return 1;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
/**
|
sl@0
|
58 |
*/
|
sl@0
|
59 |
void HidDevice::Close()
|
sl@0
|
60 |
{
|
sl@0
|
61 |
hid_close(iHidDevice);
|
sl@0
|
62 |
iHidDevice=NULL;
|
sl@0
|
63 |
//
|
sl@0
|
64 |
memset(iVendor,0,sizeof(iVendor));
|
sl@0
|
65 |
memset(iProduct,0,sizeof(iProduct));
|
sl@0
|
66 |
memset(iSerialNumber,0,sizeof(iSerialNumber));
|
sl@0
|
67 |
}
|
sl@0
|
68 |
|
sl@0
|
69 |
/**
|
sl@0
|
70 |
*/
|
sl@0
|
71 |
bool HidDevice::IsOpen()
|
sl@0
|
72 |
{
|
sl@0
|
73 |
return iHidDevice!=NULL;
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
|
sl@0
|
77 |
/**
|
sl@0
|
78 |
*/
|
sl@0
|
79 |
const wchar_t* HidDevice::Error()
|
sl@0
|
80 |
{
|
sl@0
|
81 |
return hid_error(iHidDevice);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
/**
|
sl@0
|
85 |
*/
|
sl@0
|
86 |
int HidDevice::SetNonBlocking(int aNonBlocking)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
//Success we are now connected to our HID device
|
sl@0
|
89 |
//Set read operation as non blocking
|
sl@0
|
90 |
return hid_set_nonblocking(iHidDevice, aNonBlocking);
|
sl@0
|
91 |
}
|
sl@0
|
92 |
|
sl@0
|
93 |
/**
|
sl@0
|
94 |
*/
|
sl@0
|
95 |
wchar_t* HidDevice::Vendor()
|
sl@0
|
96 |
{
|
sl@0
|
97 |
return iVendor;
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
/**
|
sl@0
|
101 |
*/
|
sl@0
|
102 |
wchar_t* HidDevice::Product()
|
sl@0
|
103 |
{
|
sl@0
|
104 |
return iProduct;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
|
sl@0
|
107 |
/**
|
sl@0
|
108 |
*/
|
sl@0
|
109 |
wchar_t* HidDevice::SerialNumber()
|
sl@0
|
110 |
{
|
sl@0
|
111 |
return iSerialNumber;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
|
sl@0
|
114 |
/**
|
sl@0
|
115 |
|
sl@0
|
116 |
*/
|
sl@0
|
117 |
void HidDevice::FetchStrings()
|
sl@0
|
118 |
{
|
sl@0
|
119 |
hid_get_manufacturer_string(iHidDevice,iVendor,sizeof(iVendor));
|
sl@0
|
120 |
hid_get_product_string(iHidDevice,iProduct,sizeof(iProduct));
|
sl@0
|
121 |
hid_get_serial_number_string(iHidDevice,iSerialNumber,sizeof(iSerialNumber));
|
sl@0
|
122 |
}
|