Exemple #1
0
    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();
    }
Exemple #2
0
    void update()
    {
        string temp = BtConnector.readLine();

        if (temp.Length > 0)
        {
            BT = temp;
        }

        if (BT.Length >= 2)
        {
            joystickxy[0] = BT[0] - 48;             // -48 used to convert char to int

            joystickxy[1] = BT[1] - 48;
        }         // do whatever you want with these data

        //// now lets try to convert these values into something
        /// similar to Input.GetAxis()
        Vector2 tempVector = new Vector2(joystickxy [0], joystickxy [1]);

        curAccess = Vector3.Lerp(curAccess, tempVector - new Vector2(4, 4), Time.deltaTime / 0.5f);


        float VerticalAxis   = Mathf.Clamp(curAccess.y, -1, 1);
        float HorizantalAxis = Mathf.Clamp(curAccess.x, -1, 1);


        //now these two variables are from -1 to 1
        // to increase resolution you need to increase
        // joystick reading scale
    }
Exemple #3
0
 public void Disconnect()
 {
     if (BtConnector.isConnected())
     {
         BtConnector.close();
     }
 }
Exemple #4
0
        private void touchesEndedHandler(object sender, TouchEventArgs e)
        {
            var count = e.Touches.Count;



            for (var i = 0; i < count; i++)
            {
                var            touch = e.Touches[i];
                TouchProxyBase proxy;
                if (!proxies.TryGetValue(touch.Id, out proxy))
                {
                    return;
                }
                proxies.Remove(touch.Id);
                pool.Release(proxy);
            }

            touchCount = 0;
            finGesture = "SX";
            BtConnector.sendString(finGesture);
            Debug.Log("SX");
            for (int i = 0; i < 3; i++)
            {
                dirArray1[i] = 'X';
                dirArray2[i] = 'X';
                dirArray3[i] = 'X';
                dirArray4[i] = 'X';
            }
        }
Exemple #5
0
    void UpdateView()
    {
        ArmButton.SetActive(!Armed && BtConnector.isConnected());
        DisArmButton.SetActive(Armed && BtConnector.isConnected());
        //JoySticks.SetActive (BtConnector.isConnected ());

        if (BtConnector.isConnected())
        {
            ButtonConnect.SetActive(false);
            ButtonDisconnect.SetActive(true);
        }
        else
        {
            if (BtConnector.isDevicePicked)
            {
                ButtonConnect.SetActive(true);
            }
            else
            {
                ButtonConnect.SetActive(false);
            }


            ButtonDisconnect.SetActive(false);
        }
    }
    public static byte [] readBuffer(int length)
    {
        if (BtConnection.mode() != 1)
        {
            BtConnection.listen(true, length, false);
        }

        return(BtConnector.readBuffer());
    }
Exemple #7
0
    void HandleBlueTooth()
    {
        //input
        UFOneAPI.AddData(BtConnector.readBuffer());

        //output
        BtConnector.sendBytes(UFOneAPI.GetRawCommand());

        UFOneAPI.Update();
    }
Exemple #8
0
    void Start()
    {
        DontDestroyOnLoad(this);
        Application.LoadLevel("menu");
        BtConnector.moduleName("HC-06");
        //MasterServer.ipAddress = "149.142.250.42";

        /*MasterServer.ipAddress = "169.232.80.18";
         * MasterServer.port = 23466;
         * Network.natFacilitatorIP = "169.232.80.18";
         * Network.natFacilitatorPort = 50005;*/
    }
