34
34
from adafruit_bus_device .i2c_device import I2CDevice
35
35
from micropython import const
36
36
37
+ try :
38
+ import typing # pylint: disable=unused-import
39
+ from typing_extensions import Literal
40
+ from busio import I2C
41
+ except ImportError :
42
+ pass
43
+
37
44
__version__ = "0.0.0+auto.0"
38
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTU21D.git"
39
46
50
57
_TEMP_RH_RES = (0 , 1 , 128 , 129 )
51
58
52
59
53
- def _crc (data ) :
60
+ def _crc (data : bytearray ) -> int :
54
61
crc = 0
55
62
for byte in data :
56
63
crc ^= byte
@@ -97,18 +104,18 @@ class HTU21D:
97
104
98
105
"""
99
106
100
- def __init__ (self , i2c_bus , address = 0x40 ):
107
+ def __init__ (self , i2c_bus : I2C , address : int = 0x40 ) -> None :
101
108
self .i2c_device = I2CDevice (i2c_bus , address )
102
109
self ._command (_RESET )
103
110
self ._measurement = 0
104
111
self ._buffer = bytearray (3 )
105
112
time .sleep (0.01 )
106
113
107
- def _command (self , command ) :
114
+ def _command (self , command : int ) -> None :
108
115
with self .i2c_device as i2c :
109
116
i2c .write (struct .pack ("B" , command ))
110
117
111
- def _data (self ):
118
+ def _data (self ) -> int :
112
119
data = bytearray (3 )
113
120
while True :
114
121
# While busy, the sensor doesn't respond to reads.
@@ -125,22 +132,22 @@ def _data(self):
125
132
return value
126
133
127
134
@property
128
- def relative_humidity (self ):
135
+ def relative_humidity (self ) -> float :
129
136
"""The measured relative humidity in percent."""
130
137
self .measurement (HUMIDITY )
131
138
self ._measurement = 0
132
139
time .sleep (0.016 )
133
140
return self ._data () * 125.0 / 65536.0 - 6.0
134
141
135
142
@property
136
- def temperature (self ):
143
+ def temperature (self ) -> float :
137
144
"""The measured temperature in degrees Celsius."""
138
145
self .measurement (TEMPERATURE )
139
146
self ._measurement = 0
140
147
time .sleep (0.050 )
141
148
return self ._data () * 175.72 / 65536.0 - 46.85
142
149
143
- def measurement (self , what ) :
150
+ def measurement (self , what : Literal [ 0xF5 , 0xF3 ]) -> float :
144
151
"""
145
152
Starts a measurement.
146
153
Starts a measurement of either ``HUMIDITY`` or ``TEMPERATURE``
@@ -163,7 +170,7 @@ def measurement(self, what):
163
170
self ._measurement = what
164
171
165
172
@property
166
- def temp_rh_resolution (self ):
173
+ def temp_rh_resolution (self ) -> int :
167
174
"""The temperature and relative humidity resolution
168
175
169
176
Have one of the following values: [#f1]_
@@ -189,7 +196,7 @@ def temp_rh_resolution(self):
189
196
return self ._buffer [0 ]
190
197
191
198
@temp_rh_resolution .setter
192
- def temp_rh_resolution (self , value ) :
199
+ def temp_rh_resolution (self , value : int ) -> None :
193
200
self ._buffer [0 ] = _READ_USER1
194
201
with self .i2c_device as i2c :
195
202
i2c .write_then_readinto (self ._buffer , self ._buffer , out_end = 1 )
0 commit comments