1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use byteorder::ByteOrder;
use byteorder::LittleEndian;
use core::fmt::Debug;
use hal::blocking::delay::DelayUs;

use crate::Device;
use crate::Error;
use crate::OneWire;
use crate::Sensor;

pub const FAMILY_CODE: u8 = 0x28;

#[repr(u8)]
pub enum Command {
    Convert = 0x44,
    WriteScratchpad = 0x4e,
    ReadScratchpad = 0xBE,
    CopyScratchpad = 0x48,
    RecallE2 = 0xB8,
    ReadPowerSupply = 0xB4,
}

#[repr(u8)]
#[derive(Debug, Copy, Clone)]
pub enum MeasureResolution {
    TC8 = 0b0001_1111,
    TC4 = 0b0011_1111,
    TC2 = 0b0101_1111,
    TC = 0b0111_1111,
}

impl MeasureResolution {
    pub fn time_ms(&self) -> u16 {
        match self {
            &MeasureResolution::TC8 => 94,
            &MeasureResolution::TC4 => 188,
            &MeasureResolution::TC2 => 375,
            &MeasureResolution::TC => 750,
        }
    }
}

pub struct DS18B20 {
    device: Device,
    resolution: MeasureResolution,
}

impl DS18B20 {
    pub fn new<E: Debug>(device: Device) -> Result<DS18B20, Error<E>> {
        if device.address[0] != FAMILY_CODE {
            Err(Error::FamilyCodeMismatch(FAMILY_CODE, device.address[0]))
        } else {
            Ok(DS18B20 {
                device,
                resolution: MeasureResolution::TC,
            })
        }
    }

    pub unsafe fn new_forced(device: Device) -> DS18B20 {
        DS18B20 {
            device,
            resolution: MeasureResolution::TC,
        }
    }

    pub fn measure_temperature<E: Debug>(
        &self,
        wire: &mut OneWire<E>,
        delay: &mut dyn DelayUs<u16>,
    ) -> Result<MeasureResolution, Error<E>> {
        wire.reset_select_write_only(delay, &self.device, &[Command::Convert as u8])?;
        Ok(self.resolution)
    }

    pub fn read_temperature<E: Debug>(
        &self,
        wire: &mut OneWire<E>,
        delay: &mut dyn DelayUs<u16>,
    ) -> Result<u16, Error<E>> {
        let mut scratchpad = [0u8; 9];
        wire.reset_select_write_read(
            delay,
            &self.device,
            &[Command::ReadScratchpad as u8],
            &mut scratchpad[..],
        )?;
        super::ensure_correct_rcr8(&self.device, &scratchpad[..8], scratchpad[8])?;
        Ok(DS18B20::read_temperature_from_scratchpad(&scratchpad))
    }

    fn read_temperature_from_scratchpad(scratchpad: &[u8]) -> u16 {
        LittleEndian::read_u16(&scratchpad[0..2])
    }
}

impl Sensor for DS18B20 {
    fn family_code() -> u8 {
        FAMILY_CODE
    }

    fn start_measurement<E: Debug>(
        &self,
        wire: &mut OneWire<E>,
        delay: &mut dyn DelayUs<u16>,
    ) -> Result<u16, Error<E>> {
        Ok(self.measure_temperature(wire, delay)?.time_ms())
    }

    fn read_measurement<E: Debug>(
        &self,
        wire: &mut OneWire<E>,
        delay: &mut dyn DelayUs<u16>,
    ) -> Result<f32, Error<E>> {
        self.read_temperature(wire, delay)
            .map(|t| t as i16 as f32 / 16_f32)
    }

    fn read_measurement_raw<E: Debug>(
        &self,
        wire: &mut OneWire<E>,
        delay: &mut dyn DelayUs<u16>,
    ) -> Result<u16, Error<E>> {
        self.read_temperature(wire, delay)
    }
}

/// Split raw u16 value to two parts: integer and fraction N
/// Original value may be calculated as: integer + fraction/10000
pub fn split_temp(temperature: u16) -> (i16, i16) {
    if temperature < 0x8000 {
        (temperature as i16 >> 4, (temperature as i16 & 0xF) * 625)
    } else {
        let abs = -(temperature as i16);
        (-(abs >> 4), -625 * (abs & 0xF))
    }
}

#[cfg(test)]
mod tests {
    use super::split_temp;
    #[test]
    fn test_temp_conv() {
        assert_eq!(split_temp(0x07d0), (125, 0));
        assert_eq!(split_temp(0x0550), (85, 0));
        assert_eq!(split_temp(0x0191), (25, 625)); // 25.0625
        assert_eq!(split_temp(0x00A2), (10, 1250)); // 10.125
        assert_eq!(split_temp(0x0008), (0, 5000)); // 0.5
        assert_eq!(split_temp(0x0000), (0, 0)); // 0
        assert_eq!(split_temp(0xfff8), (0, -5000)); // -0.5
        assert_eq!(split_temp(0xFF5E), (-10, -1250)); // -10.125
        assert_eq!(split_temp(0xFE6F), (-25, -625)); // -25.0625
        assert_eq!(split_temp(0xFC90), (-55, 0)); // -55
    }
}