Skip to content

Commit e57601b

Browse files
committed
Add methods to measure voltage instead of raw value.
1 parent 38d5219 commit e57601b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

embedded-hal/src/adc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ pub trait AdcChannel: ErrorType {
5050
/// This should wait until data is ready and then read it.
5151
/// If the ADC's precision is less than 32 bits, the value must be scaled accordingly.
5252
fn read(&mut self) -> Result<u32, Self::Error>;
53+
54+
/// Take an measurement in millivolts.
55+
fn measure_mv(&mut self) -> Result<i32, Self::Error> {
56+
Ok(self.read()? as i32)
57+
}
58+
59+
/// Take an measurement in microvolts.
60+
fn measure_uv(&mut self) -> Result<i32, Self::Error> {
61+
let uv = self.measure_mv()?;
62+
Ok(uv.saturating_mul(1000))
63+
}
64+
65+
/// Take an measurement in nanovolts.
66+
fn measure_nv(&mut self) -> Result<i32, Self::Error> {
67+
let uv = self.measure_uv()?;
68+
Ok(uv.saturating_mul(1000))
69+
}
5370
}
5471

5572
impl<T> AdcChannel for &mut T

0 commit comments

Comments
 (0)