Comparing sensitive data, confidential files or internal emails?

Most legal and privacy policies prohibit uploading sensitive data online. Diffchecker Desktop ensures your confidential information never leaves your computer. Work offline and compare documents securely.

PLUX Unity API - Hybrid-8 (All Channels Active)

Created Diff never expires
1 removal
391 lines
17 additions
398 lines
using System;
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI;


public class Hybrid8Test : MonoBehaviour
public class Hybrid8Test : MonoBehaviour
{
{
// Class Variables
// Class Variables
private PluxDeviceManager pluxDevManager;
private PluxDeviceManager pluxDevManager;


// GUI Objects.
// GUI Objects.
public Button ScanButton;
public Button ScanButton;


public Button ConnectButton;
public Button ConnectButton;
public Button DisconnectButton;
public Button DisconnectButton;
public Button StartAcqButton;
public Button StartAcqButton;
public Button StopAcqButton;
public Button StopAcqButton;
public Dropdown DeviceDropdown;
public Dropdown DeviceDropdown;
public Dropdown SamplingRateDropdown;
public Dropdown SamplingRateDropdown;
public Dropdown ResolutionDropdown;
public Dropdown ResolutionDropdown;
public Dropdown RedIntensityDropdown;
public Dropdown RedIntensityDropdown;
public Dropdown InfraredIntensityDropdown;
public Dropdown InfraredIntensityDropdown;
public Text OutputMsgText;
public Text OutputMsgText;


// Class constants (CAN BE EDITED BY IN ACCORDANCE TO THE DESIRED DEVICE CONFIGURATIONS)
// Class constants (CAN BE EDITED BY IN ACCORDANCE TO THE DESIRED DEVICE CONFIGURATIONS)
[System.NonSerialized]
[System.NonSerialized]
public List<string> domains = new List<string>() { "BTH" };
public List<string> domains = new List<string>() { "BTH" };


public int samplingRate = 100;
public int samplingRate = 100;


private int Hybrid8PID = 517;
private int Hybrid8PID = 517;
private int BiosignalspluxPID = 513;
private int BiosignalspluxPID = 513;
private int BitalinoPID = 1538;
private int BitalinoPID = 1538;
private int MuscleBanPID = 1282;
private int MuscleBanPID = 1282;
private int MuscleBanNewPID = 2049;
private int MuscleBanNewPID = 2049;
private int BiosignalspluxSoloPID = 532;
private int BiosignalspluxSoloPID = 532;
private int MaxLedIntensity = 255;
private int MaxLedIntensity = 255;


// Start is called before the first frame update
// Start is called before the first frame update
private void Start()
private void Start()
{
{
// Initialise object
// Initialise object
pluxDevManager = new PluxDeviceManager(ScanResults, ConnectionDone, AcquisitionStarted, OnDataReceived, OnEventDetected, OnExceptionRaised);
pluxDevManager = new PluxDeviceManager(ScanResults, ConnectionDone, AcquisitionStarted, OnDataReceived, OnEventDetected, OnExceptionRaised);


// Important call for debug purposes by creating a log file in the root directory of the project.
// Important call for debug purposes by creating a log file in the root directory of the project.
pluxDevManager.WelcomeFunctionUnity();
pluxDevManager.WelcomeFunctionUnity();
}
}


// Update function, being constantly invoked by Unity.
// Update function, being constantly invoked by Unity.
private void Update()
private void Update()
{ }
{ }


// Method invoked when the application was closed.
// Method invoked when the application was closed.
private void OnApplicationQuit()
private void OnApplicationQuit()
{
{
try
try
{
{
// Disconnect from device.
// Disconnect from device.
if (pluxDevManager != null)
if (pluxDevManager != null)
{
{
pluxDevManager.DisconnectPluxDev();
pluxDevManager.DisconnectPluxDev();
Console.WriteLine("Application ending after " + Time.time + " seconds");
Console.WriteLine("Application ending after " + Time.time + " seconds");
}
}
}
}
catch (Exception exc)
catch (Exception exc)
{
{
Console.WriteLine("Device already disconnected when the Application Quit.");
Console.WriteLine("Device already disconnected when the Application Quit.");
}
}
}
}


/**
/**
* =================================================================================
* =================================================================================
* ============================= GUI Events ========================================
* ============================= GUI Events ========================================
* =================================================================================
* =================================================================================
*/
*/


// Method called when the "Scan for Devices" button is pressed.
// Method called when the "Scan for Devices" button is pressed.
public void ScanButtonFunction()
public void ScanButtonFunction()
{
{
// Search for PLUX devices
// Search for PLUX devices
pluxDevManager.GetDetectableDevicesUnity(domains);
pluxDevManager.GetDetectableDevicesUnity(domains);


// Disable the "Scan for Devices" button.
// Disable the "Scan for Devices" button.
ScanButton.interactable = false;
ScanButton.interactable = false;
}
}


// Method called when the "Connect to Device" button is pressed.
// Method called when the "Connect to Device" button is pressed.
public void ConnectButtonFunction()
public void ConnectButtonFunction()
{
{
// Disable Connect button.
// Disable Connect button.
ConnectButton.interactable = false;
ConnectButton.interactable = false;


// Connect to the device selected in the Dropdown list.
// Connect to the device selected in the Dropdown list.
pluxDevManager.PluxDev(DeviceDropdown.options[DeviceDropdown.value].text);
pluxDevManager.PluxDev(DeviceDropdown.options[DeviceDropdown.value].text);
}
}


// Method called when the "Disconnect Device" button is pressed.
// Method called when the "Disconnect Device" button is pressed.
public void DisconnectButtonFunction()
public void DisconnectButtonFunction()
{
{
// Disconnect from the device.
// Disconnect from the device.
pluxDevManager.DisconnectPluxDev();
pluxDevManager.DisconnectPluxDev();


// Reboot GUI elements state.
// Reboot GUI elements state.
RebootGUI();
RebootGUI();
}
}


// Method called when the "Start Acquisition" button is pressed.
// Method called when the "Start Acquisition" button is pressed.
public void StartButtonFunction()
public void StartButtonFunction()
{
{
// Get the Sampling Rate and Resolution values.
// Get the Sampling Rate and Resolution values.
samplingRate = int.Parse(SamplingRateDropdown.options[SamplingRateDropdown.value].text);
samplingRate = int.Parse(SamplingRateDropdown.options[SamplingRateDropdown.value].text);
int resolution = int.Parse(ResolutionDropdown.options[ResolutionDropdown.value].text);
int resolution = int.Parse(ResolutionDropdown.options[ResolutionDropdown.value].text);


// Initializing the sources array.
// Initializing the sources array.
List<PluxDeviceManager.PluxSource> pluxSources = new List<PluxDeviceManager.PluxSource>();
List<PluxDeviceManager.PluxSource> pluxSources = new List<PluxDeviceManager.PluxSource>();


// biosignalsplux Hybrid-8 device (3 sensors >>> 1 Analog + 2 Digital SpO2/fNIRS)
// biosignalsplux Hybrid-8 device (3 sensors >>> 1 Analog + 2 Digital SpO2/fNIRS)
if (pluxDevManager.GetProductIdUnity() == Hybrid8PID)
if (pluxDevManager.GetProductIdUnity() == Hybrid8PID)
{
{
// Add the sources of the digital channels (CH1 and CH2).
// Add the sources of the digital channels (CH1 and CH2).
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x03));
// pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x03));
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x03));
// pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x03));


// Define the LED Intensities of both sensors (CH1 and CH2) as: {RED, INFRARED}
// Define the LED Intensities of both sensors (CH1 and CH2) as: {RED, INFRARED}
int redLedIntensity = (int)(int.Parse(RedIntensityDropdown.options[RedIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
// int redLedIntensity = (int)(int.Parse(RedIntensityDropdown.options[RedIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int infraredLedIntensity = (int)(int.Parse(InfraredIntensityDropdown.options[InfraredIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
// int infraredLedIntensity = (int)(int.Parse(InfraredIntensityDropdown.options[InfraredIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int[] ledIntensities = new int[2] { redLedIntensity, infraredLedIntensity };
// int[] ledIntensities = new int[2] { redLedIntensity, infraredLedIntensity };
pluxDevManager.SetParameter(1, 0x03, ledIntensities);
// pluxDevManager.SetParameter(1, 0x03, ledIntensities);
pluxDevManager.SetParameter(2, 0x03, ledIntensities);
// pluxDevManager.SetParameter(2, 0x03, ledIntensities);


// Add the source of the analog channel (CH8).
// Add the source of the analog channel (CH1-CH8).
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(3, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(4, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(5, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(6, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(7, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(8, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(8, 1, resolution, 0x01));


// Add the sources of the internal IMU channels (CH11 with 9 derivations [3xACC | 3xGYRO | 3xMAG] defined by the 0x01FF chMask).
// Add the sources of the internal IMU channels (CH11 with 9 derivations [3xACC | 3xGYRO | 3xMAG] defined by the 0x01FF chMask).
int imuPort = 11;
// int imuPort = 11;
pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x01FF));
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x01FF));


// Alternatively only some of the derivations can be activated.
// Alternatively only some of the derivations can be activated.
// >>> 3xACC (channel mask 0x0007)
// >>> 3xACC (channel mask 0x0007)
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x0007));
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x0007));
// >>> 3xGYR (channel mask 0x0038)
// >>> 3xGYR (channel mask 0x0038)
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x0038));
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x0038));
// >>> 3xMAG (channel mask 0x01C0)
// >>> 3xMAG (channel mask 0x01C0)
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x01C0));
// pluxSources.Add(new PluxDeviceManager.PluxSource(imuPort, 1, resolution, 0x01C0));
}
}
// biosignalsplux (2 Analog sensors)
// biosignalsplux (2 Analog sensors)
else if (pluxDevManager.GetProductIdUnity() == BiosignalspluxPID)
else if (pluxDevManager.GetProductIdUnity() == BiosignalspluxPID)
{
{
// Starting a real-time acquisition from:
// Starting a real-time acquisition from:
// >>> biosignalsplux [CH1 - EDA and CH2 - ECG active]
// >>> biosignalsplux [CH1 - EDA and CH2 - ECG active]
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01)); // CH1 | EDA
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01)); // CH1 | EDA
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x01)); // CH2 | ECG
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x01)); // CH2 | ECG


