Skip to content

Fix temperature reads and RP2040 reset quirk #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions adafruit_mpl3115a2.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def __init__(self, i2c, *, address=_MPL3115A2_ADDRESS):
pass
time.sleep(0.01)
# Poll for the reset to finish.
self._poll_reg1(_MPL3115A2_CTRL_REG1_RST)
# try/except is a hack for RP2040
try:
self._poll_reg1(_MPL3115A2_CTRL_REG1_RST)
except OSError:
self._poll_reg1(_MPL3115A2_CTRL_REG1_RST)
# Configure the chip registers with default values.
self._ctrl_reg1 = _MPL3115A2_CTRL_REG1_OS128 | _MPL3115A2_CTRL_REG1_ALT
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
Expand Down Expand Up @@ -198,7 +202,7 @@ def pressure(self):
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
# Poll status for PDR to be set.
# Poll status for PDR to be set = press conversion complete
while (
self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_PDR
== 0
Expand All @@ -211,8 +215,8 @@ def pressure(self):
(self._BUFFER[0] << 16) | (self._BUFFER[1] << 8) | self._BUFFER[2]
) & 0xFFFFFF
pressure >>= 4
# Scale down to pascals.
return pressure / 4.0
# Scale down to hectopascals.
return pressure / 400.0

@property
def altitude(self):
Expand Down Expand Up @@ -248,7 +252,12 @@ def altitude(self):
@property
def temperature(self):
"""Read the temperature as measured by the sensor in degrees Celsius."""
# Poll status for TDR to be set.
# First poll for a measurement to be finished.
self._poll_reg1(_MPL3115A2_CTRL_REG1_OST)
# Initatiate a one-shot measurement
self._ctrl_reg1 |= 0b00000010 # Set OST to 1 to start measurement.
self._write_u8(_MPL3115A2_CTRL_REG1, self._ctrl_reg1)
# Poll status for TDR to be set = temp conv complete
while (
self._read_u8(_MPL3115A2_REGISTER_STATUS) & _MPL3115A2_REGISTER_STATUS_TDR
== 0
Expand Down