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>
impl<B, D> Ds18b20<B, D>
Sourcepub fn with_resolution(self, resolution: Resolution) -> Self
pub fn with_resolution(self, resolution: Resolution) -> Self
Sourcepub fn with_alarms(self, high: i8, low: i8) -> Self
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.
Sourcepub fn rom(&self) -> Option<RomCode>
pub fn rom(&self) -> Option<RomCode>
Returns the ROM code the driver addresses, or None for the only device.
Sourcepub fn resolution(&self) -> Resolution
pub fn resolution(&self) -> Resolution
Returns the resolution the driver converts at.
Source§impl<B: OneWireBus, D: DelayNs> Ds18b20<B, D>
impl<B: OneWireBus, D: DelayNs> Ds18b20<B, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<OneWireError<B::Error>>>
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.
Sourcepub fn measure(
&mut self,
) -> Result<Scratchpad, DriverError<OneWireError<B::Error>>>
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.
Sourcepub fn read_scratchpad(
&mut self,
) -> Result<Scratchpad, DriverError<OneWireError<B::Error>>>
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.
Sourcepub fn save_settings(
&mut self,
) -> Result<(), DriverError<OneWireError<B::Error>>>
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.
Sourcepub fn powered_externally(
&mut self,
) -> Result<bool, DriverError<OneWireError<B::Error>>>
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.