// Function invoked during the onClick event of "StartButton".
        public void StartButtonFunction()
        {
            try
            {
                // Get Device Configuration input values.
                SamplingRate = Int32.Parse(SamplingRateInput.text);
                int resolution = Int32.Parse(ResolutionDropDownOptions[ResolutionDropdown.value]);

                // Update graphical window size variable (the plotting zone should contain 10 seconds of data).
                GraphWindSize      = SamplingRate * 10;
                WindowInMemorySize = Convert.ToInt32(1.1 * GraphWindSize);

                // Number of Active Channels.
                int      nbrChannels = 0;
                Toggle[] toggleArray = new Toggle[]
                { CH1Toggle, CH2Toggle, CH3Toggle, CH4Toggle, CH5Toggle, CH6Toggle, CH7Toggle, CH8Toggle };
                MultiThreadList.Add(new List <int>(Enumerable.Repeat(0, GraphWindSize).ToList()));
                for (int i = 0; i < toggleArray.Length; i++)
                {
                    if (toggleArray[i].isOn == true)
                    {
                        // Preparation of a string that will be communicated to our .dll
                        // This string will be formed by "1" or "0" characters, identifying sequentially which channels are active or not.
                        ActiveChannels.Add(i + 1);

                        // Definition of the first active channel.
                        if (VisualizationChannel == -1)
                        {
                            VisualizationChannel = i + 1;

                            // Update the label with the Current Channel Number.
                            CurrentChannel.text = "CH" + VisualizationChannel;
                        }

                        nbrChannels++;
                    }

                    // Dictionary that stores all the data received from .dll API.
                    MultiThreadList.Add(new List <int>(Enumerable.Repeat(0, GraphWindSize).ToList()));
                }

                // Check if at least one channel is active.
                if (ActiveChannels.Count != 0)
                {
                    // Start of Acquisition.
                    //Thread.CurrentThread.Name = "MAIN_THREAD";
                    if (PluxDevManager.GetDeviceTypeUnity() != "MuscleBAN BE Plux")
                    {
                        PluxDevManager.StartAcquisitionUnity(SamplingRate, ActiveChannels, resolution);
                    }
                    else
                    {
                        // Definition of the frequency divisor (subsampling ratio).
                        int freqDivisor = 10;
                        PluxDevManager.StartAcquisitionMuscleBanUnity(SamplingRate, ActiveChannels, resolution,
                                                                      freqDivisor);
                    }

                    // Enable StopButton.
                    StopButton.interactable = true;

                    // Disable ConnectButton.
                    ConnectButton.interactable = false;

                    // Disable Start Button.
                    StartButton.interactable      = false;
                    StartBySrcButton.interactable = false;

                    // Hide PlotIcon and show AcquiringIcon.
                    PlotIcon.SetActive(false);
                    TransparencyLevel.SetActive(false);
                    //AcquiringIcon.SetActive(true);

                    // Hide panel with the "Change Channel" button.
                    SelectedChannelPanel.SetActive(true);
                    if (ActiveChannels.Count == 1)
                    {
                        ChangeChannel.interactable = false;
                    }
                    else
                    {
                        ChangeChannel.interactable = true;
                    }

                    // Disable About Button to avoid entering a new scene during the acquisition.
                    AboutButton.interactable = false;
                }
                else
                {
                    // Show Info Message.
                    ChannelSelectioInfoPanel.SetActive(true);

                    // Hide object after 5 seconds.
                    StartCoroutine(RemoveAfterSeconds(5, ChannelSelectioInfoPanel));
                }
            }
            catch (Exception exc)
            {
                // Exception info.
                Debug.Log("Exception: " + exc.Message + "\n" + exc.StackTrace);

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

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

                // Reboot interface.
                ConnectButtonFunction(true);
            }
        }
        // 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));
            }
        }