// Add the sources of the digital channel (CH9 | fNIRS/SpO2).
// Add the sources of the digital channel (CH9 | fNIRS/SpO2).
pluxSources.Add(new PluxDeviceManager.PluxSource(9, 1, resolution, 0x03));
pluxSources.Add(new PluxDeviceManager.PluxSource(9, 1, resolution, 0x03));


// Define the LED Intensities of the CH9 sensor as: {RED, INFRARED}
// Define the LED Intensities of the CH9 sensor as: {RED, INFRARED}
int redLedIntensity = (int)(int.Parse(RedIntensityDropdown.options[RedIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int redLedIntensity = (int)(int.Parse(RedIntensityDropdown.options[RedIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int infraredLedIntensity = (int)(int.Parse(InfraredIntensityDropdown.options[InfraredIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int infraredLedIntensity = (int)(int.Parse(InfraredIntensityDropdown.options[InfraredIntensityDropdown.value].text) * (MaxLedIntensity / 100f)); // A 8-bit value (0-255)
int[] ledIntensities = new int[2] { redLedIntensity, infraredLedIntensity };
int[] ledIntensities = new int[2] { redLedIntensity, infraredLedIntensity };
pluxDevManager.SetParameter(9, 0x03, ledIntensities);
pluxDevManager.SetParameter(9, 0x03, ledIntensities);
}
}
// muscleBAN (7 Analog sensors)
// muscleBAN (7 Analog sensors)
else if (pluxDevManager.GetProductIdUnity() == MuscleBanPID)
else if (pluxDevManager.GetProductIdUnity() == MuscleBanPID)
{
{
// Starting a real-time acquisition from:
// Starting a real-time acquisition from:
// >>> muscleBAN [CH1 > EMG]
// >>> muscleBAN [CH1 > EMG]
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
// >>> muscleBAN [CH2-CH4 > ACC | CH5-CH7 > MAG active]
// >>> muscleBAN [CH2-CH4 > ACC | CH5-CH7 > MAG active]
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x3F));
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x3F));
}
}
// muscleBAN v2 (7 Analog sensors)
// muscleBAN v2 (7 Analog sensors)
else if (pluxDevManager.GetProductIdUnity() == MuscleBanNewPID)
else if (pluxDevManager.GetProductIdUnity() == MuscleBanNewPID)
{
{
// Starting a real-time acquisition from:
// Starting a real-time acquisition from:
// >>> muscleBAN [CH1 > EMG]
// >>> muscleBAN [CH1 > EMG]
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
// >>> muscleBAN Virtual Port [CH2-CH4 > ACC | CH5-CH7 > MAG active]
// >>> muscleBAN Virtual Port [CH2-CH4 > ACC | CH5-CH7 > MAG active]
pluxSources.Add(new PluxDeviceManager.PluxSource(11, 1, resolution, 0x3F));
pluxSources.Add(new PluxDeviceManager.PluxSource(11, 1, resolution, 0x3F));
}
}
// biosignalsplux Solo (8 Analog sensors)
// biosignalsplux Solo (8 Analog sensors)
else if (pluxDevManager.GetProductIdUnity() == BiosignalspluxSoloPID)
else if (pluxDevManager.GetProductIdUnity() == BiosignalspluxSoloPID)
{
{
// Starting a real-time acquisition from:
// Starting a real-time acquisition from:
// >>> biosignalsplux Solo [CH1 > MICRO]
// >>> biosignalsplux Solo [CH1 > MICRO]
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(1, 1, resolution, 0x01));
// >>> biosignalsplux Solo [CH2 > CUSTOM]
// >>> biosignalsplux Solo [CH2 > CUSTOM]
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x01));
pluxSources.Add(new PluxDeviceManager.PluxSource(2, 1, resolution, 0x01));
// >>> biosignalsplux Solo Virtual Port [CH3-CH5 > ACC | CH6-CH8 > MAG]
// >>> biosignalsplux Solo Virtual Port [CH3-CH5 > ACC | CH6-CH8 > MAG]
pluxSources.Add(new PluxDeviceManager.PluxSource(11, 1, resolution, 0x3F));
pluxSources.Add(new PluxDeviceManager.PluxSource(11, 1, resolution, 0x3F));
}
}


