Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 8fc232b

Browse files
author
Alex Bucknall
committed
Updated MPL3115A2.py for both Altitude and Pressure
1 parent 8e9c35a commit 8fc232b

File tree

1 file changed

+72
-67
lines changed

1 file changed

+72
-67
lines changed

pysense/lib/MPL3115A2.py

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,66 @@
11
import time
2+
from machine import I2C
23

34
class MPL3115A2exception(Exception):
45
pass
56

67
class MPL3115A2:
7-
8-
MPL3115_I2CADDR = const(0x60)
9-
MPL3115_STATUS = const(0x00)
10-
MPL3115_PRESSURE_DATA_MSB = const(0x01)
11-
MPL3115_PRESSURE_DATA_CSB = const(0x02)
12-
MPL3115_PRESSURE_DATA_LSB = const(0x03)
13-
MPL3115_TEMP_DATA_MSB = const(0x04)
14-
MPL3115_TEMP_DATA_LSB = const(0x05)
15-
MPL3115_DR_STATUS = const(0x06)
16-
MPL3115_DELTA_DATA = const(0x07)
17-
MPL3115_WHO_AM_I = const(0x0c)
18-
MPL3115_FIFO_STATUS = const(0x0d)
19-
MPL3115_FIFO_DATA = const(0x0e)
20-
MPL3115_FIFO_SETUP = const(0x0e)
21-
MPL3115_TIME_DELAY = const(0x10)
22-
MPL3115_SYS_MODE = const(0x11)
23-
MPL3115_INT_SORCE = const(0x12)
24-
MPL3115_PT_DATA_CFG = const(0x13)
25-
MPL3115_BAR_IN_MSB = const(0x14)
26-
MPL3115_P_ARLARM_MSB = const(0x16)
27-
MPL3115_T_ARLARM = const(0x18)
28-
MPL3115_P_ARLARM_WND_MSB = const(0x19)
29-
MPL3115_T_ARLARM_WND = const(0x1b)
30-
MPL3115_P_MIN_DATA = const(0x1c)
31-
MPL3115_T_MIN_DATA = const(0x1f)
32-
MPL3115_P_MAX_DATA = const(0x21)
33-
MPL3115_T_MAX_DATA = const(0x24)
34-
MPL3115_CTRL_REG1 = const(0x26)
35-
MPL3115_CTRL_REG2 = const(0x27)
36-
MPL3115_CTRL_REG3 = const(0x28)
37-
MPL3115_CTRL_REG4 = const(0x29)
38-
MPL3115_CTRL_REG5 = const(0x2a)
39-
MPL3115_OFFSET_P = const(0x2b)
40-
MPL3115_OFFSET_T = const(0x2c)
41-
MPL3115_OFFSET_H = const(0x2d)
42-
ALTITUDE = const(0)
43-
PRESSURE = const(1)
44-
45-
def __init__(self, pysense=None, sda='P22', scl='P21'):
8+
MPL3115_I2CADDR = const(0x60)
9+
MPL3115_STATUS = const(0x00)
10+
MPL3115_PRESSURE_DATA_MSB = const(0x01)
11+
MPL3115_PRESSURE_DATA_CSB = const(0x02)
12+
MPL3115_PRESSURE_DATA_LSB = const(0x03)
13+
MPL3115_TEMP_DATA_MSB = const(0x04)
14+
MPL3115_TEMP_DATA_LSB = const(0x05)
15+
MPL3115_DR_STATUS = const(0x06)
16+
MPL3115_DELTA_DATA = const(0x07)
17+
MPL3115_WHO_AM_I = const(0x0c)
18+
MPL3115_FIFO_STATUS = const(0x0d)
19+
MPL3115_FIFO_DATA = const(0x0e)
20+
MPL3115_FIFO_SETUP = const(0x0e)
21+
MPL3115_TIME_DELAY = const(0x10)
22+
MPL3115_SYS_MODE = const(0x11)
23+
MPL3115_INT_SORCE = const(0x12)
24+
MPL3115_PT_DATA_CFG = const(0x13)
25+
MPL3115_BAR_IN_MSB = const(0x14)
26+
MPL3115_P_ARLARM_MSB = const(0x16)
27+
MPL3115_T_ARLARM = const(0x18)
28+
MPL3115_P_ARLARM_WND_MSB = const(0x19)
29+
MPL3115_T_ARLARM_WND = const(0x1b)
30+
MPL3115_P_MIN_DATA = const(0x1c)
31+
MPL3115_T_MIN_DATA = const(0x1f)
32+
MPL3115_P_MAX_DATA = const(0x21)
33+
MPL3115_T_MAX_DATA = const(0x24)
34+
MPL3115_CTRL_REG1 = const(0x26)
35+
MPL3115_CTRL_REG2 = const(0x27)
36+
MPL3115_CTRL_REG3 = const(0x28)
37+
MPL3115_CTRL_REG4 = const(0x29)
38+
MPL3115_CTRL_REG5 = const(0x2a)
39+
MPL3115_OFFSET_P = const(0x2b)
40+
MPL3115_OFFSET_T = const(0x2c)
41+
MPL3115_OFFSET_H = const(0x2d)
42+
ALTITUDE = const(0)
43+
PRESSURE = const(1)
44+
45+
def __init__(self, pysense = None, sda = 'P22', scl = 'P21', mode = PRESSURE):
4646
if pysense is not None:
4747
self.i2c = pysense.i2c
4848
else:
49-
from machine import I2C
5049
self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl))
5150

