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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! bmp280-rs
//! 
//! `no_std` compatible i2c driver for the
//! [bmp280](https://www.bosch-sensortec.com/products/environmental-sensors/pressure-sensors/pressure-sensors-bmp280-1.html) temperature / air pressure sensor.
//!
//! ## Status
//!
//! Basic temperature and pressure readouts are values I expect.
//! Not rigorously tested in production though. 
#![no_std]
#![forbid(unsafe_code)]

use core::fmt::Debug;
use core::marker::PhantomData;
use embedded_hal as hal;
use hal::blocking::i2c::{Write, WriteRead};
use snafu::{ensure, OptionExt};

pub mod config;
pub mod error;
pub mod i2c_address;
mod internal;
pub mod register;

pub use config::*;
pub use error::*;
pub use register::*;

use internal::*;

pub use i2c_address::I2CAddress;
use pressure_calibration_data::PressureCalibrationData;
use temperature_calibration_data::TemperatureCalibrationData;

pub struct ModeNormal;
pub struct ModeSleep;

pub struct BMP280<I2C, Mode> {
    i2c_address: u8,
    temperature_calibration_data: TemperatureCalibrationData,
    pressure_calibration_data: PressureCalibrationData,
    t_fine: i32,
    config: Config,
    mode: PhantomData<Mode>,
    i2c: PhantomData<I2C>,
}

// Magic number that must be written to the reset register to actually trigger a reset.
const RESET_WORD: u8 = 0xB6;
const DEVICE_ID: u8 = 0x58;

