private bool ReadData() { int sequence1 = 0, sequence2 = 0; _adcCsPin.Write(GpioPinValue.Low); InitReadSession(); var waiter = new SynchronousWaiter(); InitAdConverter(waiter); //Read the first sequence for (var i = 0; i < 8; i++) { _adcClkPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.Low); waiter.Wait(2); sequence1 = sequence1 << 1 | (int)_adcDigitalIoPin.Read(); } //Read the second sequence for (var i = 0; i < 8; i++) { sequence2 = sequence2 | (int)_adcDigitalIoPin.Read() << i; _adcClkPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.Low); waiter.Wait(2); } _adcCsPin.Write(GpioPinValue.High); if (sequence1 == sequence2) { OnTemperatureMeasured?.Invoke(this, new TemperatureData { IsValid = true, Temperature = Math.Round(((255 - sequence1) - 121) * 0.21875,1) + 21.8, Timestamp = DateTimeOffset.UtcNow}); return true; } return false; }
private void InitAdConverter(SynchronousWaiter waiter) { _adcCsPin.Write(GpioPinValue.Low); _adcClkPin.Write(GpioPinValue.Low); _adcDigitalIoPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.Low); _adcDigitalIoPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.Low); _adcDigitalIoPin.Write(GpioPinValue.Low); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.High); _adcDigitalIoPin.Write(GpioPinValue.High); waiter.Wait(2); _adcClkPin.Write(GpioPinValue.Low); _adcDigitalIoPin.Write(GpioPinValue.High); waiter.Wait(2); _adcDigitalIoPin.SetDriveMode(GpioPinDriveMode.Input); }