From 98fcb2b71913412cea7377977e894c341e597de5 Mon Sep 17 00:00:00 2001 From: ladyada Date: Sat, 1 Dec 2018 02:06:56 -0500 Subject: [PATCH 1/2] removed context for pulse_in to make linux happy, and change delay to not use time.monotonic --- adafruit_dht.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 2860bee..316fad6 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -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 @@ -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: - # 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) - # 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() + 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. + 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 + 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): From 90149ac09074621cddb7575079e4f363fd444e6f Mon Sep 17 00:00:00 2001 From: ladyada Date: Sat, 1 Dec 2018 02:08:04 -0500 Subject: [PATCH 2/2] nicetabs --- adafruit_dht.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 316fad6..017f9f2 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -106,21 +106,21 @@ def _get_pulses_pulseio(self): """ pulses = array.array('H') 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. - 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 - time.sleep(0.25) - self.pulse_in.pause() - while self.pulse_in: - pulses.append(self.pulse_in.popleft()) + # 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. + 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 + 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):