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)); }
private void Timer_Tick(object sender, object e) { while (!irReceiverReader.IsEmpty) { HandleChange(irReceiverReader.GetNextItem()); } UpdateDebugString(); }
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(); } }