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 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
27 changes: 19 additions & 8 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import array
import time
from os import uname
from digitalio import DigitalInOut, Pull, Direction

_USE_PULSEIO = False
Expand All @@ -51,23 +52,33 @@ 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
:param int trig_wait: length of time to hold trigger in LOW state (microseconds)
:param boolean use_pulseio: False to force bitbang when pulseio available (only with Blinka)
"""
self._dht11 = dht11
self._pin = pin
self._trig_wait = trig_wait
self._last_called = 0
self._humidity = None
self._temperature = None
self._use_pulseio = use_pulseio
if "Linux" not in uname() and not self._use_pulseio:
raise Exception("Bitbanging is not supported when using CircuitPython.")
# 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):
""" Cleans up the PulseIn process. Must be called explicitly """
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 +119,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 +194,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 +270,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 +280,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)
10 changes: 10 additions & 0 deletions examples/dht_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D18)

# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
try:
# Print the values to the serial port
Expand All @@ -20,5 +25,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)