Skip to main content

w1_slave_text

Function w1_slave_text 

Source
pub fn w1_slave_text(scratchpad: &Scratchpad) -> String
Expand description

Renders the text the Linux kernel’s w1_therm driver serves for a scratchpad it read cleanly: the contents of a thermometer’s w1_slave file.

This is the inverse of parse_w1_slave, so a program that reads the kernel’s files is written and tested with no thermometer and no kernel. The format is the one w1_slave_show prints in drivers/w1/slaves/w1_therm.c: the nine bytes as two lowercase hex digits and a space each, then : crc=, the CRC the kernel computed over the first eight, and YES; then the nine bytes again and t= with the temperature in millidegrees, which w1_DS18B20_convert_temp takes as the raw register times 1000 divided by 16, truncated toward zero as C divides.

§Arguments

  • scratchpad - the scratchpad the kernel read.

§Returns

The file’s two lines.

§Examples

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

let raw = temperature_from_celsius(21.5, Resolution::Bits12);
let scratchpad = Scratchpad::new(raw, Resolution::Bits12, 75, -10);
let text = w1_slave_text(&scratchpad);
assert!(text.ends_with(" t=21500\n"));
assert_eq!(parse_w1_slave(&text)?, scratchpad);