Simplifying unit conversions

This commit is contained in:
Ryan Summers 2021-05-10 11:40:36 +02:00
parent fa886d2eac
commit 81bc569f0e
2 changed files with 3 additions and 3 deletions

View File

@ -96,7 +96,7 @@ impl Into<f32> 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
}

View File

@ -80,14 +80,14 @@ impl Into<f32> 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<i16> 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)
}
}