use crate::config::{ConfigurationSnapshot, MeasurementConfig};
use crate::id::DeviceId;
use crate::illuminance::{MicroLux, NominalScale};
use crate::power::PowerSavingSnapshot;
use crate::threshold::{ThresholdStatus, Thresholds};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AlsCounts(u16);
impl AlsCounts {
pub const fn from_counts(counts: u16) -> Self {
Self(counts)
}
pub const fn counts(self) -> u16 {
self.0
}
pub const fn is_max_code(self) -> bool {
self.0 == u16::MAX
}
pub const fn nominal_micro_lux(self, config: MeasurementConfig) -> MicroLux {
NominalScale::for_config(config).scale_counts(self.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct WhiteCounts(u16);
impl WhiteCounts {
pub const fn from_counts(counts: u16) -> Self {
Self(counts)
}
pub const fn counts(self) -> u16 {
self.0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MeasurementPairCoherence {
SequentialRegisters,
FrozenAfterRequestedWait,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SnapshotMeasurement {
pub als: AlsCounts,
pub white: WhiteCounts,
pub configuration: ConfigurationSnapshot,
pub power_saving: PowerSavingSnapshot,
pub coherence: MeasurementPairCoherence,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MeasurementCapture {
pub als: AlsCounts,
pub white: WhiteCounts,
pub configuration: MeasurementConfig,
pub nominal_illuminance: MicroLux,
pub requested_wait_us: u32,
pub coherence: MeasurementPairCoherence,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DeviceSnapshot {
pub id: DeviceId,
pub configuration: ConfigurationSnapshot,
pub power_saving: PowerSavingSnapshot,
pub thresholds: Thresholds,
pub threshold_status: ThresholdStatus,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn raw_count_types_preserve_the_entire_word_domain() {
for counts in [0, 1, u16::MAX - 1, u16::MAX] {
assert_eq!(AlsCounts::from_counts(counts).counts(), counts);
assert_eq!(WhiteCounts::from_counts(counts).counts(), counts);
assert_eq!(
AlsCounts::from_counts(counts).is_max_code(),
counts == u16::MAX
);
}
}
}