Skip to content

Commit 507f643

Browse files
authored
Merge pull request #20 from ladyada/master
change dht context to static var for linux
2 parents 5dbcca1 + 90149ac commit 507f643

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

adafruit_dht.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ def __init__(self, dht11, pin, trig_wait):
6060
self._last_called = 0
6161
self._humidity = None
6262
self._temperature = None
63-
63+
# We don't use a context because linux-based systems are sluggish
64+
# and we're better off having a running process
65+
if _USE_PULSEIO:
66+
self.pulse_in = pulseio.PulseIn(self._pin, 81, True)
6467

6568
def _pulses_to_binary(self, pulses, start, stop):
6669
"""Takes pulses, a list of transition times, and converts
@@ -102,25 +105,22 @@ def _get_pulses_pulseio(self):
102105
pulses will have 81 elements for the DHT11/22 type devices.
103106
"""
104107
pulses = array.array('H')
105-
# create the PulseIn object using context manager
106-
with pulseio.PulseIn(self._pin, 81, True) as pulse_in:
108+
if _USE_PULSEIO:
107109
# The DHT type device use a specialize 1-wire protocol
108110
# The microprocessor first sends a LOW signal for a
109111
# specific length of time. Then the device sends back a
110112
# series HIGH and LOW signals. The length the HIGH signals
111113
# represents the device values.
112-
pulse_in.pause()
113-
pulse_in.clear()
114-
pulse_in.resume(self._trig_wait)
114+
self.pulse_in.pause()
115+
self.pulse_in.clear()
116+
self.pulse_in.resume(self._trig_wait)
117+
115118
# loop until we get the return pulse we need or
116119
# time out after 1/4 second
117-
tmono = time.monotonic()
118-
while time.monotonic() - tmono < 0.25:
119-
pass # time out after 1/4 seconds
120-
pulse_in.pause()
121-
while pulse_in:
122-
pulses.append(pulse_in.popleft())
123-
pulse_in.resume()
120+
time.sleep(0.25)
121+
self.pulse_in.pause()
122+
while self.pulse_in:
123+
pulses.append(self.pulse_in.popleft())
124124
return pulses
125125

126126
def _get_pulses_bitbang(self):

0 commit comments

Comments
 (0)