Ejemplo n.º 1
0
 public void CanInitAndDestroy()
 {
     pluxManager.PluxDev(deviceMacAddr);
     Console.WriteLine("Initiated with success!");
     pluxManager.DisconnectPluxDev();
     Console.WriteLine("Initiated and Destroyed with success!");
 }
        // Function invoked during the onClick event of "ConnectButton".
        public void ConnectButtonFunction(bool typeOfStop)
        {
            try
            {
                // Change the color and text of "Connect" button.
                if (ConnectText.text == "Connect")
                {
                    // Get the selected device.
                    this.SelectedDevice = this.ListDevices[DeviceDropdown.value];

                    // Connection with the device.
                    Debug.Log("Trying to establish a connection with device " + this.SelectedDevice);
                    Console.WriteLine("Selected Device: " + this.SelectedDevice);
                    PluxDevManager.PluxDev(this.SelectedDevice);
                }
                else if (ConnectText.text == "Disconnect")
                {
                    try
                    {
                        // Disconnect device.
                        PluxDevManager.DisconnectPluxDev();
                    }
                    catch (Exception exception)
                    {
                        Debug.Log("Trying to disconnect from an unconnected device...");
                    }

                    ConnectText.text = "Connect";
                    GreenFlag.SetActive(false);
                    RedFlag.SetActive(true);

                    // Disable "Device Configuration" panel options.
                    SamplingRateInput.interactable  = false;
                    SamplingRateInput.text          = "1000";
                    ResolutionInput.interactable    = false;
                    ResolutionDropdown.interactable = false;

                    // Disable channel selection buttons.
                    CH1Toggle.interactable = false;
                    CH1Toggle.isOn         = true;
                    // Change the toggle state.
                    List <Toggle> toggleList = new List <Toggle>()
                    {
                        CH2Toggle, CH3Toggle, CH4Toggle, CH5Toggle, CH6Toggle, CH7Toggle, CH8Toggle
                    };
                    foreach (Toggle toggleBtn in toggleList)
                    {
                        toggleBtn.interactable = false;
                        toggleBtn.isOn         = false;
                    }

                    // Disable Start and Device Configuration buttons.
                    StartButton.interactable      = false;
                    StartBySrcButton.interactable = false;

                    // Disable the battery icons.
                    List <GameObject> ListBatteryIcons = new List <GameObject>()
                    {
                        BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100
                    };
                    foreach (var batImg in ListBatteryIcons)
                    {
                        batImg.SetActive(false);
                    }
                    BatteryIconUnknown.SetActive(true);

                    // Show the quantitative battery value.
                    BatteryLevel.text = "";

                    // Disable Drop-down options.
                    DeviceDropdown.ClearOptions();

                    //Add the options created in the List above
                    DeviceDropdown.AddOptions(new List <string>()
                    {
                        "Select Device"
                    });

                    // Disable drop-down and Connect button if a PLUX Device was disconnected.
                    DeviceDropdown.interactable = false;
                    ConnectButton.interactable  = false;

                    // Show PlotIcon.
                    PlotIcon.SetActive(true);
                    TransparencyLevel.SetActive(true);

                    // Reboot of global variables.
                    RebootVariables();
                }
            }
            catch (Exception e)
            {
                // Print information about the exception.
                Debug.Log(e);

                // Show info message.
                ConnectInfoPanel.SetActive(true);

                // Hide object after 5 seconds.
                StartCoroutine(RemoveAfterSeconds(5, ConnectInfoPanel));
            }
        }
