Exemple #1
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 #2
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);
    }
    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);
        }
    }
Exemple #4
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;
            }
        }
Exemple #5
0
    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);
        }
    }
 //---------------------------------------------------LOOP (is called once per frame)-----------------------------------------------
 void Update()                                           // start check
 {
     if (!started)                                       // wait for start
     {
         if (Cardboard.SDK.Triggered)                    // check to see if user has tapped the screen, or pressed the vr button
         {
             started = true;                             // record that user has intialised the start
             droneh  = "0";                              // set initial height to 0
         }
     }
     else                                                                                      // once started
     {
         lookDir = Cardboard.SDK.HeadPose.Orientation * Vector3.forward;                       // gets look direction vector
         // forward
         if (Cardboard.SDK.Triggered && Time.time - t > 0.5)                                   // wait for screen tap and 0.5s since last screen tap
         {
             t       = Time.time;                                                              // record time of tap
             isGoing = !isGoing;                                                               // toggle between forward and hover
             if (isGoing)                                                                      // if we want to go forward
             {
                 dronef = "15";
             }                                                                                 // set forward speed to 15
             else                                                                              // if we want to hover
             {
                 dronef = "0";
             }                                                                                 // set forward speed to 0
         }
         if (isGoing)                                                                          // if going forward
         {
             moveDir             = new Vector3(lookDir.x, 0.0f, lookDir.z);                    // puts movement only on the horizontal plane
             transform.position += moveDir * speed;                                            // move phone through VR environment by updating postion
         }
         // height
         if (lookDir.y > LOOKTHRESHOLD && transform.position.y < 30) // if phone looks up past threshold
         {
             transform.position += Vector3.up * speed;               // move phone up in VR world by an amount depending on forward speed
             height             += 30 * speed;                       // increment drone height
             int heightInt = (int)height;
             droneh = heightInt.ToString();
         }
         else if (lookDir.y < -LOOKTHRESHOLD && transform.position.y >= 2) // if phone looks down past threshold
         {
             transform.position -= Vector3.up * speed;                     // move phone down in VR world by an amount depending on forward speed
             height             -= 30 * speed;                             // decrement drone height
             int heightInt = (int)height;
             droneh = heightInt.ToString();
         }
         // rotation
         lookAngle = Cardboard.SDK.HeadPose.Orientation.eulerAngles.y - lookAngleZero;         // retrieve phone looking angle
         lookAngle = Mathf.RoundToInt(lookAngle);                                              // round it to an integer
         if (lookAngle > 180)
         {
             lookAngle -= 360;
         }                                                                                     // force angle in range [-180,180]
         else if (lookAngle < -180)
         {
             lookAngle += 360;
         }
         droner = lookAngle.ToString();                                                        // command drone rotation match phone rotation
         // send commands
         if (Time.time - timebuffer > 0.1)                                                     // every 0.1 seconds
         {
             messageToMC = "," + droneh + "," + dronef + "," + droner + "," + dronel + ",";    // format instructions into form "height,forward,rotation,left"
             //         print(messageToMC);                                                               // print instuctions to unity console
             BtConnector.sendString(messageToMC);                                              // send instructions to drone
             timebuffer = Time.time;                                                           // record time of last message sent to drone
         }
     }
 }
Exemple #7
0
    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();
        }
    }