diff --git a/Cargo.lock b/Cargo.lock index 045fd56..0fdfc4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -212,7 +212,7 @@ dependencies = [ "maybe-async-cfg", "paste", "thiserror", - "uom", + "uom 0.38.0", ] [[package]] @@ -259,7 +259,7 @@ dependencies = [ "maybe-async-cfg", "thiserror", "trybuild", - "uom", + "uom 0.37.0", ] [[package]] @@ -761,6 +761,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "uom" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a739f83872836c82a4f2527d4e54b37007b3de68cafe7edde95fd695968bf4b9" +dependencies = [ + "num-traits", + "typenum", +] + [[package]] name = "winapi-util" version = "0.1.9" diff --git a/embedded-devices/Cargo.toml b/embedded-devices/Cargo.toml index 5ddba7b..bb695d8 100644 --- a/embedded-devices/Cargo.toml +++ b/embedded-devices/Cargo.toml @@ -22,7 +22,7 @@ embedded-interfaces = { version = "0.10.3", path = "../embedded-interfaces", def embedded-devices-derive = { version = "0.10.3", path = "../embedded-devices-derive" } maybe-async-cfg = "0.2" paste = "1.0" -uom = { version = "0.37.0", features = ["f64", "si"], default-features = false } +uom = { version = "0.38.0", features = ["f64", "si"], default-features = false } # uom = { git = "https://github.com/iliekturtles/uom", rev = "b64af636bdff2119e5f1e0e5ad5206a326403e79", features = ["f64", "si"], default-features = false } crc = "3.3" thiserror = { version = "2.0.12", default-features = false } diff --git a/embedded-devices/src/devices/vishay/veml7700/mod.rs b/embedded-devices/src/devices/vishay/veml7700/mod.rs index d9a6d80..9de283c 100644 --- a/embedded-devices/src/devices/vishay/veml7700/mod.rs +++ b/embedded-devices/src/devices/vishay/veml7700/mod.rs @@ -74,7 +74,8 @@ pub mod registers; use registers::{Configuration, PowerSaving}; -//use uom::si::illuminance::lux; // requires uom 0.38.0 +use uom::si::f64::Illuminance; +use uom::si::illuminance::lux; #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Debug, thiserror::Error)] @@ -100,7 +101,7 @@ pub enum MeasurementError { /// Measurement data #[derive(Debug, embedded_devices_derive::Measurement)] pub struct Measurement { - pub lux: f32, //uom::si::Illuminance, + pub lux: Illuminance, } /// The VEML7700 is a high accuracy digital ambient light sensor. @@ -183,7 +184,7 @@ impl Result> { + ) -> Result> { const UP_THRESHOLD: u16 = 100; const DOWN_THRESHOLD: u16 = 10_000; @@ -233,7 +234,7 @@ impl(corrected_lux), }) } } @@ -319,10 +321,11 @@ impl Result, Self::Error> { let config = self.read_register::().await?; let raw_data = self.read_register::().await?; - let raw_lux = raw_data.read_als_data() as f32 * config.resolution(); + let raw_lux = raw_data.read_als_data() as f64 * config.resolution(); + let corrected_lux = lux_correction(raw_lux); Ok(Some(Measurement { - lux: lux_correction(raw_lux), + lux: Illuminance::new::(corrected_lux), })) } @@ -339,13 +342,15 @@ impl().await?; let raw_lux = self.measure_raw_lux_auto(&mut config, ps.ms()).await?; + let corrected_lux = lux_correction(raw_lux); + Ok(Measurement { - lux: lux_correction(raw_lux), + lux: Illuminance::new::(corrected_lux), }) } } -fn lux_correction(raw_lux: f32) -> f32 { +fn lux_correction(raw_lux: f64) -> f64 { if raw_lux <= 1000.0 { return raw_lux; } @@ -357,10 +362,10 @@ fn lux_correction(raw_lux: f32) -> f32 { } // coefficients from datasheet - const A: f32 = 6.0135e-13; - const B: f32 = -9.3924e-9; - const C: f32 = 8.1488e-5; - const D: f32 = 1.0023; + const A: f64 = 6.0135e-13; + const B: f64 = -9.3924e-9; + const C: f64 = 8.1488e-5; + const D: f64 = 1.0023; let res_d = D * raw_lux; diff --git a/embedded-devices/src/devices/vishay/veml7700/registers.rs b/embedded-devices/src/devices/vishay/veml7700/registers.rs index cd0d723..4b823c5 100644 --- a/embedded-devices/src/devices/vishay/veml7700/registers.rs +++ b/embedded-devices/src/devices/vishay/veml7700/registers.rs @@ -183,7 +183,7 @@ impl IntegrationTime { } } - pub(super) const fn k_hz(&self) -> f32 { + pub(super) const fn k_hz(&self) -> f64 { match self { IntegrationTime::T_25 => 0.04, IntegrationTime::T_50 => 0.02, @@ -223,7 +223,7 @@ impl IntegrationTime { } impl Gain { - pub const fn factor(&self) -> f32 { + pub const fn factor(&self) -> f64 { match self { Gain::X_1 => 1.0, Gain::X_2 => 2.0, @@ -232,7 +232,7 @@ impl Gain { } } - pub(super) const fn inv_factor(&self) -> f32 { + pub(super) const fn inv_factor(&self) -> f64 { match self { Gain::X_1_8 => 8.0, Gain::X_1_4 => 4.0, @@ -267,8 +267,8 @@ impl Gain { impl Configuration { /// Calculate the lux/count resolution based on /// the current gain and integration time settings - pub fn resolution(&self) -> f32 { - const BASE_RES: f32 = 6.72; + pub fn resolution(&self) -> f64 { + const BASE_RES: f64 = 6.72; let gain = self.read_gain().inv_factor(); let it = self.read_integration_time().k_hz();