Ejemplo n.º 3
0
        // Function invoked during the onClick event of "ConnectButton".
        public void ConnectButtonFunction(bool typeOfStop)
        {
            try
            {
                // Change the color and text of "Connect" button.
                if (ConnectText.text == "Connect")
                {
                    // Specification of the callback function (defined on this/the user Unity script) which will receive the acquired data
                    // samples as inputs.
                    PluxDevManager.SetCallbackHandler(CallbackHandler);

                    // Get the selected device.
                    string selectedDevice = this.ListDevices[DeviceDropdown.value];

                    // Connection with the device.
                    Debug.Log("Trying to establish a connection with device " + selectedDevice);
                    PluxDevManager.PluxDev(selectedDevice);
                    Debug.Log("Connection with device " + selectedDevice + " established with success!");

                    ConnectText.text = "Disconnect";
                    GreenFlag.SetActive(true);
                    RedFlag.SetActive(false);

                    // Enable "Device Configuration" panel options.
                    SamplingRateInput.interactable  = true;
                    ResolutionInput.interactable    = true;
                    ResolutionDropdown.interactable = true;

                    // Enable channel selection buttons accordingly to the type of device.
                    string devType = PluxDevManager.GetDeviceTypeUnity();
                    if (devType == "MuscleBAN BE Plux")
                    {
                        CH1Toggle.interactable = true;
                    }
                    else if (devType == "BITalino")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                        CH3Toggle.interactable = true;
                        CH4Toggle.interactable = true;
                        CH5Toggle.interactable = true;
                        CH6Toggle.interactable = true;

                        //Clear the old options of the Dropdown menu
                        ResolutionDropdown.ClearOptions();

                        //Add the options created in the List above
                        ResolutionDropdown.AddOptions(new List <string>()
                        {
                            "8"
                        });
                    }
                    else if (devType == "biosignalsplux")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                        CH3Toggle.interactable = true;
                        CH4Toggle.interactable = true;
                        CH5Toggle.interactable = true;
                        CH6Toggle.interactable = true;
                        CH7Toggle.interactable = true;
                        CH8Toggle.interactable = true;
                    }
                    else if (devType == "OpenBANPlux")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }

                    // Enable Start and Device Configuration buttons.
                    StartButton.interactable = true;

                    // Disable Connect Button.
                    //ConnectButton.interactable = false;

                    // Hide show Info message if it is active.
                    ConnectInfoPanel.SetActive(false);

                    // Update Battery Level.
                    int batteryLevel;
                    batteryLevel = PluxDevManager.GetBatteryUnity();

                    // Battery icon accordingly to the battery level.
                    List <GameObject> ListBatteryIcons = new List <GameObject>()
                    {
                        BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100, BatteryIconUnknown
                    };
                    GameObject currImage;
                    if (batteryLevel > 50)
                    {
                        BatteryIcon100.SetActive(true);
                        currImage = BatteryIcon100;
                    }
                    else if (batteryLevel <= 50 && batteryLevel > 10)
                    {
                        BatteryIcon50.SetActive(true);
                        currImage = BatteryIcon50;
                    }
                    else if (batteryLevel <= 10 && batteryLevel > 1)
                    {
                        BatteryIcon10.SetActive(true);
                        currImage = BatteryIcon10;
                    }
                    else
                    {
                        BatteryIcon0.SetActive(true);
                        currImage = BatteryIcon0;
                    }

                    // Disable the remaining images.
                    foreach (var batImg in ListBatteryIcons)
                    {
                        if (batImg != currImage)
                        {
                            batImg.SetActive(false);
                        }
                    }

                    // Show the quantitative battery value.
                    BatteryLevel.text = batteryLevel.ToString() + "%";
                }
                else if (ConnectText.text == "Disconnect")
                {
                    // Disconnect device if the stop was forced by an event or timeout exception.
                    if (typeOfStop == false)
                    {
                        PluxDevManager.DisconnectPluxDev();
                    }

                    ConnectText.text = "Connect";
                    GreenFlag.SetActive(false);
                    RedFlag.SetActive(true);

                    // Disable "Device Configuration" panel options.
                    SamplingRateInput.interactable  = false;
                    SamplingRateInput.text          = "1000";
                    ResolutionInput.interactable    = false;
                    ResolutionDropdown.interactable = false;

                    // Disable channel selection buttons.
                    CH1Toggle.interactable = false;
                    CH2Toggle.interactable = false;
                    CH3Toggle.interactable = false;
                    CH4Toggle.interactable = false;
                    CH5Toggle.interactable = false;
                    CH6Toggle.interactable = false;
                    CH7Toggle.interactable = false;
                    CH8Toggle.interactable = false;

                    // Disable Start and Device Configuration buttons.
                    StartButton.interactable = false;

                    // Disable the battery icons.
                    List <GameObject> ListBatteryIcons = new List <GameObject>()
                    {
                        BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100
                    };
                    foreach (var batImg in ListBatteryIcons)
                    {
                        batImg.SetActive(false);
                    }
                    BatteryIconUnknown.SetActive(true);

                    // Show the quantitative battery value.
                    BatteryLevel.text = "";

                    // Disable Drop-down options.
                    DeviceDropdown.ClearOptions();

                    //Add the options created in the List above
                    DeviceDropdown.AddOptions(new List <string>()
                    {
                        "Select Device"
                    });

                    // Disable drop-down and Connect button if a PLUX Device was disconnected.
                    DeviceDropdown.interactable = false;
                    ConnectButton.interactable  = false;

                    // Show PlotIcon.
                    PlotIcon.SetActive(true);
                    TransparencyLevel.SetActive(true);

                    // Reboot of global variables.
                    RebootVariables();
                }
            }
            catch (Exception e)
            {
                // Print information about the exception.
                Debug.Log(e);

                // Show info message.
                ConnectInfoPanel.SetActive(true);

                // Hide object after 5 seconds.
                StartCoroutine(RemoveAfterSeconds(5, ConnectInfoPanel));
            }
        }