diff --git a/src/main.rs b/src/main.rs index 249caf6..7f26000 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ fn main() -> ! { let dac_i = dac_feedback / Ohms(5.0); let itec = channels.read_itec(channel); - let tec_i = Amps((itec.0 - 1.5) / 8.0); + let tec_i = (itec - Volts(1.5)) / Ohms(0.4); let state = channels.channel_state(channel); let _ = writeln!( diff --git a/src/units.rs b/src/units.rs index 86e55b8..1bf8ea2 100644 --- a/src/units.rs +++ b/src/units.rs @@ -1,10 +1,29 @@ use core::{ fmt, - ops::Div, + ops::{Add, Div, Sub}, }; +macro_rules! impl_add_sub { + ($Type: ident) => { + impl Add<$Type> for $Type { + type Output = $Type; + fn add(self, rhs: $Type) -> $Type { + $Type(self.0 + rhs.0) + } + } + + impl Sub<$Type> for $Type { + type Output = $Type; + fn sub(self, rhs: $Type) -> $Type { + $Type(self.0 - rhs.0) + } + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct Volts(pub f64); +impl_add_sub!(Volts); impl fmt::Display for Volts { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -21,6 +40,7 @@ impl Div for Volts { #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct Amps(pub f64); +impl_add_sub!(Amps); impl fmt::Display for Amps { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -29,6 +49,7 @@ impl fmt::Display for Amps { } #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub struct Ohms(pub f64); +impl_add_sub!(Ohms); impl fmt::Display for Ohms { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {