@@ -125,7 +125,10 @@ class MPL3115A2:
125
125
126
126
# Class level buffer to reduce memory usage and allocations.
127
127
# Note this is not thread safe by design!
128
- _BUFFER = bytearray (4 )
128
+ _BUFFER = bytearray (5 )
129
+ # _BUFFER size was previously 4. It was increased as the current configuration
130
+ # creates a flag in _MPL3115A2_REGISTER_STATUS that we were not clearing depending
131
+ # on the properties reading order
129
132
130
133
def __init__ (self , i2c , * , address = _MPL3115A2_ADDRESS ):
131
134
self ._device = i2c_device .I2CDevice (i2c , address )
@@ -209,7 +212,7 @@ def pressure(self):
209
212
):
210
213
time .sleep (0.01 )
211
214
# Read 3 bytes of pressure data into buffer.
212
- self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER , count = 3 )
215
+ self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER )
213
216
# Reconstruct 20-bit pressure value.
214
217
pressure = (
215
218
(self ._BUFFER [0 ] << 16 ) | (self ._BUFFER [1 ] << 8 ) | self ._BUFFER [2 ]
@@ -241,7 +244,7 @@ def altitude(self):
241
244
# Read 3 bytes of altitude data into buffer.
242
245
# Yes even though this is the address of the pressure register it
243
246
# returns altitude when the ALT bit is set above.
244
- self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER , count = 3 )
247
+ self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER )
245
248
# Reconstruct signed 32-bit altitude value (actually 24 bits shifted up
246
249
# and then scaled down).
247
250
self ._BUFFER [3 ] = 0 # Top 3 bytes of buffer were read from the chip.
@@ -264,9 +267,9 @@ def temperature(self):
264
267
):
265
268
time .sleep (0.01 )
266
269
# Read 2 bytes of data from temp register.
267
- self ._read_into (_MPL3115A2_REGISTER_TEMP_MSB , self ._BUFFER , count = 2 )
270
+ self ._read_into (_MPL3115A2_REGISTER_PRESSURE_MSB , self ._BUFFER )
268
271
# Reconstruct signed 12-bit value.
269
- temperature = struct .unpack (">h" , self ._BUFFER [0 : 2 ])[0 ]
272
+ temperature = struct .unpack (">h" , self ._BUFFER [3 : 5 ])[0 ]
270
273
temperature >>= 4
271
274
# Scale down to degrees Celsius.
272
275
return temperature / 16.0
0 commit comments