kellerkindt / onewire

OneWire bus implementation in Rust using embedded-hal
Apache License 2.0
35 stars 14 forks source link

Request example of use in a function (ie type sig) #12

Open David-OConnor opened 4 years ago

David-OConnor commented 4 years ago

Eg:

 fn read(
        temp_sensor: &mut DS18B20,
        one_w: &mut OneWire<PC13<Output<OpenDrain>>>,
        delay: &mut Delay,
    ) -> Self {
      let resolution = temp_sensor.measure_temperature(one_w, delay).unwrap();
      delay.delay_ms(resolution.time_ms());
      temp_sensor.read_temperature(one_w, delay).unwrap();
}

This doesn't work due to needing the Debug trait on OneWire:

= help: the trait `core::fmt::Debug` is not implemented for `stm32f3xx_hal::gpio::gpioc::PC13<stm32f3xx_hal::gpio::Output<stm32f3xx_hal::gpio::Ope
nDrain>>`
David-OConnor commented 4 years ago

Sorted out. This was partially due to an issue I can't identify, where I was using an older version of the package. I still think this would be useful. For stm32f3:

use hal::{
    gpio::gpioc::PCx,
    gpio::{OpenDrain, Output},
 };

/// Read the temperature, from the appropriate source.
/// todo: Consider making fallible.
fn read_temp(
    temp_sensor: &mut DS18B20,
    one_w: &mut OneWire<PCx<Output<OpenDrain>>>,
    delay: &mut Delay,
) -> f32 {
    // request sensor to measure temperature
    let resolution = s.measure_temperature(one_w, delay).unwrap();
    delay.delay_ms(resolution.time_ms());
    // read temperature
    let temp = temp_sensor.read_temperature(one_w, delay).unwrap();
    temp_from_ds(temp)
}