Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QByteArray>
- #include <QDebug>
- #include "device-io.h"
- #define PHOEBETRIA_HID_INPUT_BUFFSIZE 255
- DeviceIO::DeviceIO(QObject *parent) :
- QObject(parent)
- {
- m_device = NULL;
- hid_init();
- }
- bool DeviceIO::connect(unsigned short vendorId, unsigned short productId)
- {
- struct hid_device_info *devs, *cur_dev;
- devs = hid_enumerate(vendorId, productId);
- cur_dev = devs;
- while (cur_dev) {
- printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
- cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
- printf("\n");
- printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
- printf(" Product: %ls\n", cur_dev->product_string);
- printf("\n");
- cur_dev = cur_dev->next;
- }
- hid_free_enumeration(devs);
- m_device = hid_open(vendorId, productId, NULL);
- if (m_device) hid_set_nonblocking(m_device, 1); // set non-blocking
- return m_device != NULL;
- }
- void DeviceIO::disconnect(void)
- {
- if (m_device == NULL) return;
- hid_close(m_device);
- m_device = NULL;
- }
- bool DeviceIO::isConnected(void) const
- {
- return m_device != NULL;
- }
- int DeviceIO::sendData(const unsigned char* data, int len)
- {
- return hid_write(m_device, data, len);
- }
- QString DeviceIO::lastErrorString(void) const
- {
- QString err;
- err.fromWCharArray(hid_error(m_device));
- return err;
- }
- void DeviceIO::setBlocking(bool block)
- {
- hid_set_nonblocking(m_device, block ? 0 : 1);
- }
- void DeviceIO::pollForData(void)
- {
- unsigned char buff[PHOEBETRIA_HID_INPUT_BUFFSIZE];
- int bytesRead;
- if (!m_device) return;
- while ( (bytesRead = hid_read(m_device, buff, sizeof(buff))) > 0 )
- {
- //rawdata.fromRawData((const char*) buff, bytesRead);
- QByteArray rawdata;
- for (int i = 0; i < bytesRead; i++)
- {
- rawdata.append(buff[i]);
- }
- emit dataRX(rawdata);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement