Skip to content

Commit

Permalink
Add methods to measure voltage instead of raw value.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 17, 2024
1 parent 38d5219 commit e57601b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions embedded-hal/src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ pub trait AdcChannel: ErrorType {
/// This should wait until data is ready and then read it.
/// If the ADC's precision is less than 32 bits, the value must be scaled accordingly.
fn read(&mut self) -> Result<u32, Self::Error>;

/// Take an measurement in millivolts.
fn measure_mv(&mut self) -> Result<i32, Self::Error> {
Ok(self.read()? as i32)
}

/// Take an measurement in microvolts.
fn measure_uv(&mut self) -> Result<i32, Self::Error> {
let uv = self.measure_mv()?;
Ok(uv.saturating_mul(1000))
}

/// Take an measurement in nanovolts.
fn measure_nv(&mut self) -> Result<i32, Self::Error> {
let uv = self.measure_uv()?;
Ok(uv.saturating_mul(1000))
}
}

impl<T> AdcChannel for &mut T
Expand Down

0 comments on commit e57601b

Please sign in to comment.