diff --git a/Cargo.toml b/Cargo.toml index d06311e..1552ec9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,4 @@ embedded-hal = "1.0" embedded-hal-async = { version = "1.0", optional = true } bitfield = "0.14" maybe-async-cfg = "0.2" +defmt = { version = "0.3", optional = true } diff --git a/src/error.rs b/src/error.rs index 4c10155..e368f23 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,5 +8,12 @@ impl std::fmt::Display for AirQualityConvError { } } +#[cfg(feature = "defmt")] +impl defmt::Format for AirQualityConvError { + fn format(&self, f: defmt::Formatter) { + defmt::write!(f, "{} not a valid air quality reading (400..).", self.0) + } +} + #[cfg(feature = "std")] impl std::error::Error for AirQualityConvError {} diff --git a/src/lib.rs b/src/lib.rs index 03e496a..96ce7f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,9 @@ use embedded_hal_async::i2c::I2c as AsyncI2c; use bitfield::bitfield; use error::AirQualityConvError; +#[cfg(feature = "defmt")] +use defmt::Format; + // ENS160 Register address // This 2-byte register contains the part number in little endian of the ENS160. const ENS160_PART_ID_REG: u8 = 0x00; @@ -270,6 +273,7 @@ bitfield! { // #[derive(BitfieldSpecifier)] #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(Format))] pub enum Validity { NormalOperation, WarmupPhase, @@ -291,6 +295,7 @@ impl From for Validity { bitfield! { #[derive(Default)] + #[cfg_attr(feature = "defmt", derive(Format))] struct InterruptRegister(u8); impl Debug; from into InterruptState, _, set_interrupt_state: 6, 6; @@ -302,6 +307,7 @@ bitfield! { // #[derive(BitfieldSpecifier)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(Format))] pub enum PinMode { OpenDrain, PushPull, @@ -327,6 +333,7 @@ impl From for PinMode { // #[derive(BitfieldSpecifier)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(Format))] pub enum InterruptState { ActiveLow, ActiveHigh, @@ -351,6 +358,7 @@ impl From for InterruptState { } #[derive(Debug, Default)] +#[cfg_attr(feature = "defmt", derive(Format))] pub struct InterruptConfig(InterruptRegister); impl InterruptConfig { @@ -382,6 +390,7 @@ impl InterruptConfig { } #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "defmt", derive(Format))] #[repr(u8)] pub enum AirQualityIndex { Excellent = 1, @@ -405,6 +414,7 @@ impl From for AirQualityIndex { } #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "defmt", derive(Format))] pub struct ECo2(u16); impl From for ECo2 {