impl<I2C, E, Mode> BMP280<I2C, Mode>
where
    I2C: WriteRead<Error = E> + Write<Error = E>,
    E: Debug,
{
    /// Reset the chip into sleep mode and reconfigure it with the current config.
    pub fn into_reset(self, i2c: &mut I2C) -> Result<BMP280<I2C, ModeSleep>, Error> {
        i2c
            .write(self.i2c_address, &[Register::Reset.addr(), RESET_WORD])
            .map_err(|_| Error::WriteConfig)?;

        Self::_configure(
            i2c,
            self.i2c_address,
            &self.config,
            MeasurementMode::Sleep,
        )?;

        Ok(BMP280 {
            i2c: PhantomData, 
            i2c_address: self.i2c_address,
            temperature_calibration_data: self.temperature_calibration_data,
            pressure_calibration_data: self.pressure_calibration_data,
            t_fine: self.t_fine,
            config: self.config,
            mode: PhantomData,
        })
    }

    fn _trigger_forced_measurement(&self, i2c: &mut I2C) -> Result<(), Error> {
        Self::_configure(
            i2c, 
            self.i2c_address,
            &self.config,
            MeasurementMode::Forced,
        )
    }

    fn _into_normal_mode(self, i2c: &mut I2C) -> Result<BMP280<I2C, ModeNormal>, Error> {
        let measurement_standby_time_millis = self
            .config
            .measurement_standby_time_millis
            .context(NormalModeNeedsMeasStandbyTime)?;

        Self::_configure(
            i2c,
            self.i2c_address,
            &self.config,
            MeasurementMode::Normal(measurement_standby_time_millis),
        )?;

        Ok(BMP280 {
            i2c: PhantomData, 
            i2c_address: self.i2c_address,
            temperature_calibration_data: self.temperature_calibration_data,
            pressure_calibration_data: self.pressure_calibration_data,
            t_fine: self.t_fine,
            config: self.config,
            mode: PhantomData,
        })
    }
    fn _into_sleep_mode(self, i2c: &mut I2C) -> Result<BMP280<I2C, ModeSleep>, Error> {
        Self::_configure(
            i2c,
            self.i2c_address,
            &self.config,
            MeasurementMode::Sleep,
        )?;

        Ok(BMP280 {
            i2c: self.i2c,
            i2c_address: self.i2c_address,
            temperature_calibration_data: self.temperature_calibration_data,
            pressure_calibration_data: self.pressure_calibration_data,
            t_fine: self.t_fine,
            config: self.config,
            mode: PhantomData,
        })
    }

    /// Read the uncompensated temperature data. 20 bit value.
    fn _read_raw_temperature(&self, i2c: &mut I2C) -> Result<u32, Error> {
        let bytes =
            Self::write_read_register_u32(i2c, self.i2c_address, Register::TempMSB)?;
        Ok(bytes >> 12)
    }

    /// Read the uncompensated pressure data. 20 bit value.
    fn _read_raw_pressure(&self, i2c: &mut I2C) -> Result<u32, Error> {
        let bytes =
            Self::write_read_register_u32(i2c, self.i2c_address, Register::PressMSB)?;
        Ok(bytes >> 12)
    }

    /// Read the compensated temperature.
    fn _read_temperature(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        let raw_temp = self._read_raw_temperature(i2c)?;

        let (calibrated_temperature, t_fine) =
            Self::compensate_temperature(raw_temp as i32, &self.temperature_calibration_data);

        // t_fine is needed for compensating the pressure.
        self.t_fine = t_fine;

        Ok(calibrated_temperature)
    }

    /// Read the compensated pressure.
    fn _read_pressure(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        let raw_pressure = self._read_raw_pressure(i2c)?;

        let compensated_pressure = Self::compensate_pressure(
            raw_pressure as i32,
            self.t_fine,
            &self.pressure_calibration_data,
        );

        Ok(compensated_pressure)
    }

    fn _configure(
        i2c: &mut I2C,
        i2c_address: u8,
        config: &Config,
        measurement_mode: MeasurementMode,
    ) -> Result<(), Error> {
        let config_byte = config.config_byte();
        i2c.write(i2c_address, &[Register::Config.addr(), config_byte])
            .map_err(|_| Error::WriteConfig)?;

        let ctrl_meas_byte = config.ctrl_meas_byte(measurement_mode);
        i2c.write(i2c_address, &[Register::CtrlMeas.addr(), ctrl_meas_byte])
            .map_err(|_| Error::WriteControlMeas)
    }

    fn get_temperature_calibration_data(
        i2c: &mut I2C,
        i2c_address: u8,
    ) -> Result<TemperatureCalibrationData, Error> {
        let t1 = Self::write_read_register_u16_le(i2c, i2c_address, Register::CalT1Byte0)?;
        let t2 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalT2Byte0)?;
        let t3 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalT3Byte0)?;

        Ok(TemperatureCalibrationData { t1, t2, t3 })
    }

    fn get_pressure_calibration_data(
        i2c: &mut I2C,
        i2c_address: u8,
    ) -> Result<PressureCalibrationData, Error> {
        let p1 = Self::write_read_register_u16_le(i2c, i2c_address, Register::CalP1Byte0)?;
        let p2 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP2Byte0)?;
        let p3 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP3Byte0)?;
        let p4 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP4Byte0)?;
        let p5 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP5Byte0)?;
        let p6 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP6Byte0)?;
        let p7 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP7Byte0)?;
        let p8 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP8Byte0)?;
        let p9 = Self::write_read_register_i16_le(i2c, i2c_address, Register::CalP9Byte0)?;

        Ok(PressureCalibrationData {
            p1,
            p2,
            p3,
            p4,
            p5,
            p6,
            p7,
            p8,
            p9,
        })
    }

    /// Get the device ID
    fn get_device_id(i2c: &mut I2C, i2c_address: u8) -> Result<u8, Error> {
        let input = [Register::Id.addr()];
        let mut output = [0u8];
        i2c.write_read(i2c_address, &input, &mut output)
            .map_err(|_| Error::ReadingDeviceAddressOverI2C)?;
        Ok(output[0])
    }

    fn write_read_register_u16_le(
        i2c: &mut I2C,
        i2c_address: u8,
        register: Register,
    ) -> Result<u16, Error> {
        let mut buffer = [0u8; 2];
        i2c.write_read(i2c_address, &[register.addr()], &mut buffer)
            .map_err(|_| Error::ReadRegister)?;

        Ok(u16::from_le_bytes(buffer))
    }

    fn write_read_register_i16_le(
        i2c: &mut I2C,
        i2c_address: u8,
        register: Register,
    ) -> Result<i16, Error> {
        let mut buffer = [0u8; 2];
        i2c.write_read(i2c_address, &[register.addr()], &mut buffer)
            .map_err(|_| Error::ReadRegister)?;

        Ok(i16::from_le_bytes(buffer))
    }

    fn write_read_register_u32(
        i2c: &mut I2C,
        i2c_address: u8,
        register: Register,
    ) -> Result<u32, Error> {
        let mut buffer = [0u8; 4];
        i2c.write_read(i2c_address, &[register.addr()], &mut buffer)
            .map_err(|_| Error::ReadRegister)?;

        Ok(u32::from_be_bytes(buffer))
    }

    // Line by line port of the c code from the datasheet
    fn compensate_temperature(
        raw_temp: i32,
        temperature_calibration_data: &TemperatureCalibrationData,
    ) -> (i32, i32) {
        let var1 = (((raw_temp >> 3) - ((temperature_calibration_data.t1 as i32) << 1))
            * (temperature_calibration_data.t2 as i32))
            >> 11;

        let var2 = (((((raw_temp >> 4) - (temperature_calibration_data.t1 as i32))
            * ((raw_temp >> 4) - (temperature_calibration_data.t1 as i32)))
            >> 12)
            * (temperature_calibration_data.t3 as i32))
            >> 14;

        let t_fine = var1 + var2;
        let t = (t_fine * 5 + 128) >> 8;

        (t, t_fine)
    }

    // Line by line port of the c code from the datasheet
    fn compensate_pressure(
        raw_pressure: i32,
        t_fine: i32,
        pressure_calibration_data: &PressureCalibrationData,
    ) -> i32 {
        let t_fine = t_fine as i64;

        let var1 = t_fine - 128000;
        let var2 = var1 * var1 * (pressure_calibration_data.p6 as i64);
        let var2 = var2 + ((var1 * (pressure_calibration_data.p5 as i64)) << 17);
        let var2 = var2 + ((pressure_calibration_data.p4 as i64) << 35);

        let var1 = ((var1 * var1 * (pressure_calibration_data.p3 as i64)) >> 8)
            + ((var1 * (pressure_calibration_data.p2 as i64)) << 12);
        let var1 = ((1i64 << 47) + var1) * (pressure_calibration_data.p1 as i64) >> 33;

        if var1 == 0 {
            return 0;
        }

        let p = 1048576i64 - raw_pressure as i64;
        let p = (((p << 31) - var2) * 3125) / var1;
        let var1 = ((pressure_calibration_data.p9 as i64) * (p >> 13) * (p >> 13)) >> 25;
        let var2 = ((pressure_calibration_data.p8 as i64) * p) >> 19;
        let p = ((p + var1 + var2) >> 8) + ((pressure_calibration_data.p7 as i64) << 4);
        p as i32
    }
}

