30
30
"""
31
31
32
32
import time
33
+
34
+ try :
35
+ # This is only needed for typing
36
+ import busio # pylint: disable=unused-import
37
+ except ImportError :
38
+ pass
39
+
40
+
33
41
from adafruit_bus_device .i2c_device import I2CDevice
34
42
from micropython import const
35
43
36
- __version__ = "0.0.0-auto.0"
37
- __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"
44
+ __version__ : str = "0.0.0-auto.0"
45
+ __repo__ : str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"
38
46
39
- AHTX0_I2CADDR_DEFAULT = const (0x38 ) # Default I2C address
40
- AHTX0_CMD_CALIBRATE = const (0xE1 ) # Calibration command
41
- AHTX0_CMD_TRIGGER = const (0xAC ) # Trigger reading command
42
- AHTX0_CMD_SOFTRESET = const (0xBA ) # Soft reset command
43
- AHTX0_STATUS_BUSY = const (0x80 ) # Status bit for busy
44
- AHTX0_STATUS_CALIBRATED = const (0x08 ) # Status bit for calibrated
47
+ AHTX0_I2CADDR_DEFAULT : int = const (0x38 ) # Default I2C address
48
+ AHTX0_CMD_CALIBRATE : int = const (0xE1 ) # Calibration command
49
+ AHTX0_CMD_TRIGGER : int = const (0xAC ) # Trigger reading command
50
+ AHTX0_CMD_SOFTRESET : int = const (0xBA ) # Soft reset command
51
+ AHTX0_STATUS_BUSY : int = const (0x80 ) # Status bit for busy
52
+ AHTX0_STATUS_CALIBRATED : int = const (0x08 ) # Status bit for calibrated
45
53
46
54
47
55
class AHTx0 :
@@ -78,7 +86,7 @@ class AHTx0:
78
86
79
87
"""
80
88
81
- def __init__ (self , i2c_bus , address = AHTX0_I2CADDR_DEFAULT ):
89
+ def __init__ (self , i2c_bus : busio . I2C , address : int = AHTX0_I2CADDR_DEFAULT ):
82
90
time .sleep (0.02 ) # 20ms delay to wake up
83
91
self .i2c_device = I2CDevice (i2c_bus , address )
84
92
self ._buf = bytearray (6 )
@@ -88,14 +96,14 @@ def __init__(self, i2c_bus, address=AHTX0_I2CADDR_DEFAULT):
88
96
self ._temp = None
89
97
self ._humidity = None
90
98
91
- def reset (self ):
99
+ def reset (self ) -> None :
92
100
"""Perform a soft-reset of the AHT"""
93
101
self ._buf [0 ] = AHTX0_CMD_SOFTRESET
94
102
with self .i2c_device as i2c :
95
103
i2c .write (self ._buf , start = 0 , end = 1 )
96
104
time .sleep (0.02 ) # 20ms delay to wake up
97
105
98
- def calibrate (self ):
106
+ def calibrate (self ) -> bool :
99
107
"""Ask the sensor to self-calibrate. Returns True on success, False otherwise"""
100
108
self ._buf [0 ] = AHTX0_CMD_CALIBRATE
101
109
self ._buf [1 ] = 0x08
@@ -109,26 +117,26 @@ def calibrate(self):
109
117
return True
110
118
111
119
@property
112
- def status (self ):
120
+ def status (self ) -> int :
113
121
"""The status byte initially returned from the sensor, see datasheet for details"""
114
122
with self .i2c_device as i2c :
115
123
i2c .readinto (self ._buf , start = 0 , end = 1 )
116
124
# print("status: "+hex(self._buf[0]))
117
125
return self ._buf [0 ]
118
126
119
127
@property
120
- def relative_humidity (self ):
128
+ def relative_humidity (self ) -> int :
121
129
"""The measured relative humidity in percent."""
122
130
self ._readdata ()
123
131
return self ._humidity
124
132
125
133
@property
126
- def temperature (self ):
134
+ def temperature (self ) -> int :
127
135
"""The measured temperature in degrees Celsius."""
128
136
self ._readdata ()
129
137
return self ._temp
130
138
131
- def _readdata (self ):
139
+ def _readdata (self ) -> None :
132
140
"""Internal function for triggering the AHT to read temp/humidity"""
133
141
self ._buf [0 ] = AHTX0_CMD_TRIGGER
134
142
self ._buf [1 ] = 0x33
0 commit comments