Exemple #9
0
    void OnGUI()
    {
        GUIStyle style = GUI.skin.GetStyle("label");

        style.fontSize = 20;

        if (!connected && GUI.Button(gRect, "Connect"))          //the Connect button will disappear when connecttion done
        // and appear again if it disconnected
        {
            if (!BtConnector.isBluetoothEnabled())
            {
                BtConnector.askEnableBluetooth();
            }
            else
            {
                BtConnector.connect();
            }
        }


        connected = BtConnector.isConnected();         //check connection status



        scrollPosition = GUI.BeginScrollView(new Rect(0, height * 0.1f, Screen.width, height), scrollPosition, new Rect(0, 0, Screen.width, height * 2));

        for (int i = 0; i < messages.Count; i++)        //display the List of messages
        {
            GUI.Label(new Rect(0, labelHeight * i, Screen.width, labelHeight), messages[i]);
        }

        GUI.EndScrollView();



        messageToMC = GUI.TextField(new Rect(0, Screen.height * 0.9f, Screen.width * 0.9f, Screen.height * 0.1f), messageToMC);
        if (GUI.Button(new Rect(Screen.width * 0.9f, Screen.height * 0.9f, Screen.width * 0.1f, Screen.height * 0.1f), "Send"))
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(messageToMC + '\n'.ToString());             //convert string to array of bytes and add a new line
            BtConnector.sendBytes(bytes);
        }


        if (GUI.Button(new Rect(Screen.width * 0.9f, 0, Screen.width * 0.1f, Screen.height * 0.1f), "Close Connection"))
        {
            BtConnector.close();
            connected = false;
        }

        GUI.Label(new Rect(0, 0, Screen.width * 0.9f, Screen.height * 0.1f), "From Plugin : " + controlData);
    }
    double height;                                                                                //

    //---------------------------------------------------INITIALIZATION---------------------------------------------------------------
    void Start()
    {
        t          = Time.time;                                                                   // record start time
        timebuffer = Time.time;
        if (!BtConnector.isBluetoothEnabled())                                                    // if phone's bluetooth is disabled
        {
            BtConnector.askEnableBluetooth();
        }                                                                                         // prompt user to enable it
        BtConnector.moduleName(mcID);                                                             // tell bt connecter the name of the bt module we are going to connect to
        BtConnector.connect();                                                                    // connect phone to bt module
        droneh        = "0"; dronef = "0"; droner = "0"; dronel = "0";                            // set all controls initially to zero
        lookAngleZero = Cardboard.SDK.HeadPose.Orientation.eulerAngles.y;                         // set phone reference position
        height        = 0;                                                                        // set initial height to zero
    }
Exemple #11
0
    public void Connect()
    {
        if (!BtConnector.isBluetoothEnabled())
        {
            BtConnector.askEnableBluetooth();
        }
        else
        {
            _lastNameEnteredToConnect = deviceNameInput.text;
            BtConnector.moduleName(deviceNameInput.text); //incase User Changed the Bluetooth Name
            int result = BtConnector.connect();
        }

        connected = BtConnector.isConnected();//check connection status
    }
Exemple #12
0
    void Start()
    {
        bluetoothText.text = "";

        // Initialize module and connect to paired Bluetooth device
        BtConnector.moduleName("HC-06");
        if (!BtConnector.isBluetoothEnabled())
        {
            BtConnector.askEnableBluetooth();
        }
        else
        {
            BtConnector.connect();
        }

        Assert.IsTrue(objectReceiver);
    }
Exemple #13
0
    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, Screen.width * 0.15f, Screen.height * 0.1f), "Module Name ");


        if (BtConnector.isConnected())
        {
            if (GUI.Button(new Rect(0, Screen.height * 0.5f, Screen.width, Screen.height * 0.1f), "RequestIMU"))
            {
                UFoneInterface.RequestIMU();
            }
            if (GUI.Button(new Rect(0, Screen.height * 0.6f, Screen.width, Screen.height * 0.1f), "RequestRC"))
            {
                UFoneInterface.RequestRC();
            }

            if (GUI.Button(new Rect(0, Screen.height * 0.7f, Screen.width, Screen.height * 0.1f), "ARM"))
            {
                UFoneInterface.SetRawRC(1400, 1410, 1420, 1000, 1950, 1500, 1500, 1501);
            }

            if (GUI.Button(new Rect(0, Screen.height * 0.8f, Screen.width, Screen.height * 0.1f), "DisArm"))
            {
                UFoneInterface.SetRawRC(1400, 1410, 1420, 1000, 1050, 1500, 1500, 1500);
            }
        }
        else
        {
            if (GUI.Button(new Rect(0, Screen.height * 0.4f, Screen.width, Screen.height * 0.1f), "Try Connect"))
            {
                if (!BtConnector.isBluetoothEnabled())
                {
                    BtConnector.askEnableBluetooth();
                }
                else
                {
                    BtConnector.connect();
                }
            }

            if (GUI.Button(new Rect(0, Screen.height * 0.9f, Screen.width, Screen.height * 0.1f), "Show"))
            {
                BtConnector.showDevices();
            }
        }
    }
