31
31
import time
32
32
33
33
from micropython import const
34
+
34
35
try :
35
36
import ustruct as struct
36
37
except ImportError :
43
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2.git"
44
45
45
46
46
- #pylint: disable=bad-whitespace
47
+ # pylint: disable=bad-whitespace
47
48
# Internal constants:
48
- _MPL3115A2_ADDRESS = const (0x60 )
49
- _MPL3115A2_REGISTER_STATUS = const (0x00 )
50
- _MPL3115A2_REGISTER_PRESSURE_MSB = const (0x01 )
51
- _MPL3115A2_REGISTER_PRESSURE_CSB = const (0x02 )
52
- _MPL3115A2_REGISTER_PRESSURE_LSB = const (0x03 )
53
- _MPL3115A2_REGISTER_TEMP_MSB = const (0x04 )
54
- _MPL3115A2_REGISTER_TEMP_LSB = const (0x05 )
55
- _MPL3115A2_REGISTER_DR_STATUS = const (0x06 )
56
- _MPL3115A2_OUT_P_DELTA_MSB = const (0x07 )
57
- _MPL3115A2_OUT_P_DELTA_CSB = const (0x08 )
58
- _MPL3115A2_OUT_P_DELTA_LSB = const (0x09 )
59
- _MPL3115A2_OUT_T_DELTA_MSB = const (0x0A )
60
- _MPL3115A2_OUT_T_DELTA_LSB = const (0x0B )
61
- _MPL3115A2_WHOAMI = const (0x0C )
62
- _MPL3115A2_BAR_IN_MSB = const (0x14 )
63
- _MPL3115A2_BAR_IN_LSB = const (0x15 )
64
-
65
- _MPL3115A2_REGISTER_STATUS_TDR = const (0x02 )
66
- _MPL3115A2_REGISTER_STATUS_PDR = const (0x04 )
67
- _MPL3115A2_REGISTER_STATUS_PTDR = const (0x08 )
68
-
69
- _MPL3115A2_PT_DATA_CFG = const (0x13 )
70
- _MPL3115A2_PT_DATA_CFG_TDEFE = const (0x01 )
71
- _MPL3115A2_PT_DATA_CFG_PDEFE = const (0x02 )
72
- _MPL3115A2_PT_DATA_CFG_DREM = const (0x04 )
73
-
74
- _MPL3115A2_CTRL_REG1 = const (0x26 )
75
- _MPL3115A2_CTRL_REG2 = const (0x27 )
76
- _MPL3115A2_CTRL_REG3 = const (0x28 )
77
- _MPL3115A2_CTRL_REG4 = const (0x29 )
78
- _MPL3115A2_CTRL_REG5 = const (0x2A )
79
-
80
- _MPL3115A2_CTRL_REG1_SBYB = const (0x01 )
81
- _MPL3115A2_CTRL_REG1_OST = const (0x02 )
82
- _MPL3115A2_CTRL_REG1_RST = const (0x04 )
83
- _MPL3115A2_CTRL_REG1_RAW = const (0x40 )
84
- _MPL3115A2_CTRL_REG1_ALT = const (0x80 )
85
- _MPL3115A2_CTRL_REG1_BAR = const (0x00 )
86
-
87
- _MPL3115A2_CTRL_REG1_OS1 = const (0x00 )
88
- _MPL3115A2_CTRL_REG1_OS2 = const (0x08 )
89
- _MPL3115A2_CTRL_REG1_OS4 = const (0x10 )
90
- _MPL3115A2_CTRL_REG1_OS8 = const (0x18 )
91
- _MPL3115A2_CTRL_REG1_OS16 = const (0x20 )
92
- _MPL3115A2_CTRL_REG1_OS32 = const (0x28 )
93
- _MPL3115A2_CTRL_REG1_OS64 = const (0x30 )
94
- _MPL3115A2_CTRL_REG1_OS128 = const (0x38 )
95
-
96
- _MPL3115A2_REGISTER_STARTCONVERSION = const (0x12 )
97
- #pylint: enable=bad-whitespace
49
+ _MPL3115A2_ADDRESS = const (0x60 )
50
+ _MPL3115A2_REGISTER_STATUS = const (0x00 )
51
+ _MPL3115A2_REGISTER_PRESSURE_MSB = const (0x01 )
52
+ _MPL3115A2_REGISTER_PRESSURE_CSB = const (0x02 )
53
+ _MPL3115A2_REGISTER_PRESSURE_LSB = const (0x03 )
54
+ _MPL3115A2_REGISTER_TEMP_MSB = const (0x04 )
55
+ _MPL3115A2_REGISTER_TEMP_LSB = const (0x05 )
56
+ _MPL3115A2_REGISTER_DR_STATUS = const (0x06 )
57
+ _MPL3115A2_OUT_P_DELTA_MSB = const (0x07 )
58
+ _MPL3115A2_OUT_P_DELTA_CSB = const (0x08 )
59
+ _MPL3115A2_OUT_P_DELTA_LSB = const (0x09 )
60
+ _MPL3115A2_OUT_T_DELTA_MSB = const (0x0A )
61
+ _MPL3115A2_OUT_T_DELTA_LSB = const (0x0B )
62
+ _MPL3115A2_WHOAMI = const (0x0C )
63
+ _MPL3115A2_BAR_IN_MSB = const (0x14 )
64
+ _MPL3115A2_BAR_IN_LSB = const (0x15 )
65
+
66
+ _MPL3115A2_REGISTER_STATUS_TDR = const (0x02 )
67
+ _MPL3115A2_REGISTER_STATUS_PDR = const (0x04 )
68
+ _MPL3115A2_REGISTER_STATUS_PTDR = const (0x08 )
69
+
70
+ _MPL3115A2_PT_DATA_CFG = const (0x13 )
71
+ _MPL3115A2_PT_DATA_CFG_TDEFE = const (0x01 )
72
+ _MPL3115A2_PT_DATA_CFG_PDEFE = const (0x02 )
73
+ _MPL3115A2_PT_DATA_CFG_DREM = const (0x04 )
74
+
75
+ _MPL3115A2_CTRL_REG1 = const (0x26 )
76
+ _MPL3115A2_CTRL_REG2 = const (0x27 )
77
+ _MPL3115A2_CTRL_REG3 = const (0x28 )
78
+ _MPL3115A2_CTRL_REG4 = const (0x29 )
79
+ _MPL3115A2_CTRL_REG5 = const (0x2A )
80
+
81
+ _MPL3115A2_CTRL_REG1_SBYB = const (0x01 )
82
+ _MPL3115A2_CTRL_REG1_OST = const (0x02 )
83
+ _MPL3115A2_CTRL_REG1_RST = const (0x04 )
84
+ _MPL3115A2_CTRL_REG1_RAW = const (0x40 )
85
+ _MPL3115A2_CTRL_REG1_ALT = const (0x80 )
86
+ _MPL3115A2_CTRL_REG1_BAR = const (0x00 )
87
+
88
+ _MPL3115A2_CTRL_REG1_OS1 = const (0x00 )
89
+ _MPL3115A2_CTRL_REG1_OS2 = const (0x08 )
90
+ _MPL3115A2_CTRL_REG1_OS4 = const (0x10 )
91
+ _MPL3115A2_CTRL_REG1_OS8 = const (0x18 )
92
+ _MPL3115A2_CTRL_REG1_OS16 = const (0x20 )
93
+ _MPL3115A2_CTRL_REG1_OS32 = const (0x28 )
94
+ _MPL3115A2_CTRL_REG1_OS64 = const (0x30 )
95
+ _MPL3115A2_CTRL_REG1_OS128 = const (0x38 )
96
+
97
+ _MPL3115A2_REGISTER_STARTCONVERSION = const (0x12 )
98
+ # pylint: enable=bad-whitespace
98
99
99
100
100
101
class MPL3115A2 :
@@ -114,7 +115,7 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
114
115
self ._device = i2c_device .I2CDevice (i2c , address )
115
116
# Validate the chip ID.
116
117
if self ._read_u8 (_MPL3115A2_WHOAMI ) != 0xC4 :
117
- raise RuntimeError (' Failed to find MPL3115A2, check your wiring!' )
118
+ raise RuntimeError (" Failed to find MPL3115A2, check your wiring!" )
118
119
# Reset. Note the chip immediately resets and won't send an I2C back
119
120
# so we need to catch the OSError and swallow it (otherwise this fails
120
121
# expecting an ACK that never comes).
@@ -128,9 +129,12 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
128
129
# Configure the chip registers with default values.
129
130
self ._ctrl_reg1 = _MPL3115A2_CTRL_REG1_OS128 | _MPL3115A2_CTRL_REG1_ALT
130
131
self ._write_u8 (_MPL3115A2_CTRL_REG1 , self ._ctrl_reg1 )
131
- self ._write_u8 (_MPL3115A2_PT_DATA_CFG , _MPL3115A2_PT_DATA_CFG_TDEFE | \
132
- _MPL3115A2_PT_DATA_CFG_PDEFE | \
133
- _MPL3115A2_PT_DATA_CFG_DREM )
132
+ self ._write_u8 (
133
+ _MPL3115A2_PT_DATA_CFG ,
134
+ _MPL3115A2_PT_DATA_CFG_TDEFE
135
+ | _MPL3115A2_PT_DATA_CFG_PDEFE
136
+ | _MPL3115A2_PT_DATA_CFG_DREM ,
137
+ )
134
138
135
139
def _read_into (self , address , buf , count = None ):
136
140
# Read bytes from the specified 8-bit address into the provided buffer.
@@ -176,17 +180,20 @@ def pressure(self):
176
180
# Set control bits for pressure reading.
177
181
self ._ctrl_reg1 &= ~ 0b10000000 # Turn off bit 7, ALT.
178
182
self ._write_u8 (_MPL3115A2_CTRL_REG1 , self ._ctrl_reg1 )
179
- self ._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
183
+ self ._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
180
184
self ._write_u8 (_MPL3115A2_CTRL_REG1 , self ._ctrl_reg1 )
181
185
# Poll status for PDR to be set.
182
- while self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_PDR == 0 :
186
+ while (
187
+ self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_PDR
188
+ == 0
189
+ ):
183
190
time .sleep (0.01 )
184
191
# Read 3 bytes of pressure data into buffer.
185
192
self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER , count = 3 )
186
193
# Reconstruct 20-bit pressure value.
187
- pressure = (( self . _BUFFER [ 0 ] << 16 ) | \
188
- (self ._BUFFER [1 ] << 8 ) | \
189
- self . _BUFFER [ 2 ] ) & 0xFFFFFF
194
+ pressure = (
195
+ ( self . _BUFFER [ 0 ] << 16 ) | (self ._BUFFER [1 ] << 8 ) | self . _BUFFER [ 2 ]
196
+ ) & 0xFFFFFF
190
197
pressure >>= 4
191
198
# Scale down to pascals.
192
199
return pressure / 4.0
@@ -203,10 +210,13 @@ def altitude(self):
203
210
# Set control bits for pressure reading.
204
211
self ._ctrl_reg1 |= 0b10000000 # Turn on bit 0, ALT.
205
212
self ._write_u8 (_MPL3115A2_CTRL_REG1 , self ._ctrl_reg1 )
206
- self ._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
213
+ self ._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
207
214
self ._write_u8 (_MPL3115A2_CTRL_REG1 , self ._ctrl_reg1 )
208
215
# Poll status for PDR to be set.
209
- while self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_PDR == 0 :
216
+ while (
217
+ self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_PDR
218
+ == 0
219
+ ):
210
220
time .sleep (0.01 )
211
221
# Read 3 bytes of altitude data into buffer.
212
222
# Yes even though this is the address of the pressure register it
@@ -215,7 +225,7 @@ def altitude(self):
215
225
# Reconstruct signed 32-bit altitude value (actually 24 bits shifted up
216
226
# and then scaled down).
217
227
self ._BUFFER [3 ] = 0 # Top 3 bytes of buffer were read from the chip.
218
- altitude = struct .unpack ('>i' , self ._BUFFER [0 :4 ])[0 ]
228
+ altitude = struct .unpack (">i" , self ._BUFFER [0 :4 ])[0 ]
219
229
# Scale down to meters.
220
230
return altitude / 65535.0
221
231
@@ -224,12 +234,15 @@ def temperature(self):
224
234
"""Read the temperature as measured by the sensor in degrees Celsius.
225
235
"""
226
236
# Poll status for TDR to be set.
227
- while self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_TDR == 0 :
237
+ while (
238
+ self ._read_u8 (_MPL3115A2_REGISTER_STATUS ) & _MPL3115A2_REGISTER_STATUS_TDR
239
+ == 0
240
+ ):
228
241
time .sleep (0.01 )
229
242
# Read 2 bytes of data from temp register.
230
243
self ._read_into (_MPL3115A2_REGISTER_TEMP_MSB , self ._BUFFER , count = 2 )
231
244
# Reconstruct signed 12-bit value.
232
- temperature = struct .unpack ('>h' , self ._BUFFER [0 :2 ])[0 ]
245
+ temperature = struct .unpack (">h" , self ._BUFFER [0 :2 ])[0 ]
233
246
temperature >>= 4
234
247
# Scale down to degrees Celsius.
235
248
return temperature / 16.0
0 commit comments