impl<I2C, E> BMP280<I2C, ModeNormal>
where
    I2C: WriteRead<Error = E> + Write<Error = E>,
    E: Debug,
{
    /// Change into sleep mode
    pub fn into_sleep_mode(self, i2c: &mut I2C) -> Result<BMP280<I2C, ModeSleep>, Error> {
        Self::_into_sleep_mode(self, i2c)
    }

    /// Read the uncompensated pressure data. 20 bit value.
    pub fn read_raw_pressure(&mut self, i2c: &mut I2C) -> Result<u32, Error> {
        Self::_read_raw_pressure(self, i2c)
    }

    /// Read the uncompensated temperature data. 20 bit value.
    pub fn read_raw_temperature(&mut self, i2c: &mut I2C) -> Result<u32, Error> {
        Self::_read_raw_temperature(self, i2c)
    }

    /// Read the compensated pressure.
    pub fn read_pressure(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        Self::_read_pressure(self, i2c)
    }

    /// Read the compensated temperature.
    pub fn read_temperature(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        Self::_read_temperature(self, i2c)
    }
}

impl<I2C, E> BMP280<I2C, ModeSleep>
where
    I2C: WriteRead<Error = E> + Write<Error = E>,
    E: Debug,
{
    pub fn new(i2c: &mut I2C, i2c_address: I2CAddress, config: Config) -> Result<Self, Error> {
        let i2c_address = i2c_address.addr();

        ensure!(
            DEVICE_ID == Self::get_device_id(i2c, i2c_address)?,
            IncorrectDeviceId
        );

        let temperature_calibration_data =
            Self::get_temperature_calibration_data(i2c, i2c_address)?;

        let pressure_calibration_data = Self::get_pressure_calibration_data(i2c, i2c_address)?;

        Self::_configure(i2c, i2c_address, &config, MeasurementMode::Sleep)?;

        let bmp280 = BMP280 {
            i2c: PhantomData,
            i2c_address,
            temperature_calibration_data,
            pressure_calibration_data,
            t_fine: 0,
            config,
            mode: PhantomData,
        };

        Ok(bmp280)
    }

    /// Convert into normal mode.
    ///
    /// Normal mode continuously samples.
    pub fn into_normal_mode(self, i2c: &mut I2C) -> Result<BMP280<I2C, ModeNormal>, Error> {
        Self::_into_normal_mode(self, i2c)
    }

    /// Trigger a "forced" mode single measurement.
    ///
    /// Device internally returns to sleep mode after measurement is complete and then the pressure
    /// / temperature can be read.
    pub fn trigger_measurement(&self, i2c: &mut I2C) -> Result<(), Error> {
        Self::_trigger_forced_measurement(self, i2c)
    }

    /// Read the uncompensated pressure data. 20 bit value.
    ///
    /// Call `trigger_measurement` before calling this.
    /// NB You need to allow time for the measurement to complete!
    pub fn read_raw_pressure(&mut self, i2c: &mut I2C) -> Result<u32, Error> {
        Self::_read_raw_pressure(self, i2c)
    }

    /// Read the uncompensated temperature data. 20 bit value.
    ///
    /// Call `trigger_measurement` before calling this.
    /// NB You need to allow time for the measurement to complete!
    pub fn read_raw_temperature(&mut self, i2c: &mut I2C) -> Result<u32, Error> {
        Self::_read_raw_temperature(self, i2c)
    }

    /// Read the compensated pressure.
    ///
    /// Call `trigger_measurement` before calling this.
    /// NB You need to allow time for the measurement to complete!
    pub fn read_pressure(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        Self::_read_pressure(self, i2c)
    }

    /// Read the compensated temperature.
    ///
    /// Call `trigger_measurement` before calling this.
    /// NB You need to allow time for the measurement to complete!
    pub fn read_temperature(&mut self, i2c: &mut I2C) -> Result<i32, Error> {
        Self::_read_temperature(self, i2c)
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use embedded_hal_mock::i2c::Mock as I2CMock;

    #[test]
    fn temperature_calibration() {
        // Values from datasheet page 23
        let ut = 519888i32;
        let temperature_calibration_data = TemperatureCalibrationData {
            t1: 27504,
            t2: 26435,
            t3: -1000,
        };
        let (result, t_fine) =
            BMP280::<I2CMock, ModeSleep>::compensate_temperature(ut, &temperature_calibration_data);
        assert_eq!(t_fine, 128422);
        assert_eq!(result, 2508);
    }

    #[test]
    fn pressure_calibration() {
        // Values from datasheet page 23
        let up = 415148;
        let t_fine = 128422;
        let pressure_calibration_data = PressureCalibrationData {
            p1: 36477,
            p2: -10685,
            p3: 3024,
            p4: 2855,
            p5: 140,
            p6: -7,
            p7: 15500,
            p8: -14600,
            p9: 6000,
        };
        let result = BMP280::<I2CMock, ModeSleep>::compensate_pressure(
            up,
            t_fine,
            &pressure_calibration_data,
        );

        // This is the value in the datasheet but it uses floating point and we're 3 away.
        //assert_eq!(result, 25767236);

        // I'm gonna say it's close enough
        assert_eq!(result, 25767233);
    }
}