Skip to content

Commit abb882e

Browse files
authored
Merge pull request #13 from tekktrik/dev/clearer-i2c-failure
Clearer error printout for I2C communication error
2 parents 96cdd6d + 149a0ef commit abb882e

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

adafruit_scd4x.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,29 @@ def stop_periodic_measurement(self):
229229
self._send_command(_SCD4X_STOPPERIODICMEASUREMENT, cmd_delay=0.5)
230230

231231
def start_periodic_measurement(self):
232-
"""Put sensor into working mode, about 5s per measurement"""
232+
"""Put sensor into working mode, about 5s per measurement
233+
234+
.. note::
235+
Only the following commands will work once in working mode:
236+
237+
* :attr:`CO2 <adafruit_scd4x.SCD4X.CO2>`
238+
* :attr:`temperature <adafruit_scd4x.SCD4X.temperature>`
239+
* :attr:`relative_humidity <adafruit_scd4x.SCD4X.relative_humidity>`
240+
* :meth:`data_ready() <adafruit_scd4x.SCD4x.data_ready>`
241+
* :meth:`reinit() <adafruit_scd4x.SCD4X.reinit>`
242+
* :meth:`factory_reset() <adafruit_scd4x.SCD4X.factory_reset>`
243+
* :meth:`force_calibration() <adafruit_scd4x.SCD4X.force_calibration>`
244+
* :meth:`self_test() <adafruit_scd4x.SCD4X.self_test>`
245+
* :meth:`set_ambient_pressure() <adafruit_scd4x.SCD4X.set_ambient_pressure>`
246+
247+
"""
233248
self._send_command(_SCD4X_STARTPERIODICMEASUREMENT)
234249

235250
def start_low_periodic_measurement(self):
236-
"""Put sensor into low power working mode, about 30s per measurement"""
251+
"""Put sensor into low power working mode, about 30s per measurement. See
252+
:meth:`start_periodic_measurement() <adafruit_scd4x.SCD4X.start_perodic_measurement>`
253+
for more details.
254+
"""
237255
self._send_command(_SCD4X_STARTLOWPOWERPERIODICMEASUREMENT)
238256

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

306-
with self.i2c_device as i2c:
307-
i2c.write(self._cmd, end=2)
324+
try:
325+
with self.i2c_device as i2c:
326+
i2c.write(self._cmd, end=2)
327+
except OSError as err:
328+
raise RuntimeError(
329+
"Could not communicate via I2C, some commands/settings "
330+
"unavailable while in working mode"
331+
) from err
308332
time.sleep(cmd_delay)
309333

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

0 commit comments

Comments
 (0)