/// <summary> /// Disconnect the glove and stop updating until the RetryConnection is called. /// This allows a developer to change the communication variables before calling the RetryConnection method. /// </summary> public void Disconnect() { if (glove != null) { this.glove.OnFingerCalibrationFinished -= Glove_OnFingerCalibrationFinished; } this.gloveReady = false; if (this.gloveData != null) { SenseGlove_Manager.SetUsed(this.gloveData.deviceID, false); } this.glove = null; //The DeviceScanner will still keep them, specifically their communicator, in memory. this.standBy = true; }
//-------------------------------------------------------------------------------------------------------------------------------- // Glove Management /// <summary> Tell the SenseGlove_Manager that this deviceID is now in use or no longer in use. </summary> /// <param name="deviceID"></param> /// <param name="inUse" public static void SetUsed(string deviceID, bool inUse) { if (SenseGlove_Manager.connectedIDs == null) { SenseGlove_Manager.connectedIDs = new List <string>(); } if (inUse) { SenseGlove_Manager.connectedIDs.Add(deviceID); } //this device is now in use else //remove this device from use. { int index = SenseGlove_Manager.UseIndex(deviceID); if (index > -1) { SenseGlove_Manager.connectedIDs.RemoveAt(index); } } }
//------------------------------------------------------------------------------------------------------------------------------------ // Communication methods. #region Communication /// <summary> /// Extract a SenseGlove matching the parameters of this SenseGlove_Object from a list retieved by the SenseGloveCs.DeviceScanner. /// </summary> /// <param name="devices"></param> /// <returns></returns> private SenseGlove ExtractSenseGlove(SenseGloveCs.IODevice[] devices) { for (int i = 0; i < devices.Length; i++) { if (devices[i] is SenseGlove) { SenseGlove tempGlove = ((SenseGlove)devices[i]); GloveData tempData = tempGlove.GetData(false); //SenseGlove_Debugger.Log("Dataloaded = " + tempData.dataLoaded + ". IsUsed = " + SenseGlove_Manager.IsUsed(tempData.deviceID) + ". isRight = " + tempData.isRight); if (tempData.dataLoaded && !SenseGlove_Manager.IsUsed(tempData.deviceID)) { //the SenseGlove is done loading data AND is not already in memory if ((this.connectionMethod == ConnectionMethod.FindNextGlove) || (this.connectionMethod == ConnectionMethod.FindNextLeftHand && !tempData.isRight) || (this.connectionMethod == ConnectionMethod.FindNextRightHand && tempData.isRight)) { return(tempGlove); } } } } return(null); }
/// <summary> Check if a specific deviceID is already in use. </summary> /// <param name="deviceID"></param> /// <returns></returns> public static bool IsUsed(string deviceID) { return(SenseGlove_Manager.UseIndex(deviceID) > -1); }
// Update is called once per frame void Update() { if (!standBy) { if (this.elapsedTime < SenseGlove_Object.setupTime) { this.elapsedTime += Time.deltaTime; } else if (this.glove == null) //No connection yet... { if (this.connectionMethod == ConnectionMethod.HardCoded) { //no glove was ever assigned... this.RetryConnection(); //keep trying! } else { SenseGlove myGlove = ExtractSenseGlove(SenseGloveCs.DeviceScanner.GetDevices()); if (myGlove != null) //The glove matches our parameters! { this.glove = myGlove; SenseGlove_Manager.SetUsed(this.glove.GetData(false).deviceID, true); this.glove.OnFingerCalibrationFinished += Glove_OnFingerCalibrationFinished; } else { if (this.canReport) { string message = this.gameObject.name + " looking for SenseGlove..."; if (this.connectionMethod == ConnectionMethod.FindNextLeftHand) { message = this.gameObject.name + " looking for left-handed SenseGlove..."; } else if (this.connectionMethod == ConnectionMethod.FindNextRightHand) { message = this.gameObject.name + " looking for right-handed SenseGlove..."; } SenseGlove_Debugger.Log(message); this.canReport = false; } this.elapsedTime = 0; } } } else if (this.connectionMethod == ConnectionMethod.HardCoded && !this.glove.IsConnected()) { //lost connection :( this.canReport = true; this.RetryConnection(); //keep trying! } else if (!gloveReady) { if (this.glove.GetData(false).dataLoaded) { bool runSetup = this.gloveData == null; //used to raise event only once! float[][] oldFingerLengths = null; float[][] oldStartPositions = null; if (this.gloveData != null) { oldFingerLengths = this.GetFingerLengths(); oldStartPositions = SenseGlove_Util.ToPosition(this.GetStartJointPositions()); } this.gloveData = this.glove.GetData(false); //get the latest data without calculating anything. this.rightHand = this.gloveData.isRight; //so that users can use this variable during setup. if (oldFingerLengths != null) { this.SetFingerLengths(oldFingerLengths); } //re-apply old fingerlengths, if possible. if (oldStartPositions != null) { this.SetStartJointPositions(oldStartPositions); } //re=apply joint positions, if possible. this.convertedGloveData = new SenseGlove_Data(this.gloveData, this.glove.communicator.samplesPerSecond, this.glove.TotalCalibrationSteps(), this.glove.TotalCalibrationSteps()); this.SetupWrist(); this.calibratedWrist = false; this.gloveReady = true; if (runSetup) { this.originalLengths = this.gloveData.handModel.GetFingerLengths(); this.originalJoints = this.gloveData.handModel.GetJointPositions(); SenseGlove_Debugger.Log("Sense Glove " + this.convertedGloveData.deviceID + " is ready!"); this.GloveLoaded(); //raise the event! } } } else //glove != null && gloveReady! { this.CheckCalibration(); //Update to the latest GloveData. this.UpdateGloveData(); //Calibrate once more after reconnecting to the glove. if (!calibratedWrist) { this.CalibrateWrist(); this.calibratedWrist = true; } //Update the public values automatically. if (connectionMethod != ConnectionMethod.HardCoded) { this.address = glove.communicator.Address(); } this.rightHand = glove.IsRight(); } } }