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
//! This is an embedded-hal device driver for the MPL3115A2 Altitude sensor.
//! 
//! It is accurate to around 0.3m.
//! 
//! There is also a temperature and pressure readings avaliable.
//! 
//! See the examples folder for a blocking implementation.
//! 
//! The pressure mode is likely wildly inaccurate and needs to be tested against a known good pressure sensor.

#![no_std]

mod reg;
use reg::*;

use core::fmt::Debug;

use embedded_hal as hal;
use hal::blocking::i2c::{Write, WriteRead};
use cast::f32;

#[derive(Debug)]
pub enum Error<E> {
    /// I²C bus error
    I2c(E),
    /// Failed to parse sensor data (Not yet used)
    InvalidData,
    /// Chip ID doesn't match expected value
    UnsupportedChip,
}

/// Device Mode
/// 
/// Useful for "non" blocking measurements
#[derive(Copy,Clone,PartialEq)]
pub enum Mode {
	Inactive,
	Active,
	TakingReading,
}

/// Pressure or Altitude Mode
/// 
/// Toggle as required
#[derive(Copy,Clone,PartialEq)]
pub enum PressureAlt {
	Pressure,
	Altitude,
}

/// `MPL3115A2` driver
/// 
/// Will start off deactivated and in the PressureAlt mode set
pub struct MPL3115A2<I2C> {
    /// The concrete I²C device implementation
    i2c: I2C,

    /// Mode (Inactive, Active, Taking Sample)
	mode: Mode,
	
	/// Pressure or Altitude Mode
	pa: PressureAlt,
}

/// Interrupt setting and status
/*pub struct Int<'a, REG, I2C> {
    dev: &'a mut MMA8452q<I2C>,
    reg: PhantomData<REG>,
}*/

