39
39
except (ImportError , NotImplementedError ):
40
40
pass # This is OK, we'll try to bitbang it!
41
41
42
+ try :
43
+ # Used only for typing
44
+ from typing import Union
45
+ from microcontroller import Pin
46
+ except ImportError :
47
+ pass
42
48
43
49
__version__ = "0.0.0-auto.0"
44
50
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DHT.git"
@@ -49,7 +55,7 @@ class DHTBase:
49
55
50
56
__hiLevel = 51
51
57
52
- def __init__ (self , dht11 , pin , trig_wait , use_pulseio ):
58
+ def __init__ (self , dht11 : bool , pin : Pin , trig_wait : int , use_pulseio : bool ):
53
59
"""
54
60
:param boolean dht11: True if device is DHT11, otherwise DHT22.
55
61
:param ~board.Pin pin: digital pin used for communication
@@ -68,16 +74,16 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio):
68
74
# We don't use a context because linux-based systems are sluggish
69
75
# and we're better off having a running process
70
76
if self ._use_pulseio :
71
- self .pulse_in = PulseIn (self ._pin , 81 , True )
77
+ self .pulse_in = PulseIn (self ._pin , maxlen = 81 , idle_state = True )
72
78
self .pulse_in .pause ()
73
79
74
- def exit (self ):
75
- """ Cleans up the PulseIn process. Must be called explicitly """
80
+ def exit (self ) -> None :
81
+ """Cleans up the PulseIn process. Must be called explicitly"""
76
82
if self ._use_pulseio :
77
83
print ("De-initializing self.pulse_in" )
78
84
self .pulse_in .deinit ()
79
85
80
- def _pulses_to_binary (self , pulses , start , stop ) :
86
+ def _pulses_to_binary (self , pulses : array . array , start : int , stop : int ) -> int :
81
87
"""Takes pulses, a list of transition times, and converts
82
88
them to a 1's or 0's. The pulses array contains the transition times.
83
89
pulses starts with a low transition time followed by a high transistion time.
@@ -106,7 +112,7 @@ def _pulses_to_binary(self, pulses, start, stop):
106
112
107
113
return binary
108
114
109
- def _get_pulses_pulseio (self ):
115
+ def _get_pulses_pulseio (self ) -> array . array :
110
116
"""_get_pulses implements the communication protocol for
111
117
DHT11 and DHT22 type devices. It sends a start signal
112
118
of a specific length and listens and measures the
@@ -134,7 +140,7 @@ def _get_pulses_pulseio(self):
134
140
pulses .append (self .pulse_in .popleft ())
135
141
return pulses
136
142
137
- def _get_pulses_bitbang (self ):
143
+ def _get_pulses_bitbang (self ) -> array . array :
138
144
"""_get_pulses implements the communication protcol for
139
145
DHT11 and DHT22 type devices. It sends a start signal
140
146
of a specific length and listens and measures the
@@ -179,7 +185,7 @@ def _get_pulses_bitbang(self):
179
185
pulses .append (min (pulses_micro_sec , 65535 ))
180
186
return pulses
181
187
182
- def measure (self ):
188
+ def measure (self ) -> None :
183
189
"""measure runs the communications to the DHT11/22 type device.
184
190
if successful, the class properties temperature and humidity will
185
191
return the reading returned from the device.
@@ -250,7 +256,7 @@ def measure(self):
250
256
self ._humidity = new_humidity
251
257
252
258
@property
253
- def temperature (self ):
259
+ def temperature (self ) -> Union [ int , float , None ] :
254
260
"""temperature current reading. It makes sure a reading is available
255
261
256
262
Raises RuntimeError exception for checksum failure and for insufficient
@@ -260,7 +266,7 @@ def temperature(self):
260
266
return self ._temperature
261
267
262
268
@property
263
- def humidity (self ):
269
+ def humidity (self ) -> Union [ int , float , None ] :
264
270
"""humidity current reading. It makes sure a reading is available
265
271
266
272
Raises RuntimeError exception for checksum failure and for insufficient
@@ -276,7 +282,7 @@ class DHT11(DHTBase):
276
282
:param ~board.Pin pin: digital pin used for communication
277
283
"""
278
284
279
- def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
285
+ def __init__ (self , pin : Pin , use_pulseio : bool = _USE_PULSEIO ):
280
286
super ().__init__ (True , pin , 18000 , use_pulseio )
281
287
282
288
@@ -286,5 +292,5 @@ class DHT22(DHTBase):
286
292
:param ~board.Pin pin: digital pin used for communication
287
293
"""
288
294
289
- def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
295
+ def __init__ (self , pin : Pin , use_pulseio : bool = _USE_PULSEIO ):
290
296
super ().__init__ (False , pin , 1000 , use_pulseio )
0 commit comments