Skip to content

revert PR_9 #16

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
Apr 21, 2020
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
21 changes: 3 additions & 18 deletions adafruit_am2320.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ def _crc16(data):
return crc


class AM2320Exception(Exception):
"""Base class for exceptions."""


class AM2320DeviceNotFound(AM2320Exception, ValueError):
"""Indicates that a device couldn't be found."""


class AM2320ReadError(AM2320Exception, RuntimeError):
"""indicates that valid data could not be read from the sensor.

This may be due to a regular I2C read failure, or due to a checksum
mismatch."""


class AM2320:
"""A driver for the AM2320 temperature and humidity sensor.

Expand All @@ -109,7 +94,7 @@ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR):
except ValueError:
pass
time.sleep(0.25)
raise AM2320DeviceNotFound("AM2320 not found")
raise ValueError("AM2320 not found")

def _read_register(self, register, length):
with self._i2c as i2c:
Expand All @@ -130,12 +115,12 @@ def _read_register(self, register, length):
# print("$%02X => %s" % (register, [hex(i) for i in result]))
# Check preamble indicates correct readings
if result[0] != 0x3 or result[1] != length:
raise AM2320ReadError("I2C read failure")
raise RuntimeError("I2C read failure")
# Check CRC on all but last 2 bytes
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
crc2 = _crc16(result[0:-2])
if crc1 != crc2:
raise AM2320ReadError("CRC failure 0x%04X vs 0x%04X" % (crc1, crc2))
raise RuntimeError("CRC failure 0x%04X vs 0x%04X" % (crc1, crc2))
return result[2:-2]

@property
Expand Down