1
1
import time
2
+ from machine import I2C
2
3
3
4
class MPL3115A2exception (Exception ):
4
5
pass
5
6
6
7
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 ):
46
46
if pysense is not None :
47
47
self .i2c = pysense .i2c
48
48
else :
49
- from machine import I2C
50
49
self .i2c = I2C (0 , mode = I2C .MASTER , pins = (sda , scl ))
51
50
52
51
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" )
57
64
58
65
if self ._read_status ():
59
66
pass
@@ -62,7 +69,6 @@ def __init__(self, pysense=None, sda='P22', scl='P21'):
62
69
63
70
def _read_status (self ):
64
71
while True :
65
-
66
72
self .i2c .readfrom_mem_into (MPL3115_I2CADDR , MPL3115_STATUS , self .STA_reg )
67
73
68
74
if (self .STA_reg [0 ] == 0 ):
@@ -73,41 +79,40 @@ def _read_status(self):
73
79
else :
74
80
return False
75
81
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" )
78
85
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 )
81
91
82
- return fixed_decimal
92
+ def altitude (self ):
93
+ if self .mode == PRESSURE :
94
+ raise MPL3115A2exception ("Incorrect Measurement Mode MPL3115A2" )
83
95
84
- def alt (self ):
85
96
OUT_P_MSB = self .i2c .readfrom_mem (MPL3115_I2CADDR , MPL3115_PRESSURE_DATA_MSB ,1 )
86
97
OUT_P_CSB = self .i2c .readfrom_mem (MPL3115_I2CADDR , MPL3115_PRESSURE_DATA_CSB ,1 )
87
98
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 ])
92
99
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 )
95
102
96
- pres_frac = self ._fixed_decimal (pres_frac )
103
+ if alt_int > 32767 :
104
+ alt_int -= 65536
97
105
98
- return ( float (str ( pres_int ) + str ( pres_frac )[ 1 :]) )
106
+ return float (alt_int + alt_frac / 4.0 )
99
107
100
- def temp (self ):
108
+ def temperature (self ):
101
109
OUT_T_MSB = self .i2c .readfrom_mem (MPL3115_I2CADDR , MPL3115_TEMP_DATA_MSB ,1 )
102
110
OUT_T_LSB = self .i2c .readfrom_mem (MPL3115_I2CADDR , MPL3115_TEMP_DATA_LSB ,1 )
103
111
104
- temp_frac = OUT_T_LSB [0 ] >> 4
105
-
106
112
temp_int = OUT_T_MSB [0 ]
113
+ temp_frac = OUT_T_LSB [0 ]
107
114
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
112
117
113
- return ( float (str ( temp_int ) + str ( temp_frac )[ 1 :]) )
118
+ return float (temp_int + temp_frac / 256.0 )
0 commit comments