// BITalino (2 Analog sensors)
// BITalino (2 Analog sensors)
if (pluxDevManager.GetProductIdUnity() == BitalinoPID)
if (pluxDevManager.GetProductIdUnity() == BitalinoPID)
{
{
// Starting a real-time acquisition from:
// Starting a real-time acquisition from:
// >>> BITalino [Channels A2 and A5 active]
// >>> BITalino [Channels A2 and A5 active]
pluxDevManager.StartAcquisitionUnity(samplingRate, new List<int> { 2, 5 }, 10);
pluxDevManager.StartAcquisitionUnity(samplingRate, new List<int> { 2, 5 }, 10);
}
}
else
else
{
{
// Start a real-time acquisition with the created sources.
// Start a real-time acquisition with the created sources.
pluxDevManager.StartAcquisitionBySourcesUnity(samplingRate, pluxSources.ToArray());
pluxDevManager.StartAcquisitionBySourcesUnity(samplingRate, pluxSources.ToArray());
}
}
}
}


// Method called when the "Stop Acquisition" button is pressed.
// Method called when the "Stop Acquisition" button is pressed.
public void StopButtonFunction()
public void StopButtonFunction()
{
{
// Stop the real-time acquisition.
// Stop the real-time acquisition.
pluxDevManager.StopAcquisitionUnity();
pluxDevManager.StopAcquisitionUnity();


// Enable the "Start Acquisition" button and disable the "Stop Acquisition" button.
// Enable the "Start Acquisition" button and disable the "Stop Acquisition" button.
StartAcqButton.interactable = true;
StartAcqButton.interactable = true;
StopAcqButton.interactable = false;
StopAcqButton.interactable = false;
}
}


