3030
3131import array
3232import time
33+ from os import uname
3334from digitalio import DigitalInOut , Pull , Direction
3435
3536_USE_PULSEIO = False
@@ -51,23 +52,33 @@ class DHTBase:
5152
5253 __hiLevel = 51
5354
54- def __init__ (self , dht11 , pin , trig_wait ):
55+ def __init__ (self , dht11 , pin , trig_wait , use_pulseio ):
5556 """
5657 :param boolean dht11: True if device is DHT11, otherwise DHT22.
5758 :param ~board.Pin pin: digital pin used for communication
5859 :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)
5961 """
6062 self ._dht11 = dht11
6163 self ._pin = pin
6264 self ._trig_wait = trig_wait
6365 self ._last_called = 0
6466 self ._humidity = None
6567 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." )
6671 # We don't use a context because linux-based systems are sluggish
6772 # and we're better off having a running process
68- if _USE_PULSEIO :
73+ if self . _use_pulseio :
6974 self .pulse_in = PulseIn (self ._pin , 81 , True )
7075
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+
7182 def _pulses_to_binary (self , pulses , start , stop ):
7283 """Takes pulses, a list of transition times, and converts
7384 them to a 1's or 0's. The pulses array contains the transition times.
@@ -108,7 +119,7 @@ def _get_pulses_pulseio(self):
108119 pulses will have 81 elements for the DHT11/22 type devices.
109120 """
110121 pulses = array .array ("H" )
111- if _USE_PULSEIO :
122+ if self . _use_pulseio :
112123 # The DHT type device use a specialize 1-wire protocol
113124 # The microprocessor first sends a LOW signal for a
114125 # specific length of time. Then the device sends back a
@@ -183,7 +194,7 @@ def measure(self):
183194 new_temperature = 0
184195 new_humidity = 0
185196
186- if _USE_PULSEIO :
197+ if self . _use_pulseio :
187198 pulses = self ._get_pulses_pulseio ()
188199 else :
189200 pulses = self ._get_pulses_bitbang ()
@@ -259,8 +270,8 @@ class DHT11(DHTBase):
259270 :param ~board.Pin pin: digital pin used for communication
260271 """
261272
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 )
264275
265276
266277class DHT22 (DHTBase ):
@@ -269,5 +280,5 @@ class DHT22(DHTBase):
269280 :param ~board.Pin pin: digital pin used for communication
270281 """
271282
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