From e57601bc3366051845d34e437911f6c0f2d54fd1 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 17 Jan 2024 20:16:24 +0100 Subject: [PATCH] Add methods to measure voltage instead of raw value. --- embedded-hal/src/adc.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/embedded-hal/src/adc.rs b/embedded-hal/src/adc.rs index c2885716..a91e3d41 100644 --- a/embedded-hal/src/adc.rs +++ b/embedded-hal/src/adc.rs @@ -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; + + /// Take an measurement in millivolts. + fn measure_mv(&mut self) -> Result { + Ok(self.read()? as i32) + } + + /// Take an measurement in microvolts. + fn measure_uv(&mut self) -> Result { + let uv = self.measure_mv()?; + Ok(uv.saturating_mul(1000)) + } + + /// Take an measurement in nanovolts. + fn measure_nv(&mut self) -> Result { + let uv = self.measure_uv()?; + Ok(uv.saturating_mul(1000)) + } } impl AdcChannel for &mut T