Skip to content

Clearer error printout for I2C communication error #13

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 4 commits into from
Mar 8, 2022
Merged
Changes from 3 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
32 changes: 28 additions & 4 deletions adafruit_scd4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,29 @@ def stop_periodic_measurement(self):
self._send_command(_SCD4X_STOPPERIODICMEASUREMENT, cmd_delay=0.5)

def start_periodic_measurement(self):
"""Put sensor into working mode, about 5s per measurement"""
"""Put sensor into working mode, about 5s per measurement

.. note::
Only the following commands will work once in working mode:

* :attr:`CO2 <adafruit_scd4x.SCD4X.CO2>`
* :attr:`temperature <adafruit_scd4x.SCD4X.temperature>`
* :attr:`relative_humidity <adafruit_scd4x.SCD4X.relative_humidity>`
* :meth:`data_ready() <adafruit_scd4x.SCD4x.data_ready>`
* :meth:`reinit() <adafruit_scd4x.SCD4X.reinit>`
* :meth:`factory_reset() <adafruit_scd4x.SCD4X.factory_reset>`
* :meth:`force_calibration() <adafruit_scd4x.SCD4X.force_calibration>`
* :meth:`self_test() <adafruit_scd4x.SCD4X.self_test>`
* :meth:`set_ambient_pressure() <adafruit_scd4x.SCD4X.set_ambient_pressure>`

"""
self._send_command(_SCD4X_STARTPERIODICMEASUREMENT)

def start_low_periodic_measurement(self):
"""Put sensor into low power working mode, about 30s per measurement"""
"""Put sensor into low power working mode, about 30s per measurement. See
:meth:`start_periodic_measurement() <adafruit_scd4x.SCD4X.start_perodic_measurement>`
for more details.
"""
self._send_command(_SCD4X_STARTLOWPOWERPERIODICMEASUREMENT)

def persist_settings(self):
Expand Down Expand Up @@ -303,8 +321,14 @@ def _send_command(self, cmd: int, cmd_delay: float = 0) -> None:
self._cmd[0] = (cmd >> 8) & 0xFF
self._cmd[1] = cmd & 0xFF

with self.i2c_device as i2c:
i2c.write(self._cmd, end=2)
try:
with self.i2c_device as i2c:
i2c.write(self._cmd, end=2)
except OSError as err:
raise RuntimeError(
"Could not communicate via I2C, some commands/settings \
unavailable while in working mode"
) from err
time.sleep(cmd_delay)

def _set_command_value(self, cmd, value, cmd_delay=0):
Expand Down