Exemple #14
0
    void OnGUI()
    {
        GUIStyle style = GUI.skin.GetStyle("label");

        style.fontSize = 20;

        if (!BtConnector.isConnected() && GUI.Button(gRect, "Connect"))           //the Connect button will disappear when connecttion done
        // and appear again if it disconnected
        {
            if (!BtConnector.isBluetoothEnabled())
            {
                BtConnector.askEnableBluetooth();
            }
            else
            {
                BtConnector.connect();
            }
        }



        scrollPosition = GUI.BeginScrollView(new Rect(0, height * 0.1f, Screen.width, height), scrollPosition, new Rect(0, 0, Screen.width, height * 2));

        for (int i = 0; i < messages.Count; i++)        //display the List of messages
        {
            GUI.Label(new Rect(0, labelHeight * i, Screen.width, labelHeight), messages[i]);
        }

        GUI.EndScrollView();



        messageToMC = GUI.TextField(new Rect(0, Screen.height * 0.9f, Screen.width * 0.9f, Screen.height * 0.1f), messageToMC);
        if (GUI.Button(new Rect(Screen.width * 0.9f, Screen.height * 0.9f, Screen.width * 0.1f, Screen.height * 0.1f), "Send"))
        {
            BtConnector.sendString(messageToMC);
        }


        if (GUI.Button(new Rect(Screen.width * 0.9f, 0, Screen.width * 0.1f, Screen.height * 0.1f), "Close Connection"))
        {
            BtConnector.close();
        }

        GUI.Label(new Rect(0, 0, Screen.width * 0.9f, Screen.height * 0.1f), "From Plugin : " + controlData);
    }
Exemple #15
0
    public static bool autoConnect(float time)
    {
        int tmp = -3;


        while (!BtConnector.isConnected() && time > 0)
        {
            time -= Time.deltaTime;

            if (tmp == -3)
            {
                BtConnector.connect();
            }

            tmp = BtConnector.controlData();
        }
        return(BtConnector.isConnected());
    }
Exemple #16
0
    public void Connect()
    {
        if (BtConnector.isDevicePicked)
        {
            if (!BtConnector.isBluetoothEnabled())
            {
                BtConnector.askEnableBluetooth();
            }
            else
            {
                string name = BtConnector.getPickedDeviceName();
                BtConnector.moduleName(name);
                BtConnector.connect();

                Debug.Log("try to connect " + name);
            }
        }
    }
    void Update()
    {
        arduinotext = BtConnector.readLine();

        if (PhotonNetwork.connected && PhotonNetwork.isMasterClient && flag)
        {
            roundtime          -= Time.smoothDeltaTime;
            roundtimetext.text  = roundtime.ToString("N0");
            roundtimetextR.text = roundtimetext.text;
            ViewRoundTime(roundtime.ToString("N0"));
            if (roundtime < 0.0f)
            {
                ViewScore();
                PhotonNetwork.DestroyAll();
                flag = false;
            }
        }
    }
Exemple #18
0
    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 Start()
    {
        BtConnector.moduleMAC("20:15:07:02:69:59");

        if (!BtConnector.isBluetoothEnabled())
        {
            BtConnector.askEnableBluetooth();
        }
        else
        {
            BtConnector.moduleMAC("20:15:07:02:69:59");
            BtConnector.connect();
        }


        photonView = GetComponent <PhotonView> ();
        messages   = new Queue <string> (messageCount);

        PhotonNetwork.logLevel = PhotonLogLevel.Full;
        PhotonNetwork.ConnectUsingSettings("1.0");
        StartCoroutine("UpdateConnectionString");
    }
Exemple #20
0
    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();
    }
Exemple #21
0
    // Use this for initialization
    void Start()
    {
        UFOneAPI = new MultiWiiProtocol();
        BtConnector.askEnableBluetooth();

        UFOneAPI.DebugMessageEvent += (string log) =>
        {
            Debug.Log(log);
        };

        leftStick.ControllerMovedEvent += (position, stick) =>
        {
            int dif  = (RANGE_MAX - RANGE_MIN) / 2;
            int mean = (RANGE_MAX + RANGE_MIN) / 2;

            ROLL  = (ushort)(position.x * dif / 4 + mean);
            PITCH = (ushort)(position.y * dif / 4 + mean);

            Debug.Log("Left:" + position.x + ":" + position.y);
        };

        leftStick.FingerLiftedEvent += (CNAbstractController o) =>
        {
            ROLL  = _defaultRoll;
            PITCH = _defaultPitch;
        };

        rightStick.ControllerMovedEvent += (position, stick) =>
        {
            int dif  = (RANGE_MAX - RANGE_MIN) / 2;
            int mean = (RANGE_MAX + RANGE_MIN) / 2;

            YAW      = (ushort)(position.x * dif / 4 + mean);
            THROTTLE = (ushort)(position.y * dif + mean);

            Debug.Log("Right:" + position.x + ":" + position.y);
        };
    }
