Skip to content

Commit df0c9fc

Browse files
authored
Merge pull request #26 from jposada202020/correcting_readings
reading correct values
2 parents b32af7d + 37b9b70 commit df0c9fc

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

adafruit_mpl3115a2.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ class MPL3115A2:
125125

126126
# Class level buffer to reduce memory usage and allocations.
127127
# 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
129132

130133
def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
131134
self._device = i2c_device.I2CDevice(i2c, address)
@@ -209,7 +212,7 @@ def pressure(self):
209212
):
210213
time.sleep(0.01)
211214
# 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)
213216
# Reconstruct 20-bit pressure value.
214217
pressure = (
215218
(self._BUFFER[0] << 16) | (self._BUFFER[1] << 8) | self._BUFFER[2]
@@ -241,7 +244,7 @@ def altitude(self):
241244
# Read 3 bytes of altitude data into buffer.
242245
# Yes even though this is the address of the pressure register it
243246
# 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)
245248
# Reconstruct signed 32-bit altitude value (actually 24 bits shifted up
246249
# and then scaled down).
247250
self._BUFFER[3] = 0 # Top 3 bytes of buffer were read from the chip.
@@ -264,9 +267,9 @@ def temperature(self):
264267
):
265268
time.sleep(0.01)
266269
# 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)
268271
# 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]
270273
temperature >>= 4
271274
# Scale down to degrees Celsius.
272275
return temperature / 16.0

0 commit comments

Comments
 (0)