/**
/**
* =================================================================================
* =================================================================================
* ============================= Callbacks =========================================
* ============================= Callbacks =========================================
* =================================================================================
* =================================================================================
*/
*/


// Callback that receives the list of PLUX devices found during the Bluetooth scan.
// Callback that receives the list of PLUX devices found during the Bluetooth scan.
public void ScanResults(List<string> listDevices)
public void ScanResults(List<string> listDevices)
{
{
// Enable the "Scan for Devices" button.
// Enable the "Scan for Devices" button.
ScanButton.interactable = true;
ScanButton.interactable = true;


if (listDevices.Count > 0)
if (listDevices.Count > 0)
{
{
// Update list of devices.
// Update list of devices.
DeviceDropdown.ClearOptions();
DeviceDropdown.ClearOptions();
DeviceDropdown.AddOptions(listDevices);
DeviceDropdown.AddOptions(listDevices);


// Enable the Dropdown and the Connect button.
// Enable the Dropdown and the Connect button.
DeviceDropdown.interactable = true;
DeviceDropdown.interactable = true;
ConnectButton.interactable = true;
ConnectButton.interactable = true;


// Show an informative message about the number of detected devices.
// Show an informative message about the number of detected devices.
OutputMsgText.text = "Scan completed.\nNumber of devices found: " + listDevices.Count;
OutputMsgText.text = "Scan completed.\nNumber of devices found: " + listDevices.Count;
}
}
else
else
{
{
// Show an informative message stating the none devices were found.
// Show an informative message stating the none devices were found.
OutputMsgText.text = "Bluetooth device scan didn't found any valid devices.";
OutputMsgText.text = "Bluetooth device scan didn't found any valid devices.";
}
}
}
}


