use crate::quantity::{Ohms, Volts}; /// Given the resistance of the second resistor in a resistive voltage divider, calculate the resistance of the first resistor. pub fn solve_r1( voltage_src: Volts, voltage_read: Volts, r2: Ohms, ) -> Ohms { Ohms(r2.0 * (voltage_src.0 / voltage_read.0 - 1.0)) } /// Given the resistance of the first resistor in a resistive voltage divider, calculate the resistance of the second resistor. pub fn solve_r2( voltage_src: Volts, voltage_read: Volts, r1: Ohms, ) -> Ohms { Ohms((r1.0 * voltage_read.0) / (voltage_src.0 - voltage_read.0)) } // --------------------------------------------------------------------------------------------------------------------- // ----- Tests ------------------------ // --------------------------------------------------------------------------------------------------------------------- #[cfg(test)] mod tests { use crate::quantity::{OhmsVal, VoltsVal}; use super::*; use float_cmp::assert_approx_eq; #[test] fn solve_r1_test() { let resistance = solve_r1(3.3.volts(), 2.0.volts(), 1_000.0.ohms()); assert_approx_eq!(f32, 650.0, resistance.0); } #[test] fn solve_r2_test() { let resistance = solve_r2(3.3.volts(), 2.0.volts(), 1_000.0.ohms()); assert_approx_eq!(f32, 1538.462, resistance.0); } }