From 928f574ebe4a30509dc195c5d7e4749b15638d49 Mon Sep 17 00:00:00 2001 From: DemiVis Date: Thu, 14 Dec 2023 22:24:14 -0800 Subject: [PATCH 1/2] Retry Calibration with AHT20 command on failure If try-except around legacy calibrate command fails, try to send new one instead afterwards --- adafruit_ahtx0.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index 0b8f7b3..d327f06 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -45,7 +45,8 @@ __repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git" AHTX0_I2CADDR_DEFAULT: int = const(0x38) # Default I2C address -AHTX0_CMD_CALIBRATE: int = const(0xE1) # Calibration command +AHT10_CMD_CALIBRATE: int = const(0xE1) # Calibration command for AHT10 sensor +AHT20_CMD_CALIBRATE: int = const(0xBE) # Calibration command for AHT20 sensor AHTX0_CMD_TRIGGER: int = const(0xAC) # Trigger reading command AHTX0_CMD_SOFTRESET: int = const(0xBA) # Soft reset command AHTX0_STATUS_BUSY: int = const(0x80) # Status bit for busy @@ -107,15 +108,27 @@ 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 - self._buf[0] = AHTX0_CMD_CALIBRATE + self._buf[0] = AHT10_CMD_CALIBRATE self._buf[1] = 0x08 self._buf[2] = 0x00 + calibration_failed = False with self.i2c_device as i2c: try: + # Newer AHT20's may not succeed with old command, so wrapping in try/except i2c.write(self._buf, start=0, end=3) except Exception: # pylint: disable=broad-except - pass + calibration_failed = True + + if calibration_failed: + # try another calibration command for newer AHT20's + time.sleep(0.01) + self._buf[0] = AHT20_CMD_CALIBRATE + with self.i2c_device as i2c: + try: + i2c.write(self._buf, start=0, end=3) + except Exception: + pass + while self.status & AHTX0_STATUS_BUSY: time.sleep(0.01) if not self.status & AHTX0_STATUS_CALIBRATED: From ae806959d3ddffe02dd7cff569fb6f76d4efd409 Mon Sep 17 00:00:00 2001 From: DemiVis Date: Thu, 14 Dec 2023 22:43:43 -0800 Subject: [PATCH 2/2] Make excepts more specific Switch from generic Exception to RuntimeError or OSError (the two I got while debugging this issue) to pass the pylint process checking --- adafruit_ahtx0.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_ahtx0.py b/adafruit_ahtx0.py index d327f06..3d13553 100644 --- a/adafruit_ahtx0.py +++ b/adafruit_ahtx0.py @@ -116,17 +116,18 @@ def calibrate(self) -> bool: try: # Newer AHT20's may not succeed with old command, so wrapping in try/except i2c.write(self._buf, start=0, end=3) - except Exception: # pylint: disable=broad-except + except (RuntimeError, OSError): calibration_failed = True if calibration_failed: # try another calibration command for newer AHT20's + # print("Calibration failed, trying AH20 command") time.sleep(0.01) self._buf[0] = AHT20_CMD_CALIBRATE with self.i2c_device as i2c: try: i2c.write(self._buf, start=0, end=3) - except Exception: + except (RuntimeError, OSError): pass while self.status & AHTX0_STATUS_BUSY: