Skip to content

Feature/defmt#4

Merged
zeldan merged 3 commits intorust-dd:mainfrom
syrtcevvi:feature/defmt
Oct 7, 2025
Merged

Feature/defmt#4
zeldan merged 3 commits intorust-dd:mainfrom
syrtcevvi:feature/defmt

Conversation

@syrtcevvi
Copy link
Copy Markdown
Contributor

Hello and thank you for your awesome crate and the article!

The problem

In my project I use the defmt logger. I faced the problem with printing the SensorError value to the logs:

use esp_println as _;
use defmt::{error, info, Debug2Format};

match dht11.read() {
    Ok(sensor_reading) => info!(
        "DHT 11 Sensor - Temperature: {} °C, humidity: {} %",
        sensor_reading.temperature, sensor_reading.humidity
    ),
    Err(error) => error!("An error occured: {}", error),
}

Workarounds

The SensorError implements the Debug trait, so it's possible to print it to logs, but it brings a bit overhead and it's unnecessary verbose (Debug2Format):

match dht11.read() {
    Ok(sensor_reading) => info!(
        "DHT 11 Sensor - Temperature: {} °C, humidity: {} %",
        sensor_reading.temperature, sensor_reading.humidity
    ),
    Err(error) => Err(error) => error!("An error occured: {}", defmt::Debug2Format(&error)),
}

Changes

  • I've added the optional defmt feature and derived the defmt::Format for the SensorError
  • cargo fmt slightly changed the code in a few places

@zeldan zeldan merged commit 11cc2a9 into rust-dd:main Oct 7, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants