-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhid.cpp
76 lines (65 loc) · 1.89 KB
/
hid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "hid.h"
#include <QDebug>
HID::HID() : _vid(0xf055), _pid(0x1234), _serial(nullptr), _connectedDevice(nullptr) {
this->start();
}
QMap<QString, QString> HID::getCompatibleDevices() {
QMap<QString, QString> out;
struct hid_device_info *devices = nullptr;
struct hid_device_info *cur_dev = nullptr;
devices = hid_enumerate(_vid, _pid);
cur_dev = devices;
while (cur_dev) {
QString description = QString::fromWCharArray(cur_dev->manufacturer_string);
description.append(" ");
description.append(QString::fromWCharArray(cur_dev->product_string));
description.append(" (");
description.append(QString::fromWCharArray(cur_dev->serial_number));
description.append(")");
QString path = cur_dev->path;
out.insert(path, description);
cur_dev = cur_dev->next;
}
hid_free_enumeration(devices);
return out;
}
void HID::connectToPath(QString &path) {
if (_connectedDevice != nullptr) {
hid_close(_connectedDevice);
}
if (path == "") {
_connectedDevice = nullptr;
return;
}
_connectedDevice = hid_open_path(path.toLatin1().data());
if (!_connectedDevice) {
_connectedDevice = nullptr;
qDebug() << "Error connecting to device " << path;
} else {
qDebug() << "Connected to " << path;
}
}
void HID::setVidPid(uint16_t vid, uint16_t pid, wchar_t *serial) {
_vid = vid;
_pid = pid;
_serial = serial;
}
void HID::run() {
_running = true;
while (_running) {
if (_connectedDevice == nullptr) {
msleep(500);
continue;
}
uint8_t data[1024];
int res = hid_read_timeout(_connectedDevice, data, 1024, 500);
if (res > 0) {
emit packetReceived(data, res);
}
}
}
void HID::cleanup() {
_running = false;
terminate();
msleep(100);
}