Exemple #22
0
 void OnGUI()
 {
     if (!Network.isClient && !Network.isServer)
     {
         if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
         {
             StartServer();
         }
         if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
         {
             RefreshHostList();
         }
         if (hostList != null)
         {
             for (int i = 0; i < hostList.Length; i++)
             {
                 if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList [i].gameName))
                 {
                     JoinServer(hostList [i]);
                 }
             }
         }
         if (GUI.Button(new Rect(400, 250, 250, 100), "Connect"))
         {
             if (!BtConnector.isBluetoothEnabled())
             {
                 BtConnector.askEnableBluetooth();
             }
             else
             {
                 BtConnector.connect();
             }
             BtConnector.stopListen();
         }
     }
 }
Exemple #23
0
    void Start()
    {
        BtConnector.moduleName(stringToEdit);



        //BtConnector.sho

        UFoneInterface = new MultiWiiProtocol();
        UFoneInterface.DebugMessageEvent += (string log) =>
        {
            Debug.Log(log);
        };

        UFoneInterface.IMUResultEvent += (short accx, short accy, short accz, short gyrx, short gyry, short gyrz, short magx, short magy, short magz) =>
        {
            Debug.Log("IMU: accX=" + accx + " accY=" + accy + " accZ=" + accz + " gyrx=" + gyrx + " gyry=" + gyry + " gyrz=" + gyrz + " magx=" + magx + " magy=" + magy + " magz=" + magz);
        };

        UFoneInterface.RCResultEvent += (ushort Roll, ushort Pitch, ushort Yaw, ushort Throttle, ushort AUX1, ushort AUX2, ushort AUX3, ushort AUX4) =>
        {
            Debug.Log("RC: Roll=" + Roll + " PITCH=" + Pitch + " Yaw=" + Yaw + " Throttle=" + Throttle + " AUX1=" + AUX1 + " AUX2=" + AUX2 + " AUX3=" + AUX3 + " AUX4=" + AUX4);
        };
    }
 void OnGUI()
 {
     GUI.TextArea(new Rect(Screen.width - 500, 0, 500, 100), BtConnector.readControlData());
     GUI.TextArea(new Rect(Screen.width - 500, 100, 500, 100), BtConnector.readLine());
     GUI.TextArea(new Rect(Screen.width - 500, 200, 500, 100), BtConnector.read());
 }
    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()
 {
     //use one of the following two methods to change the default bluetooth module.
     //BtConnector.moduleMAC("00:13:12:09:55:17");
     BtConnector.moduleName("HC-06");
 }
Exemple #27
0
        void OnGUI()
        {
            GUI.Label(new Rect(0, Screen.height * 0.3f, Screen.width, Screen.height * 0.1f), "from PlugIn : " + BtConnector.readControlData());
            GUI.Label(new Rect(0, Screen.height * 0.4f, Screen.width, Screen.height * 0.1f), "Gesture     : " + finGesture);
            GUI.Label(new Rect(0, Screen.height * 0.5f, Screen.width, Screen.height * 0.1f), "Debug       : " + curPos1 + " dir:" + dir1);  //debug
            GUI.Label(new Rect(0, Screen.height * 0.55f, Screen.width, Screen.height * 0.1f), "            : " + curPos2 + " dir:" + dir2); //debug
            GUI.Label(new Rect(0, Screen.height * 0.6f, Screen.width, Screen.height * 0.1f), "            : " + curPos3 + " dir:" + dir3);  //debug
            GUI.Label(new Rect(0, Screen.height * 0.65f, Screen.width, Screen.height * 0.1f), "            : " + curPos4 + " dir:" + dir4); //debug

            GUI.Label(new Rect(Screen.width * 0.95f, Screen.height * 0.40f, Screen.width * 0.5f, Screen.height * 0.1f), gestureList[0]);
            GUI.Label(new Rect(Screen.width * 0.95f, Screen.height * 0.45f, Screen.width * 0.5f, Screen.height * 0.1f), gestureList[1]);
            GUI.Label(new Rect(Screen.width * 0.95f, Screen.height * 0.50f, Screen.width * 0.5f, Screen.height * 0.1f), gestureList[2]);
            GUI.Label(new Rect(Screen.width * 0.95f, Screen.height * 0.55f, Screen.width * 0.5f, Screen.height * 0.1f), gestureList[3]);
        }