impl<I2C, E> MPL3115A2<I2C>
where
    I2C: WriteRead<Error = E> + Write<Error = E>,
    E: Debug,
{
    /// Create a new `MPL3115A2` driver from the given `I2C` peripheral
    pub fn new(i2c: I2C, pa: PressureAlt) -> Result<Self, Error<E>> {

		//Create the device
        let mut dev = Self {
            i2c,
            mode: Mode::Inactive,
            pa: pa,
        };

        // Ensure we have the correct device ID
        if dev.get_device_id()? != DEVICE_ID {
            return Err(Error::UnsupportedChip);
        }

		//Enables the pressure and temp measurement event flags so that we can
		//test against them. This is recommended in datasheet during setup.
		//Enable all three pressure and temp event flags 
		dev.write_reg(Register::PT_DATA_CFG,EVENT_FLAGS).map_err(Error::I2c)?; 

		//Set the required PA mode
		dev.change_reading_type(pa)?;

        Ok(dev)
    }

    /// Destroy driver instance, return `I2C` bus instance
    pub fn destroy(self) -> I2C {
        self.i2c
    }

    /// Get the `WHO_AM_I` register
    pub fn get_device_id(&mut self) -> Result<u8, Error<E>> {
        self.read_reg(Register::WHO_AM_I).map_err(Error::I2c)
    }

    /// Activate the Device
    pub fn activate(&mut self) -> Result<(), Error<E>> {
		self.reg_set_bits(Register::CTRL_REG1, DEVICE_EN).map_err(Error::I2c)?;
		self.mode = Mode::Active;
        Ok(())
	}

	/// De-activate the Device
	pub fn deactivate(&mut self) -> Result<(), Error<E>> {
		self.reg_reset_bits(Register::CTRL_REG1, DEVICE_EN).map_err(Error::I2c)?;
		self.mode = Mode::Inactive;
        Ok(())
	}

	/// Change between altitude and pressure
	pub fn change_reading_type(&mut self, pa: PressureAlt) -> Result<(), Error<E>> {

		match pa {
			PressureAlt::Altitude => {
				self.reg_set_bits(Register::CTRL_REG1, ALT_EN).map_err(Error::I2c)?;
			},
			PressureAlt::Pressure => {
				self.reg_reset_bits(Register::CTRL_REG1, ALT_EN).map_err(Error::I2c)?;
			}
		}
	
		self.pa = pa;
        Ok(())
	}

	/// Get one (blocking) Pressure or Altitude value
	pub fn take_one_pa_reading(&mut self) -> Result<f32, Error<E>> {
		//Trigger a one shot reading
		self.start_reading()?;

		//Wait for PDR bit, indicates we have new pressure data
		while ! self.check_pa_reading()? {

		}

		//Get the data
		self.get_pa_reading()
	}

	/// Get one (blocking) Temperature value
	pub fn take_one_temp_reading(&mut self) -> Result<f32, Error<E>> {
		//Trigger a one shot reading
		self.start_reading()?;

		//Wait for TDR bit, indicates we have new temperature data
		while ! self.check_temp_reading()? {

		}

		//Get the data
		self.get_temp_reading()
	}

	/// Clear then set the OST bit which causes the sensor to immediately take another reading
	/// Needed to sample faster than 1Hz
	pub fn start_reading(&mut self) -> Result<(), Error<E>> {

		self.reg_reset_bits(Register::CTRL_REG1, ONE_SHOT).map_err(Error::I2c)?;
		self.reg_set_bits(Register::CTRL_REG1, ONE_SHOT).map_err(Error::I2c)?;
	  
		//We are now waiting for data
		self.mode = Mode::TakingReading;
        Ok(())
	}

	/// Check the PDR bit for new data
	pub fn check_pa_reading(&mut self) -> Result<bool, Error<E>> {
		let status_reg = self.read_reg(Register::STATUS).map_err(Error::I2c)?;
		Ok(status_reg & PDR != 0)
	}

	/// Check the TDR bit for new data
	pub fn check_temp_reading(&mut self) -> Result<bool, Error<E>> {
		let status_reg = self.read_reg(Register::STATUS).map_err(Error::I2c)?;

		Ok(status_reg & TDR != 0)
	}

	/// Get and process the pressure or altitude data
	pub fn get_pa_reading(&mut self) -> Result<f32, Error<E>> {

		// Read pressure registers
		let mut buf = [0u8; 3];
		self.read_regs(Register::OUT_P_MSB, &mut buf).map_err(Error::I2c)?;

		//Change the device back to active
		self.mode = Mode::Active;

		// The least significant bytes l_altitude and l_temp are 4-bit,
		// fractional values, so you must cast the calulation in (float),
		// shift the value over 4 spots to the right and divide by 16 (since 
		// there are 16 values in 4-bits). 
		match self.pa {
			PressureAlt::Altitude => {
				
				let lsb = buf[2]>>4;
				let tempcsb = f32(lsb)/16.0;
				let int_buf = [buf[0], buf[1]];

				let altitude = f32( i16::from_be_bytes(int_buf)) + tempcsb;
			
				Ok(altitude)
			},
			PressureAlt::Pressure => {
				//Reads the current pressure in Pa
				// Pressure comes back as a left shifted 20 bit number
				let int_buf = [0u8, buf[0], buf[1], buf[2]];
				let mut pressure_whole: u32 = u32::from_be_bytes(int_buf);
				pressure_whole >>= 6; //Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion.
		
				buf[2] &= 0b0011_0000; //Bits 5/4 represent the fractional component
				buf[2] >>= 4; //Get it right aligned
				let pressure_decimal = f32(buf[2])/4.0; //Turn it into fraction
		
				let pressure = f32(pressure_whole) + pressure_decimal;
		
				Ok(pressure)
		
			}

		}
	}


	///Get and process the temperature data
	pub fn get_temp_reading(&mut self) -> Result<f32, Error<E>> {

		// Read temperature registers
		let mut buf = [0u8; 2];
		self.read_regs(Register::OUT_T_MSB, &mut buf).map_err(Error::I2c)?;

		//Change the device back to active
		self.mode = Mode::Active;

		//Negative temperature fix by D.D.G.
		//let mut foo: u16 = 0;
		let mut neg_sign = false;

		//Check for 2s compliment
		if buf[0] > 0x7F
		{
			let mut foo = u16::from_be_bytes(buf);
			foo = !foo + 1;  //2’s complement
			buf = foo.to_be_bytes();
			buf[1] = buf[1] & 0xF0; 
			neg_sign = true;
		}

		// The least significant bytes l_altitude and l_temp are 4-bit,
		// fractional values, so you must cast the calulation in (float),
		// shift the value over 4 spots to the right and divide by 16 (since 
		// there are 16 values in 4-bits). 
		let templsb = f32(buf[1]>>4)/16.0; //temp, fraction of a degree

		let mut temperature = f32(buf[0]) + templsb;

		if neg_sign {
			temperature = 0.0 - temperature;
		}

		Ok(temperature)
	}


	/// Set the number of samples the device makes before saving the data
	/// Call with a rate from 0 to 7. Datasheet calls for 128 but you can set it from 1 to 128 samples. 
	/// The higher the oversample rate the greater the time between data samples.
	///
	/// Example Times:
	/// * 0 = 8ms
    /// * 3 = 30ms
    /// * 7 = 380ms
	pub fn set_oversample_rate(&mut self, mut sample_rate: u8)  -> Result<(), Error<E>> {
		if sample_rate > 7 {
			sample_rate = 7; //OS cannot be larger than 0b.0111
		}
		sample_rate <<= 3; //Align it for the CTRL_REG1 register
		
		let mut temp_setting = self.read_reg(Register::CTRL_REG1).map_err(Error::I2c)?; //Read current settings
		temp_setting &= 0b1100_0111; //Clear out old OS bits
		temp_setting |= sample_rate; //Mask in new OS bits
		self.write_reg(Register::CTRL_REG1,temp_setting).map_err(Error::I2c)
	}

	#[inline]
	fn read_reg(&mut self, reg: Register) -> Result<u8, E> {
		let mut buf = [0u8];
		self.i2c.write_read(I2C_SAD, &[reg.addr()], &mut buf)?;
		Ok(buf[0])
	}

	#[inline]
	fn read_regs(&mut self, reg: Register, buffer: &mut [u8]) -> Result<(), E> {
		self.i2c
			.write_read(I2C_SAD, &[reg.addr()], buffer)
	}

	#[inline]
	fn write_reg(&mut self, reg: Register, val: u8) -> Result<(), E> {
		self.i2c.write(I2C_SAD, &[reg.addr(), val])
	}

	#[inline]
	fn modify_reg<F>(&mut self, reg: Register, f: F) -> Result<(), E>
	where
		F: FnOnce(u8) -> u8,
	{
		let r = self.read_reg(reg)?;
		self.write_reg(reg, f(r))?;
		Ok(())
	}

	#[inline]
	fn reg_set_bits(&mut self, reg: Register, bits: u8) -> Result<(), E> {
		self.modify_reg(reg, |v| v | bits)
	}

	#[inline]
	fn reg_reset_bits(&mut self, reg: Register, bits: u8) -> Result<(), E> {
		self.modify_reg(reg, |v| v & !bits)
	}

	#[inline]
	fn reg_xset_bits(&mut self, reg: Register, bits: u8, set: bool) -> Result<(), E> {
		if set {
			self.reg_set_bits(reg, bits)
		} else {
			self.reg_reset_bits(reg, bits)
		}
	}
}