dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
rawhid.h
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #ifndef RAWHID_H
29 #define RAWHID_H
30 
31 #include "rawhid_global.h"
32 
33 #include <QByteArray>
34 #include <QIODevice>
35 #include <QMutex>
36 #include <QThread>
37 #include <QWaitCondition>
38 
39 #include <coreplugin/iconnection.h>
40 
41 #include "hidapi/hidapi.h"
42 #include "usbmonitor.h"
43 #include "usbdevice.h"
44 
45 //#define RAW_HID_DEBUG
46 #ifdef RAW_HID_DEBUG
47 #define RAW_HID_QXTLOG_DEBUG(...) qDebug() << __VA_ARGS__
48 #else // RAW_HID_DEBUG
49 #define RAW_HID_QXTLOG_DEBUG(...)
50 #endif // RAW_HID_DEBUG
51 
55 class RawHIDReadThread : public QThread
56 {
57  Q_OBJECT
58 
59 public:
60  RawHIDReadThread(hid_device *device);
61  virtual ~RawHIDReadThread();
62 
64  int getReadData(char *data, int size);
65 
67  qint64 getBytesAvailable();
68 
69  void stop() { m_running = false; }
70 
71 signals:
72  void readyToRead();
73 
74 protected:
75  void run();
76 
79  QByteArray m_readBuffer;
80 
82  QMutex m_readBufMtx;
83 
84  hid_device *m_handle;
85 
86  bool m_running;
87 };
88 
89 // *********************************************************************************
90 
94 class RawHIDWriteThread : public QThread
95 {
96  Q_OBJECT
97 
98 public:
99  RawHIDWriteThread(hid_device *device);
100  virtual ~RawHIDWriteThread();
101 
103  int pushDataToWrite(const char *data, int size);
104 
106  qint64 getBytesToWrite();
107 
108  void stop();
109 
110 protected:
111  void run();
112 
115  QByteArray m_writeBuffer;
116 
119 
121  QWaitCondition m_newDataToWrite;
122 
123  hid_device *m_handle;
124 
125  bool m_running;
126 };
127 
128 // *********************************************************************************
129 
134 class RAWHID_EXPORT RawHID : public QIODevice
135 {
136  Q_OBJECT
137 
138 public:
139  RawHID();
140  RawHID(USBDevice *deviceName);
141  virtual ~RawHID();
142 
143  virtual bool open(OpenMode mode);
144  virtual void close();
145  virtual bool isSequential() const;
146 
147 private slots:
148  void sendReadyRead();
149 
150 protected:
151  virtual qint64 readData(char *data, qint64 maxSize);
152  virtual qint64 writeData(const char *data, qint64 maxSize);
153  virtual qint64 bytesAvailable() const;
154  virtual qint64 bytesToWrite() const;
155 
157  hid_device *m_handle;
158 
161 };
162 
163 #endif // RAWHID_H
void stop()
Definition: rawhid.h:69
RawHIDReadThread * m_readThread
Definition: rawhid.h:159
RawHIDReadThread(hid_device *device)
Definition: rawhid.cpp:48
QByteArray m_writeBuffer
Definition: rawhid.h:115
virtual ~RawHIDReadThread()
Definition: rawhid.cpp:54
void stop()
Tell the thread to stop and make sure it wakes up immediately.
Definition: rawhid.cpp:196
QMutex m_writeBufMtx
Definition: rawhid.h:118
hid_device * m_handle
Definition: rawhid.h:84
DataFields data
RawHIDWriteThread(hid_device *device)
Definition: rawhid.cpp:121
Definition: rawhid.h:134
QByteArray m_readBuffer
Definition: rawhid.h:79
QMutex m_readBufMtx
Definition: rawhid.h:82
int pushDataToWrite(const char *data, int size)
Definition: rawhid.cpp:205
qint64 getBytesAvailable()
Definition: rawhid.cpp:113
hid_device * m_handle
Definition: rawhid.h:123
bool m_running
Definition: rawhid.h:86
virtual ~RawHIDWriteThread()
Definition: rawhid.cpp:127
RawHIDWriteThread * m_writeThread
Definition: rawhid.h:160
hid_device * m_handle
Definition: rawhid.h:157
int getReadData(char *data, int size)
Definition: rawhid.cpp:101
qint64 getBytesToWrite()
Definition: rawhid.cpp:215
USBDevice * m_deviceInfo
Definition: rawhid.h:156
QWaitCondition m_newDataToWrite
Definition: rawhid.h:121