adafruit / Adafruit_CircuitPython_SCD4X

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

Clearer error printout for I2C communication error #13

Closed tekktrik closed 2 years ago

tekktrik commented 2 years ago

Resolves #10 by:

  1. Transforming the OSError into a RuntimeError that explains that some commands are unavailable in working mode. Still mentions it as an I2C communication just in case it really is
  2. Referenced the datasheet to add the list of commands that should work while in working mode to the docstring of start_periodic_measurement() (and a reference to that in start_low_periodic_measurement()) to help clarify.

Not tested yet, but wanted to know if this is the right solution

ladyada commented 2 years ago

looks ok to me @caternuson can also take a look

caternuson commented 2 years ago

Oh yah, that issue. Thanks for picking this up and PRing a fix.

LGTM. Even tested. Did black change the long string message syntax to be multi-line? Looks like it's adding a bunch of white space.

BEFORE

Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit ItsyBitsy M4 Express with samd51g19
>>> import board
>>> import adafruit_scd4x
>>> scd4x = adafruit_scd4x.SCD4X(board.I2C())
>>> scd4x.altitude
0
>>> scd4x.altitude = 42
>>> scd4x.altitude
42
>>> scd4x.start_periodic_measurement()
>>> scd4x.altitude
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_scd4x.py", line 284, in altitude
  File "/lib/adafruit_scd4x.py", line 307, in _send_command
OSError: [Errno 5] Input/output error
>>> 

AFTER

Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit ItsyBitsy M4 Express with samd51g19
>>> import board
>>> import adafruit_scd4x
>>> scd4x = adafruit_scd4x.SCD4X(board.I2C())
>>> scd4x.altitude
42
>>> scd4x.start_periodic_measurement()
>>> scd4x.altitude
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_scd4x.py", line 302, in altitude
  File "/lib/adafruit_scd4x.py", line 329, in _send_command
RuntimeError: Could not communicate via I2C, some commands/settings                     unavailable while in working mode
>>>
tekktrik commented 2 years ago

Thanks for testing! I think I botched how I accounted for breaking up the string, this new commit should fix that whitespace.

caternuson commented 2 years ago

Yep. Looks good:

Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit ItsyBitsy M4 Express with samd51g19
>>> import board
>>> import adafruit_scd4x
>>> scd4x = adafruit_scd4x.SCD4X(board.I2C())
>>> scd4x.altitude
42
>>> scd4x.start_periodic_measurement()
>>> scd4x.altitude
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_scd4x.py", line 302, in altitude
  File "/lib/adafruit_scd4x.py", line 329, in _send_command
RuntimeError: Could not communicate via I2C, some commands/settings unavailable while in working mode
>>>