Skip to main content

Ds18b20

Struct Ds18b20 

Source
pub struct Ds18b20<B, D> { /* private fields */ }
Expand description

A DS18B20 on a 1-Wire bus, measuring on demand.

Ds18b20::new addresses the only thermometer on the bus with SKIP ROM; Ds18b20::addressed addresses one of several by its ROM code with MATCH ROM. init writes the resolution and alarm thresholds to the scratchpad and reads it back to confirm the part answers. measure issues CONVERT T, waits the datasheet’s maximum conversion time for the resolution in use, then reads the nine-byte scratchpad, whose CRC is checked before a temperature is taken from it. As a [Sensor] its reading is the Scratchpad; one channel is selected with [Sensor::map].

The driver assumes the part is powered from its VDD pin. A part on parasite power needs a strong pull-up during every conversion, which the bus underneath has to supply; powered_externally reports which wiring the part has.

§Examples

A bit-banged bus over a scripted pin: the pin answers the presence sample low, then shifts out a scratchpad of a part at 25.0625 C.

use pamoja_core::Sensor;
use pamoja_hal::digital::PinState::{High, Low};
use pamoja_hal::onewire::BitBang;
use pamoja_hal::script::{block_on, DelayLog, PinScript};
use pamoja_sensors::ds18b20::{temperature_from_celsius, Ds18b20, Resolution, Scratchpad};

let raw = temperature_from_celsius(25.0625, Resolution::Bits12);
let scratchpad = Scratchpad::new(raw, Resolution::Bits12, 75, -10).to_bytes();
let mut levels = vec![Low, Low];
for byte in scratchpad {
    levels.extend((0..8).map(|bit| if (byte >> bit) & 1 == 1 { High } else { Low }));
}

let bus = BitBang::new(PinScript::new(levels), DelayLog::new());
let mut probe = Ds18b20::new(bus, DelayLog::new());
let mut celsius = probe.map(|scratchpad| scratchpad.temperature_celsius());
assert_eq!(block_on(celsius.read())?, 25.0625);

Implementations§

Source§

impl<B, D> Ds18b20<B, D>

Source

pub fn new(bus: B, delay: D) -> Self

Wraps a bus carrying a single thermometer.

§Arguments
  • bus - the 1-Wire bus.
  • delay - the timer that paces the conversions.
§Returns

The driver, at 12-bit resolution with the alarm thresholds at the part’s limits.

Source

pub fn addressed(bus: B, delay: D, rom: RomCode) -> Self

Wraps a bus carrying several devices and addresses one by its ROM code.

§Arguments
  • bus - the 1-Wire bus.
  • delay - the timer that paces the conversions.
  • rom - the thermometer’s ROM code, as a Search returns it.
§Returns

The driver.

Source

pub fn with_resolution(self, resolution: Resolution) -> Self

Sets the conversion resolution, which also sets the conversion time.

The setting is written to the part by init.

§Arguments
  • resolution - 9 to 12 bits.
§Returns

The driver.

Source

pub fn with_alarms(self, high: i8, low: i8) -> Self

Sets the alarm thresholds the part compares each conversion against.

The thresholds are written to the part by init and matter to an alarm search.

§Arguments
  • high - the upper threshold in whole degrees Celsius.
  • low - the lower threshold in whole degrees Celsius.
§Returns

The driver.

Source

pub fn rom(&self) -> Option<RomCode>

Returns the ROM code the driver addresses, or None for the only device.

Source

pub fn resolution(&self) -> Resolution

Returns the resolution the driver converts at.

Source

pub fn release(self) -> (B, D)

Gives back the bus and the delay.

§Returns

The bus and delay the driver was built from.

Source§

impl<B: OneWireBus, D: DelayNs> Ds18b20<B, D>

Source

pub fn init(&mut self) -> Result<(), DriverError<OneWireError<B::Error>>>

Writes the resolution and alarm thresholds and reads the scratchpad back.

The sequence is the datasheet’s WRITE SCRATCHPAD of the three configurable bytes, then a READ SCRATCHPAD whose CRC and configuration byte confirm the part took them. The settings live in the scratchpad until power is lost; save_settings copies them to the EEPROM.

§Errors

Returns DriverError::Bus if no device answers or the line fails, DriverError::Sensor if the scratchpad read back fails its CRC or does not hold the resolution written.

Source

pub fn measure( &mut self, ) -> Result<Scratchpad, DriverError<OneWireError<B::Error>>>

Runs one conversion and returns the scratchpad.

The sequence is the datasheet’s: address the part, CONVERT T, wait the maximum conversion time for the resolution, address the part again, and READ SCRATCHPAD. The resolution the driver waits for is the one it holds, so run init first if the part’s EEPROM setting differs.

§Returns

The CRC-checked scratchpad, with the temperature in it.

§Errors

Returns DriverError::Bus if no device answers or the line fails, and DriverError::Sensor if the scratchpad fails its CRC.

Source

pub fn read_scratchpad( &mut self, ) -> Result<Scratchpad, DriverError<OneWireError<B::Error>>>

Reads the scratchpad without converting, for the last result or the settings.

§Returns

The CRC-checked scratchpad.

§Errors

Returns DriverError::Bus if no device answers or the line fails, and DriverError::Sensor if the scratchpad fails its CRC.

Source

pub fn save_settings( &mut self, ) -> Result<(), DriverError<OneWireError<B::Error>>>

Copies the scratchpad settings to the EEPROM, so they survive power loss.

§Errors

Returns DriverError::Bus if no device answers or the line fails.

Source

pub fn powered_externally( &mut self, ) -> Result<bool, DriverError<OneWireError<B::Error>>>

Asks the part whether it is powered from VDD or parasitically from the line.

§Returns

true for external power, false for parasite power, which needs a strong pull-up during conversions.

§Errors

Returns DriverError::Bus if no device answers or the line fails.

Trait Implementations§

Source§

impl<B: Debug, D: Debug> Debug for Ds18b20<B, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B, D> Sensor for Ds18b20<B, D>
where B: OneWireBus + Send, B::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Scratchpad

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<Scratchpad>

Takes a single reading from the sensor. Read more
§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<B, D> Freeze for Ds18b20<B, D>
where B: Freeze, D: Freeze,

§

impl<B, D> RefUnwindSafe for Ds18b20<B, D>

§

impl<B, D> Send for Ds18b20<B, D>
where B: Send, D: Send,

§

impl<B, D> Sync for Ds18b20<B, D>
where B: Sync, D: Sync,

§

impl<B, D> Unpin for Ds18b20<B, D>
where B: Unpin, D: Unpin,

§

impl<B, D> UnsafeUnpin for Ds18b20<B, D>
where B: UnsafeUnpin, D: UnsafeUnpin,

§

impl<B, D> UnwindSafe for Ds18b20<B, D>
where B: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.