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
impl Scratchpad
Sourcepub fn parse(bytes: &[u8; 9]) -> Result<Scratchpad, SensorError>
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.
Sourcepub fn new(
raw_temperature: i16,
resolution: Resolution,
alarm_high: i8,
alarm_low: i8,
) -> Scratchpad
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_celsiusbuilds 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.
Sourcepub fn to_bytes(&self) -> [u8; 9]
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.
Sourcepub fn raw_temperature(&self) -> i16
pub fn raw_temperature(&self) -> i16
Returns the raw temperature register value.
§Returns
The 16-bit two’s-complement register, as a signed value.
Sourcepub fn temperature_micro_celsius(&self) -> i32
pub fn temperature_micro_celsius(&self) -> i32
Returns the temperature in micro-degrees Celsius.
§Returns
The temperature, exact, in millionths of a degree Celsius.
Sourcepub fn temperature_celsius(&self) -> f32
pub fn temperature_celsius(&self) -> f32
Sourcepub fn resolution(&self) -> Resolution
pub fn resolution(&self) -> Resolution
Returns the configured conversion resolution.
Sourcepub fn alarm_high(&self) -> i8
pub fn alarm_high(&self) -> i8
Returns the high temperature alarm threshold (TH), in whole degrees Celsius.
Trait Implementations§
Source§impl Clone for Scratchpad
impl Clone for Scratchpad
Source§fn clone(&self) -> Scratchpad
fn clone(&self) -> Scratchpad
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for Scratchpad
Source§impl Debug for Scratchpad
impl Debug for Scratchpad
impl Eq for Scratchpad
Source§impl PartialEq for Scratchpad
impl PartialEq for Scratchpad
Source§fn eq(&self, other: &Scratchpad) -> bool
fn eq(&self, other: &Scratchpad) -> bool
self and other values to be equal, and is used by ==.