30
30
31
31
import array
32
32
import time
33
+ from os import uname
33
34
from digitalio import DigitalInOut , Pull , Direction
34
35
35
36
_USE_PULSEIO = False
@@ -51,23 +52,33 @@ class DHTBase:
51
52
52
53
__hiLevel = 51
53
54
54
- def __init__ (self , dht11 , pin , trig_wait ):
55
+ def __init__ (self , dht11 , pin , trig_wait , use_pulseio ):
55
56
"""
56
57
:param boolean dht11: True if device is DHT11, otherwise DHT22.
57
58
:param ~board.Pin pin: digital pin used for communication
58
59
:param int trig_wait: length of time to hold trigger in LOW state (microseconds)
60
+ :param boolean use_pulseio: False to force bitbang when pulseio available (only with Blinka)
59
61
"""
60
62
self ._dht11 = dht11
61
63
self ._pin = pin
62
64
self ._trig_wait = trig_wait
63
65
self ._last_called = 0
64
66
self ._humidity = None
65
67
self ._temperature = None
68
+ self ._use_pulseio = use_pulseio
69
+ if "Linux" not in uname () and not self ._use_pulseio :
70
+ raise Exception ("Bitbanging is not supported when using CircuitPython." )
66
71
# We don't use a context because linux-based systems are sluggish
67
72
# and we're better off having a running process
68
- if _USE_PULSEIO :
73
+ if self . _use_pulseio :
69
74
self .pulse_in = PulseIn (self ._pin , 81 , True )
70
75
76
+ def exit (self ):
77
+ """ Cleans up the PulseIn process. Must be called explicitly """
78
+ if self ._use_pulseio :
79
+ print ("De-initializing self.pulse_in" )
80
+ self .pulse_in .deinit ()
81
+
71
82
def _pulses_to_binary (self , pulses , start , stop ):
72
83
"""Takes pulses, a list of transition times, and converts
73
84
them to a 1's or 0's. The pulses array contains the transition times.
@@ -108,7 +119,7 @@ def _get_pulses_pulseio(self):
108
119
pulses will have 81 elements for the DHT11/22 type devices.
109
120
"""
110
121
pulses = array .array ("H" )
111
- if _USE_PULSEIO :
122
+ if self . _use_pulseio :
112
123
# The DHT type device use a specialize 1-wire protocol
113
124
# The microprocessor first sends a LOW signal for a
114
125
# specific length of time. Then the device sends back a
@@ -183,7 +194,7 @@ def measure(self):
183
194
new_temperature = 0
184
195
new_humidity = 0
185
196
186
- if _USE_PULSEIO :
197
+ if self . _use_pulseio :
187
198
pulses = self ._get_pulses_pulseio ()
188
199
else :
189
200
pulses = self ._get_pulses_bitbang ()
@@ -259,8 +270,8 @@ class DHT11(DHTBase):
259
270
:param ~board.Pin pin: digital pin used for communication
260
271
"""
261
272
262
- def __init__ (self , pin ):
263
- super ().__init__ (True , pin , 18000 )
273
+ def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
274
+ super ().__init__ (True , pin , 18000 , use_pulseio )
264
275
265
276
266
277
class DHT22 (DHTBase ):
@@ -269,5 +280,5 @@ class DHT22(DHTBase):
269
280
:param ~board.Pin pin: digital pin used for communication
270
281
"""
271
282
272
- def __init__ (self , pin ):
273
- super ().__init__ (False , pin , 1000 )
283
+ def __init__ (self , pin , use_pulseio = _USE_PULSEIO ):
284
+ super ().__init__ (False , pin , 1000 , use_pulseio )
0 commit comments