From 29c027afde01c559cb74182d903cf4251fa55b79 Mon Sep 17 00:00:00 2001 From: Liz Date: Mon, 14 Aug 2023 10:31:49 -0400 Subject: [PATCH 1/4] Calibration patch for newer AHT20's Updating calibration function so that it isn't required to pass for newer AHT20's. This same change was made for the [Arduino library](https://github.com/adafruit/Adafruit_AHTX0/pull/13) --- adafruit_ahtx0.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index 75f49c6..104a53f 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -106,7 +106,7 @@ def reset(self) -> None: time.sleep(0.02) # 20ms delay to wake up def calibrate(self) -> bool: - """Ask the sensor to self-calibrate. Returns True on success, False otherwise""" + """Ask the sensor to self-calibrate. May not 'succeed' on newer AHT20s.""" self._buf[0] = AHTX0_CMD_CALIBRATE self._buf[1] = 0x08 self._buf[2] = 0x00 @@ -114,8 +114,6 @@ def calibrate(self) -> bool: i2c.write(self._buf, start=0, end=3) while self.status & AHTX0_STATUS_BUSY: time.sleep(0.01) - if not self.status & AHTX0_STATUS_CALIBRATED: - return False return True @property From cc89d00fcfbd58385c3e200753561fd8ef7871a0 Mon Sep 17 00:00:00 2001 From: Liz Date: Mon, 14 Aug 2023 11:36:50 -0400 Subject: [PATCH 2/4] Updating to use try/except --- adafruit_ahtx0.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index 104a53f..2c22cb1 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -106,15 +106,21 @@ def reset(self) -> None: time.sleep(0.02) # 20ms delay to wake up def calibrate(self) -> bool: - """Ask the sensor to self-calibrate. May not 'succeed' on newer AHT20s.""" - self._buf[0] = AHTX0_CMD_CALIBRATE - self._buf[1] = 0x08 - self._buf[2] = 0x00 - with self.i2c_device as i2c: - i2c.write(self._buf, start=0, end=3) - while self.status & AHTX0_STATUS_BUSY: - time.sleep(0.01) - return True + """Ask the sensor to self-calibrate. Returns True on success, False otherwise""" + """Newer AHT20's may not succeed, so wrapping in try/except""" + try: + self._buf[0] = AHTX0_CMD_CALIBRATE + self._buf[1] = 0x08 + self._buf[2] = 0x00 + with self.i2c_device as i2c: + i2c.write(self._buf, start=0, end=3) + while self.status & AHTX0_STATUS_BUSY: + time.sleep(0.01) + if not self.status & AHTX0_STATUS_CALIBRATED: + return False + return True + except: + pass @property def status(self) -> int: From e5867079bc59653ec76d43e0d56d32f57363bbc8 Mon Sep 17 00:00:00 2001 From: Liz Date: Mon, 14 Aug 2023 12:01:35 -0400 Subject: [PATCH 3/4] updating to use try/except --- adafruit_ahtx0.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index 2c22cb1..e942413 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -107,7 +107,7 @@ def reset(self) -> None: def calibrate(self) -> bool: """Ask the sensor to self-calibrate. Returns True on success, False otherwise""" - """Newer AHT20's may not succeed, so wrapping in try/except""" + # Newer AHT20's may not succeed, so wrapping in try/except try: self._buf[0] = AHTX0_CMD_CALIBRATE self._buf[1] = 0x08 @@ -118,9 +118,9 @@ def calibrate(self) -> bool: time.sleep(0.01) if not self.status & AHTX0_STATUS_CALIBRATED: return False - return True - except: + except Exception: # pylint: disable=broad-except pass + return True @property def status(self) -> int: From fe1abce79aa84afa9eee546c076a3ea3ea4848a6 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 15 Aug 2023 13:13:03 -0400 Subject: [PATCH 4/4] moving try/except --- adafruit_ahtx0.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index e942413..0b8f7b3 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -108,18 +108,18 @@ def reset(self) -> None: def calibrate(self) -> bool: """Ask the sensor to self-calibrate. Returns True on success, False otherwise""" # Newer AHT20's may not succeed, so wrapping in try/except - try: - self._buf[0] = AHTX0_CMD_CALIBRATE - self._buf[1] = 0x08 - self._buf[2] = 0x00 - with self.i2c_device as i2c: + self._buf[0] = AHTX0_CMD_CALIBRATE + self._buf[1] = 0x08 + self._buf[2] = 0x00 + with self.i2c_device as i2c: + try: i2c.write(self._buf, start=0, end=3) - while self.status & AHTX0_STATUS_BUSY: - time.sleep(0.01) - if not self.status & AHTX0_STATUS_CALIBRATED: - return False - except Exception: # pylint: disable=broad-except - pass + except Exception: # pylint: disable=broad-except + pass + while self.status & AHTX0_STATUS_BUSY: + time.sleep(0.01) + if not self.status & AHTX0_STATUS_CALIBRATED: + return False return True @property