// Update is called once per frame void Update() { if (device.IsReading) { isActive = true; //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { for (int i = 0; i < packets.Count; i++) { //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To parse a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(i); int size = packets.get_packet_size(i); string content = System.Text.ASCIIEncoding.ASCII.GetString(packets.Buffer, indx, size); content = content.Substring(6, content.IndexOf("FINAL") - 6); string[] data = content.Split(new string[] { " " }, StringSplitOptions.None); accelerometer = new Vector3(float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2])); gyroscope = new Vector3(float.Parse(data[3]), float.Parse(data[4]), float.Parse(data[5])); magnetometer = new Vector3(float.Parse(data[6]), float.Parse(data[7]), float.Parse(data[8])); textRead = content; } } } else { } }
IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; while (device.IsConnected && device.IsReading) { //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { /* * parse packets, packets are ordered by indecies (0,1,2,3 ... N), * where Nth packet is the latest packet and 0th is the oldest/first arrived packet. * * Since this while loop is looping one time per frame, we only need the Nth(the latest potentiometer/joystick position in this frame). * */ int N = packets.Count - 1; //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To get a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); if (size == 4) { // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) int val1 = (packets.Buffer[indx + 1] << 8) | packets.Buffer[indx]; //Shift back 3 bits, because there was << 3 in Arduino val1 = val1 >> 3; int val2 = (packets.Buffer[indx + 3] << 8) | packets.Buffer[indx + 2]; //Shift back 3 bits, because there was << 3 in Arduino val2 = val2 >> 3; //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### //since any val is from 0 to 1023 float Axis1 = ((float)val1 / 1023f) * 2f - 1f; float Axis2 = ((float)val2 / 1023f) * 2f - 1f; logsOnScreen.text = Axis1 + "," + Axis2; MoveCube(Axis1, Axis2); /* * * Now Axis1 or Axis2 value will be in the range -1...1. Similar to Input.GetAxis * Check out : * * https://docs.unity3d.com/ScriptReference/Input.GetAxis.html * * https://unity3d.com/learn/tutorials/topics/scripting/getaxis */ } } yield return(null); } statusText.text = "Status : Done Reading"; }
//############### Reading Data ##################### //Please note that you don't have to use this Couroutienes/IEnumerator, you can just put your code in the Update() method IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; while (device.IsConnected && device.IsReading) { count = (count + 1) % 500; statusText.text = "Status : Connected & Can read" + count; //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { /* * parse packets, packets are ordered by indecies (0,1,2,3 ... N), * where Nth packet is the latest packet and 0th is the oldest/first arrived packet. * * Since this while loop is looping one time per frame, we only need the Nth(the latest potentiometer/joystick position in this frame). * */ int N = packets.Count - 1; //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To get a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); if (size == 2) { // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) val1 = packets.Buffer[indx]; val2 = packets.Buffer[indx + 1]; theta = val1; //theta=theta+1; t = theta / 255.0 * 2 * Mathf.PI; cube.transform.position = new Vector3(5 * Mathf.Cos((float)t), 0, 5 * Mathf.Sin((float)t)); //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### //since any val is from 0 to 1023 logsOnScreen.text = val1 + "," + val2; //MoveCube(Axis1,Axis2); /* * * Now Axis1 or Axis2 value will be in the range -1...1. Similar to Input.GetAxis * Check out : * * https://docs.unity3d.com/ScriptReference/Input.GetAxis.html * * https://unity3d.com/learn/tutorials/topics/scripting/getaxis */ } } yield return(null); } statusText.text = "Status : Done Reading"; }
IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status: Connected & Can Read"; while (device.IsConnected && device.IsReading) { BtPackets packets = device.readAllPackets(); if (packets != null) { int N = packets.Count - 1; int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); if (size == 6) { int fire = (packets.Buffer [indx + 1] << 8) | packets.Buffer [indx]; fire = fire >> 3; int acX = (packets.Buffer [indx + 3] << 8) | packets.Buffer [indx + 2]; acX = acX >> 3; int acY = (packets.Buffer [indx + 5] << 8) | packets.Buffer [indx + 4]; acY = acY >> 3; vrRotation = new Vector3(acX, acY, 0); if (statusText != null) { statusText.text = fire.ToString() + ", " + acX.ToString() + ", " + acY.ToString(); } if (fire == 1) { isShooting = true; } else if (fire == 0) { isShooting = false; } } } yield return(null); } }
IEnumerator ManageConnection(BluetoothDevice device) { while (true) { BtPackets packets = device.readAllPackets(); if (packets != null) { for (int i = 0; i < packets.Count; i++) { int idx = packets.get_packet_offset_index(i); int size = packets.get_packet_size(i); string content = System.Text.Encoding.ASCII.GetString(packets.Buffer, idx, size); //char direction; //char.TryParse(content, out direction); debug.text = content[0].ToString(); inputReceivedEvent.Invoke(content[0]); } } yield return(null); } }
//############### Reading Data ##################### //Please note that you don't have to use Couroutienes, you can just put your code in the Update() method //If you want to achieve a minimum delay please check the "High Bit Rate Terminal" demo IEnumerator ManageConnection(BluetoothDevice device) { //Manage Reading Coroutine //Switch to Terminal View InfoCanvas.SetActive(false); DataCanvas.SetActive(true); while (device.IsReading) { //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { /* * parse packets, packets are ordered by indecies (0,1,2,3 ... N), * where Nth packet is the latest packet and 0th is the oldest/first arrived packet. * */ for (int i = 0; i < packets.Count; i++) { //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To parse a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(i); int size = packets.get_packet_size(i); string content = System.Text.ASCIIEncoding.ASCII.GetString(packets.Buffer, indx, size); readDataText.add(device.Name, content); } } yield return(null); } //Switch to Menue View after reading stoped DataCanvas.SetActive(false); InfoCanvas.SetActive(true); }
IEnumerator ManageConnection(BluetoothDevice device) { send(); while (device.IsReading) { send(); BtPackets packets = device.readAllPackets(); if (packets != null) { send(); for (int i = 0; i < packets.Count; i++) { int indx = packets.get_packet_offset_index(i); int size = packets.get_packet_size(i); string content = System.Text.ASCIIEncoding.ASCII.GetString(packets.Buffer, indx, size); string[] Numbers = content.Split(':'); short.TryParse(Numbers[0], out ax); gas = short.Parse(Numbers[1]); send(); } } yield return(null); } }
IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; while (device.IsConnected && device.IsReading) { //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { /* * parse packets, packets are ordered by indecies (0,1,2,3 ... N), * where Nth packet is the latest packet and 0th is the oldest/first arrived packet. * * Since this while loop is looping one time per frame, we only need the Nth(the latest potentiometer/joystick position in this frame). * */ int N = packets.Count - 1; //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To get a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); if (size == 4) { // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) int val1 = (packets.Buffer[indx + 1] << 8) | packets.Buffer[indx]; //Shift back 3 bits, because there was << 3 in Arduino val1 = val1 >> 3; int val2 = (packets.Buffer[indx + 3] << 8) | packets.Buffer[indx + 2]; //Shift back 3 bits, because there was << 3 in Arduino val2 = val2 >> 3; //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### //since any val is from 0 to 1023 float Axis1 = ((float)val1 / 1023f) * 2f - 1f; if (Axis1 == 1) { eje = 2; } if (Axis1 == -1) { eje = 4; } float Axis2 = ((float)val2 / 1023f) * 2f - 1f; if (Axis2 == 1) { eje = 1; } if (Axis2 == -1) { eje = 3; } if (Axis1 <= 0.03 && Axis1 >= -0.03 && Axis2 <= 0.03 && Axis2 >= -0.03) { eje = 0; } if (eje == 0) { ahora = 0; antes = 0; } if (ahora == eje) { ahora = ahora; } else if (ahora != eje) { antes = ahora; ahora = eje; } if (antes == 1 && ahora == 2 || antes == 2 && ahora == 3 || antes == 3 && ahora == 4 || antes == 4 && ahora == 1) { direccion.text = "la direccion es adelante"; mover = 5; } else if (antes == 1 && ahora == 4 || antes == 4 && ahora == 3 || antes == 3 && ahora == 2 || antes == 2 && ahora == 1) { direccion.text = "la direccion es atras"; mover = -5; } if (antes == 0 && ahora == 0) { mover = 0; direccion.text = "sin movimineto"; } variables.text = "Ahora= " + ahora + " antes= " + antes + " eje= " + eje; axis.text = "Axis1= " + Axis1 + " Axis2= " + Axis2; logsOnScreen.text = eje.ToString(); MoveCube(mover, 0); /* * * Now Axis1 or Axis2 value will be in the range -1...1. Similar to Input.GetAxis * Check out : * * https://docs.unity3d.com/ScriptReference/Input.GetAxis.html * * https://unity3d.com/learn/tutorials/topics/scripting/getaxis */ } } yield return(null); } statusText.text = "Status : Done Reading"; }
//############### Reading Data ##################### IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; Main.BluetoothMessage("Remove"); while (device.IsReading) { BtPackets packets = device.readAllPackets(); if (packets != null) { for (int i = 0; i < packets.Count; i++) { //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To parse a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(i); int size = packets.get_packet_size(i); string content = System.Text.ASCIIEncoding.ASCII.GetString(packets.Buffer, indx, size); statusText.text = "MSG : " + content; if (content.Contains(" : ")) { //it's a movement command! string RightWheelInput = content.Remove(content.IndexOf(" : ")); string LeftWheelInput = content.Replace(RightWheelInput + " : ", ""); try { float RightWheel = float.Parse(RightWheelInput); float LeftWheel = float.Parse(LeftWheelInput); if ( //test to make sure the data makes sense, the blueooth module is picking up nasty power RightWheel > 0 && RightWheel < 65536 && LeftWheel > 0 && LeftWheel < 65536 ) { Wheelchaircontrol.WheelchairMovement(LeftWheel, RightWheel); //device.send(System.Text.Encoding.ASCII.GetBytes("MA100" + (char)10)); } } catch { } } else if (content.Contains("lagtestreply")) { //we got the reply from the arduino, now to send it back! WebSocketClient.messageforws = "messagePair lagtestreply"; WebSocketClient.LagTestSwitch = false; statusText.text = WebSocketClient.LagTestTime.ToString() + "ms for a reply from arduino"; WebSocketClient.messageforws = WebSocketClient.LagTestTime.ToString() + "ms for a reply from arduino"; WebSocketClient.LagTestTime = 0f; } } } if (!messagesent) { foreach (string message in MessageToSendList) { device.send(System.Text.Encoding.ASCII.GetBytes(message + (char)10)); } MessageToSendList.Clear(); //device.send(System.Text.Encoding.ASCII.GetBytes(MessageToSend + (char)10)); messagesent = true; } yield return(null); } statusText.text = "Status : Done Reading"; }
IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; while (device.IsConnected && device.IsReading) { //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { /* * parse packets, packets are ordered by indecies (0,1,2,3 ... N), * where Nth packet is the latest packet and 0th is the oldest/first arrived packet. * * Since this while loop is looping one time per frame, we only need the Nth(the latest potentiometer/joystick position in this frame). * */ int N = packets.Count - 1; //packets.Buffer contains all the needed packets plus a header of meta data (indecies and sizes) //To get a packet we need the INDEX and SIZE of that packet. int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); /*if(size == 4){ * // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) * int val1 = (packets.Buffer[indx+1] << 8) | packets.Buffer[indx]; * //Shift back 3 bits, because there was << 3 in Arduino * val1 = val1 >> 3; * int val2 = (packets.Buffer[indx+3] << 8) | packets.Buffer[indx+2]; * //Shift back 3 bits, because there was << 3 in Arduino * val2 = val2 >> 3; * * //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### * //since any val is from 0 to 1023 * float Axis1 = ((float)val1/1023f)*2f - 1f; * float Axis2 = ((float)val2/1023f)*2f - 1f; * * * * * logsOnScreen.text = Axis1 + "," + Axis2; * MoveCube(Axis1,Axis2); * * } * * * }*/ if (size == 8) { myText.text = "8"; // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) int val1 = (packets.Buffer[indx + 1] << 8) | packets.Buffer[indx]; //Shift back 3 bits, because there was << 3 in Arduino val1 = val1 >> 3; int val2 = (packets.Buffer[indx + 3] << 8) | packets.Buffer[indx + 2]; //Shift back 3 bits, because there was << 3 in Arduino val2 = val2 >> 3; int val3 = (packets.Buffer[indx + 5] << 8) | packets.Buffer[indx + 4]; val3 = val3 >> 3; int val4 = (packets.Buffer[indx + 7] << 8) | packets.Buffer[indx + 6]; val4 = val4 >> 3; //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### //since any val is from 0 to 1023 float Axis1 = ((float)val1 / 1023f) * 2f - 1f; float Axis2 = ((float)val2 / 1023f) * 2f - 1f; float Axis3 = ((float)val3 / 1023f) * 2f - 1f; float Axis4 = ((float)val4 / 1023f) * 2f - 1f; logsOnScreen.text = Axis1 + "," + Axis2 + " : " + val1 + " : " + val2; MoveCube(cube, Axis1, Axis2); MoveCube(cube2, Axis3, Axis4); } } yield return(null); } statusText.text = "Status : Done Reading"; }
IEnumerator ManageConnection(BluetoothDevice device) { statusText.text = "Status : Connected & Can read"; while (device.IsConnected && device.IsReading) { //polll all available packets BtPackets packets = device.readAllPackets(); if (packets != null) { int N = packets.Count - 1; int indx = packets.get_packet_offset_index(N); int size = packets.get_packet_size(N); if (size == 8) { // packets.Buffer[indx] equals lowByte(x1) and packets.Buffer[indx+1] equals highByte(x2) int val1 = (packets.Buffer[indx + 1] << 8) | packets.Buffer[indx]; //Shift back 3 bits, because there was << 3 in Arduino val1 = val1 >> 3; int val2 = (packets.Buffer[indx + 3] << 8) | packets.Buffer[indx + 2]; //Shift back 3 bits, because there was << 3 in Arduino val2 = val2 >> 3; int val3 = (packets.Buffer[indx + 5] << 8) | packets.Buffer[indx + 4]; val3 = val3 >> 3; int val4 = (packets.Buffer[indx + 7] << 8) | packets.Buffer[indx + 6]; val4 = val4 >> 3; //#########Converting val1, val2 into something similar to Input.GetAxis (Which is from -1 to 1)######### //since any val is from 0 to 1023 float Axis1 = ((float)val1 / 1023f) * 2f - 1f; if (Axis1 <= 1 && Axis1 >= 0.75) { eje = 2; } if (Axis1 >= -1 && Axis1 <= -0.75) { eje = 4; } float Axis2 = ((float)val2 / 1023f) * 2f - 1f; if (Axis2 <= 1 && Axis2 >= 0.85) { eje = 1; } if (Axis2 >= -1 && Axis2 <= -0.85) { eje = 3; } if (Axis1 <= 0.8 && Axis1 >= -0.6 && Axis2 <= 0.8 && Axis2 >= -0.6) { eje = 0; } if (eje == 0) { ahora = 0; antes = 0; } if (ahora == eje) { ahora = ahora; } else if (ahora != eje) { antes = ahora; ahora = eje; } if (antes == 1 && ahora == 2 || antes == 2 && ahora == 3 || antes == 3 && ahora == 4 || antes == 4 && ahora == 1) { j2.text = "adelante true j1"; adelante1 = true; atras1 = false; //direccion1 = "adelante"; //direccion.text = "la direccion es adelante"; mover = 5; } else if (antes == 1 && ahora == 4 || antes == 4 && ahora == 3 || antes == 3 && ahora == 2 || antes == 2 && ahora == 1) { j2.text = "atras true j1"; atras1 = true; adelante1 = false; //direccion1 = "atras"; //direccion.text = "la direccion es atras"; mover = -5; } if (antes == 0 && ahora == 0) { j2.text = "j1 sin mover"; atras1 = false; adelante1 = false; //direccion1 = "ND"; mover = 0; //direccion.text = "sin movimineto"; } float Axis3 = ((float)val3 / 1023f) * 2f - 1f; if (Axis3 <= 1 && Axis3 >= 0.85) { eje2 = 2; } if (Axis3 >= -1 && Axis3 <= -0.85) { eje2 = 4; } float Axis4 = ((float)val4 / 1023f) * 2f - 1f; if (Axis4 <= 1 && Axis4 >= 0.75) { eje2 = 1; } if (Axis4 >= -1 && Axis4 <= -0.75) { eje2 = 3; } if (Axis3 <= 0.8 && Axis3 >= -0.6 && Axis4 <= 0.8 && Axis4 >= -0.6) { eje2 = 0; } if (eje2 == 0) { ahora2 = 0; antes2 = 0; } if (ahora2 == eje2) { ahora2 = ahora2; } else if (ahora2 != eje2) { antes2 = ahora2; ahora2 = eje2; } if (antes2 == 1 && ahora2 == 2 || antes2 == 2 && ahora2 == 3 || antes2 == 3 && ahora2 == 4 || antes2 == 4 && ahora2 == 1) { j1.text = "j2 adelante true"; adelante2 = true; atras2 = false; //direccionj2 = "adelante"; //direccion2.text = "la direccion es adelante"; mover2 = 5; } else if (antes2 == 1 && ahora2 == 4 || antes2 == 4 && ahora2 == 3 || antes2 == 3 && ahora2 == 2 || antes2 == 2 && ahora2 == 1) { j1.text = "j2 atras true"; atras2 = true; adelante2 = false; //direccionj2 = "atras"; //direccion2.text = "la direccion es atras"; mover2 = -5; } if (antes2 == 0 && ahora2 == 0) { j1.text = "sin mover j2"; atras2 = false; adelante2 = false; //direccionj2 = "ND"; mover2 = 0; //direccion2.text = "sin movimineto"; } //movimiento del barco if (adelante1 && adelante2) { moverBarco = -1; giro = 0; barcotxt.text = "adelante " + moverBarco.ToString(); } else if (atras1 && atras2) { moverBarco = 1; giro = 0; barcotxt.text = "atras " + moverBarco.ToString(); } else { moverBarco = 0; barcotxt.text = "SN"; } if (moverBarco == 0 && adelante1 == true) { giro = 1; } else if (moverBarco == 0 && adelante2 == true) { giro = -1; } else if (moverBarco > 0 || moverBarco < 0 || adelante1 == false || adelante2 == false) { giro = 0; } else { giro = 0; } //GirarCubo(b, giro); //MoveCube(cube, 0, mover); //MoveCube(cube2, 0, mover2); } } yield return(null); } statusText.text = "Status : Done Reading"; }