Skip to content

Commit

Permalink
Updating to use try/except
Browse files Browse the repository at this point in the history
  • Loading branch information
BlitzCityDIY committed Aug 14, 2023
1 parent 29c027a commit cc89d00
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions adafruit_ahtx0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit cc89d00

Please sign in to comment.