From 81bc569f0e153cb142ee18bd279262084757433f Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 10 May 2021 11:40:36 +0200 Subject: [PATCH] Simplifying unit conversions --- src/hardware/adc.rs | 2 +- src/hardware/dac.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hardware/adc.rs b/src/hardware/adc.rs index 29eb290..9289097 100644 --- a/src/hardware/adc.rs +++ b/src/hardware/adc.rs @@ -96,7 +96,7 @@ impl Into for AdcCode { fn into(self) -> f32 { // The ADC has a differential input with a range of +/- 4.096 V and 16-bit resolution. // The gain into the two inputs is 1/5. - let adc_volts_per_lsb = 5.0 / 2.0 * 4.096 / i16::MAX as f32; + let adc_volts_per_lsb = 5.0 / 2.0 * 4.096 / (1u16 << 15) as f32; (self.0 as i16) as f32 * adc_volts_per_lsb } diff --git a/src/hardware/dac.rs b/src/hardware/dac.rs index a8a8af9..56f3033 100644 --- a/src/hardware/dac.rs +++ b/src/hardware/dac.rs @@ -80,14 +80,14 @@ impl Into for DacCode { // V with 16-bit resolution. The anti-aliasing filter has an additional gain of 2.5. let dac_volts_per_lsb = 4.096 * 2.5 / (1u16 << 15) as f32; - (self.0 ^ 0x8000) as i16 as f32 * dac_volts_per_lsb + (self.0 as i16).wrapping_add(i16::MIN) as f32 * dac_volts_per_lsb } } impl From for DacCode { /// Encode signed 16-bit values into DAC offset binary for a bipolar output configuration. fn from(value: i16) -> Self { - Self(value as u16 ^ 0x8000) + Self(value.wrapping_add(i16::MIN) as u16) } }