30
30
import time
31
31
from os import uname
32
32
from digitalio import DigitalInOut , Pull , Direction
33
+ from microcontroller import Pin
33
34
34
35
_USE_PULSEIO = False
35
36
try :
@@ -49,7 +50,7 @@ class DHTBase:
49
50
50
51
__hiLevel = 51
51
52
52
- def __init__ (self , dht11 , pin , trig_wait , use_pulseio ):
53
+ def __init__ (self , dht11 : bool , pin : Pin , trig_wait : int , use_pulseio : bool ):
53
54
"""
54
55
:param boolean dht11: True if device is DHT11, otherwise DHT22.
55
56
:param ~board.Pin pin: digital pin used for communication
@@ -59,25 +60,25 @@ def __init__(self, dht11, pin, trig_wait, use_pulseio):
59
60
self ._dht11 = dht11
60
61
self ._pin = pin
61
62
self ._trig_wait = trig_wait
62
- self ._last_called = 0
63
- self ._humidity = None
64
- self ._temperature = None
63
+ self ._last_called : float = 0
64
+ self ._humidity : int | float | None = None
65
+ self ._temperature : int | float | None = None
65
66
self ._use_pulseio = use_pulseio
66
67
if "Linux" not in uname () and not self ._use_pulseio :
67
68
raise Exception ("Bitbanging is not supported when using CircuitPython." )
68
69
# We don't use a context because linux-based systems are sluggish
69
70
# and we're better off having a running process
70
71
if self ._use_pulseio :
71
- self .pulse_in = PulseIn (self ._pin , 81 , True )
72
+ self .pulse_in = PulseIn (self ._pin , maxlen = 81 , idle_state = True )
72
73
self .pulse_in .pause ()
73
74
74
- def exit (self ):
75
- """ Cleans up the PulseIn process. Must be called explicitly """
75
+ def exit (self ) -> None :
76
+ """Cleans up the PulseIn process. Must be called explicitly"""
76
77
if self ._use_pulseio :
77
78
print ("De-initializing self.pulse_in" )
78
79
self .pulse_in .deinit ()
79
80
80
- def _pulses_to_binary (self , pulses , start , stop ) :
81
+ def _pulses_to_binary (self , pulses : array . array [ int ] , start : int , stop : int ) -> int :
81
82
"""Takes pulses, a list of transition times, and converts
82
83
them to a 1's or 0's. The pulses array contains the transition times.
83
84
pulses starts with a low transition time followed by a high transistion time.
@@ -106,7 +107,7 @@ def _pulses_to_binary(self, pulses, start, stop):
106
107
107
108
return binary
108
109
109
- def _get_pulses_pulseio (self ):
110
+ def _get_pulses_pulseio (self ) -> array . array [ int ] :
110
111
"""_get_pulses implements the communication protocol for
111
112
DHT11 and DHT22 type devices. It sends a start signal
112
113
of a specific length and listens and measures the
@@ -134,7 +135,7 @@ def _get_pulses_pulseio(self):
134
135
pulses .append (self .pulse_in .popleft ())
135
136
return pulses
136
137
137
- def _get_pulses_bitbang (self ):
138
+ def _get_pulses_bitbang (self ) -> array . array [ int ] :
138
139
"""_get_pulses implements the communication protcol for
139
140
DHT11 and DHT22 type devices. It sends a start signal
140
141
of a specific length and listens and measures the
@@ -179,7 +180,7 @@ def _get_pulses_bitbang(self):
179
180
pulses .append (min (pulses_micro_sec , 65535 ))
180
181
return pulses
181
182
182
- def measure (self ):
183
+ def measure (self ) -> None :
183
184
"""measure runs the communications to the DHT11/22 type device.
184
185
if successful, the class properties temperature and humidity will
185
186
return the reading returned from the device.
@@ -197,8 +198,8 @@ def measure(self):
197
198
):
198
199
self ._last_called = time .monotonic ()
199
200
200
- new_temperature = 0
201
- new_humidity = 0
201
+ new_temperature : int | float = 0
202
+ new_humidity : int | float = 0
202
203
203
204
if self ._use_pulseio :
204
205
pulses = self ._get_pulses_pulseio ()
@@ -250,7 +251,7 @@ def measure(self):
250
251
self ._humidity = new_humidity
251
252
252
253
@property
253
- def temperature (self ):
254
+ def temperature (self ) -> int | float | None :
254
255
"""temperature current reading. It makes sure a reading is available
255
256
256
257
Raises RuntimeError exception for checksum failure and for insufficient
@@ -260,7 +261,7 @@ def temperature(self):
260
261
return self ._temperature
261
262
262
263
@property
263
- def humidity (self ):
264
+ def humidity (self ) -> int | float | None :
264
265
"""humidity current reading. It makes sure a reading is available
265
266
266
267
Raises RuntimeError exception for checksum failure and for insufficient
@@ -276,7 +277,7 @@ class DHT11(DHTBase):
276
277
:param ~board.Pin pin: digital pin used for communication
277
278
"""
278
279
279
- def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
280
+ def __init__ (self , pin : Pin , use_pulseio : bool = _USE_PULSEIO ):
280
281
super ().__init__ (True , pin , 18000 , use_pulseio )
281
282
282
283
@@ -286,5 +287,5 @@ class DHT22(DHTBase):
286
287
:param ~board.Pin pin: digital pin used for communication
287
288
"""
288
289
289
- def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
290
+ def __init__ (self , pin : Pin , use_pulseio : bool = _USE_PULSEIO ):
290
291
super ().__init__ (False , pin , 1000 , use_pulseio )
0 commit comments