Skip to content

change dht context to static var for linux #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def __init__(self, dht11, pin, trig_wait):
self._last_called = 0
self._humidity = None
self._temperature = None

# We don't use a context because linux-based systems are sluggish
# and we're better off having a running process
if _USE_PULSEIO:
self.pulse_in = pulseio.PulseIn(self._pin, 81, True)

def _pulses_to_binary(self, pulses, start, stop):
"""Takes pulses, a list of transition times, and converts
Expand Down Expand Up @@ -102,25 +105,22 @@ def _get_pulses_pulseio(self):
pulses will have 81 elements for the DHT11/22 type devices.
"""
pulses = array.array('H')
# create the PulseIn object using context manager
with pulseio.PulseIn(self._pin, 81, True) as pulse_in:
if _USE_PULSEIO:
# The DHT type device use a specialize 1-wire protocol
# The microprocessor first sends a LOW signal for a
# specific length of time. Then the device sends back a
# series HIGH and LOW signals. The length the HIGH signals
# represents the device values.
pulse_in.pause()
pulse_in.clear()
pulse_in.resume(self._trig_wait)
self.pulse_in.pause()
self.pulse_in.clear()
self.pulse_in.resume(self._trig_wait)

# loop until we get the return pulse we need or
# time out after 1/4 second
tmono = time.monotonic()
while time.monotonic() - tmono < 0.25:
pass # time out after 1/4 seconds
pulse_in.pause()
while pulse_in:
pulses.append(pulse_in.popleft())
pulse_in.resume()
time.sleep(0.25)
self.pulse_in.pause()
while self.pulse_in:
pulses.append(self.pulse_in.popleft())
return pulses

def _get_pulses_bitbang(self):
Expand Down