pub fn parse_w1_slave(text: &str) -> Result<Scratchpad, SensorError>Expand description
Decodes the text the Linux kernel’s w1_therm driver serves for a thermometer.
The w1_slave file under /sys/bus/w1/devices/28-*/ holds two lines: the nine
scratchpad bytes in hex followed by : crc=xx YES or NO, then the same bytes
followed by t= and the temperature in millidegrees. This takes the first line’s
bytes and parses them as a Scratchpad, so the CRC is checked here as well; a
NO from the kernel is reported without a second look.
§Arguments
text- the file’s contents.
§Returns
The scratchpad.
§Errors
Returns SensorError::Crc if the kernel or this decoder rejects the CRC, and
SensorError::Invalid if the text is not in the driver’s format.
§Examples
use pamoja_sensors::ds18b20::{parse_w1_slave, temperature_from_celsius, Resolution, Scratchpad};
// What the kernel prints for a part at 20.8125 C, built from the same library
// that decodes it.
let raw = temperature_from_celsius(20.8125, Resolution::Bits12);
let bytes = Scratchpad::new(raw, Resolution::Bits12, 75, -10).to_bytes();
let hex: Vec<String> = bytes.iter().map(|byte| format!("{byte:02x}")).collect();
let text = format!("{} : crc={:02x} YES\n", hex.join(" "), bytes[8]);
let scratchpad = parse_w1_slave(&text)?;
assert_eq!(scratchpad.temperature_micro_celsius(), 20_812_500);