Advertisement
Guest User

Untitled

a guest
Dec 18th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <QByteArray>
  2. #include <QDebug>
  3. #include "device-io.h"
  4.  
  5. #define PHOEBETRIA_HID_INPUT_BUFFSIZE 255
  6.  
  7. DeviceIO::DeviceIO(QObject *parent) :
  8.     QObject(parent)
  9. {
  10.     m_device = NULL;
  11.     hid_init();
  12. }
  13.  
  14. bool DeviceIO::connect(unsigned short vendorId, unsigned short productId)
  15. {
  16.     struct hid_device_info *devs, *cur_dev;
  17.    
  18.     devs = hid_enumerate(vendorId, productId);
  19.     cur_dev = devs;
  20.     while (cur_dev) {
  21.         printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
  22.             cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
  23.         printf("\n");
  24.         printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
  25.         printf("  Product:      %ls\n", cur_dev->product_string);
  26.         printf("\n");
  27.         cur_dev = cur_dev->next;
  28.     }
  29.     hid_free_enumeration(devs);
  30.     m_device = hid_open(vendorId, productId, NULL);
  31.     if (m_device) hid_set_nonblocking(m_device, 1); // set non-blocking
  32.  
  33.     return m_device != NULL;
  34. }
  35.  
  36. void DeviceIO::disconnect(void)
  37. {
  38.     if (m_device == NULL) return;
  39.     hid_close(m_device);
  40.     m_device = NULL;
  41. }
  42.  
  43. bool DeviceIO::isConnected(void) const
  44. {
  45.     return m_device != NULL;
  46. }
  47.  
  48. int DeviceIO::sendData(const unsigned char* data, int len)
  49. {
  50.     return hid_write(m_device, data, len);
  51. }
  52.  
  53. QString DeviceIO::lastErrorString(void) const
  54. {
  55.     QString err;
  56.  
  57.     err.fromWCharArray(hid_error(m_device));
  58.  
  59.     return err;
  60. }
  61.  
  62. void DeviceIO::setBlocking(bool block)
  63. {
  64.     hid_set_nonblocking(m_device, block ? 0 : 1);
  65. }
  66.  
  67. void DeviceIO::pollForData(void)
  68. {
  69.     unsigned char buff[PHOEBETRIA_HID_INPUT_BUFFSIZE];
  70.     int bytesRead;
  71.  
  72.     if (!m_device) return;
  73.  
  74.     while ( (bytesRead = hid_read(m_device, buff, sizeof(buff))) > 0 )
  75.     {
  76.         //rawdata.fromRawData((const char*) buff, bytesRead);
  77.         QByteArray rawdata;
  78.         for (int i = 0; i < bytesRead; i++)
  79.         {
  80.             rawdata.append(buff[i]);
  81.         }
  82.         emit dataRX(rawdata);
  83.     }
  84.  
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement