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]); } }
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; }
/// <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); } } }
/// <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); } } }
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(); }
void Update() { if (animationRunning) { if (Input.GetKeyDown(KeyCode.Space)) { unicornDevice.StopAcquisition(); animationRunning = false; text.text += "\nPAUSIERT"; print("Animation pausiert"); Time.timeScale = 0f; } float[] values; // particle system modules var noise = ps.noise; // read values from each EEG channel into a buffer: values = ReadUnicornData(); for (int i = 0; i < 8; i++) { print(values[i]); // normalize values in some way, save them in the buffer channels (TODO: find good way to normalize) arrays[i][pos] = Math.Abs(values[i] * 1 / 2000000) + 3; } pos = (pos + 1) % arrays[0].Length; // if all buffers are full (every time 60 values have been read) if (pos == 0) { // find the max value out of all 8 buffers float arrayMax = 0; for (int i = 0; i < 8; i++) { if (arrayMax < arrays[i].Max()) { arrayMax = arrays[i].Max(); } } // manipulate noise module strength noise.strength = arrayMax; // on-screen debug text text.text = "Höchster Wert auf allen Kanälen:\n" + arrayMax.ToString("F2"); } } else { if (Input.GetKeyDown(KeyCode.Space)) { unicornDevice.StartAcquisition(false); animationRunning = true; Time.timeScale = 1f; print("Animation fortgesetzt."); } } }
/// <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); } } }