public override IObservable <NeuropixDataFrame> Generate() { return(Observable.Create <NeuropixDataFrame>((observer, cancellationToken) => { return Task.Factory.StartNew(() => { var stopwatch = new Stopwatch(); using (var basestation = new NeuropixBasestation()) using (var sampleSignal = new ManualResetEvent(false)) { basestation.Open(Path); try { basestation.Mode = AsicMode.Recording; basestation.DataMode = true; var packets = new ElectrodePacket[BufferSize]; for (int i = 0; i < packets.Length; i++) { packets[i] = new ElectrodePacket(); } while (!cancellationToken.IsCancellationRequested) { stopwatch.Restart(); for (int i = 0; i < packets.Length; i++) { if (!basestation.ReadElectrodeData(packets[i])) { observer.OnCompleted(); return; } } var result = new NeuropixDataFrame(packets, 0); observer.OnNext(result); var frequency = Frequency; var sampleInterval = frequency > 0 ? 1000.0 / Frequency : 0; var dueTime = Math.Max(0, (sampleInterval * packets.Length * NeuropixDataFrame.SampleCount) - stopwatch.Elapsed.TotalMilliseconds); if (dueTime > 0) { sampleSignal.WaitOne(TimeSpan.FromMilliseconds(dueTime)); } } } finally { basestation.Close(); } } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); })); }
public override IObservable <NeuropixDataFrame> Generate() { return(Observable.Create <NeuropixDataFrame>((observer, cancellationToken) => { return Task.Factory.StartNew(() => { try { using (var basestation = new NeuropixBasestation()) { var version = basestation.GetAPIVersion(); Console.WriteLine("Neuropix API version: {0}.{1}", version.Major, version.Minor); Console.WriteLine("Opening basestation data and configuration link..."); basestation.Open(); version = basestation.GetHardwareVersion(); Console.WriteLine("Neuropix FPGA bootcode version: {0}.{1}", version.Major, version.Minor); version = basestation.GetBSVersion(); Console.WriteLine("Neuropix Basestation connect board version: {0}.{1}", version.Major, version.Minor); var probeID = basestation.ReadID(); Console.WriteLine("Neuropix probe S/N: {0} Option {1}", probeID.SerialNumber, probeID.ProbeType); Console.Write("Neuropix ADC calibration... "); var comparatorCalibration = ComparatorCalibration; var adcOffsetCalibration = AdcOffsetCalibration; var adcSlopeCalibration = AdcSlopeCalibration; if (!string.IsNullOrEmpty(comparatorCalibration) && !string.IsNullOrEmpty(adcOffsetCalibration) && !string.IsNullOrEmpty(adcSlopeCalibration)) { basestation.ApplyAdcCalibrationFromCsv(comparatorCalibration, adcOffsetCalibration, adcSlopeCalibration); } else { basestation.ApplyAdcCalibrationFromEeprom(); } Console.WriteLine("OK"); Console.WriteLine("Neuropix setting gain and filter bandwidth... "); basestation.WriteAllAPGains(GainAP); basestation.WriteAllLFPGains(GainLFP); basestation.SetFilter(FilterBandwidth); Console.Write("Neuropix Gain correction... "); var gainCorrection = GainCorrection; if (!string.IsNullOrEmpty(gainCorrection)) { basestation.ApplyGainCalibrationFromCsv(gainCorrection); } //else basestation.ApplyGainCalibrationFromEeprom(); Console.WriteLine("OK"); basestation.LedOff(LedOff); basestation.Mode = AsicMode.Recording; basestation.DataMode = true; basestation.TriggerMode = false; basestation.SetNrst(false); basestation.ResetDatapath(); var fileName = StreamingFileName; if (!string.IsNullOrEmpty(fileName) && FileStreaming) { PathHelper.EnsureDirectory(fileName); fileName = PathHelper.AppendSuffix(fileName, StreamingSuffix); basestation.StartRecording(fileName); } Console.WriteLine("Neuropix recording armed."); basestation.SetNrst(true); basestation.NeuralStart(); Console.WriteLine("Neuropix recording start..."); int packetCounter = 0; float bufferCapacity = 0; var packet = new ElectrodePacket(); while (!cancellationToken.IsCancellationRequested) { if (packetCounter == 0) { bufferCapacity = basestation.FifoFilling; } basestation.ReadElectrodeData(packet); var result = new NeuropixDataFrame(packet, bufferCapacity); observer.OnNext(result); packetCounter = (packetCounter + 1) % 50; } Console.WriteLine("Neuropix stop recording..."); if (!string.IsNullOrEmpty(fileName) && FileStreaming) { basestation.StopRecording(); } basestation.Close(); } } catch (System.Runtime.InteropServices.SEHException ex) { Console.WriteLine(ex); throw; } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); })); }