Crate simmer

source ·
Expand description

Simmer

A collection of tools to easily represent different temperatures units and convert between them. Mostly for my embedded thermocouple projects. 😄

These should work in embedded (no_std) environments, too!

Usage

There’s nothing complex here. You can wrap your floating point types in a Temperature to make sure it keeps its unit. It’ll also convert automatically between units, use Display and Debug, and help you explicitly unwrap the values when you don’t need the unit anymore!

It’s also worth mentioning that embedded users should enable the f32 feature flag. This will help you avoid f64 problems, particularly on AVR devices where 64-bit floating point values aren’t supported.

The ufmt crate’s uDisplay and uDebug traits are implemented, so you can use Temperature values much like you’d use ufmt_float. Feel free to unwrap the values and manually print it, though! 🥹

Anyways, here’s an example…

 use simmer::Temperature;

 let ice = Temperature::Fahrenheit(32.0);
 println!("water freezes at {ice} degrees fahrenheit");

 let ice_c = ice.to_celsius();
 println!("water freezes at {ice_c} degrees celsius");

 // i want that cool number 🥁
 let ice_raw_c: f64 = ice_c.into(); // it's an f32 if the feature is enabled!
 println!("here's a number: {ice_raw_c}");

Checked

There’s also a [CheckedTemperature] type so you can safely store and use temperatures. It works on embedded and implements many of the same functions that Temperature does!

See the [checked] module for more!

Here’s an example showing how to use it:

use simmer::{CheckedTemperature, Temperature};

fn main() -> anyhow::Result<()> {
    let ice = CheckedTemperature::new(Temperature::Fahrenheit(32.0))?;
    println!("water freezes at {ice} degrees fahrenheit");

    let ice_c = ice.to_celsius()?;
    let ice_raw_c: f64 = ice_c.into(); // can also use `f32` 😄
    println!("here's a number: {ice_raw_c}");

    Ok(())
}

Enums

  • A value that’s one of many common temperature units.