public static byte [] readBuffer(int length) { if (BtConnection.mode() != 1) { BtConnection.listen(true, length, false); } return(BtConnector.readBuffer()); }
void HandleBlueTooth() { //input UFOneAPI.AddData(BtConnector.readBuffer()); //output BtConnector.sendBytes(UFOneAPI.GetRawCommand()); UFOneAPI.Update(); }
void Update() { UFoneInterface.AddData(BtConnector.readBuffer()); UFoneInterface.Update(); Debug.Log("Picked:" + BtConnector.isDevicePicked + ":" + BtConnector.getPickedDeviceName()); //if(BtConnector.isConnected()) //UFoneInterface.SetRawRC(1400,1410,1420,1000,1950,1500,1500,1501); //SendData byte[] dataToSend = UFoneInterface.GetRawCommand(); if (dataToSend.Length > 0) { BtConnector.sendBytes(dataToSend); Debug.Log("sendBytes:" + byteToIntStr(dataToSend)); } //GetData }
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 : " + ByteArrayToString(buffer)); 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.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. } } 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"); } } 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")) { //fromArduino = BtConnector.readLine(); buffer = BtConnector.readBuffer(); } if (GUI.Button(new Rect(0, Screen.height * 0.9f, Screen.width, Screen.height * 0.1f), "change ModuleName")) { BtConnector.moduleName(stringToEdit); } }
void Start() { DontDestroyOnLoad(this); joystick = GameObject.Find("MobileJoystick"); jumpButton = GameObject.Find("JumpButton"); health = GameObject.FindWithTag("Health"); header = false; counter = 0; buffer5HasRead = false; calibrateControllerCounter = 0; posXCali = 0; posYCali = 0; newStartPosX = 0; newStartPosY = 0; //A bluetooth controller is CONNECTED, don't use the virtual joystick and button if (BtConnector.isConnected()) { buffer = BtConnector.readBuffer(); counter = 0; buffer5HasRead = false; for (int i = 0; i < buffer.Length; i++) { if (buffer [i] == 10 && counter == 0) { header = true; } else if (header == true) { buffer5 [counter] = buffer [i]; counter++; if (counter == 5) { header = false; counter = 0; buffer5HasRead = true; break; } } } if (buffer5HasRead == true) { posX = (sbyte)buffer5 [1]; posY = (sbyte)buffer5 [2]; button = (sbyte)buffer5 [3]; parity = (sbyte)buffer5 [4]; } //startPos = new Vector3(10,-4, 0); //TODO: Change the default startPos for the controller startPos = new Vector3(-posX, posY, 0); //TODO: Change the default startPos for the controller } //If a bluetooth controller is NOT CONNECTED, use the virtual joystick and button else { if (joystick) { startPos = joystick.transform.position; } if (jumpButton) { EventTrigger eventTrigger = jumpButton.GetComponent <EventTrigger> (); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.PointerDown; entry.callback = new EventTrigger.TriggerEvent(); UnityEngine.Events.UnityAction <BaseEventData> callback = new UnityEngine.Events.UnityAction <BaseEventData> (FireBullet); entry.callback.AddListener(callback); eventTrigger.delegates.Add(entry); //eventTrigger.enabled = false; } } }
// Rotate player in sync with joystick and move in that direction void FixedUpdate() { if (BtConnector.isConnected()) { buffer = BtConnector.readBuffer(); counter = 0; buffer5HasRead = false; for (int i = 0; i < buffer.Length; i++) { if (buffer [i] == 10 && counter == 0) { header = true; } else if (header == true) { buffer5 [counter] = buffer [i]; counter++; if (counter == 5) { header = false; counter = 0; buffer5HasRead = true; break; } } } //If successfully read, buffer5 contains the 5 bytes (excluding the newline) sent by the controller //buffer5[0] = 4 //buffer5[1] = p->x //buffer5[2] = p->y //buffer5[3] = p->buttons //buffer5[4] = p->parity if (buffer5HasRead == true) { posX = (sbyte)buffer5 [1]; posY = (sbyte)buffer5 [2]; button = (sbyte)buffer5 [3]; parity = (sbyte)buffer5 [4]; } //Eliminate any bad initial conditions introduced during the menu scene if (Mathf.Abs(startPos.x) > 5 || Mathf.Abs(startPos.y) > 5) { startPos = new Vector3(-posX, posY, 0); } //Fire bullet if A (has a value of 0b00010) button is pressed if (button == 2) { FireBullet2(); //button = 0; } newPos = new Vector3(-posX, posY, 0); Vector3 diff = newPos - startPos; diff.Normalize(); float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg; var pos = this.transform.position; if ((Mathf.Abs(diff.x) > 0.1 || Mathf.Abs(diff.y) > 0.1) && GetComponent <NetworkView> ().isMine) { transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90); GetComponent <Rigidbody2D> ().AddForce(gameObject.transform.up * speed); } // Prevent the player from going off screen if (pos.x < 25) { pos.x = 25; } else if (pos.x > Screen.width - 25) { pos.x = Screen.width - 25; } if (pos.y < 25) { pos.y = 25; } else if (pos.y > Screen.height - 25) { pos.y = Screen.height - 25; } this.transform.position = pos; } else { if (joystick) { Vector3 diff = joystick.transform.position - startPos; diff.Normalize(); float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg; var pos = this.transform.position; if ((Mathf.Abs(diff.x) > 0.1 || Mathf.Abs(diff.y) > 0.1) && GetComponent <NetworkView> ().isMine) { transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90); GetComponent <Rigidbody2D> ().AddForce(gameObject.transform.up * speed); } // Prevent the player from going off screen if (pos.x < 25) { pos.x = 25; } else if (pos.x > Screen.width - 25) { pos.x = Screen.width - 25; } if (pos.y < 25) { pos.y = 25; } else if (pos.y > Screen.height - 25) { pos.y = Screen.height - 25; } this.transform.position = pos; } } }
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(); }