From 20059aff5c4a577237dbf516b5a03aac9cb48002 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 24 Sep 2020 23:33:16 +0200 Subject: [PATCH] channel_state: recognize unplugged thermistor --- src/ad7172/mod.rs | 2 ++ src/channel_state.rs | 8 +++++++- src/main.rs | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ad7172/mod.rs b/src/ad7172/mod.rs index 21afe57..6e6921d 100644 --- a/src/ad7172/mod.rs +++ b/src/ad7172/mod.rs @@ -19,6 +19,8 @@ pub const SPI_MODE: spi::Mode = spi::Mode { /// 2 MHz pub const SPI_CLOCK: MegaHertz = MegaHertz(2); +pub const MAX_VALUE: u32 = 0xFF_FFFF; + #[derive(Clone, Copy, Debug)] #[repr(u8)] diff --git a/src/channel_state.rs b/src/channel_state.rs index 7719e09..aab68d4 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -63,7 +63,13 @@ impl ChannelState { } pub fn get_adc(&self) -> Option { - Some(self.adc_calibration.convert_data(self.adc_data?)) + let data = self.adc_data?; + if data == ad7172::MAX_VALUE { + // this means there is no thermistor plugged into the ADC. + None + } else { + Some(self.adc_calibration.convert_data(data)) + } } /// Get `SENS[01]` input resistance diff --git a/src/main.rs b/src/main.rs index 0eb93f2..f5d1d23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,10 +182,10 @@ fn main() -> ! { let state = channels.channel_state(channel); let _ = writeln!( - socket, "channel {}: t={:.0} adc{}={:.3} adc_r={:.3} vref={:.3} dac_feedback={:.3} itec={:.3} tec={:.3} tec_u_meas={:.3} r={:.3}", + socket, "channel {}: t={:.0} adc{}={:.3} adc_r={:?} vref={:.3} dac_feedback={:.3} itec={:.3} tec={:.3} tec_u_meas={:.3} r={:.3}", channel, state.adc_time, channel, adc_input.into_format_args(volt, Abbreviation), - state.get_sens().unwrap().into_format_args(ohm, Abbreviation), + state.get_sens().map(|sens| sens.into_format_args(ohm, Abbreviation)), vref.into_format_args(volt, Abbreviation), dac_feedback.into_format_args(volt, Abbreviation), itec.into_format_args(volt, Abbreviation), tec_i.into_format_args(ampere, Abbreviation), tec_u_meas.into_format_args(volt, Abbreviation),