Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion embedded-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
31 changes: 18 additions & 13 deletions embedded-devices/src/devices/vishay/veml7700/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -100,7 +101,7 @@ pub enum MeasurementError<BusError> {
/// 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.
Expand Down Expand Up @@ -183,7 +184,7 @@ impl<D: hal::delay::DelayNs, I: embedded_interfaces::registers::RegisterInterfac
&mut self,
config: &mut Configuration,
ps_sleep: u32,
) -> Result<f32, MeasurementError<I::BusError>> {
) -> Result<f64, MeasurementError<I::BusError>> {
const UP_THRESHOLD: u16 = 100;
const DOWN_THRESHOLD: u16 = 10_000;

Expand Down Expand Up @@ -233,7 +234,7 @@ impl<D: hal::delay::DelayNs, I: embedded_interfaces::registers::RegisterInterfac
self.configure(config).await?;
}

Ok(raw_lux as f32 * config.resolution())
Ok(raw_lux as f64 * config.resolution())
}
}

Expand All @@ -260,9 +261,10 @@ impl<D: hal::delay::DelayNs, I: embedded_interfaces::registers::RegisterInterfac
self.write_register(config).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::<lux>(corrected_lux),
})
}
}
Expand Down Expand Up @@ -319,10 +321,11 @@ impl<D: hal::delay::DelayNs, I: embedded_interfaces::registers::RegisterInterfac
async fn current_measurement(&mut self) -> Result<Option<Self::Measurement>, Self::Error> {
let config = self.read_register::<Configuration>().await?;
let raw_data = self.read_register::<registers::ALSData>().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::<lux>(corrected_lux),
}))
}

Expand All @@ -339,13 +342,15 @@ impl<D: hal::delay::DelayNs, I: embedded_interfaces::registers::RegisterInterfac
let ps = self.read_register::<PowerSaving>().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::<lux>(corrected_lux),
})
}
}

fn lux_correction(raw_lux: f32) -> f32 {
fn lux_correction(raw_lux: f64) -> f64 {
if raw_lux <= 1000.0 {
return raw_lux;
}
Expand All @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions embedded-devices/src/devices/vishay/veml7700/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
Loading