adafruit / Adafruit_CircuitPython_SCD4X

CircuitPython/Python driver for Sensirion SCD40 & SCD41
MIT License
19 stars 10 forks source link

Better protect against changing settings during continuous measurement #18

Open caternuson opened 10 months ago

caternuson commented 10 months ago

Re this thread: https://forums.adafruit.com/viewtopic.php?t=204928

Attempting to make changes to onboard SCD40 register settings while continuous measurement is running can lead to an exception:

Adafruit CircuitPython 8.2.6 on 2023-09-12; Adafruit QT Py RP2040 with rp2040
>>> import board
>>> import adafruit_scd4x
>>> i2c = board.STEMMA_I2C()
>>> scd4x = adafruit_scd4x.SCD4X(i2c)
>>> scd4x.temperature_offset = 4
>>> scd4x.start_periodic_measurement()
>>> scd4x.temperature_offset = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_scd4x.py", line 307, in temperature_offset
  File "adafruit_scd4x.py", line 358, in _set_command_value
OSError: [Errno 5] Input/output error
>>> if scd4x.data_ready:
...        print(f"{scd4x.temperature}")
...
20.4968
>>> scd4x.temperature_offset = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_scd4x.py", line 307, in temperature_offset
  File "adafruit_scd4x.py", line 358, in _set_command_value
OSError: [Errno 5] Input/output error
>>> scd4x.stop_periodic_measurement()
>>> scd4x.temperature_offset = 4
>>> 

This behavior is potentially confusing. Would be good to catch this and provide better exception and message.