// Callback invoked once the connection with a PLUX device was established.
// Callback invoked once the connection with a PLUX device was established.
// connectionStatus -> A boolean flag stating if the connection was established with success (true) or not (false).
// connectionStatus -> A boolean flag stating if the connection was established with success (true) or not (false).
public void ConnectionDone(bool connectionStatus)
public void ConnectionDone(bool connectionStatus)
{
{
if (connectionStatus)
if (connectionStatus)
{
{
// Disable some GUI elements.
// Disable some GUI elements.
ScanButton.interactable = false;
ScanButton.interactable = false;
DeviceDropdown.interactable = false;
DeviceDropdown.interactable = false;
ConnectButton.interactable = false;
ConnectButton.interactable = false;


// Enable some generic GUI elements.
// Enable some generic GUI elements.
if (pluxDevManager.GetProductIdUnity() != BitalinoPID)
if (pluxDevManager.GetProductIdUnity() != BitalinoPID)
{
{
ResolutionDropdown.interactable = true;
ResolutionDropdown.interactable = true;
}
}


SamplingRateDropdown.interactable = true;
SamplingRateDropdown.interactable = true;
StartAcqButton.interactable = true;
StartAcqButton.interactable = true;
DisconnectButton.interactable = true;
DisconnectButton.interactable = true;


// Enable some biosignalsplux Hybrid-8 specific GUI elements.
// Enable some biosignalsplux Hybrid-8 specific GUI elements.
if (pluxDevManager.GetProductIdUnity() == Hybrid8PID || pluxDevManager.GetProductIdUnity() == BiosignalspluxPID)
if (pluxDevManager.GetProductIdUnity() == Hybrid8PID || pluxDevManager.GetProductIdUnity() == BiosignalspluxPID)
{
{
RedIntensityDropdown.interactable = true;
RedIntensityDropdown.interactable = true;
InfraredIntensityDropdown.interactable = true;
InfraredIntensityDropdown.interactable = true;
}
}
}
}
else
else
{
{
// Enable Connect button.
// Enable Connect button.
ConnectButton.interactable = true;
ConnectButton.interactable = true;


// Show an informative message stating the connection with the device was not established with success.
// Show an informative message stating the connection with the device was not established with success.
OutputMsgText.text = "It was not possible to establish a connection with the device. Please, try to repeat the connection procedure.";
OutputMsgText.text = "It was not possible to establish a connection with the device. Please, try to repeat the connection procedure.";
}
}
}
}


