Skip to content

Commit b5d08e2

Browse files
committed
Fix behavior on reset and control register bit set logic.
1 parent 42bac66 commit b5d08e2

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

adafruit_mpl3115a2.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
112112
# Validate the chip ID.
113113
if self._read_u8(_MPL3115A2_WHOAMI) != 0xC4:
114114
raise RuntimeError('Failed to find MPL3115A2, check your wiring!')
115-
# Reset.
116-
self._write_u8(_MPL3115A2_CTRL_REG1, _MPL3115A2_CTRL_REG1_RST)
115+
# Reset. Note the chip immediately resets and won't send an I2C back
116+
# so we need to catch the OSError and swallow it (otherwise this fails
117+
# expecting an ACK that never comes).
118+
try:
119+
self._write_u8(_MPL3115A2_CTRL_REG1, _MPL3115A2_CTRL_REG1_RST)
120+
except OSError:
121+
pass
117122
time.sleep(0.01)
118123
# Poll for the reset to finish.
119124
self._poll_reg1(_MPL3115A2_CTRL_REG1_RST)
@@ -168,9 +173,9 @@ def pressure(self):
168173
# First poll for a measurement to be finished.
169174
self._poll_reg1(_MPL3115A2_CTRL_REG1_OST)
170175
# Set control bits for pressure reading.
171-
self._ctrl_reg1 &= ~0b00000001 # Turn off bit 0, ALT.
176+
self._ctrl_reg1 &= ~0b10000000 # Turn off bit 7, ALT.
172177
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
173-
self._ctrl_reg1 |= 0b00000100 # Set OST to 1
178+
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
174179
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
175180
# Poll status for PDR to be set.
176181
while self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR == 0:
@@ -195,9 +200,9 @@ def altitude(self):
195200
# First poll for a measurement to be finished.
196201
self._poll_reg1(_MPL3115A2_CTRL_REG1_OST)
197202
# Set control bits for pressure reading.
198-
self._ctrl_reg1 |= 0b00000001 # Turn on bit 0, ALT.
203+
self._ctrl_reg1 |= 0b10000000 # Turn on bit 0, ALT.
199204
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
200-
self._ctrl_reg1 |= 0b00000100 # Set OST to 1
205+
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
201206
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
202207
# Poll status for PDR to be set.
203208
while self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR == 0:

0 commit comments

Comments
 (0)