-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqhidevice.cpp
More file actions
70 lines (60 loc) · 2.11 KB
/
Copy pathqhidevice.cpp
File metadata and controls
70 lines (60 loc) · 2.11 KB
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
#include "qhidevice.h"
#include <cassert>
#include <QDebug>
QHIDevice::QHIDevice(hid_device *dev, QObject *parent) : QObject(parent), _device(dev) {
if (_device == nullptr) return;
hid_set_nonblocking(_device,true);
poll.setInterval(100);
_timeout.setSingleShot(true);
connect(&_timeout,&QTimer::timeout,[this] () { poll.stop(); emit timeout(); });
connect(&poll,&QTimer::timeout, [this] () {
unsigned char* buf = new unsigned char[expectData];
int res = hid_read(_device,buf,expectData);
if (res == -1) {
qDebug() << "ERR!";
//TODO What to do on error?
}
recvBuf.append(reinterpret_cast<char*>(buf),res);
expectData -= res;
if (expectData == 0) {
poll.stop();
_timeout.stop();
emit receivedData(recvBuf);
}
delete[] buf;
});
}
QHIDevice::QHIDevice(QString path, QObject *parent) :
QHIDevice::QHIDevice(hid_open_path(path.toStdString().c_str()),parent)
{}
QHIDevice::QHIDevice(quint16 vendor_id, quint16 product_id, QObject *parent) :
QHIDevice(hid_open(vendor_id,product_id,nullptr),parent)
{}
QHIDevice::QHIDevice(quint16 vendor_id, quint16 product_id, const QString &serial_number, QObject *parent) :
QHIDevice(hid_open(vendor_id,product_id, serial_number.toStdWString().c_str()),parent)
{}
bool QHIDevice::good() {
return _device != nullptr;
}
int QHIDevice::write(const QByteArray &data) {
if (_device == nullptr) return -1;
return hid_write(_device,reinterpret_cast<const unsigned char*>(data.constData()),data.size());
}
int QHIDevice::sendReport(const QByteArray &data) {
if (_device != nullptr) return -1;
return hid_send_feature_report(_device,reinterpret_cast<const unsigned char*>(data.constData()),data.size());
}
void QHIDevice::getReport(size_t length, int timeout_ms) {
if (_device == nullptr) return;
_timeout.start(timeout_ms);
expectData = length;
recvBuf.clear();
poll.start();
}
void QHIDevice::read(size_t length, int timeout_ms) {
if (_device == nullptr) return;
_timeout.start(timeout_ms);
expectData = length;
recvBuf.clear();
poll.start();
}