// Callback invoked once the data streaming between the PLUX device and the computer is started.
// Callback invoked once the data streaming between the PLUX device and the computer is started.
// acquisitionStatus -> A boolean flag stating if the acquisition was started with success (true) or not (false).
// acquisitionStatus -> A boolean flag stating if the acquisition was started with success (true) or not (false).
// exceptionRaised -> A boolean flag that identifies if an exception was raised and should be presented in the GUI (true) or not (false).
// exceptionRaised -> A boolean flag that identifies if an exception was raised and should be presented in the GUI (true) or not (false).
public void AcquisitionStarted(bool acquisitionStatus, bool exceptionRaised = false, string exceptionMessage = "")
public void AcquisitionStarted(bool acquisitionStatus, bool exceptionRaised = false, string exceptionMessage = "")
{
{
if (acquisitionStatus)
if (acquisitionStatus)
{
{
// Enable the "Stop Acquisition" button and disable the "Start Acquisition" button.
// Enable the "Stop Acquisition" button and disable the "Start Acquisition" button.
StartAcqButton.interactable = false;
StartAcqButton.interactable = false;
StopAcqButton.interactable = true;
StopAcqButton.interactable = true;
}
}
else
else
{
{
// Present an informative message about the error.
// Present an informative message about the error.
OutputMsgText.text = !exceptionRaised ? "It was not possible to start a real-time data acquisition. Please, try to repeat the scan/connect/start workflow." : exceptionMessage;
OutputMsgText.text = !exceptionRaised ? "It was not possible to start a real-time data acquisition. Please, try to repeat the scan/connect/start workflow." : exceptionMessage;


// Reboot GUI.
// Reboot GUI.
RebootGUI();
RebootGUI();
}
}
}
}


// Callback invoked every time an exception is raised in the PLUX API Plugin.
// Callback invoked every time an exception is raised in the PLUX API Plugin.
// exceptionCode -> ID number of the exception to be raised.
// exceptionCode -> ID number of the exception to be raised.
// exceptionDescription -> Descriptive message about the exception.
// exceptionDescription -> Descriptive message about the exception.
public void OnExceptionRaised(int exceptionCode, string exceptionDescription)
public void OnExceptionRaised(int exceptionCode, string exceptionDescription)
{
{
if (pluxDevManager.IsAcquisitionInProgress())
if (pluxDevManager.IsAcquisitionInProgress())
{
{
// Present an informative message about the error.
// Present an informative message about the error.
OutputMsgText.text = exceptionDescription;
OutputMsgText.text = exceptionDescription;


// Reboot GUI.
// Reboot GUI.
RebootGUI();
RebootGUI();
}
}
}
}


