コード例 #1
0
    void UpdateSerial()
    {
        //return;
        //int data=0;
        for (int j = 0; j < 5; j++)
        {
            string tempString = serialClass.ReadFromArduino();
            //print (tempString);
            //GameObject.Find ("debugText").GetComponent<Text> ().text = tempString;
            string[] stringArray;


            if (tempString != null)
            {
                if (tempString.StartsWith("S"))                    //Start quest
                {
                    timerController.paused = false;
                    timerController.setStartTime();
                }
                else if (tempString.StartsWith("k") && tempString.Length >= transmittedCharsNumber)                      //key
                {
                    if (tempString.StartsWith("k--"))
                    {
                        for (int i = 0; i < keyStates.Length; i++)
                        {
                            keyStates [i] = false;
                        }
                        unlockObject.reset();
                    }
                    else
                    {
                        //print (tempString);
                        //data = (int)tempString[1] + 256*(int)tempString[2];
                        tempString = hexToBin(tempString.Substring(1));
                        //tempString = Convert.ToString(data, 2);
                        //print (tempString);
                        for (int i = 0; i < charsNumber; i++)
                        {
                            if (tempString.Length > i)
                            {
                                keyStates [i] = (tempString [tempString.Length - 1 - i] == '1' ? true : false);
                            }
                            else
                            {
                                keyStates [i] = false;
                            }
                        }
                    }
                }
                else if (tempString.StartsWith("s"))                      //sound

                //AudioController.Play(audioIDs[int.Parse(tempString.Substring(1), System.Globalization.NumberStyles.HexNumber)]);
                {
                }
                else if (tempString.StartsWith("m"))                      //show slave monitor
                {
                    print("--------------------- SLAVE ON");
                    GameObject.Find("Camera-Slices").GetComponent <Camera> ().enabled = true;
                }
                else if (tempString.StartsWith("M"))                      //show MAIN monitor
                {
                    print("--------------------- SLAVE OFF");
                    GameObject.Find("Camera-Slices").GetComponent <Camera> ().enabled = false;
                }
                else if (tempString.StartsWith("U"))                 // USB connected
                {
                    if (tempString [1] == '1')                       // FIRST USB
                    {
                        usbDriveConnected = true;
                    }
                    else
                    {
                    }
                }
                else if (tempString.StartsWith("u"))                 // USB DISconnected
                {
                    if (tempString [1] == '1')                       // FIRST USB
                    {
                        usbDriveConnected = false;
                    }
                    else
                    {
                    }
                }
                else if (tempString.StartsWith("o"))                      // "OTHER" - ONE CHAR COMMANDS
                //for(int i=1;i<tempString.Length;i++){
                {
                    if (tempString [1] == 'D')                       // Door opened

                    {
                        doorOpen = true;
                        GameObject.Find("Camera-Slices").GetComponent <Camera> ().enabled = false;
                    }
                    else if (tempString [1] == 'C')                         // Battery charged
                    {
                        batteryCharged = true;
                    }
                    else if (tempString [1] == 'c')                         // Battery DIScharged
                    {
                        batteryCharged = false;
                    }
                    else if (tempString [1] == 'B')                         // Battery installed
                    {
                        batteryInstalled = true;
                    }
                    else if (tempString [1] == 'b')                         // Battery UNinstalled
                    {
                        batteryInstalled = false;
                    }
                    //}
                }
                else if (tempString.StartsWith("J"))                      // JOYSTICK UPDATE

                {
                    if (state >= STATE_SATELLITE_LOCKED)
                    {
                        SerialComm.WriteToArduino("#S");                          //SATELLITE LOCKED COMMAND
                    }

                    joystickX = (hexToDec(tempString.Substring(1, 1)) - 7) / 7f;
                    joystickY = (hexToDec(tempString.Substring(2, 1)) - 7) / 7f;

                    //joystickX = ((int)Char.GetNumericValue (tempString [1]) - 4)/4f;
                    //joystickY = ((int)Char.GetNumericValue (tempString [2]) - 4)/4f;
                }
                else if (tempString.StartsWith("E"))                      // ENCODER UPDATE

                {
                    encoderPosition = int.Parse(tempString.Substring(1));
                }
                else if (tempString.StartsWith("P"))
                {
                    stringArray = tempString.Substring(1).Split('P');

                    if (stringArray.Length >= 5)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            potReadings [i, currentReadingIndex] = int.Parse(stringArray [i]);

                            if (potRotators [i] != null)
                            {
                                potRotators [i].value = potReadings [i, currentReadingIndex];
                            }
                        }
                        currentReadingIndex = (currentReadingIndex + 1) % samples;
                    }
                }
                else if (tempString.StartsWith(">"))
                {
                    tempString = longHexToBin(tempString.Substring(1));

                    while (tempString.Length < 40)
                    {
                        tempString += '0';
                    }


                    if (buttonsData.Length != tempString.Length)
                    {
                        buttonsData = tempString;
                    }
                    else
                    {
                        for (int i = 0; i < tempString.Length; i++)
                        {
                            if (buttonsData [i] != tempString [i] || !buttonDataInitialized)
                            {
                                buttonMatrixChanged(i, tempString [i]);
                                print("Different Index = " + i);
                            }
                        }
                    }

                    buttonsData           = tempString;
                    buttonDataInitialized = true;
                }
                else if (tempString.StartsWith("R"))
                {
                    regionSelect = int.Parse(tempString.Substring(1, 1));
                    iqSelect     = int.Parse(tempString.Substring(2, 1));
                    ageSelect    = int.Parse(tempString.Substring(3, 1));
                }
                else if (tempString.StartsWith("F"))
                {
                    if (tempString [1] == '1')
                    {
                        DOTween.PlayBackwards("flash_hide");
                        black_2.SetActive(false);
                    }
                    else
                    {
                        DOTween.PlayForward("flash_hide");
                        black_2.SetActive(true);
                    }
                }

                if (tempString.StartsWith("<"))
                {
                    Application.LoadLevel(Application.loadedLevel);
                }



                if (tempString.StartsWith("f"))                    // 6 PUZZLE FRAGMENTS
                {
                    print("--------------------- " + tempString);


                    textureChangeObject.GetComponent <TextureChange> ().changeTexture((int)Char.GetNumericValue(tempString [1]) - 1, (int)Char.GetNumericValue(tempString [2]));
                }
            }
            else                 //NOHING to read
            {
                break;
            }
        }
    }