embedded_drivers/onewire/
ds18b20.rs

1use super::Device;
2
3/// DS18B20 digital thermometer.
4pub struct DS18B20;
5
6/// Initiates a single temperature conversion.
7pub const CMD_CONVERT_T: u8 = 0x44;
8/// Writes 3 bytes of data to the DS18B20’s scratchpad.
9pub const CMD_WRITE_SCRATCHPAD: u8 = 0x4e;
10/// Reads the contents of the scratchpad.
11pub const CMD_READ_SCRATCHPAD: u8 = 0xbe;
12/// Copies the contents of the scratchpad T_H, T_L and configuration registers
13/// (bytes 2, 3 and 4) to EEPROM.
14pub const CMD_COPY_SCRATCHPAD: u8 = 0x48;
15/// Recalls the alarm trigger values and configuration data from EEPROM and
16/// places the data in bytes 2, 3, and 4, respectively, in the scratchpad memory.
17pub const CMD_RECALL_EEPROM: u8 = 0xb8;
18/// Determines if any DS18B20s on the bus are using parasite power.
19pub const CMD_POWER_SUPPLY: u8 = 0xb4;
20
21#[repr(u8)]
22pub enum Resolution {
23    Nine = 0b000_11111,
24    Ten = 0b001_11111,
25    Eleven = 0b010_11111,
26    Twelve = 0b011_11111,
27}
28
29impl Resolution {
30    pub fn bits(&self) -> u8 {
31        match self {
32            Resolution::Nine => 9,
33            Resolution::Ten => 10,
34            Resolution::Eleven => 11,
35            Resolution::Twelve => 12,
36        }
37    }
38
39    pub fn conversion_time_ms(&self) -> u16 {
40        match self {
41            Resolution::Nine => 94,
42            Resolution::Ten => 188,
43            Resolution::Eleven => 375,
44            Resolution::Twelve => 750,
45        }
46    }
47}
48
49impl Default for Resolution {
50    fn default() -> Self {
51        Resolution::Twelve
52    }
53}
54
55pub struct Config {
56    pub resolution: Resolution,
57    pub t_h: i8,
58    pub t_l: i8,
59}
60
61impl Default for Config {
62    fn default() -> Self {
63        Config {
64            resolution: Resolution::Twelve,
65            t_h: 125,
66            t_l: -55,
67        }
68    }
69}
70
71pub trait DB18B20Ext {
72    /// Write Scratchpad
73    fn set_config(&mut self, config: Config);
74
75    fn read_config(&mut self) -> Config;
76
77    /// Convert T
78    fn start_measurement(&mut self);
79
80    /// Read Scratchpad
81    fn read_measurement(&mut self) -> f32;
82
83    /// Read raw measurement, avoiding using floats.
84    fn read_raw_measurement(&mut self) -> [u8; 2];
85
86    // Save config to EEPROM
87    /// Copy Scratchpad
88    fn save_config(&mut self);
89
90    /// Recall E^2
91    fn load_saved_config(&mut self);
92
93    /// Read Power Supply
94    fn is_parasite(&mut self) -> bool;
95}