14
14
#include < QApplication>
15
15
16
16
17
- // HANDLE m_hNextColorFrameEvent;
18
-
19
-
20
17
Window::Window ()
21
18
: _tick_ms(33 ), _ar_mode(Qt::IgnoreAspectRatio), _fps(0 ),
22
19
_video_width(640 ), _video_height(480 ), _timer(NULL ), _nui_sensor(NULL )
@@ -27,10 +24,17 @@ Window::Window()
27
24
// Initialize device and prepare it to send RGB data
28
25
if (!_initKinect ())
29
26
{
30
- std::cout << " * FAILED! Is your Kinect device connected?" << std::endl;
27
+ std::cout << " * FAILED! Is your Kinect device connected? Is it powered? " << std::endl;
31
28
return ;
32
29
}
33
30
31
+ std::cout << " * Special keys: " << std::endl;
32
+ std::cout << " \t ESC - Quit application" << std::endl;
33
+ std::cout << " \t M - Change aspect ratio" << std::endl;
34
+ std::cout << " \t Up - Increase elevation" << std::endl;
35
+ std::cout << " \t Down - Decrease elevation" << std::endl;
36
+ std::cout << " \t LEFT - Reset elevation to 0 degrees" << std::endl;
37
+
34
38
// Start timer to read frames from Kinect
35
39
_timer = new QTimer ();
36
40
_timer->start (_tick_ms);
@@ -44,6 +48,10 @@ Window::~Window()
44
48
_timer->stop ();
45
49
delete _timer;
46
50
}
51
+
52
+ // Shutdown Kinect properly
53
+ if (_nui_sensor)
54
+ _nui_sensor->NuiShutdown ();
47
55
}
48
56
49
57
/* _tick(): called every few milliseconds.
@@ -53,7 +61,7 @@ void Window::_tick()
53
61
{
54
62
if (!_nui_sensor)
55
63
{
56
- std::cout << " _tick !!! _initKinect() failed, didn't it? !" << std::endl;
64
+ std::cout << " _tick !!! _initKinect() failed: ABORT !" << std::endl;
57
65
return ;
58
66
}
59
67
@@ -82,6 +90,7 @@ void Window::_tick()
82
90
surface_desc.Width , surface_desc.Height ,
83
91
QImage::Format_RGB32);
84
92
93
+
85
94
// Trigger paint event to redraw the window
86
95
if (!_image.isNull ())
87
96
emit update ();
@@ -95,7 +104,7 @@ void Window::_tick()
95
104
bool Window::_initKinect ()
96
105
{
97
106
int sensor_count = 0 ;
98
- if (NuiGetSensorCount (&sensor_count) < 0 )
107
+ if (NuiGetSensorCount (&sensor_count) < 0 || sensor_count < 1 )
99
108
{
100
109
std::cout << " _initKinect !!! NuiGetSensorCount() failed" << std::endl;
101
110
return false ;
@@ -105,28 +114,39 @@ bool Window::_initKinect()
105
114
106
115
/* Look at each Kinect sensor */
107
116
108
- for (int i = 0 ; i < sensor_count; ++i)
117
+ bool sensor_found = false ;
118
+ for (int i = 0 ; i < sensor_count; i++)
109
119
{
120
+ std::cout << " * Checking sensor #" << i << " ... " ;
121
+
110
122
// Create the sensor so we can check status, if we can't create it, move on to the next
111
123
if (NuiCreateSensorByIndex (i, &_nui_sensor) < 0 )
124
+ {
125
+ std::cout << " SKIPPED!" << std::endl;
112
126
continue ;
127
+ }
113
128
114
129
// Get the status of the sensor, and if connected, then we can initialize it
115
- if (_nui_sensor->NuiStatus () = = S_OK)
130
+ if (_nui_sensor->NuiStatus () ! = S_OK)
116
131
{
117
- std::cout << " * Selected sensor #" << i << std::endl;
132
+ std::cout << " NOT READY!" << std::endl;
133
+ }
134
+ else
135
+ {
136
+ std::cout << " SELECTED!" << std::endl;
137
+ sensor_found = true ;
118
138
break ;
119
139
}
120
140
121
141
// This sensor wasn't OK, so release it since we're not using it
122
142
_nui_sensor->Release ();
123
- }
143
+ }
124
144
125
- if (_nui_sensor == NULL )
126
- {
127
- std::cout << " _initKinect !!! No compatible sensors detected " << std::endl;
145
+ if (_nui_sensor == NULL || !sensor_found )
146
+ {
147
+ std::cout << " _initKinect !!! No ready Kinect found. " << std::endl;
128
148
return false ;
129
- }
149
+ }
130
150
131
151
// Initialize the Kinect and specify that we'll be using color
132
152
if (_nui_sensor->NuiInitialize (NUI_INITIALIZE_FLAG_USES_COLOR) >= 0 ) // if (SUCCEEDED(hr))
@@ -212,24 +232,73 @@ void Window::keyPressEvent(QKeyEvent* event)
212
232
{
213
233
switch (event->key ())
214
234
{
235
+ // ESC: exit application
236
+ case Qt::Key_Escape:
237
+ {
238
+ std::cout << " * (ESC) Bye bye." << std::endl;
239
+ QApplication::instance ()->quit ();
240
+ }
241
+ break ;
242
+
215
243
// M: changes aspect ratio mode
216
244
case Qt::Key_M:
217
245
{
218
246
if (_ar_mode == Qt::IgnoreAspectRatio)
247
+ {
219
248
_ar_mode = Qt::KeepAspectRatio;
249
+ std::cout << " * (M) AR = keep aspect ratio" << std::endl;
250
+ }
220
251
else if (_ar_mode == Qt::KeepAspectRatio)
252
+ {
221
253
_ar_mode = Qt::KeepAspectRatioByExpanding;
254
+ std::cout << " * (M) AR = keep aspect ratio by expanding" << std::endl;
255
+ }
222
256
else
257
+ {
223
258
_ar_mode = Qt::IgnoreAspectRatio;
259
+ std::cout << " * (M) AR = ignore aspect ratio" << std::endl;
260
+ }
224
261
}
225
262
break ;
226
263
227
- // ESC: exit application
228
- case Qt::Key_Escape :
264
+ // Up arrow: increases elevation angle by 2 degrees
265
+ case Qt::Key_Up :
229
266
{
230
- std::cout << " * Bye bye." << std::endl;
231
- QApplication::instance ()->quit ();
267
+ long angle = -1 ;
268
+ if (NuiCameraElevationGetAngle (&angle) >= 0 )
269
+ {
270
+ angle += 2 ;
271
+ if (NuiCameraElevationSetAngle (angle) >= 0 )
272
+ std::cout << " * (UP) Current elevation angle: " << angle << std::endl;
273
+ }
274
+ }
275
+ break ;
276
+
277
+ // Down arrow: decreases elevation angle by 2 degrees
278
+ case Qt::Key_Down:
279
+ {
280
+ long angle = -1 ;
281
+ if (NuiCameraElevationGetAngle (&angle) >= 0 )
282
+ {
283
+ angle -= 2 ;
284
+ if (NuiCameraElevationSetAngle (angle) >= 0 )
285
+ std::cout << " * (DOWN) Current elevation angle: " << angle << std::endl;
286
+ }
287
+ }
288
+ break ;
289
+
290
+ // Left arrow: reset elevation angle
291
+ case Qt::Key_Left:
292
+ {
293
+ long angle = -1 ;
294
+ if (NuiCameraElevationGetAngle (&angle) >= 0 )
295
+ {
296
+ angle = 0 ;
297
+ if (NuiCameraElevationSetAngle (angle) >= 0 )
298
+ std::cout << " * (LEFT) Current elevation angle: " << angle << std::endl;
299
+ }
232
300
}
233
301
break ;
302
+
234
303
}
235
304
}
0 commit comments