Skip to content

Deinitialize pulsein object on exit, gave option to choose whether or not to use pulseio #46

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 5 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 14 additions & 8 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DHTBase:

__hiLevel = 51

def __init__(self, dht11, pin, trig_wait):
def __init__(self, dht11, pin, trig_wait, use_pulseio):
"""
:param boolean dht11: True if device is DHT11, otherwise DHT22.
:param ~board.Pin pin: digital pin used for communication
Expand All @@ -63,11 +63,17 @@ def __init__(self, dht11, pin, trig_wait):
self._last_called = 0
self._humidity = None
self._temperature = None
self._use_pulseio = use_pulseio
# We don't use a context because linux-based systems are sluggish
# and we're better off having a running process
if _USE_PULSEIO:
if self._use_pulseio:
self.pulse_in = PulseIn(self._pin, 81, True)

def exit(self):
if self._use_pulseio:
print("De-initializing self.pulse_in")
self.pulse_in.deinit()

def _pulses_to_binary(self, pulses, start, stop):
"""Takes pulses, a list of transition times, and converts
them to a 1's or 0's. The pulses array contains the transition times.
Expand Down Expand Up @@ -108,7 +114,7 @@ def _get_pulses_pulseio(self):
pulses will have 81 elements for the DHT11/22 type devices.
"""
pulses = array.array("H")
if _USE_PULSEIO:
if self._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
Expand Down Expand Up @@ -183,7 +189,7 @@ def measure(self):
new_temperature = 0
new_humidity = 0

if _USE_PULSEIO:
if self._use_pulseio:
pulses = self._get_pulses_pulseio()
else:
pulses = self._get_pulses_bitbang()
Expand Down Expand Up @@ -259,8 +265,8 @@ class DHT11(DHTBase):
:param ~board.Pin pin: digital pin used for communication
"""

def __init__(self, pin):
super().__init__(True, pin, 18000)
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
super().__init__(True, pin, 18000, use_pulseio)


class DHT22(DHTBase):
Expand All @@ -269,5 +275,5 @@ class DHT22(DHTBase):
:param ~board.Pin pin: digital pin used for communication
"""

def __init__(self, pin):
super().__init__(False, pin, 1000)
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
super().__init__(False, pin, 1000, use_pulseio)
7 changes: 6 additions & 1 deletion examples/dht_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D18)
dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
try:
Expand All @@ -20,5 +20,10 @@
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error

time.sleep(2.0)