コード例 #1
0
        /// <summary>
        /// Connects to a Unicorn.
        /// </summary>
        /// <param name="serial">The serial of the device to connect.</param>
        private void ConnectionThread_DoWork(string serial, string streamName)
        {
            try
            {
                UpdateUIElements(DeviceStates.Connecting);

                //Open device
                _device = new Unicorn(serial);

                //Initialize lsl
                _lslInfo   = new liblsl.StreamInfo(streamName, "Data", (int)_device.GetNumberOfAcquiredChannels(), Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial);
                _lslOutlet = new liblsl.StreamOutlet(_lslInfo);

                UpdateUIElements(DeviceStates.Connected);
            }
            catch (DeviceException ex)
            {
                _device = null;
                UpdateUIElements(DeviceStates.NotConnected);
                ShowErrorBox(ex.Message);
            }
            catch (Exception ex)
            {
                _device = null;
                UpdateUIElements(DeviceStates.NotConnected);
                ShowErrorBox(String.Format("Could not open device. {0}", ex.Message));
            }
        }
コード例 #2
0
    void Start()
    {
        if (useRecordedFile)
        {
            csvP = new TestCSVParser("Assets/TestFiles/" + testFileName);
        }

        else
        {
            unicornDevice = new Unicorn("UN-2019.02.86");
            print(unicornDevice.GetDeviceInformation().DeviceVersion);
            FrameLength = 1;
            uint numberOfAcquiredChannels = unicornDevice.GetNumberOfAcquiredChannels();
            print(numberOfAcquiredChannels);
            receiveBuffer       = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
            receiveBufferHandle = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);

            unicornDevice.StartAcquisition(false);
        }

        ps = GetComponent <ParticleSystem>();

        for (int i = 0; i < 8; i++)
        {
            arrays.Add(new float[60]);
        }
    }
コード例 #3
0
    private void initUnicorn()
    {
        count         = 0;
        startAvg      = 0;
        unicornDevice = new Unicorn("UN-2019.02.86");
        print(unicornDevice.GetDeviceInformation().DeviceVersion);
        FrameLength = 1;
        uint numberOfAcquiredChannels = unicornDevice.GetNumberOfAcquiredChannels();

        print(numberOfAcquiredChannels);
        receiveBuffer       = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
        receiveBufferHandle = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);
        unicornDevice.StartAcquisition(false);
        acquisitionRunning = true;
    }
コード例 #4
0
        /// <summary>
        /// The acquisition thread.
        /// Acquires data from the Unicorn and sends it to LSL.
        /// </summary>
        private void AcquisitionThread_DoWork()
        {
            try
            {
                //Initialize Unicorn acquisition members
                uint     numberOfAcquiredChannels = _device.GetNumberOfAcquiredChannels();
                byte[]   receiveBuffer            = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
                GCHandle receiveBufferHandle      = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);
                float[]  receiveBufferFloat       = new float[receiveBuffer.Length / sizeof(float)];

                //Start acquisition
                _device.StartAcquisition(false);
                _acquisitionRunning = true;
                UpdateUIElements(DeviceStates.Acquiring);

                //acquisition loop
                while (_acquisitionRunning)
                {
                    //get data
                    _device.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float)));

                    //convert byte array to float array for LSL
                    for (int j = 0; j < receiveBuffer.Length / sizeof(float); j++)
                    {
                        receiveBufferFloat[j] = BitConverter.ToSingle(receiveBuffer, j * sizeof(float));
                    }

                    // send sample via LSL
                    _lslOutlet.push_sample(receiveBufferFloat);
                }
            }
            finally
            {
                try
                {
                    _device.StopAcquisition();
                }
                catch
                {
                    _device.Dispose();
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// The acquisition thread.
        /// Acquires data from the Unicorn and sends it to LSL.
        /// </summary>
        private void AcquisitionThread_DoWork()
        {
            try
            {
                //Initialize Unicorn acquisition members
                uint     numberOfAcquiredChannels = _device.GetNumberOfAcquiredChannels();
                byte[]   receiveBuffer            = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
                GCHandle receiveBufferHandle      = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);

                //Start acquisition
                _device.StartAcquisition(false);
                _acquisitionRunning = true;
                UpdateUIElements(DeviceStates.Acquiring);

                //acquisition loop
                while (_acquisitionRunning)
                {
                    //get data
                    _device.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float)));

                    // send sample via UDP
                    _socket.SendTo(receiveBuffer, _endPoint);
                }
            }
            finally
            {
                try
                {
                    _device.StopAcquisition();
                }
                catch
                {
                    _device.Dispose();
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                }
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("API Version: " + Unicorn.GetApiVersion());

            Unicorn _device     = new Unicorn("UN-2019.02.86");
            uint    FrameLength = 1;
            uint    numberOfAcquiredChannels = _device.GetNumberOfAcquiredChannels();

            byte[]   receiveBuffer       = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
            GCHandle receiveBufferHandle = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);

            _device.StartAcquisition(false);

            while (true)
            //for (int i = 0; i < 100; i++)
            {
                _device.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float)));
                for (int k = 0; k < 17; k++)
                {
                    byte[] tmp = new byte[4];
                    for (int j = 4 * k + 0; j < 4 * k + 4; j++)
                    {
                        tmp[j % 4] = receiveBuffer[j];
                    }
                    float result = BitConverter.ToSingle(tmp, 0);
                    Console.Write(result + " ");
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.WriteLine();

            _device.StopAcquisition();

            Console.ReadLine();
        }
