public static IRMessage EndRecording() { double newTime, oldTime; List <double> intervalList = new List <double>(); GpioChangeRecord changeRecord; statusLED.Write(GpioPinValue.Low); irReceiverReader.Stop(); if (irReceiverReader.IsEmpty) { return(null); } changeRecord = irReceiverReader.GetNextItem(); oldTime = changeRecord.RelativeTime.TotalMilliseconds; while (!irReceiverReader.IsEmpty) { changeRecord = irReceiverReader.GetNextItem(); newTime = changeRecord.RelativeTime.TotalMilliseconds; intervalList.Add(newTime - oldTime); oldTime = newTime; } if (!intervalList.Any()) { return(null); } return(new IRMessage(intervalList)); }
protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); _timer?.Stop(); _changeReader?.Stop(); }
public async void getGPIODistance1() { if (_echoBackReader != null) { ManualResetEvent mre = new ManualResetEvent(false); CancellationTokenSource source = new CancellationTokenSource(); source.CancelAfter(TimeSpan.FromMilliseconds(250)); _echoBackReader.Clear(); _echoBackReader.Start(); //Send pulse _triggerPin.Write(GpioPinValue.High); mre.WaitOne(TimeSpan.FromMilliseconds(0.01)); _triggerPin.Write(GpioPinValue.Low); try { await _echoBackReader.WaitForItemsAsync(2).AsTask(source.Token); IList <GpioChangeRecord> changeRecords = this._echoBackReader.GetAllItems(); //foreach (var i in changeRecords) //{ // Debug.WriteLine(i.RelativeTime.ToString() + " " + (i.Edge == GpioPinEdge.FallingEdge ? "Falling" : "Rising")); //} TimeSpan d = changeRecords[1].RelativeTime.Subtract(changeRecords[0].RelativeTime); GPIOTime = d.TotalSeconds; GPIODistance = ROUNDTRIPSPEEDOFSOUND * GPIOTime; } catch (OperationCanceledException) { GPIOTime = -1; GPIODistance = -1; //no measurement } _echoBackReader.Stop(); } }
private async Task ReadValuesFromDhtAsync() { try { var reading = new DhtSensorReading(); CancellationTokenSource cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(TIMEOUT_DATA_READING); _changeReader.Clear(); _changeReader.Start(); await SendInitialPulseAsync(); // Wait for 43 falling edges to show up await _changeReader.WaitForItemsAsync(43).AsTask(cancellationSource.Token); // discard first two falling edges _changeReader.GetNextItem(); _changeReader.GetNextItem(); // pulse widths greater than 110us are considered 1's TimeSpan oneThreshold = new TimeSpan(110 * 10000000 / 1000000); TimeSpan current = _changeReader.GetNextItem().RelativeTime; for (int i = 0; i < 40; i++) { TimeSpan next = _changeReader.GetNextItem().RelativeTime; TimeSpan pulseWidth = next.Duration() - current.Duration(); if (pulseWidth.Duration() > oneThreshold.Duration()) { reading.Bits[40 - i - 1] = true; } current = next; } if (reading.IsValid()) { UpdateValuesOnUI(reading.Temperature(), reading.Humidity()); UpdateStatus("OK"); } else { throw new Exception("Invalid sensor data received!"); } } catch (Exception ex) { UpdateValuesOnUI(double.NaN, double.NaN); if (ex is TaskCanceledException) { UpdateStatus("Timeout while reading sensor data!"); } else { UpdateStatus(ex.Message); } } finally { _changeReader.Stop(); } }