void Update() { if (BtConnector.isConnected()) { if (BtConnector.available()) //check if there's an available data { messageFromMC = BtConnector.readLine(); //hold the data in messageFromMC //cause BtConnector.read () will delete the buffer if (messageFromMC.Length > 0) // recheck if there's an available data //this check is more important than BtConnector.available () //actually you could delete BtConnector.available () with no effect. { messages.Add(messageFromMC); //add the string to the list if (labelHeight * messages.Count >= (height - labelHeight)) { scrollPosition.y += labelHeight; //it will slide the ScrollView down } //if the whole screen filled with messages if (labelHeight * messages.Count >= height * 2) { messages.RemoveAt(0); //when the ScrollView filled ,delet old messages } } } } //read control data from the Module. controlData = BtConnector.readControlData(); }
void Update() { if (BtConnector.isConnected() && BtConnector.available()) //check connection status { messageFromMC = BtConnector.readBuffer(); //read bytes till a new line or a max of 100 bytes if (messageFromMC.Length > 0) { messages.Add(System.Text.Encoding.UTF8.GetString(messageFromMC)); //convert array of bytes into string if (labelHeight * messages.Count >= (height - labelHeight)) { scrollPosition.y += labelHeight; //slide the Scrollview down,when the screen filled with messages } if (labelHeight * messages.Count >= height * 2) { messages.RemoveAt(0); //remove old messages,when ScrollView filled } } } //read control data from the Module. controlData = BtConnector.readControlData(); }
void OnGUI() { GUI.Label(new Rect(0, 0, Screen.width * 0.15f, Screen.height * 0.1f), "Module Name "); stringToEdit = GUI.TextField(new Rect(Screen.width * 0.15f, 0, Screen.width * 0.8f, Screen.height * 0.1f), stringToEdit); GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, Screen.height * 0.1f), "Arduino Says : " + fromArduino); GUI.Label(new Rect(0, Screen.height * 0.3f, Screen.width, Screen.height * 0.1f), "from PlugIn : " + BtConnector.readControlData()); if (GUI.Button(new Rect(0, Screen.height * 0.4f, Screen.width, Screen.height * 0.1f), "Connect")) { if (!BtConnector.isBluetoothEnabled()) { BtConnector.askEnableBluetooth(); } else { BtConnector.moduleName(stringToEdit); //incase User Changed the Bluetooth Name BtConnector.connect(); } } ///the hidden code here let you connect directly without askin the user /// if you want to use it, make sure to hide the code from line 23 to lin 33 /* * if( GUILayout.Button ("Connect")){ * * startConnection = true; * * } * * if(GUI.Button(new Rect(0,Screen.height*0.4f,Screen.width,Screen.height*0.1f), "Connect")) * { * if (!BtConnector.isBluetoothEnabled ()){ * BtConnector.enableBluetooth(); * * } else { * * BtConnector.connect(); * * startConnection = false; * * } * * } */ ///////////// if (GUI.Button(new Rect(0, Screen.height * 0.6f, Screen.width, Screen.height * 0.1f), "sendChar")) { if (BtConnector.isConnected()) { BtConnector.sendChar('h'); BtConnector.sendChar('e'); BtConnector.sendChar('l'); BtConnector.sendChar('l'); BtConnector.sendChar('o'); BtConnector.sendChar('\n'); //because we are going to read it using .readLine() which reads lines. //don't call the send method multiple times unless you really need to, because it will kill performance. } } if (GUI.Button(new Rect(0, Screen.height * 0.5f, Screen.width, Screen.height * 0.1f), "sendString")) { if (BtConnector.isConnected()) { BtConnector.sendString("Hii"); BtConnector.sendString("you can do this"); //BtConnector.sendBytes(new byte[] {55,55,55,10}); //don't call the send method multiple times unless you really need to, because it will kill performance. } } if (GUI.Button(new Rect(0, Screen.height * 0.7f, Screen.width, Screen.height * 0.1f), "Close")) { BtConnector.close(); } if (GUI.Button(new Rect(0, Screen.height * 0.8f, Screen.width, Screen.height * 0.1f), "readData")) { if (BtConnector.available()) { fromArduino = BtConnector.readLine(); } } if (GUI.Button(new Rect(0, Screen.height * 0.9f, Screen.width, Screen.height * 0.1f), "change ModuleName")) { BtConnector.moduleName(stringToEdit); } }
void Update() { //q1.transform.Rotate(0.1f, 0, 0.1f); //q2.transform.Rotate(-0.1f, 0, -0.1f); connected = BtConnector.isConnected();//check connection status if (BtConnector.isConnected() != _connectButtonStatus) { _connectButtonStatus = BtConnector.isConnected(); connectButtonImage.color = _connectButtonStatus == true ? connectedButtonColour : notConnectedButtonColour; connectButtonText.text = _connectButtonStatus == true ? "connected!" : "connect"; if (_connectButtonStatus == true) { PlayerPrefs.SetString("lastConnectedDeviceName", _lastNameEnteredToConnect); if (ifcal) { UIManager.Instance.notConnectedGameErrorObject.SetActive(false); } } else { UIManager.Instance.notConnectedGameErrorObject.SetActive(true); UIManager.Instance.setMinToggle.isOn = false; UIManager.Instance.setMaxToggle.isOn = false; fullyCalibrated = false; // TODO : pause game and allow reconnection if (GameManager.Instance != null) { GameManager.Instance.ExitGame(); } } } if (BtConnector.isConnected() && BtConnector.available()) //check connection status { messageFromMC = BtConnector.readBuffer(); //read bytes till a new line or a max of 100 bytes if (messageFromMC.Length > 0) { if (messageFromMC.Length == 16) { qq1.w = (float)(messageFromMC[0] * 256 + messageFromMC[1]); // (10000.0f); qq1.x = (float)(messageFromMC[2] * 256 + messageFromMC[3]); // (10000.0f); qq1.y = (float)(messageFromMC[4] * 256 + messageFromMC[5]); // (10000.0f); qq1.z = (float)(messageFromMC[6] * 256 + messageFromMC[7]); // (10000.0f); qq2.w = (float)(messageFromMC[8] * 256 + messageFromMC[9]); // (10000.0f); qq2.x = (float)(messageFromMC[10] * 256 + messageFromMC[11]); // (10000.0f); qq2.y = (float)(messageFromMC[12] * 256 + messageFromMC[13]); // (10000.0f); qq2.z = (float)(messageFromMC[14] * 256 + messageFromMC[15]); // (10000.0f); if (qq1.w >= 32768) { qq1.w = qq1.w - 65536; } if (qq1.x >= 32768) { qq1.x = qq1.x - 65536; } if (qq1.y >= 32768) { qq1.y = qq1.y - 65536; } if (qq1.z >= 32768) { qq1.z = qq1.z - 65536; } if (qq2.w >= 32768) { qq2.w = qq2.w - 65536; } if (qq2.x >= 32768) { qq2.x = qq2.x - 65536; } if (qq2.y >= 32768) { qq2.y = qq2.y - 65536; } if (qq2.z >= 32768) { qq2.z = qq2.z - 65536; } ////////////////////////////////////////////////////////// if (qq1.w > 10000) { qq1.w -= 10000; } if (qq1.x > 10000) { qq1.x -= 10000; } if (qq1.y > 10000) { qq1.y -= 10000; } if (qq1.z > 10000) { qq1.z -= 10000; } if (qq2.w > 10000) { qq2.w -= 10000; } if (qq2.x > 10000) { qq2.x -= 10000; } if (qq2.y > 10000) { qq2.y -= 10000; } if (qq2.z > 10000) { qq2.z -= 10000; } qq1.w /= 10000; qq1.x /= 10000; qq1.y /= 10000; qq1.z /= 10000; qq2.w /= 10000; qq2.x /= 10000; qq2.y /= 10000; qq2.z /= 10000; float s1, s2; s1 = (qq1.w * qq1.w) + (qq1.x * qq1.x) + (qq1.y * qq1.y) + (qq1.z * qq1.z); s2 = (qq2.w * qq2.w) + (qq2.x * qq2.x) + (qq2.y * qq2.y) + (qq2.z * qq2.z); if ((int)(s1 * 100) == 99 || (int)(s1 * 100) == 100) { q1.transform.rotation = qq1; } if ((int)(s2 * 100) == 99 || (int)(s2 * 100) == 100) { q2.transform.rotation = qq2; } if (((int)(s1 * 100) == 99 || (int)(s1 * 100) == 100) && ((int)(s2 * 100) == 99 || (int)(s2 * 100) == 100)) { Angletext.text = "Not Set!"; if (ifcal) { q3.transform.rotation = qq3 = product(divideQbyR(qq2, qq2_0), qq1_0); diff2 = divideQbyR(qq1, qq3); //float currentAngle = Quaternion.Angle(qqzero, diff2); arm.transform.localRotation = diff2;//current position v3cur = (toparm.transform.position - sp.transform.position); v3cur_ = Vector3.ProjectOnPlane(v3cur, Vector3.Cross(v3max, v3min)); currentAngle = Vector3.Angle(v3min, v3cur_); if (Mathf.Abs((Vector3.Angle(v3min, v3max) + Vector3.Angle(v3min, v3cur_)) - Vector3.Angle(v3max, v3cur_)) < 1) { currentAngle *= -1; } else if (Mathf.Abs(Vector3.Angle(v3min, v3max) + Vector3.Angle(v3min, v3cur_) + Vector3.Angle(v3max, v3cur_) - 360) < 1) { currentAngle *= -1; } currentAngle = (angold * 3 + currentAngle) / 4.0f; Angletext.text = "\n min: " + v3min.ToString() + "\n v3cur: " + v3cur.ToString() + "\n v3cur_: " + v3cur_.ToString() + "\n angle mm: " + Vector3.Angle(v3min, v3max).ToString() + "\n angle n : " + Vector3.Angle(v3min, v3cur_).ToString() + "\n angle x : " + Vector3.Angle(v3max, v3cur_).ToString() + "\n CurrentAngle :" + currentAngle.ToString(); // don't want to increase max angle in hockey or pop pop //if (currentAngle > maxAngle && (Hockey.Instance == null || PopManager.instance != null)) // maxAngle = currentAngle; // if there is a gamemmanger in scene, it means we're acting on input if (GameManager.Instance != null) { // update image of current value GameManager.Instance.UpdateCurrentValue(minAngle, currentAngle, maxAngle); // check if we've reset bool hasReset = GameManager.Instance.hasResetToggle != null ? GameManager.Instance.hasResetToggle.isOn : true; // accept input within 10 % of the maximum angle if (GameManager.Instance.GameInProgress && !GameManager.Instance.locked && hasReset && (Mathf.InverseLerp(minAngle, maxAngle, currentAngle) > 0.90f || Input.GetKeyDown(KeyCode.Space))) { GameManager.Instance.Action(); } // else register a reset if we're within 10% of the minimum angle else if (GameManager.Instance.GameInProgress && !hasReset && Mathf.InverseLerp(minAngle, maxAngle, currentAngle) < 0.10f) { GameManager.Instance.hasResetToggle.isOn = true; } if (GameManager.Instance.GameInProgress) { thisGamesFrequencies.Add(currentAngle); thisGamesMaxAngles.Add(maxAngle); } } angold = currentAngle; } else { q3.transform.rotation = qq2; } } } } //if (t.text.Length > 500) // t.text = ""; //convert array of bytes into string /////////////////////////////// //if (labelHeight * messages.Count >= (height - labelHeight)) // scrollPosition.y += labelHeight;//slide the Scrollview down,when the screen filled with messages //if (labelHeight * messages.Count >= height * 2) // messages.RemoveAt (0);//remove old messages,when ScrollView filled } //read control data from the Module. controlData = BtConnector.readControlData(); }
void Update() { // Display current status of Bluetooth module statusText.text = BtConnector.readControlData(); // If already connected if (BtConnector.isConnected()) { objectReceiver.ActivateSensor(true); // Send PING if (!waitResponse) { BtConnector.sendString("PING"); waitResponse = true; } // Check for PONG (non-blocking) if (BtConnector.available()) { string response = BtConnector.readLine(); if (response.Length > 0) { waitResponse = false; if (response[0] == ' ') { // string to tell us whether or not to activate the stasis activateText.text = activateStasis; powerupText.text = response.Substring(3); activateStasis = response.Substring(1, 1); powerUpString = response.Substring(3); if (activateStasis[0] == '1') { objectReceiver.FireCharge(); } objectReceiver.PowerUp(float.Parse(powerUpString)); } else if (response == "PONG BUTTON ON") { bluetoothText.text = "Button On!"; } else if (response == "PONG BUTTON OFF") { bluetoothText.text = "..."; } else { bluetoothText.text = response; } } } else { bluetoothText.text = "Not Available"; } } else { bluetoothText.text = "Not Connected"; //BtConnector.connect(); } }