コード例 #7
0
        /// <summary>
        /// The acquisition thread.
        /// Acquires data from the Unicorn and sends it to LSL.
        /// </summary>
        private void AcquisitionThread_DoWork()
        {
            try
            {
                //Initialize Unicorn acquisition members
                uint     numberOfAcquiredChannels = _device.GetNumberOfAcquiredChannels();
                byte[]   receiveBuffer            = new byte[FrameLength * sizeof(float) * numberOfAcquiredChannels];
                GCHandle receiveBufferHandle      = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned);
                float[]  receiveBufferFloat       = new float[receiveBuffer.Length / sizeof(float)];

                //Start acquisition
                _device.StartAcquisition(false);
                _acquisitionRunning = true;
                UpdateUIElements(DeviceStates.Acquiring);

                //acquisition loop
                while (_acquisitionRunning)
                {
                    //get data
                    _device.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float)));

                    //convert byte array to float array for LSL
                    for (int j = 0; j < receiveBuffer.Length / sizeof(float); j++)
                    {
                        receiveBufferFloat[j] = BitConverter.ToSingle(receiveBuffer, j * sizeof(float));
                    }

                    // send sample via LSL
                    if (rbCombineSigbnals.Checked)
                    {
                        _lslOutlets[0].push_sample(receiveBufferFloat);
                    }
                    else
                    {
                        for (int i = 0; i < _lslOutlets.Count; i++)
                        {
                            if (i == 0)
                            {
                                float[] eeg = new float[Unicorn.EEGChannelsCount];
                                Array.Copy(receiveBufferFloat, 0, eeg, 0, Unicorn.EEGChannelsCount);
                                _lslOutlets[i].push_sample(eeg);
                            }
                            else if (i == 1)
                            {
                                float[] acc = new float[Unicorn.AccelerometerChannelsCount];
                                Array.Copy(receiveBufferFloat, Unicorn.EEGChannelsCount, acc, 0, Unicorn.AccelerometerChannelsCount);
                                _lslOutlets[i].push_sample(acc);
                            }
                            else if (i == 2)
                            {
                                float[] gyr = new float[Unicorn.GyroscopeChannelsCount];
                                Array.Copy(receiveBufferFloat, Unicorn.EEGChannelsCount + Unicorn.AccelerometerChannelsCount, gyr, 0, Unicorn.GyroscopeChannelsCount);
                                _lslOutlets[i].push_sample(gyr);
                            }
                            else if (i == 3)
                            {
                                float[] cnt = new float[1];
                                Array.Copy(receiveBufferFloat, Unicorn.EEGChannelsCount + Unicorn.AccelerometerChannelsCount + Unicorn.GyroscopeChannelsCount, cnt, 0, 1);
                                _lslOutlets[i].push_sample(cnt);
                            }
                            else if (i == 4)
                            {
                                float[] bat = new float[1];
                                Array.Copy(receiveBufferFloat, Unicorn.EEGChannelsCount + Unicorn.AccelerometerChannelsCount + Unicorn.GyroscopeChannelsCount + 1, bat, 0, 1);
                                _lslOutlets[i].push_sample(bat);
                            }
                            else if (i == 5)
                            {
                                float[] val = new float[1];
                                Array.Copy(receiveBufferFloat, Unicorn.EEGChannelsCount + Unicorn.AccelerometerChannelsCount + Unicorn.GyroscopeChannelsCount + 2, val, 0, 1);
                                _lslOutlets[i].push_sample(val);
                            }
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    _device.StopAcquisition();
                }
                catch
                {
                    _device.Dispose();
                    _device = null;
                    UpdateUIElements(DeviceStates.NotConnected);
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Connects to a Unicorn.
        /// </summary>
        /// <param name="serial">The serial of the device to connect.</param>
        private void ConnectionThread_DoWork(string serial, string streamName)
        {
            try
            {
                UpdateUIElements(DeviceStates.Connecting);

                //Open device
                _device = new Unicorn(serial);

                //Initialize lsl
                if (_lslInfos == null)
                {
                    _lslInfos = new List <liblsl.StreamInfo>();
                }

                if (_lslOutlets == null)
                {
                    _lslOutlets = new List <liblsl.StreamOutlet>();
                }

                if (rbCombineSigbnals.Checked)
                {
                    _lslInfos.Add(new liblsl.StreamInfo(streamName, "Data", (int)_device.GetNumberOfAcquiredChannels(), Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[0]));
                }
                else
                {
                    int cnt = 0;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_EEG", "EEG", (int)Unicorn.EEGChannelsCount, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                    cnt++;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_ACC", "ACC", (int)Unicorn.AccelerometerChannelsCount, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                    cnt++;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_GYR", "GYR", (int)Unicorn.GyroscopeChannelsCount, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                    cnt++;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_CNT", "CNT", (int)1, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                    cnt++;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_BAT", "BAT", (int)1, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                    cnt++;
                    _lslInfos.Add(new liblsl.StreamInfo(streamName + "_VALID", "VALID", (int)1, Unicorn.SamplingRate, liblsl.channel_format_t.cf_float32, serial));
                    _lslOutlets.Add(new liblsl.StreamOutlet(_lslInfos[cnt]));
                }

                UpdateUIElements(DeviceStates.Connected);
            }
            catch (DeviceException ex)
            {
                _device = null;
                UpdateUIElements(DeviceStates.NotConnected);
                ShowErrorBox(ex.Message);
            }
            catch (Exception ex)
            {
                _device = null;
                UpdateUIElements(DeviceStates.NotConnected);
                ShowErrorBox(String.Format("Could not open device. {0}", ex.Message));
            }
        }