private static void WorkerContextTeardown(WorkerContext context) { UnityEngine.Debug.Log("Tearing down PSMove Tracking Context"); // Delete the controllers for (int psmove_id = 0; psmove_id < context.PSMoves.Length; psmove_id++) { if (context.PSMoves[psmove_id] != IntPtr.Zero) { UnityEngine.Debug.Log(string.Format("Disconnecting PSMove controller {0}", psmove_id)); context.WorkerControllerDataArray[psmove_id].IsConnected = false; context.WorkerControllerDataArray[psmove_id].IsEnabled = false; PSMoveAPI.psmove_disconnect(context.PSMoves[psmove_id]); context.PSMoves[psmove_id] = IntPtr.Zero; } } // Delete the tracking fusion state if (context.PSMoveFusion != IntPtr.Zero) { UnityEngine.Debug.Log("PSMove fusion disposed"); PSMoveAPI.psmove_fusion_free(context.PSMoveFusion); context.PSMoveFusion = IntPtr.Zero; } // Delete the tracker state if (context.PSMoveTracker != IntPtr.Zero) { UnityEngine.Debug.Log("PSMove tracker disposed"); PSMoveAPI.psmove_tracker_free(context.PSMoveTracker); context.PSMoveTracker = IntPtr.Zero; } context.Reset(); }
private static void WorkerContextTeardown(WorkerContext context) { // Delete the controllers for (int psmove_id = 0; psmove_id < context.PSMoves.Length; psmove_id++) { if (context.PSMoves[psmove_id] != IntPtr.Zero) { Debug.WriteLine(string.Format("Disconnecting PSMove controller {0}", psmove_id)); context.WorkerControllerDataArray[psmove_id].IsConnected = false; context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled = false; PSMoveAPI.psmove_disconnect(context.PSMoves[psmove_id]); context.PSMoves[psmove_id] = IntPtr.Zero; } } // Delete the tracker WorkerContextTeardownTracking(context); context.Reset(); }
private static bool WorkerContextUpdateControllerConnections(WorkerContext context) { bool controllerCountChanged = false; if (context.moveCountCheckTimer.ElapsedMilliseconds >= WorkerContext.CONTROLLER_COUNT_POLL_INTERVAL) { // Update the number int newcount = PSMoveAPI.psmove_count_connected(); if (context.PSMoveCount != newcount) { Debug.WriteLine("PSMove Controllers count changed: {0} -> {1}.", context.PSMoveCount, newcount); context.PSMoveCount = newcount; controllerCountChanged = true; } // Refresh the connection and tracking state of every controller entry for (int psmove_id = 0; psmove_id < context.PSMoves.Length; psmove_id++) { if (psmove_id < context.PSMoveCount) { if (context.PSMoves[psmove_id] == IntPtr.Zero) { // The controller should be connected context.PSMoves[psmove_id] = PSMoveAPI.psmove_connect_by_id(psmove_id); if (context.PSMoves[psmove_id] != IntPtr.Zero) { PSMoveAPI.psmove_enable_orientation(context.PSMoves[psmove_id], PSMove_Bool.PSMove_True); System.Diagnostics.Debug.Assert(PSMoveAPI.psmove_has_orientation(context.PSMoves[psmove_id]) == PSMove_Bool.PSMove_True); context.WorkerControllerDataArray[psmove_id].IsConnected = true; } else { context.WorkerControllerDataArray[psmove_id].IsConnected = false; Debug.WriteLine("Failed to connect to PSMove controller {0}", psmove_id); } } if (context.PSMoves[psmove_id] != IntPtr.Zero && context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled == false && context.WorkerSettings.bTrackerEnabled && WorkerContextIsTrackingSetup(context)) { // The controller is connected, but not tracking yet // Enable tracking for this controller with next available color. if (PSMoveAPI.psmove_tracker_enable( context.PSMoveTracker, context.PSMoves[psmove_id]) == PSMoveTracker_Status.Tracker_CALIBRATED) { context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled = true; } else { Debug.WriteLine("Failed to enable tracking for PSMove controller {0}", psmove_id); } } } else { // The controller should no longer be tracked if (context.PSMoves[psmove_id] != IntPtr.Zero) { PSMoveAPI.psmove_disconnect(context.PSMoves[psmove_id]); context.PSMoves[psmove_id] = IntPtr.Zero; context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled = false; context.WorkerControllerDataArray[psmove_id].IsConnected = false; } } } // Remember the last time we polled the move count context.moveCountCheckTimer.Reset(); context.moveCountCheckTimer.Start(); } return(controllerCountChanged); }
private void OnApplicationQuit() { PSMoveAPI.psmove_disconnect(handle); }
private static bool WorkerContextUpdateControllerConnections(WorkerContext context) { bool controllerCountChanged = false; if (context.moveCountCheckTimer.ElapsedMilliseconds >= WorkerContext.CONTROLLER_COUNT_POLL_INTERVAL) { // Update the number int newcount = PSMoveAPI.psmove_count_connected(); if (context.PSMoveCount != newcount) { UnityEngine.Debug.Log(string.Format("PSMove Controllers count changed: {0} -> {1}.", context.PSMoveCount, newcount)); context.PSMoveCount = newcount; controllerCountChanged = true; } // Refresh the connection and tracking state of every controller entry for (int psmove_id = 0; psmove_id < context.PSMoves.Length; psmove_id++) { if (psmove_id < context.PSMoveCount) { if (context.PSMoves[psmove_id] == IntPtr.Zero) { // The controller should be connected context.PSMoves[psmove_id] = PSMoveAPI.psmove_connect_by_id(psmove_id); if (context.PSMoves[psmove_id] != IntPtr.Zero) { PSMoveAPI.psmove_enable_orientation(context.PSMoves[psmove_id], PSMove_Bool.PSMove_True); System.Diagnostics.Debug.Assert(PSMoveAPI.psmove_has_orientation(context.PSMoves[psmove_id]) == PSMove_Bool.PSMove_True); context.WorkerControllerDataArray[psmove_id].IsConnected = true; } else { context.WorkerControllerDataArray[psmove_id].IsConnected = false; UnityEngine.Debug.LogError(string.Format("Failed to connect to PSMove controller {0}", psmove_id)); } } if (context.PSMoves[psmove_id] != IntPtr.Zero && context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled == false && context.WorkerSettings.bTrackerEnabled && WorkerContextIsTrackingSetup(context)) { int happyTrackerCount = 0; // Attempt to enable any trackers that haven't successfully calibrated the controller yet for (int tracker_index = 0; tracker_index < context.TrackerCount; ++tracker_index) { PSMoveTracker_Status tracker_status = PSMoveAPI.psmove_tracker_get_status( context.PSMoveTrackers[tracker_index], context.PSMoves[psmove_id]); if (tracker_status == PSMoveTracker_Status.Tracker_CALIBRATED || tracker_status == PSMoveTracker_Status.Tracker_TRACKING) { ++happyTrackerCount; } else { // The controller is connected, but not tracking yet // Enable tracking for this controller with next available color. if (PSMoveAPI.psmove_tracker_enable( context.PSMoveTrackers[tracker_index], context.PSMoves[psmove_id]) == PSMoveTracker_Status.Tracker_CALIBRATED) { ++happyTrackerCount; } else { UnityEngine.Debug.LogError(string.Format("Failed to enable tracking for PSMove controller {0} on tracker {1}", psmove_id, tracker_index)); } } } if (happyTrackerCount >= context.TrackerCount) { context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled = true; } } } else { // The controller should no longer be tracked if (context.PSMoves[psmove_id] != IntPtr.Zero) { PSMoveAPI.psmove_disconnect(context.PSMoves[psmove_id]); context.PSMoves[psmove_id] = IntPtr.Zero; context.WorkerControllerDataArray[psmove_id].IsTrackingEnabled = false; context.WorkerControllerDataArray[psmove_id].IsConnected = false; } } } // Remember the last time we polled the move count context.moveCountCheckTimer.Reset(); context.moveCountCheckTimer.Start(); } return(controllerCountChanged); }