// Callback that receives the data acquired from the PLUX devices that are streaming real-time data.
// Callback that receives the data acquired from the PLUX devices that are streaming real-time data.
// nSeq -> Number of sequence identifying the number of the current package of data.
// nSeq -> Number of sequence identifying the number of the current package of data.
// data -> Package of data containing the RAW data samples collected from each active channel ([sample_first_active_channel, sample_second_active_channel,...]).
// data -> Package of data containing the RAW data samples collected from each active channel ([sample_first_active_channel, sample_second_active_channel,...]).
public void OnDataReceived(int nSeq, int[] data)
public void OnDataReceived(int nSeq, int[] data)
{
{
// Show samples with a 1s interval.
// Show samples with a 1s interval.
if (nSeq % samplingRate == 0)
if (nSeq % samplingRate == 0)
{
{
// Show the current package of data.
// Show the current package of data.
string outputString = "Acquired Data:\n";
string outputString = "Acquired Data:\n";
for (int j = 0; j < data.Length; j++)
for (int j = 0; j < data.Length; j++)
{
{
outputString += data[j] + "\t";
outputString += data[j] + "\t";
}
}


// Show the values in the GUI.
// Show the values in the GUI.
OutputMsgText.text = outputString;
OutputMsgText.text = outputString;
}
}
}
}


// Callback that receives the events raised from the PLUX devices that are streaming real-time data.
// Callback that receives the events raised from the PLUX devices that are streaming real-time data.
// pluxEvent -> Event object raised by the PLUX API.
// pluxEvent -> Event object raised by the PLUX API.
public void OnEventDetected(PluxDeviceManager.PluxEvent pluxEvent)
public void OnEventDetected(PluxDeviceManager.PluxEvent pluxEvent)
{
{
if (pluxEvent is PluxDeviceManager.PluxDisconnectEvent)
if (pluxEvent is PluxDeviceManager.PluxDisconnectEvent)
{
{
// Present an error message.
// Present an error message.
OutputMsgText.text =
OutputMsgText.text =
"The connection between the computer and the PLUX device was interrupted due to the following event: " +
"The connection between the computer and the PLUX device was interrupted due to the following event: " +
(pluxEvent as PluxDeviceManager.PluxDisconnectEvent).reason;
(pluxEvent as PluxDeviceManager.PluxDisconnectEvent).reason;


// Securely stop the real-time acquisition.
// Securely stop the real-time acquisition.
pluxDevManager.StopAcquisitionUnity(-1);
pluxDevManager.StopAcquisitionUnity(-1);


// Reboot GUI.
// Reboot GUI.
RebootGUI();
RebootGUI();
}
}
else if (pluxEvent is PluxDeviceManager.PluxDigInUpdateEvent)
else if (pluxEvent is PluxDeviceManager.PluxDigInUpdateEvent)
{
{
PluxDeviceManager.PluxDigInUpdateEvent digInEvent = (pluxEvent as PluxDeviceManager.PluxDigInUpdateEvent);
PluxDeviceManager.PluxDigInUpdateEvent digInEvent = (pluxEvent as PluxDeviceManager.PluxDigInUpdateEvent);
Console.WriteLine("Digital Input Update Event Detected on channel " + digInEvent.channel + ". Current state: " + digInEvent.state);
Console.WriteLine("Digital Input Update Event Detected on channel " + digInEvent.channel + ". Current state: " + digInEvent.state);
}
}
}
}


/**
/**
* =================================================================================
* =================================================================================
* ========================== Auxiliary Methods ====================================
* ========================== Auxiliary Methods ====================================
* =================================================================================
* =================================================================================
*/
*/


// Auxiliary method used to reboot the GUI elements.
// Auxiliary method used to reboot the GUI elements.
public void RebootGUI()
public void RebootGUI()
{
{
ScanButton.interactable = true;
ScanButton.interactable = true;
ConnectButton.interactable = false;
ConnectButton.interactable = false;
DisconnectButton.interactable = false;
DisconnectButton.interactable = false;
StartAcqButton.interactable = false;
StartAcqButton.interactable = false;
StopAcqButton.interactable = false;
StopAcqButton.interactable = false;
DeviceDropdown.interactable = false;
DeviceDropdown.interactable = false;
SamplingRateDropdown.interactable = false;
SamplingRateDropdown.interactable = false;
ResolutionDropdown.interactable = false;
ResolutionDropdown.interactable = false;
RedIntensityDropdown.interactable = false;
RedIntensityDropdown.interactable = false;
InfraredIntensityDropdown.interactable = false;
InfraredIntensityDropdown.interactable = false;
}
}
}
}