private float[] ReadUnicornData() { try { unicornDevice.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float))); } catch (Exception e) { text.text = e.Message; receiveBuffer = new byte[FrameLength * sizeof(float) * 17]; receiveBufferHandle = GCHandle.Alloc(receiveBuffer, GCHandleType.Pinned); } for (int k = 0; k < 8; k++) { byte[] tmp = new byte[4]; for (int j = 4 * k + 0; j < 4 * k + 4; j++) { tmp[j % 4] = receiveBuffer[j]; } result[k] = BitConverter.ToSingle(tmp, 0); } return(result); }
private float[] ReadUnicornData() { unicornDevice.GetData(FrameLength, receiveBufferHandle.AddrOfPinnedObject(), (uint)(receiveBuffer.Length / sizeof(float))); for (int k = 0; k < 8; k++) { byte[] tmp = new byte[4]; for (int j = 4 * k + 0; j < 4 * k + 4; j++) { tmp[j % 4] = receiveBuffer[j]; } result[k] = BitConverter.ToSingle(tmp, 0); } return(result); }
/// <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(); }
/// <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); } } }