-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
378 lines (334 loc) · 9.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/****************************************************************************
KINECT PS3
Main Module
Author: Shantanu Goel (http://tech.shantanugoel.com/)
****************************************************************************/
/*
Copyright (C) 2011 Shantanu Goel
This file is part of Kinect-PS3
Kinect-PS3 is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Kinect PS3 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not , see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <XnOpenNI.h>
#include <XnVSessionManager.h>
#include "XnVMultiProcessFlowClient.h"
#include <XnVWaveDetector.h>
#include <XnVSelectableSlider2D.h>
#include <XnVFlowRouter.h>
#include "kps3-input.h"
#include <linux/input.h>
#include "kbhit.h"
#include "signal_catch.h"
#define SAMPLE_XML_FILE "../../Data/Sample-Tracking.xml"
#define KINECT_PS3_VERSION "0.0.1"
XnBool g_bQuit = false;
XnUInt32 g_TP_XDim = 10;
XnUInt32 g_TP_YDim = 10;
XnUInt32 g_TP_XPrev = 0;
XnUInt32 g_TP_YPrev = 0;
int g_ScrollingOn = 1;
int g_numFwdSelect = 0;
int g_numBkwdSelect = 0;
int mouseX = 0;
int mouseY = 0;
int mouseZ = 0;
int mousePrevX = 0;
int mousePrevY = 0;
int mousePrevZ = 0;
int depth_averaging_mode = 1;
int num_samples_for_depth_avg = 10;
int cur_num_samples = 0;
int depth_avg = 0;
int movement_for_depth_change_action = 150;
int dir_button = 0;
int dir_button_prev = 0;
typedef enum
{
PROFILE_UNKNOWN = 0,
PROFILE_MENU,
PROFILE_GAME
} profile_t;
profile_t g_curProfile = PROFILE_UNKNOWN;
#define PROFILE_DEFAULT PROFILE_MENU
XnVSessionManager* g_pSessionManager = NULL;
XnVSelectableSlider2D* g_pTrackPad = NULL;
XnVWaveDetector* g_pWaveDetector = NULL;
XnVFlowRouter* g_pFlowRouter = NULL;
void XN_CALLBACK_TYPE TrackPad_ItemSelect(XnInt32 nXItem, XnInt32 nYItem,
XnVDirection eDir, void* cxt);
void XN_CALLBACK_TYPE SessionProgress(const XnChar* strFocus,
const XnPoint3D& ptFocusPoint,
XnFloat fProgress, void* UserCxt)
{
printf("Session progress (%6.2f,%6.2f,%6.2f) - %6.2f [%s]\n", ptFocusPoint.X,
ptFocusPoint.Y, ptFocusPoint.Z, fProgress, strFocus);
}
void XN_CALLBACK_TYPE SessionStart(const XnPoint3D& ptFocusPoint, void* UserCxt)
{
printf("Session started. Please wave (%6.2f,%6.2f,%6.2f)...\n",
ptFocusPoint.X, ptFocusPoint.Y, ptFocusPoint.Z);
}
void XN_CALLBACK_TYPE SessionEnd(void* UserCxt)
{
printf("Session ended. Please perform focus gesture to start session\n");
}
void XN_CALLBACK_TYPE OnPointUpdate(const XnVHandPointContext* pContext,
void* cxt)
{
printf("%d: (%f,%f,%f) [%f]\n", pContext->nID, pContext->ptPosition.X,
pContext->ptPosition.Y, pContext->ptPosition.Z, pContext->fTime);
mouseX = pContext->ptPosition.X - mousePrevX;
mouseY = -(pContext->ptPosition.Y) + mousePrevY;
mouseZ = pContext->ptPosition.Z - mousePrevZ;
printf("%d %d %d %d %d %d\n", mouseX, mouseY, mouseZ, mousePrevX, mousePrevY,
mousePrevZ);
mousePrevX = pContext->ptPosition.X;
mousePrevY = pContext->ptPosition.Y;
mousePrevZ = pContext->ptPosition.Z;
/*
This works for using mouse "Minority Report" style on PC but somehow doesn't
work too well on PS3. Maybe PS3 requires absolute positions instead of
relative.
*/
//move_cursor(mouseX, mouseY, mouseZ);
move_cursor((int)(pContext->ptPosition.X/4),(int) -(pContext->ptPosition.Y/4),
(int) (pContext->ptPosition.Z/4));
}
void XN_CALLBACK_TYPE TrackPad_ItemHover(XnInt32 nXItem, XnInt32 nYItem,
void* cxt)
{
printf("Hover %d %d\n", nXItem, nYItem);
g_numFwdSelect = 0;
g_numBkwdSelect = 0;
int key = 0;
if(g_ScrollingOn == 1)
{
if(nXItem > g_TP_XPrev)
{
printf("Right\n");
key = KEY_D;
}
else if(nXItem < g_TP_XPrev)
{
printf("Left\n");
key = KEY_A;
}
g_TP_XPrev = nXItem;
if(nYItem > g_TP_YPrev)
{
printf("UP\n");
key = KEY_W;
}
else if(nYItem < g_TP_YPrev)
{
printf("Down\n");
key = KEY_S;
}
if(0 != key)
{
press_button(key);
/*
Using a 100ms delay between key press and key release, otherwise
many times the key press wasn't picked up correctly and only key
release event was being sent to the PS3.
*/
usleep(100*1000);
rls_button(key);
}
g_TP_XPrev = nXItem;
g_TP_YPrev = nYItem;
}
}
int SwitchProfile(profile_t profile)
{
if(profile != g_curProfile)
{
switch(profile)
{
case PROFILE_MENU:
{
printf("Activating menu profile\n");
g_pFlowRouter->SetActive(g_pTrackPad);
break;
}
case PROFILE_GAME:
{
printf("Activating game profile\n");
g_pFlowRouter->SetActive(g_pWaveDetector);
break;
}
}
g_curProfile = profile;
}
}
void XN_CALLBACK_TYPE TrackPad_ItemSelect(XnInt32 nXItem, XnInt32 nYItem,
XnVDirection eDir, void* cxt)
{
printf("Select: %d,%d (%s)\n", nXItem, nYItem, XnVDirectionAsString(eDir));
if(DIRECTION_BACKWARD == eDir)
{
g_numFwdSelect = 0;
/*
1 backward selection toggles scrolling with hand.
3 successive backward selections switch the profile
*/
if(g_numBkwdSelect < 2)
{
g_numBkwdSelect++;
if(g_ScrollingOn == 0)
{
g_ScrollingOn = 1;
}
else
{
g_ScrollingOn = 0;
}
g_TP_XPrev = nXItem;
g_TP_YPrev = nYItem;
printf("Toggled Scrolling to %d\n", g_ScrollingOn);
}
else
{
g_numBkwdSelect = 0;
printf("Switching Profiles\n");
if(g_curProfile == PROFILE_MENU)
{
SwitchProfile(PROFILE_GAME);
}
else
{
SwitchProfile(PROFILE_MENU);
}
}
}
else if(DIRECTION_FORWARD == eDir)
{
g_numBkwdSelect = 0;
/*
2 successive forward selections will activate/select the current item.
*/
if(g_numFwdSelect == 0)
{
g_numFwdSelect++;
printf("Waiting for 2nd select\n");
}
else if(g_numFwdSelect == 1)
{
g_numFwdSelect = 0;
printf("Selecting Item\n");
press_button(KEY_X);
usleep(100*1000);
rls_button(KEY_X);
}
}
}
void cleanup()
{
if(NULL != g_pSessionManager)
{
delete g_pSessionManager;
}
if(NULL != g_pFlowRouter)
{
delete g_pFlowRouter;
}
if(NULL != g_pTrackPad)
{
delete g_pTrackPad;
}
if(NULL != g_pWaveDetector)
{
delete g_pWaveDetector;
}
}
int main(int argc, char** argv)
{
xn::Context context;
if(argc > 1)
{
if(strcmp(argv[1], "-h"))
{
printf("Invalid Args\n");
}
else
{
printf("Kinect PS3 by Shantanu Goel (http://tech.shantanugoel.com/) - \
Version %s\n", KINECT_PS3_VERSION);
exit(0);
}
}
XnStatus rc = context.InitFromXmlFile(SAMPLE_XML_FILE);
if (rc != XN_STATUS_OK)
{
printf("Couldn't initialize: %s\n", xnGetStatusString(rc));
return 1;
}
setup_uinput_device();
g_pSessionManager = new XnVSessionManager();
rc = ((XnVSessionManager*)g_pSessionManager)->Initialize(&context, "Wave",
"Wave");
if (rc != XN_STATUS_OK)
{
printf("Session Manager couldn't initialize: %s\n", xnGetStatusString(rc));
goto cleanup_exit;
}
if(NULL == g_pTrackPad)
{
g_pTrackPad = new XnVSelectableSlider2D(g_TP_XDim, g_TP_YDim);
}
if(NULL == g_pTrackPad)
{
printf("Couldn't create TrackPad\n");
goto cleanup_exit;
}
if(NULL == g_pWaveDetector)
{
g_pWaveDetector = new XnVWaveDetector();
}
if(NULL == g_pWaveDetector)
{
printf("Couldn't create Wave Detector\n");
goto cleanup_exit;
}
if(NULL == g_pFlowRouter)
{
g_pFlowRouter = new XnVFlowRouter();
}
if(NULL == g_pFlowRouter)
{
printf("Couldn't create Flow Router\n");
goto cleanup_exit;
}
context.StartGeneratingAll();
g_pSessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd,
&SessionProgress);
CatchSignals(&g_bQuit);
g_pWaveDetector->RegisterPointUpdate(NULL, OnPointUpdate);
g_pTrackPad->RegisterItemHover(NULL, &TrackPad_ItemHover);
g_pTrackPad->RegisterItemSelect(NULL, &TrackPad_ItemSelect);
SwitchProfile(PROFILE_MENU);
g_pSessionManager->AddListener(g_pFlowRouter);
printf("Please perform focus gesture to start session\n");
printf("Hit any key to exit\n");
// Main loop
while ((!_kbhit()) && (!g_bQuit))
{
context.WaitAndUpdateAll();
((XnVSessionManager*)g_pSessionManager)->Update(&context);
}
cleanup_exit:
cleanup();
context.Shutdown();
return 0;
}