Skip to content

Added type annotations to adafruit_ahtx0.py #11

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
Feb 12, 2022
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
38 changes: 23 additions & 15 deletions adafruit_ahtx0.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,26 @@
"""

import time

try:
# This is only needed for typing
import busio # pylint: disable=unused-import
except ImportError:
pass


from adafruit_bus_device.i2c_device import I2CDevice
from micropython import const

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"
__version__: str = "0.0.0-auto.0"
__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"

AHTX0_I2CADDR_DEFAULT = const(0x38) # Default I2C address
AHTX0_CMD_CALIBRATE = const(0xE1) # Calibration command
AHTX0_CMD_TRIGGER = const(0xAC) # Trigger reading command
AHTX0_CMD_SOFTRESET = const(0xBA) # Soft reset command
AHTX0_STATUS_BUSY = const(0x80) # Status bit for busy
AHTX0_STATUS_CALIBRATED = const(0x08) # Status bit for calibrated
AHTX0_I2CADDR_DEFAULT: int = const(0x38) # Default I2C address
AHTX0_CMD_CALIBRATE: int = const(0xE1) # Calibration command
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
AHTX0_STATUS_CALIBRATED: int = const(0x08) # Status bit for calibrated


class AHTx0:
Expand Down Expand Up @@ -78,7 +86,7 @@ class AHTx0:

"""

def __init__(self, i2c_bus, address=AHTX0_I2CADDR_DEFAULT):
def __init__(self, i2c_bus: busio.I2C, address: int = AHTX0_I2CADDR_DEFAULT):
time.sleep(0.02) # 20ms delay to wake up
self.i2c_device = I2CDevice(i2c_bus, address)
self._buf = bytearray(6)
Expand All @@ -88,14 +96,14 @@ def __init__(self, i2c_bus, address=AHTX0_I2CADDR_DEFAULT):
self._temp = None
self._humidity = None

def reset(self):
def reset(self) -> None:
"""Perform a soft-reset of the AHT"""
self._buf[0] = AHTX0_CMD_SOFTRESET
with self.i2c_device as i2c:
i2c.write(self._buf, start=0, end=1)
time.sleep(0.02) # 20ms delay to wake up

def calibrate(self):
def calibrate(self) -> bool:
"""Ask the sensor to self-calibrate. Returns True on success, False otherwise"""
self._buf[0] = AHTX0_CMD_CALIBRATE
self._buf[1] = 0x08
Expand All @@ -109,26 +117,26 @@ def calibrate(self):
return True

@property
def status(self):
def status(self) -> int:
"""The status byte initially returned from the sensor, see datasheet for details"""
with self.i2c_device as i2c:
i2c.readinto(self._buf, start=0, end=1)
# print("status: "+hex(self._buf[0]))
return self._buf[0]

@property
def relative_humidity(self):
def relative_humidity(self) -> int:
"""The measured relative humidity in percent."""
self._readdata()
return self._humidity

@property
def temperature(self):
def temperature(self) -> int:
"""The measured temperature in degrees Celsius."""
self._readdata()
return self._temp

def _readdata(self):
def _readdata(self) -> None:
"""Internal function for triggering the AHT to read temp/humidity"""
self._buf[0] = AHTX0_CMD_TRIGGER
self._buf[1] = 0x33
Expand Down