XBMCMove 1.0
Controlar o XBMC com gestos
|
00001 00008 #include "callbacks.h" 00009 00013 void CleanupExit() { 00014 if (NULL != g_pSessionManager) { 00015 delete g_pSessionManager; 00016 g_pSessionManager = NULL; 00017 free(g_pSessionManager); 00018 } 00019 if (NULL != g_pCircle) { 00020 delete g_pCircle; 00021 g_pCircle = NULL; 00022 free(g_pCircle); 00023 } 00024 if (NULL != g_pSwipeD) { 00025 delete g_pSwipeD; 00026 g_pSwipeD = NULL; 00027 free(g_pSwipeD); 00028 } 00029 00030 free(g_pTrackPad); 00031 free(wc); 00032 00033 g_Context.Release(); 00034 g_Context2.Release(); 00035 } 00036 00037 #define CHECK_RC(nRetVal, what) \ 00038 if (nRetVal != XN_STATUS_OK) \ 00039 { \ 00040 printf("[ERROR] %s failed: %s\n", what, xnGetStatusString(nRetVal));\ 00041 /*return nRetVal;*/ \ 00042 } 00043 00047 int initKinect() { 00048 00049 // Configure 00050 rc = g_Context2.InitFromXmlFile(SAMPLE_XML_FILE, g_ScriptNode, &errors); 00051 if (rc == XN_STATUS_NO_NODE_PRESENT) { 00052 XnChar strError[1024]; 00053 errors.ToString(strError, 1024); 00054 //printf("%s\n", strError); 00055 } else if (rc != XN_STATUS_OK) { 00056 printf("[ERROR] Open failed: %s\n", xnGetStatusString(rc)); 00057 return rc; 00058 } 00059 00060 rc = g_Context2.FindExistingNode(XN_NODE_TYPE_DEPTH, g_DepthGenerator); 00061 CHECK_RC(rc, "Find depth generator"); 00062 rc = g_Context2.FindExistingNode(XN_NODE_TYPE_USER, g_UserGenerator); 00063 if (rc != XN_STATUS_OK) { 00064 rc = g_UserGenerator.Create(g_Context2); 00065 CHECK_RC(rc, "Find user generator"); 00066 } 00067 00068 XnCallbackHandle hUserCallbacks, hCalibrationStart, hCalibrationComplete, hPoseDetected, hCalibrationInProgress, hPoseInProgress; 00069 if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_SKELETON)) { 00070 printf("Supplied user generator doesn't support skeleton\n"); 00071 //return 1; 00072 } 00073 rc = g_UserGenerator.RegisterUserCallbacks(User_NewUser, User_LostUser, NULL, hUserCallbacks); 00074 CHECK_RC(rc, "Register to user callbacks"); 00075 rc = g_UserGenerator.GetSkeletonCap().RegisterToCalibrationStart(UserCalibration_CalibrationStart, NULL, hCalibrationStart); 00076 CHECK_RC(rc, "Register to calibration start"); 00077 rc = g_UserGenerator.GetSkeletonCap().RegisterToCalibrationComplete(UserCalibration_CalibrationComplete, NULL, hCalibrationComplete); 00078 CHECK_RC(rc, "Register to calibration complete"); 00079 00080 if (g_UserGenerator.GetSkeletonCap().NeedPoseForCalibration()) { 00081 g_bNeedPose = TRUE; 00082 if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_POSE_DETECTION)) { 00083 printf("Pose required, but not supported\n"); 00084 //return 1; 00085 } 00086 rc = g_UserGenerator.GetPoseDetectionCap().RegisterToPoseDetected(UserPose_PoseDetected, NULL, hPoseDetected); 00087 CHECK_RC(rc, "Register to Pose Detected"); 00088 g_UserGenerator.GetSkeletonCap().GetCalibrationPose(g_strPose); 00089 } 00090 00091 g_UserGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL); 00092 return 0; 00093 } 00094 00095 00099 float getJointImgCoordinates(const SkeletonCapability &skeletonCapability, const XnUserID userId, const XnSkeletonJoint skeletonJoint, float *v) { 00100 XnVector3D projective; 00101 XnSkeletonJointPosition skeletonJointPosition; 00102 skeletonCapability.GetSkeletonJointPosition(userId, skeletonJoint, skeletonJointPosition); 00103 00104 g_DepthGenerator.ConvertRealWorldToProjective(1, &skeletonJointPosition.position, &projective); 00105 00106 v[0] = projective.X; 00107 v[1] = projective.Y; 00108 v[2] = projective.Z / 1000.0f; 00109 00110 return skeletonJointPosition.fConfidence; 00111 } 00112 00116 int carregarKinect() { 00117 00118 //Iniciar Configurações do kinect, separado para organizar 00119 rc = XN_STATUS_OK; 00120 00121 // Configure 00122 rc = g_Context.InitFromXmlFile(SAMPLE_XML_FILE, g_ScriptNode, &errors); 00123 if (rc == XN_STATUS_NO_NODE_PRESENT) { 00124 XnChar strError[1024]; 00125 errors.ToString(strError, 1024); 00126 CleanupExit(); 00127 printf("%s\n", strError); 00128 } else if (rc != XN_STATUS_OK) { 00129 CleanupExit(); 00130 //printf("Open failed: %s\n", xnGetStatusString(rc)); 00131 return rc; 00132 } 00133 00134 00135 // Create the gesture and hands generators 00136 rc = g_GestureGenerator.Create(g_Context); 00137 rc = g_HandsGenerator.Create(g_Context); 00138 00139 if (rc != XN_STATUS_OK) { 00140 CleanupExit(); 00141 //printf("Couldn't initialize from file: %s\n", xnGetStatusString(rc)); 00142 return rc; 00143 } 00144 00145 // Register to callbacks 00146 g_GestureGenerator.RegisterGestureCallbacks(Gesture_Recognized, Gesture_Process, NULL, h1); 00147 g_HandsGenerator.RegisterHandCallbacks(Hand_Create, Hand_Update, Hand_Destroy, NULL, h2); 00148 00149 00150 // Create and initialize point tracker 00151 g_pSessionManager = new XnVSessionManager(); 00152 rc = g_pSessionManager->Initialize(&g_Context, "RaiseHand", NULL); 00153 if (rc != XN_STATUS_OK) { 00154 printf("[ERROR] Couldn't initialize the Session Manager: %s\n", xnGetStatusString(rc)); 00155 CleanupExit(); 00156 } 00157 00158 g_pSessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd); 00159 00160 // Registrar e Reconhecer gesto: Circulo 00161 g_pCircle = new XnVCircleDetector; 00162 g_pCircle->RegisterCircle(NULL, &CircleCB); 00163 g_pCircle->RegisterNoCircle(NULL, &NoCircleCB); 00164 g_pCircle->RegisterPrimaryPointCreate(NULL, &Circle_PrimaryCreate); 00165 g_pCircle->RegisterPrimaryPointDestroy(NULL, &Circle_PrimaryDestroy); 00166 //g_pSessionManager->AddListener(g_pCircle); 00167 00168 // Registrar e Reconhecer gesto: Wave 00169 wc = new XnVWaveDetector(); 00170 wc->RegisterWave(NULL, OnWaveCB); 00171 g_pSessionManager->AddListener(wc); 00172 00173 //Registrar e Reconhecer gesto: Swipe 00174 g_pSwipeD = new XnVSwipeDetector(true); 00175 g_pSwipeD->RegisterSwipeLeft(NULL, SwipeLeftCB); 00176 g_pSwipeD->RegisterSwipeRight(NULL, SwipeRightCB); 00177 g_pSwipeD->RegisterSwipeUp(NULL, SwipeUpCB); 00178 g_pSwipeD->RegisterSwipeDown(NULL, SwipeDownCB); 00179 g_pSwipeD->SetSteadyMaxVelocity(0.8); 00180 //printf("VAR=%f\n",g_pSwipeD->GetSteadyMaxStdDev()); 00181 //g_pSwipeD->SetSteadyMaxStdDev(50); 00182 g_pSwipeD->SetSteadyDuration(500); 00183 g_pSwipeD->SetXAngleThreshold(75); 00184 g_pSwipeD->SetYAngleThreshold(75); 00185 g_pSessionManager->AddListener(g_pSwipeD); 00186 00187 g_pTrackPad = new XnVSelectableSlider2D(g_TP_XDim, g_TP_YDim); 00188 00189 // Add TrackPad to the point tracker 00190 g_TrackPadHandle = g_pSessionManager->AddListener(g_pTrackPad); 00191 00192 // Register for the Hover event of the TrackPad 00193 g_nItemHoverHandle = g_pTrackPad->RegisterItemHover(NULL, &TrackPad_ItemHover); 00194 // Register for the Value Change event of the TrackPad 00195 g_nValueChangeHandle = g_pTrackPad->RegisterValueChange(NULL, &TrackPad_ValueChange); 00196 // Register for the Select event of the TrackPad 00197 g_nItemSelectHandle = g_pTrackPad->RegisterItemSelect(NULL, &TrackPad_ItemSelect); 00198 00199 // Register for Input Start event of the TrackPad 00200 g_nPrimaryCreateHandle = g_pTrackPad->RegisterPrimaryPointCreate(NULL, &TrackPad_PrimaryCreate); 00201 // Register for Input Stop event of the TrackPad 00202 g_nPrimaryDestroyHandle = g_pTrackPad->RegisterPrimaryPointDestroy(NULL, &TrackPad_PrimaryDestroy); 00203 00204 initKinect(); 00205 00206 } 00207 00211 void iniciar() { 00212 00213 //Começa a ouvir 00214 rc = g_Context.StartGeneratingAll(); 00215 CHECK_RC(rc, "StartGeneratingAll"); 00216 00217 g_GestureGenerator.AddGesture("RaiseHand", NULL); 00218 //g_GestureGenerator.AddGesture("Click", NULL); 00219 00220 rc = g_Context2.StartGeneratingAll(); 00221 CHECK_RC(rc, "StartGeneratingAll2"); 00222 00223 } 00224 00228 XnStatus getStatus() { 00229 return rc; 00230 } 00231 00236 void gestoFinal() { 00237 if (!gFechar) { 00238 if (lh[0] != rh[0]) { 00239 if (((rh[1] > (head[1])) && (lh[1] > (head[1]))) && ((rh[1] < (t[1])) && (lh[1] < (t[1]))) && (rh[0] < lh[0])) { 00240 fecharCont++; 00241 if (fecharCont >= 10) { 00242 printf("[INFO] Gesto 'Fechar' detectado!\nFinalizando XBMC!\n"); 00243 system("killall xbmc"); 00244 system("killall xbmc.bin"); 00245 gFechar = true; 00246 } 00247 } else { 00248 fecharCont = 0; 00249 } 00250 } 00251 } 00252 } 00253 00258 void gestoVolume() { 00259 if (lh[0] != rh[0]) { 00260 if (((rh[1] < (t[1])) && (lh[1] < (t[1]))) && (rh[0] > lh[0])) { 00261 if (posInit[0] != 0) { 00262 if (volumeCont >= 20) { 00263 if ((rh[0] - lh[0]) >= (distInit * 1.9)) { 00264 printf("[INFO] Aumentar MASTER!\n"); 00265 teclar->simular(XK_plus); 00266 volumeCont = 20; 00267 } else { 00268 00269 if ((rh[0] - lh[0]) <= (distInit * 0.1)) { 00270 printf("[INFO] Diminur MASTER!\n"); 00271 teclar->simular(XK_minus); 00272 volumeCont = 20; 00273 } else { 00274 if (((rh[0] - lh[0]) > (distInit * 1.4)) && ((rh[0] - lh[0]) < (distInit * 1.9))) { 00275 printf("[INFO] Aumentar bastante!\n"); 00276 teclar->simular(XK_plus); 00277 volumeCont = 15; 00278 } else { 00279 if ((rh[0] - lh[0]) < (distInit * 0.6)) { 00280 printf("[INFO] Diminuir bastante!\n"); 00281 teclar->simular(XK_minus); 00282 volumeCont = 15; 00283 } else { 00284 if (((rh[0] - lh[0]) > (distInit * 0.6)) && ((rh[0] - lh[0]) < (distInit))) { 00285 printf("[INFO] Diminuir um pouco!\n"); 00286 teclar->simular(XK_minus); 00287 volumeCont = 5; 00288 } else { 00289 printf("[INFO] Aumentar um pouco!\n"); 00290 teclar->simular(XK_plus); 00291 volumeCont = 5; 00292 } 00293 } 00294 } 00295 } 00296 } 00297 } else { 00298 volumeCont++; 00299 } 00300 00301 } else { 00302 posInit[0] = lh[0]; 00303 posInit[1] = rh[0]; 00304 distInit = posInit[1] - posInit[0]; 00305 volumeCont = 1; 00306 } 00307 } else { 00308 volumeCont = distInit = posInit[0] = posInit[1] = 0; 00309 } 00310 } 00311 } 00312 00316 void *updateC(void *threadid) { 00317 00318 g_Context2.WaitOneUpdateAll(g_DepthGenerator); 00319 00320 //g_Context2.WaitAnyUpdateAll(); 00321 00322 SkeletonCapability skelCap = g_UserGenerator.GetSkeletonCap(); 00323 00324 nUsers = g_UserGenerator.GetNumberOfUsers(); 00325 g_UserGenerator.GetUsers(userIds, nUsers); 00326 id = userIds[0]; 00327 00328 getJointImgCoordinates(skelCap, id, XN_SKEL_HEAD, head); // Pega posição da cabeça 00329 //getJointImgCoordinates(skelCap, id, XN_SKEL_RIGHT_SHOULDER, ombroD); //Ombro Direito 00330 //getJointImgCoordinates(skelCap, id, XN_SKEL_LEFT_SHOULDER, ombroE); //Ombro Esquerdo 00331 //getJointImgCoordinates(skelCap, id, XN_SKEL_RIGHT_ELBOW, cotoveloD); // Cotovelo Direito 00332 //getJointImgCoordinates(skelCap, id, XN_SKEL_LEFT_ELBOW, cotoveloE); // Cotovelo Esquerdo 00333 getJointImgCoordinates(skelCap, id, XN_SKEL_TORSO, t); 00334 getJointImgCoordinates(skelCap, id, XN_SKEL_RIGHT_HAND, rh); 00335 getJointImgCoordinates(skelCap, id, XN_SKEL_LEFT_HAND, lh); 00336 00337 gestoVolume(); 00338 gestoFinal(); 00339 00340 pthread_exit(NULL); 00341 } 00342 00346 void *update(void *threadid) { 00347 pthread_t update2; 00348 long t2; 00349 00350 while (rodando) { 00351 00352 pthread_create(&update2, NULL, updateC, (void *) t2); 00353 pthread_join(update2, NULL); //Aguarda thread ser finalizada corretamente! 00354 //updateC(); 00355 // Le os proximos dados disponiveis 00356 g_Context.WaitAnyUpdateAll(); 00357 00358 // Processa as informações 00359 g_pSessionManager->Update(&g_Context); 00360 00361 //if (inSession) { 00362 00363 //} 00364 } 00365 pthread_exit(NULL); 00366 } 00367 00368