5251
self.STA_reg = bytearray(1)
53-
54-
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0xB8]))
55-
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_PT_DATA_CFG, bytes([0x07]))
56-
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0xB9]))
52+
self.mode = mode
53+
54+
if self.mode is PRESSURE:
55+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x38])) # barometer mode, not raw, oversampling 128, minimum time 512 ms
56+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_PT_DATA_CFG, bytes([0x07])) # no events detected
57+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x39])) # active
58+
elif self.mode is ALTITUDE:
59+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0xB8])) # altitude mode, not raw, oversampling 128, minimum time 512 ms
60+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_PT_DATA_CFG, bytes([0x07])) # no events detected
61+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0xB9])) # active
62+
else:
63+
raise MPL3115A2exception("Invalid Mode MPL3115A2")
5764

5865
if self._read_status():
5966
pass
@@ -62,7 +69,6 @@ def __init__(self, pysense=None, sda='P22', scl='P21'):
6269

6370
def _read_status(self):
6471
while True:
65-
6672
self.i2c.readfrom_mem_into(MPL3115_I2CADDR, MPL3115_STATUS, self.STA_reg)
6773

6874
if(self.STA_reg[0] == 0):
@@ -73,41 +79,40 @@ def _read_status(self):
7379
else:
7480
return False
7581

76-
def _fixed_decimal(self, frac_value):
77-
fixed_decimal = 0
82+
def pressure(self):
83+
if self.mode == ALTITUDE:
84+
raise MPL3115A2exception("Incorrect Measurement Mode MPL3115A2")
7885

79-
for x in range(2,len(bin(frac_value))):
80-
fixed_decimal += int(bin(frac_value)[x])*(2**(-(x-1)))
86+
OUT_P_MSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_MSB,1)
87+
OUT_P_CSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_CSB,1)
88+
OUT_P_LSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_LSB,1)
89+
90+
return float((OUT_P_MSB[0] << 10) + (OUT_P_CSB[0] << 2) + ((OUT_P_LSB[0] >> 6) & 0x03) + ((OUT_P_LSB[0] >> 4) & 0x03) / 4.0)
8191

82-
return fixed_decimal
92+
def altitude(self):
93+
if self.mode == PRESSURE:
94+
raise MPL3115A2exception("Incorrect Measurement Mode MPL3115A2")
8395

84-
def alt(self):
8596
OUT_P_MSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_MSB,1)
8697
OUT_P_CSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_CSB,1)
8798
OUT_P_LSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_LSB,1)
88-
OUT_P_ALL = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_PRESSURE_DATA_MSB,5)
89-
90-
pres_frac = OUT_P_LSB[0] >> 4
91-
pres_int = (OUT_P_MSB[0] << 8)|(OUT_P_CSB[0])
9299

93-
if (pres_int & (1 << (16 - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
94-
pres_int = pres_int - (1 << 16)
100+
alt_int = (OUT_P_MSB[0] << 8) + (OUT_P_CSB[0])
101+
alt_frac = ((OUT_P_LSB[0] >> 4) & 0x0F)
95102

96-
pres_frac = self._fixed_decimal(pres_frac)
103+
if alt_int > 32767:
104+
alt_int -= 65536
97105

98-
return(float(str(pres_int)+str(pres_frac)[1:]))
106+
return float(alt_int + alt_frac / 4.0)
99107

100-
def temp(self):
108+
def temperature(self):
101109
OUT_T_MSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_TEMP_DATA_MSB,1)
102110
OUT_T_LSB = self.i2c.readfrom_mem(MPL3115_I2CADDR, MPL3115_TEMP_DATA_LSB,1)
103111

104-
temp_frac = OUT_T_LSB[0] >> 4
105-
106112
temp_int = OUT_T_MSB[0]
113+
temp_frac = OUT_T_LSB[0]
107114

108-
if (temp_int & (1 << (8 - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
109-
temp_int = temp_int - (1 << 8)
110-
111-
temp_frac = self._fixed_decimal(temp_frac)
115+
if temp_int > 127:
116+
temp_int -= 256
112117

113-
return(float(str(temp_int)+str(temp_frac)[1:]))
118+
return float(temp_int + temp_frac / 256.0)

0 commit comments

Comments
 (0)