Skip to content

Commit

Permalink
Merge pull request #11 from lonesometraveler/defmt
Browse files Browse the repository at this point in the history
Implement defmt::Format
  • Loading branch information
teamplayer3 authored Mar 6, 2024
2 parents c5453ab + ede0502 commit 42f90d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -270,6 +273,7 @@ bitfield! {

// #[derive(BitfieldSpecifier)]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(Format))]
pub enum Validity {
NormalOperation,
WarmupPhase,
Expand All @@ -291,6 +295,7 @@ impl From<u8> for Validity {

bitfield! {
#[derive(Default)]
#[cfg_attr(feature = "defmt", derive(Format))]
struct InterruptRegister(u8);
impl Debug;
from into InterruptState, _, set_interrupt_state: 6, 6;
Expand All @@ -302,6 +307,7 @@ bitfield! {

// #[derive(BitfieldSpecifier)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(Format))]
pub enum PinMode {
OpenDrain,
PushPull,
Expand All @@ -327,6 +333,7 @@ impl From<u8> for PinMode {

// #[derive(BitfieldSpecifier)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(Format))]
pub enum InterruptState {
ActiveLow,
ActiveHigh,
Expand All @@ -351,6 +358,7 @@ impl From<u8> for InterruptState {
}

#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt", derive(Format))]
pub struct InterruptConfig(InterruptRegister);

impl InterruptConfig {
Expand Down Expand Up @@ -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,
Expand All @@ -405,6 +414,7 @@ impl From<u8> for AirQualityIndex {
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(Format))]
pub struct ECo2(u16);

impl From<u16> for ECo2 {
Expand Down

0 comments on commit 42f90d2

Please sign in to comment.