direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
QKinect.h
Go to the documentation of this file.
1 /***************************************************************************
2  * This file is part of the OpenKinect Project. http://www.openkinect.org *
3  * *
4  * Copyright (c) 2010 individual OpenKinect contributors. *
5  * See the CONTRIB file for details. *
6  * *
7  * This file is part of direcs. *
8  * www.direcs.de *
9  * *
10  * direcs is free software: you can redistribute it and/or modify it *
11  * under the terms of the GNU General Public License as published *
12  * by the Free Software Foundation, version 3 of the License. *
13  * *
14  * direcs is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
21  * *
22  ***************************************************************************/
23 
24 //----------------------------------------------------------------------------------------------------------------------
28 //----------------------------------------------------------------------------------------------------------------------
29 
30 #ifndef __QKINECT_H__
31 #define __QKINECT_H__
32 
33 #include <QObject>
34 #include <QThread>
35 #include <stdexcept>
36 #include <QMutex>
37 #include <QMutexLocker>
38 #include <vector>
39 #include <libfreenect/libfreenect.h>
40 
41 //----------------------------------------------------------------------------------------------------------------------
48 //----------------------------------------------------------------------------------------------------------------------
49 
50 class QKinectProcessEvents : public QThread
51 {
52 public :
53  //----------------------------------------------------------------------------------------------------------------------
56  //----------------------------------------------------------------------------------------------------------------------
57  inline QKinectProcessEvents(freenect_context *_ctx){m_ctx=_ctx;}
58 
59  //----------------------------------------------------------------------------------------------------------------------
62  //----------------------------------------------------------------------------------------------------------------------
63  inline void setActive(){m_active=true;}
64 
65  //----------------------------------------------------------------------------------------------------------------------
68  //----------------------------------------------------------------------------------------------------------------------
69  inline void setInActive(){m_active=false;}
70 
71 
72 protected :
73  //----------------------------------------------------------------------------------------------------------------------
76  //----------------------------------------------------------------------------------------------------------------------
77  void run();
78 
79 
80 private :
81  //----------------------------------------------------------------------------------------------------------------------
84  //----------------------------------------------------------------------------------------------------------------------
85  bool m_active;
86  //----------------------------------------------------------------------------------------------------------------------
89  //----------------------------------------------------------------------------------------------------------------------
90  freenect_context *m_ctx;
91 
92 };
93 
94 //----------------------------------------------------------------------------------------------------------------------
103 //----------------------------------------------------------------------------------------------------------------------
104 
105 class QKinect : public QObject
106 {
107 // must include this macro so we inherit all the core QT features such as singnals and slots
108 Q_OBJECT
109 public :
110  //----------------------------------------------------------------------------------------------------------------------
116  //----------------------------------------------------------------------------------------------------------------------
117  static QKinect *instance();
118 
119  //----------------------------------------------------------------------------------------------------------------------
122  //----------------------------------------------------------------------------------------------------------------------
123  inline void setUserDeviceNumber(int _m){m_userDeviceNumber=_m;}
124 
125  //----------------------------------------------------------------------------------------------------------------------
127  //----------------------------------------------------------------------------------------------------------------------
128  void shutDownKinect();
129 
130  //----------------------------------------------------------------------------------------------------------------------
132  //----------------------------------------------------------------------------------------------------------------------
134 
135 
136 public slots :
137  //----------------------------------------------------------------------------------------------------------------------
140  //----------------------------------------------------------------------------------------------------------------------
141  void setAngle(double _angle);
142 
143  //----------------------------------------------------------------------------------------------------------------------
145  //----------------------------------------------------------------------------------------------------------------------
146  void setLedOff();
147 
148  //----------------------------------------------------------------------------------------------------------------------
150  //----------------------------------------------------------------------------------------------------------------------
151  void setRedLed();
152 
153  //----------------------------------------------------------------------------------------------------------------------
155  //----------------------------------------------------------------------------------------------------------------------
156  void setGreenLed();
157 
158  //----------------------------------------------------------------------------------------------------------------------
160  //----------------------------------------------------------------------------------------------------------------------
161  void setYellowLed();
162 
163  //----------------------------------------------------------------------------------------------------------------------
165  //----------------------------------------------------------------------------------------------------------------------
166  void setRedLedFlash();
167 
168  //----------------------------------------------------------------------------------------------------------------------
170  //----------------------------------------------------------------------------------------------------------------------
171  void setGreenLedFlash();
172 
173  //----------------------------------------------------------------------------------------------------------------------
175  //----------------------------------------------------------------------------------------------------------------------
176  void setYellowLedFlash();
177 
178  //----------------------------------------------------------------------------------------------------------------------
184  //----------------------------------------------------------------------------------------------------------------------
185  void setVideoMode(int _mode);
186 
187  //----------------------------------------------------------------------------------------------------------------------
189  //----------------------------------------------------------------------------------------------------------------------
190  void resetAngle();
191 
192  //----------------------------------------------------------------------------------------------------------------------
196  //----------------------------------------------------------------------------------------------------------------------
197  bool getDepth(std::vector<uint8_t> &o_buffer);
198 
199  //----------------------------------------------------------------------------------------------------------------------
203  //----------------------------------------------------------------------------------------------------------------------
204  bool getDepthRaw(std::vector<uint8_t> &o_buffer);
205 
206  //----------------------------------------------------------------------------------------------------------------------
210  //----------------------------------------------------------------------------------------------------------------------
211  bool getDepth16Bit(std::vector<uint16_t> &o_buffer);
212 
213  //----------------------------------------------------------------------------------------------------------------------
216  //----------------------------------------------------------------------------------------------------------------------
217  bool getRGB(std::vector<uint8_t> &o_buffer);
218 
219  //----------------------------------------------------------------------------------------------------------------------
221  //----------------------------------------------------------------------------------------------------------------------
222  void startDepth();
223 
224  //----------------------------------------------------------------------------------------------------------------------
226  //----------------------------------------------------------------------------------------------------------------------
227  void stopDepth();
228 
229  //----------------------------------------------------------------------------------------------------------------------
231  //----------------------------------------------------------------------------------------------------------------------
232  void startVideo();
233 
234  //----------------------------------------------------------------------------------------------------------------------
236  //----------------------------------------------------------------------------------------------------------------------
237  void stopVideo();
238 
239  //----------------------------------------------------------------------------------------------------------------------
242  //----------------------------------------------------------------------------------------------------------------------
243  void toggleVideoState(bool _mode);
244 
245  //----------------------------------------------------------------------------------------------------------------------
248  //----------------------------------------------------------------------------------------------------------------------
249  void toggleDepthState(bool _mode);
250 
251  //----------------------------------------------------------------------------------------------------------------------
254  //----------------------------------------------------------------------------------------------------------------------
255  inline freenect_context *getContext(){return m_ctx;}
256 
257 
258 signals:
263  void message(QString text);
264 
265 
266 private :
267  //----------------------------------------------------------------------------------------------------------------------
269  //----------------------------------------------------------------------------------------------------------------------
270  QKinect();
271 
272  //----------------------------------------------------------------------------------------------------------------------
274  //----------------------------------------------------------------------------------------------------------------------
275  ~QKinect();
276 
277  //----------------------------------------------------------------------------------------------------------------------
279  //----------------------------------------------------------------------------------------------------------------------
280  Q_DISABLE_COPY(QKinect)
281 
282  //----------------------------------------------------------------------------------------------------------------------
286  //----------------------------------------------------------------------------------------------------------------------
287  void init();
288 
289  //----------------------------------------------------------------------------------------------------------------------
291  //----------------------------------------------------------------------------------------------------------------------
293 
294  //----------------------------------------------------------------------------------------------------------------------
296  //----------------------------------------------------------------------------------------------------------------------
297  freenect_context *m_ctx;
298 
299  //----------------------------------------------------------------------------------------------------------------------
301  //----------------------------------------------------------------------------------------------------------------------
302  freenect_device *m_dev;
303 
304  //----------------------------------------------------------------------------------------------------------------------
306  //----------------------------------------------------------------------------------------------------------------------
308 
309  //----------------------------------------------------------------------------------------------------------------------
314  //----------------------------------------------------------------------------------------------------------------------
315  void depthFunc(freenect_device *_dev, void *o_depth, uint32_t _timestamp);
316 
317  //----------------------------------------------------------------------------------------------------------------------
322  //----------------------------------------------------------------------------------------------------------------------
323  void rgbFunc(freenect_device *_dev, void *o_rgb, uint32_t _timestamp);
324 
325  //----------------------------------------------------------------------------------------------------------------------
331  //----------------------------------------------------------------------------------------------------------------------
332  std::vector<uint8_t> m_bufferDepth;
333 
334  //----------------------------------------------------------------------------------------------------------------------
337  //----------------------------------------------------------------------------------------------------------------------
338  std::vector<uint8_t> m_bufferVideo;
339 
340  //----------------------------------------------------------------------------------------------------------------------
345  //----------------------------------------------------------------------------------------------------------------------
346  std::vector<uint8_t> m_bufferDepthRaw;
347 
348  //----------------------------------------------------------------------------------------------------------------------
351  //----------------------------------------------------------------------------------------------------------------------
352  std::vector<uint16_t> m_bufferDepthRaw16;
353 
354  //----------------------------------------------------------------------------------------------------------------------
357  //----------------------------------------------------------------------------------------------------------------------
358  std::vector<uint16_t> m_gamma;
359 
360  //----------------------------------------------------------------------------------------------------------------------
362  //----------------------------------------------------------------------------------------------------------------------
364 
365  //----------------------------------------------------------------------------------------------------------------------
367  //----------------------------------------------------------------------------------------------------------------------
369 
370  //----------------------------------------------------------------------------------------------------------------------
372  //----------------------------------------------------------------------------------------------------------------------
374 
375  //----------------------------------------------------------------------------------------------------------------------
377  //----------------------------------------------------------------------------------------------------------------------
379 
380  //----------------------------------------------------------------------------------------------------------------------
382  //----------------------------------------------------------------------------------------------------------------------
384 
385  //----------------------------------------------------------------------------------------------------------------------
388  //----------------------------------------------------------------------------------------------------------------------
389  QMutex m_mutex;
390 
391  //----------------------------------------------------------------------------------------------------------------------
397  //----------------------------------------------------------------------------------------------------------------------
398  void grabDepth(void *_depth, uint32_t timestamp);
399 
400  //----------------------------------------------------------------------------------------------------------------------
406  //----------------------------------------------------------------------------------------------------------------------
407  void grabVideo(void *_video, uint32_t timestamp);
408 
409  //----------------------------------------------------------------------------------------------------------------------
415  //----------------------------------------------------------------------------------------------------------------------
416  static inline void depthCallback(freenect_device *_dev, void *_depth, uint32_t _timestamp=0)
417  {
419  QKinect *kinect=QKinect::instance();
421  kinect->grabDepth(_depth,_timestamp);
422 
423  Q_UNUSED(_dev);
424  Q_UNUSED(_timestamp);
425  }
426 
427  //----------------------------------------------------------------------------------------------------------------------
433  //----------------------------------------------------------------------------------------------------------------------
434  static inline void videoCallback(freenect_device *_dev, void *_video, uint32_t _timestamp=0)
435  {
437  QKinect *kinect=QKinect::instance();
439  kinect->grabVideo(_video, _timestamp);
440 
441  Q_UNUSED(_dev);
442  Q_UNUSED(_timestamp);
443  }
444 };
445 
446 #endif