Exemple #28
0
        void Update()
        {
            if ((Mathf.Sqrt((curPos1.y - startPos1.y) * (curPos1.y - startPos1.y) + (curPos1.x - startPos1.x) * (curPos1.x - startPos1.x))) > (Screen.height * Screen.width * 0.000025) && timer1 + 0.050 < Time.time)
            {
                timer1    = Time.time;
                testCount = 0;

                dirArray1[3] = dirArray1[2];
                dirArray1[2] = dirArray1[1];
                dirArray1[1] = dirArray1[0];

                dirArray2[3] = dirArray2[2];
                dirArray2[2] = dirArray2[1];
                dirArray2[1] = dirArray2[0];

                dirArray3[3] = dirArray3[2];
                dirArray3[2] = dirArray3[1];
                dirArray3[1] = dirArray3[0];

                dirArray4[3] = dirArray4[2];
                dirArray4[2] = dirArray4[1];
                dirArray4[1] = dirArray4[0];


                #region Calculations
                if (touchCount > 0)
                {
                    calc1 = Mathf.Atan2((curPos1.y - startPos1.y), curPos1.x - startPos1.x) * 57.2958;

                    if (calc1 < -157.5 || calc1 >= 157.5)
                    {
                        dirArray1[testCount] = 'A';
                        dir1 = 'A';
                    }
                    else if (calc1 < 157.5 && calc1 >= 112.5)
                    {
                        dirArray1[testCount] = 'B';
                        dir1 = 'B';
                    }
                    else if (calc1 < 112.5 && calc1 >= 67.5)
                    {
                        dirArray1[testCount] = 'C';
                        dir1 = 'C';
                    }
                    else if (calc1 < 67.5 && calc1 >= 22.5)
                    {
                        dirArray1[testCount] = 'D';
                        dir1 = 'D';
                    }
                    else if (calc1 < 22.5 && calc1 >= -22.5)
                    {
                        dirArray1[testCount] = 'E';
                        dir1 = 'E';
                    }
                    else if (calc1 < -22.5 && calc1 >= -67.5)
                    {
                        dirArray1[testCount] = 'F';
                        dir1 = 'F';
                    }
                    else if (calc1 < -67.5 && calc1 >= -112.5)
                    {
                        dirArray1[testCount] = 'G';
                        dir1 = 'G';
                    }
                    else if (calc1 < -112.5 && calc1 > -157.5)
                    {
                        dirArray1[testCount] = 'H';
                        dir1 = 'H';
                    }
                }
                if (touchCount > 1)
                {
                    calc2 = Mathf.Atan2((curPos2.y - startPos2.y), curPos2.x - startPos2.x) * 57.2958;

                    if (calc2 < -157.5 || calc2 >= 157.5)
                    {
                        dirArray2[testCount] = 'A';
                        dir2 = 'A';
                    }
                    else if (calc2 < 157.5 && calc2 >= 112.5)
                    {
                        dirArray2[testCount] = 'B';
                        dir2 = 'B';
                    }
                    else if (calc2 < 112.5 && calc2 >= 67.5)
                    {
                        dirArray2[testCount] = 'C';
                        dir2 = 'C';
                    }
                    else if (calc2 < 67.5 && calc2 >= 22.5)
                    {
                        dirArray2[testCount] = 'D';
                        dir2 = 'D';
                    }
                    else if (calc2 < 22.5 && calc2 >= -22.5)
                    {
                        dirArray2[testCount] = 'E';
                        dir2 = 'E';
                    }
                    else if (calc2 < -22.5 && calc2 >= -67.5)
                    {
                        dirArray2[testCount] = 'F';
                        dir2 = 'F';
                    }
                    else if (calc2 < -67.5 && calc2 >= -112.5)
                    {
                        dirArray2[testCount] = 'G';
                        dir2 = 'G';
                    }
                    else if (calc2 < -112.5 && calc2 > -157.5)
                    {
                        dirArray2[testCount] = 'H';
                        dir2 = 'H';
                    }
                }
                if (touchCount > 2)
                {
                    calc3 = Mathf.Atan2((curPos3.y - startPos3.y), curPos3.x - startPos3.x) * 57.2958;

                    if (calc3 < -157.5 || calc3 >= 157.5)
                    {
                        dirArray3[testCount] = 'A';
                        dir3 = 'A';
                    }
                    else if (calc3 < 157.5 && calc3 >= 112.5)
                    {
                        dirArray3[testCount] = 'B';
                        dir3 = 'B';
                    }
                    else if (calc3 < 112.5 && calc3 >= 67.5)
                    {
                        dirArray3[testCount] = 'C';
                        dir3 = 'C';
                    }
                    else if (calc3 < 67.5 && calc3 >= 22.5)
                    {
                        dirArray3[testCount] = 'D';
                        dir3 = 'D';
                    }
                    else if (calc3 < 22.5 && calc3 >= -22.5)
                    {
                        dirArray3[testCount] = 'E';
                        dir3 = 'E';
                    }
                    else if (calc3 < -22.5 && calc3 >= -67.5)
                    {
                        dirArray3[testCount] = 'F';
                        dir3 = 'F';
                    }
                    else if (calc3 < -67.5 && calc3 >= -112.5)
                    {
                        dirArray3[testCount] = 'G';
                        dir3 = 'G';
                    }
                    else if (calc3 < -112.5 && calc3 > -157.5)
                    {
                        dirArray3[testCount] = 'H';
                        dir3 = 'H';
                    }
                }
                if (touchCount > 3)
                {
                    calc4 = Mathf.Atan2((curPos4.y - startPos4.y), curPos4.x - startPos4.x) * 57.2958;

                    if (calc4 < -157.5 || calc4 >= 157.5)
                    {
                        dirArray4[testCount] = 'A';
                        dir4 = 'A';
                    }
                    else if (calc4 < 157.5 && calc4 >= 112.5)
                    {
                        dirArray4[testCount] = 'B';
                        dir4 = 'B';
                    }
                    else if (calc4 < 112.5 && calc4 >= 67.5)
                    {
                        dirArray4[testCount] = 'C';
                        dir4 = 'C';
                    }
                    else if (calc4 < 67.5 && calc4 >= 22.5)
                    {
                        dirArray4[testCount] = 'D';
                        dir4 = 'D';
                    }
                    else if (calc4 < 22.5 && calc4 >= -22.5)
                    {
                        dirArray4[testCount] = 'E';
                        dir4 = 'E';
                    }
                    else if (calc4 < -22.5 && calc4 >= -67.5)
                    {
                        dirArray4[testCount] = 'F';
                        dir4 = 'F';
                    }
                    else if (calc4 < -67.5 && calc4 >= -112.5)
                    {
                        dirArray4[testCount] = 'G';
                        dir4 = 'G';
                    }
                    else if (calc4 < -112.5 && calc4 > -157.5)
                    {
                        dirArray4[testCount] = 'H';
                        dir4 = 'H';
                    }
                }
                #endregion

                #region Finding gesture
                if (touchCount == 1)
                {
                    if (finGesture == "SX" || finGesture == "SL" || finGesture == "SR")
                    {
                        if (dirArray1[0] == 'A' && dirArray1[1] == 'A' && dirArray1[2] == 'A')
                        {
                            finGesture = "SL";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'E' && dirArray1[2] == 'E')
                        {
                            finGesture = "SR";
                        }
                    }

                    if (finGesture == "SX" || finGesture == "SU" || finGesture == "SD")
                    {
                        if (dirArray1[0] == 'C' && dirArray1[1] == 'C' && dirArray1[2] == 'C')
                        {
                            finGesture = "SU";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'G' && dirArray1[2] == 'G')
                        {
                            finGesture = "SD";
                        }
                    }
                    if (finGesture == "SX" || finGesture == "RB" || finGesture == "RF" || finGesture == "SU" || finGesture == "SD" || finGesture == "SL" || finGesture == "SR")
                    {
                        if (dirArray1[0] == 'A' && dirArray1[1] == 'B' && dirArray1[2] == 'C')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'B' && dirArray1[1] == 'C' && dirArray1[2] == 'D')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'C' && dirArray1[1] == 'D' && dirArray1[2] == 'E')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'D' && dirArray1[1] == 'E' && dirArray1[2] == 'F')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'F' && dirArray1[2] == 'G')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'F' && dirArray1[1] == 'G' && dirArray1[2] == 'H')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'H' && dirArray1[2] == 'A')
                        {
                            finGesture = "RF";
                        }
                        else if (dirArray1[0] == 'H' && dirArray1[1] == 'A' && dirArray1[2] == 'B')
                        {
                            finGesture = "RF";
                        }

                        else if (dirArray1[0] == 'A' && dirArray1[1] == 'H' && dirArray1[2] == 'G')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'H' && dirArray1[1] == 'G' && dirArray1[2] == 'F')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'F' && dirArray1[2] == 'E')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'F' && dirArray1[1] == 'E' && dirArray1[2] == 'D')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'D' && dirArray1[2] == 'C')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'D' && dirArray1[1] == 'C' && dirArray1[2] == 'B')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'C' && dirArray1[1] == 'B' && dirArray1[2] == 'A')
                        {
                            finGesture = "RB";
                        }
                        else if (dirArray1[0] == 'B' && dirArray1[1] == 'A' && dirArray1[2] == 'H')
                        {
                            finGesture = "RB";
                        }
                    }
                }
                if (touchCount == 2)
                {
                    if (finGesture == "SX" || finGesture == "CI" || finGesture == "CO")
                    {
                        if (dirArray1[0] == 'A' && dirArray1[1] == 'A' && dirArray1[2] == 'A' &&
                            dirArray2[0] == 'A' && dirArray2[1] == 'A' && dirArray2[2] == 'A')
                        {
                            finGesture = "CO";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'E' && dirArray1[2] == 'E' &&
                                 dirArray2[0] == 'E' && dirArray2[1] == 'E' && dirArray2[2] == 'E')
                        {
                            finGesture = "CI";
                        }
                    }
                    if (finGesture == "SX" || finGesture == "AU" || finGesture == "AD")
                    {
                        if (dirArray1[0] == 'C' && dirArray1[1] == 'C' && dirArray1[2] == 'C' &&
                            dirArray2[0] == 'C' && dirArray2[1] == 'C' && dirArray2[2] == 'C')
                        {
                            finGesture = "AU";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'G' && dirArray1[2] == 'G' &&
                                 dirArray2[0] == 'G' && dirArray2[1] == 'G' && dirArray2[2] == 'G')
                        {
                            finGesture = "AD";
                        }
                    }
                    if (finGesture == "SX" || finGesture == "UA" || finGesture == "UF" || finGesture == "AU" || finGesture == "AD" || finGesture == "CI" || finGesture == "CO")
                    {
                        if (dirArray1[0] == 'A' && dirArray1[1] == 'B' && dirArray1[2] == 'C' &&
                            dirArray2[0] == 'A' && dirArray2[1] == 'B' && dirArray2[2] == 'C')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'B' && dirArray1[1] == 'C' && dirArray1[2] == 'D' &&
                                 dirArray2[0] == 'B' && dirArray2[1] == 'C' && dirArray2[2] == 'D')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'C' && dirArray1[1] == 'D' && dirArray1[2] == 'E' &&
                                 dirArray2[0] == 'C' && dirArray2[1] == 'D' && dirArray2[2] == 'E')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'D' && dirArray1[1] == 'E' && dirArray1[2] == 'F' &&
                                 dirArray2[0] == 'D' && dirArray2[1] == 'E' && dirArray2[2] == 'F')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'F' && dirArray1[2] == 'G' &&
                                 dirArray2[0] == 'E' && dirArray2[1] == 'F' && dirArray2[2] == 'G')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'F' && dirArray1[1] == 'G' && dirArray1[2] == 'H' &&
                                 dirArray2[0] == 'F' && dirArray2[1] == 'G' && dirArray2[2] == 'H')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'H' && dirArray1[2] == 'A' &&
                                 dirArray2[0] == 'G' && dirArray2[1] == 'H' && dirArray2[2] == 'A')
                        {
                            finGesture = "UF";
                        }
                        else if (dirArray1[0] == 'H' && dirArray1[1] == 'A' && dirArray1[2] == 'B' &&
                                 dirArray2[0] == 'H' && dirArray2[1] == 'A' && dirArray2[2] == 'B')
                        {
                            finGesture = "UF";
                        }

                        else if (dirArray1[0] == 'A' && dirArray1[1] == 'H' && dirArray1[2] == 'G' &&
                                 dirArray2[0] == 'A' && dirArray2[1] == 'H' && dirArray2[2] == 'G')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'H' && dirArray1[1] == 'G' && dirArray1[2] == 'F' &&
                                 dirArray2[0] == 'H' && dirArray2[1] == 'G' && dirArray2[2] == 'F')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'G' && dirArray1[1] == 'F' && dirArray1[2] == 'E' &&
                                 dirArray2[0] == 'G' && dirArray2[1] == 'F' && dirArray2[2] == 'E')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'F' && dirArray1[1] == 'E' && dirArray1[2] == 'D' &&
                                 dirArray2[0] == 'F' && dirArray2[1] == 'E' && dirArray2[2] == 'D')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'E' && dirArray1[1] == 'D' && dirArray1[2] == 'C' &&
                                 dirArray2[0] == 'E' && dirArray2[1] == 'D' && dirArray2[2] == 'C')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'D' && dirArray1[1] == 'C' && dirArray1[2] == 'B' &&
                                 dirArray2[0] == 'D' && dirArray2[1] == 'C' && dirArray2[2] == 'B')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'C' && dirArray1[1] == 'B' && dirArray1[2] == 'A' &&
                                 dirArray2[0] == 'C' && dirArray2[1] == 'B' && dirArray2[2] == 'A')
                        {
                            finGesture = "UA";
                        }
                        else if (dirArray1[0] == 'B' && dirArray1[1] == 'A' && dirArray1[2] == 'H' &&
                                 dirArray2[0] == 'B' && dirArray2[1] == 'A' && dirArray2[2] == 'H')
                        {
                            finGesture = "UA";
                        }
                    }
                }
                if (touchCount == 3)
                {
                    if (dirArray1[0] == 'A' && dirArray1[1] == 'A' && dirArray1[2] == 'A' &&
                        dirArray2[0] == 'A' && dirArray2[1] == 'A' && dirArray2[2] == 'A' &&
                        dirArray3[0] == 'A' && dirArray3[1] == 'A' && dirArray3[2] == 'A')
                    {
                        finGesture = "LF";
                    }
                    else if (dirArray1[0] == 'E' && dirArray1[1] == 'E' && dirArray1[2] == 'E' &&
                             dirArray2[0] == 'E' && dirArray2[1] == 'E' && dirArray2[2] == 'E' &&
                             dirArray3[0] == 'E' && dirArray3[1] == 'E' && dirArray3[2] == 'E')
                    {
                        finGesture = "LV";
                    }
                }
                if (touchCount == 4)
                {
                    if (dirArray1[0] == 'C' && dirArray1[1] == 'C' && dirArray1[2] == 'C' &&
                        dirArray2[0] == 'C' && dirArray2[1] == 'C' && dirArray2[2] == 'C' &&
                        dirArray3[0] == 'C' && dirArray3[1] == 'C' && dirArray3[2] == 'C' &&
                        dirArray4[0] == 'C' && dirArray4[1] == 'C' && dirArray4[2] == 'C')
                    {
                        finGesture = "HU";
                    }
                    else if (dirArray1[0] == 'G' && dirArray1[1] == 'G' && dirArray1[2] == 'G' &&
                             dirArray2[0] == 'G' && dirArray2[1] == 'G' && dirArray2[2] == 'G' &&
                             dirArray3[0] == 'G' && dirArray3[1] == 'G' && dirArray3[2] == 'G' &&
                             dirArray4[0] == 'G' && dirArray4[1] == 'G' && dirArray4[2] == 'G')
                    {
                        finGesture = "HD";
                    }
                }
                #endregion

                Debug.Log(finGesture);

                BtConnector.sendString(finGesture);

                if (gestureList[0] != finGesture)
                {
                    gestureList[3] = gestureList[2];
                    gestureList[2] = gestureList[1];
                    gestureList[1] = gestureList[0];
                    gestureList[0] = finGesture;
                }

                startPos1 = curPos1;
                startPos2 = curPos2;
                startPos3 = curPos3;
                startPos4 = curPos4;
            }
        }
    // 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 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;
            }
        }
    }