private void ReadFast(SampleValue[] samples) { byte[] readBuffer = new byte[3]; /* Buffer to hold read data*/ byte[] writeBuffer = new byte[3] { 0x00, 0x00, 0x00 }; Func <byte[], int> convertToIntFunc = null; /* Setup the appropriate ADC configuration byte */ switch (ADC_DEVICE) { case AdcDevice.MCP3002: writeBuffer[0] = MCP3002_CONFIG; convertToIntFunc = convertToIntMCP3002; break; case AdcDevice.MCP3208: writeBuffer[0] = MCP3208_CONFIG; convertToIntFunc = convertToIntMCP3208; break; } Stopwatch.StartNew(); int sampleSize = samples.Length; for (int sampleIndex = 0; sampleIndex < sampleSize; sampleIndex++) { SpiADC.TransferFullDuplex(writeBuffer, readBuffer); /* Read data from the ADC */ adcValue = convertToIntFunc(readBuffer); /* Convert the returned bytes into an integer value */ samples[sampleIndex] = new SampleValue() { Tick = Stopwatch.GetTimestamp(), Value = adcValue }; } }
private void TakeReadings() { SampleValue[] samples = new SampleValue[sampleSize]; ReadFast(samples); int average = 0; int min = int.MaxValue; int max = int.MinValue; OscopeHelper.ProcessStats(samples, ref average, ref min, ref max); if (normalized) { OscopeHelper.NormalizeToAverage(samples, (min + max) / 2); } var crossings = OscopeHelper.ProcessZeroCrossings(samples, positiveTrigger, normalized ? 0 : 512, 2, 1); double cross = crossings.Any() ? crossings.First().Value : 0; double oneCycle = crossings.Count > 1 ? (crossings.ElementAt(1).Value - crossings.ElementAt(0).Value) : 0; var task2 = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { var freq = 1000.0 / Stopwatch.Frequency; viewModel.Points = samples.Select(s => new ScatterDataPoint { XValue = (s.Tick - cross) * freq, YValue = s.Value, }); viewModel.Average = average; viewModel.Min = min; viewModel.Max = max; viewModel.OneCycleMS = oneCycle * freq; var now = DateTime.Now; viewModel.RunMS = (now - lastRun).TotalMilliseconds; lastRun = now; }); }
private void ReadFast(SampleValue[] samples) { byte[] readBuffer = new byte[3]; /* Buffer to hold read data*/ byte[] writeBuffer = new byte[3] { 0x00, 0x00, 0x00 }; Func<byte[], int> convertToIntFunc = null; /* Setup the appropriate ADC configuration byte */ switch (ADC_DEVICE) { case AdcDevice.MCP3002: writeBuffer[0] = MCP3002_CONFIG; convertToIntFunc = convertToIntMCP3002; break; case AdcDevice.MCP3208: writeBuffer[0] = MCP3208_CONFIG; convertToIntFunc = convertToIntMCP3208; break; } Stopwatch.StartNew(); int sampleSize = samples.Length; for (int sampleIndex = 0; sampleIndex < sampleSize; sampleIndex++) { SpiADC.TransferFullDuplex(writeBuffer, readBuffer); /* Read data from the ADC */ adcValue = convertToIntFunc(readBuffer); /* Convert the returned bytes into an integer value */ samples[sampleIndex] = new SampleValue() { Tick = Stopwatch.GetTimestamp(), Value = adcValue }; } }