|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import time |
| 4 | +from Adafruit_I2C import Adafruit_I2C |
| 5 | + |
| 6 | +class _MPL115A2_Register: |
| 7 | + |
| 8 | + # Each register is two bytes wide, with the values left-aligned in the |
| 9 | + # register. |
| 10 | + # TOTAL_BITS gives the total number of data bits (and is used to calculate |
| 11 | + # how much right shifting is needed to eliminate the padding). |
| 12 | + # SIGN_BITS is the number of bits reserved for sign information and |
| 13 | + # determines whether the value is read as a signed or unsigned number. |
| 14 | + # FRACTIONAL_BITS gives the number of bits that should be considered to |
| 15 | + # follow the decimal point. |
| 16 | + # DECIMAL_ZERO_PADDING gives the number of additional zero bits that should |
| 17 | + # be considered to exist between the decimal point and the first data bit. |
| 18 | + |
| 19 | + def __init__(self, address, total_bits, sign_bits, fractional_bits, decimal_zero_padding): |
| 20 | + self.address = address |
| 21 | + self.total_bits = total_bits |
| 22 | + self.sign_bits = sign_bits |
| 23 | + self.fractional_bits = fractional_bits |
| 24 | + self.decimal_zero_padding = decimal_zero_padding |
| 25 | + |
| 26 | + def read(self, i2c): |
| 27 | + # Get the data from the register. |
| 28 | + raw_value = i2c.readU16(self.address) |
| 29 | + |
| 30 | + # Negative values are stored in two's-complement |
| 31 | + if self.sign_bits == 1 and raw_value & 0x8000: |
| 32 | + raw_value = raw_value ^ 0xFFFF * -1 |
| 33 | + |
| 34 | + # Apply the bit parameters. |
| 35 | + return (raw_value >> (16 - self.total_bits)) / float(2**(self.fractional_bits+self.decimal_zero_padding)) |
| 36 | + |
| 37 | + |
| 38 | +class MPL115A2: |
| 39 | + |
| 40 | + # From the section 3.1 table: |
| 41 | + # address bits |
| 42 | + # total sign frac dec.zero.pad |
| 43 | + __REGISTER_PADC = _MPL115A2_Register(0x00, 10, 0, 0, 0) |
| 44 | + __REGISTER_TADC = _MPL115A2_Register(0x02, 10, 0, 0, 0) |
| 45 | + __REGISTER_A0 = _MPL115A2_Register(0x04, 16, 1, 3, 0) |
| 46 | + __REGISTER_B1 = _MPL115A2_Register(0x06, 16, 1, 13, 0) |
| 47 | + __REGISTER_B2 = _MPL115A2_Register(0x08, 16, 1, 14, 0) |
| 48 | + __REGISTER_C12 = _MPL115A2_Register(0x0A, 14, 1, 13, 9) |
| 49 | + |
| 50 | + |
| 51 | + def __init__(self, address=0x60, debug=False): |
| 52 | + self.i2c = Adafruit_I2C(address) |
| 53 | + |
| 54 | + # Read coefficients |
| 55 | + self.a0 = self.__REGISTER_A0.read(self.i2c) |
| 56 | + self.b1 = self.__REGISTER_B1.read(self.i2c) |
| 57 | + self.b2 = self.__REGISTER_B2.read(self.i2c) |
| 58 | + self.c12 = self.__REGISTER_C12.read(self.i2c) |
| 59 | + |
| 60 | + |
| 61 | + def getPT(self): |
| 62 | + """Returns a tuple of (pressure, temperature) as measured by the sensor.""" |
| 63 | + |
| 64 | + # Instruct the sensor to begin data conversion. |
| 65 | + self.i2c.write8(0x12, 0x00) |
| 66 | + |
| 67 | + # Wait until conversion has finished. The datasheet says "3ms". We'll |
| 68 | + # wait 5ms just to be sure. Please note the documentation about |
| 69 | + # time.sleep(), though: |
| 70 | + # http://docs.python.org/3.0/library/time.html#time.sleep |
| 71 | + time.sleep(0.005) |
| 72 | + |
| 73 | + # Read the raw values. |
| 74 | + padc = self.__REGISTER_PADC.read(self.i2c) |
| 75 | + tadc = self.__REGISTER_TADC.read(self.i2c) |
| 76 | + |
| 77 | + # Calculate compensated pressure value, section 3.2 of the datasheet. |
| 78 | + pcomp = self.a0 + (self.b1 + self.c12 * tadc) * padc + self.b2 * tadc |
| 79 | + |
| 80 | + # Calculate final values. The formula for pressure is from section 3.2 |
| 81 | + # of the datasheet. The formula for temperature is basically magic: |
| 82 | + # http://www.adafruit.com/forums/viewtopic.php?f=19&t=41347 |
| 83 | + pressure = pcomp * ((115.0 - 50.0) / 1023.0) + 50.0 |
| 84 | + temperature = (tadc - 498.0) / -5.35 + 25.0 |
| 85 | + |
| 86 | + return (pressure, temperature) |
| 87 | + |
| 88 | + |
| 89 | + def getPressure(self): |
| 90 | + return self.getPT()[0] |
| 91 | + |
| 92 | + |
| 93 | + def getTemperature(self): |
| 94 | + return self.getPT()[1] |
0 commit comments