Skip to main content

Scratchpad

Struct Scratchpad 

Source
pub struct Scratchpad { /* private fields */ }
Expand description

A decoded, CRC-verified DS18B20 scratchpad.

The scratchpad is nine bytes: temperature LSB and MSB, the high and low alarm thresholds, the configuration byte, three reserved bytes, and a CRC. parse checks the CRC before exposing any of it.

§Examples

use pamoja_sensors::ds18b20::{temperature_from_celsius, Resolution, Scratchpad};

// What a part sitting at +25.0625 °C, set to 12-bit steps with its thresholds at
// +75/-10 °C, puts on the bus. A driver reads these nine bytes off the wire.
let bytes = Scratchpad::new(
    temperature_from_celsius(25.0625, Resolution::Bits12),
    Resolution::Bits12,
    75,
    -10,
)
.to_bytes();

let scratchpad = Scratchpad::parse(&bytes)?;
assert_eq!(scratchpad.temperature_micro_celsius(), 25_062_500);
assert_eq!(scratchpad.resolution(), Resolution::Bits12);

Implementations§

Source§

impl Scratchpad

Source

pub fn parse(bytes: &[u8; 9]) -> Result<Scratchpad, SensorError>

Parses and CRC-checks a nine-byte scratchpad.

§Arguments
  • bytes - the nine scratchpad bytes in the order the device sends them, the ninth being the CRC.
§Returns

The decoded scratchpad.

§Errors

Returns SensorError::Crc if the CRC byte does not match the first eight, which means the read was corrupted and should be repeated.

Source

pub fn new( raw_temperature: i16, resolution: Resolution, alarm_high: i8, alarm_low: i8, ) -> Scratchpad

Builds the scratchpad a part in the given state reports.

This is the inverse of parse, so a node can be developed and tested against the bytes a thermometer would send without one attached.

§Arguments
  • raw_temperature - the 16-bit two’s-complement temperature register, as a signed value; temperature_from_celsius builds one from a temperature.
  • resolution - the resolution the part is configured for.
  • alarm_high - the high alarm threshold in whole degrees Celsius (TH).
  • alarm_low - the low alarm threshold in whole degrees Celsius (TL).
§Returns

The scratchpad holding those values.

Source

pub fn to_bytes(&self) -> [u8; 9]

Returns the nine bytes a part holding this scratchpad puts on the bus.

Bytes 5 to 7 are the reserved bytes, which a part reports but which carry no reading; they are filled with the values its scratchpad figure shows so the frame is the one a device would actually send. The ninth byte is the CRC over the other eight, so the result parses.

§Returns

The nine scratchpad bytes in transmission order, CRC last.

Source

pub fn raw_temperature(&self) -> i16

Returns the raw temperature register value.

§Returns

The 16-bit two’s-complement register, as a signed value.

Source

pub fn temperature_micro_celsius(&self) -> i32

Returns the temperature in micro-degrees Celsius.

§Returns

The temperature, exact, in millionths of a degree Celsius.

Source

pub fn temperature_celsius(&self) -> f32

Returns the temperature in degrees Celsius.

§Returns

The temperature in degrees Celsius.

Source

pub fn resolution(&self) -> Resolution

Returns the configured conversion resolution.

Source

pub fn alarm_high(&self) -> i8

Returns the high temperature alarm threshold (TH), in whole degrees Celsius.

Source

pub fn alarm_low(&self) -> i8

Returns the low temperature alarm threshold (TL), in whole degrees Celsius.

Trait Implementations§

Source§

impl Clone for Scratchpad

Source§

fn clone(&self) -> Scratchpad

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Scratchpad

Source§

impl Debug for Scratchpad

Source§

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

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

impl Eq for Scratchpad

Source§

impl PartialEq for Scratchpad

Source§

fn eq(&self, other: &Scratchpad) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Scratchpad

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.