Skip to content

Commit 1051579

Browse files
committed
create private functions for pulseio and bitbang version of getting pulses
1 parent d2da873 commit 1051579

File tree

1 file changed

+61
-54
lines changed

1 file changed

+61
-54
lines changed

adafruit_dht.py

+61-54
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _pulses_to_binary(self, pulses, start, stop):
9191

9292
return binary
9393

94-
def _get_pulses(self):
94+
def _get_pulses_pulseio(self):
9595
""" _get_pulses implements the communication protcol for
9696
DHT11 and DHT22 type devices. It sends a start signal
9797
of a specific length and listens and measures the
@@ -102,54 +102,61 @@ def _get_pulses(self):
102102
pulses will have 81 elements for the DHT11/22 type devices.
103103
"""
104104
pulses = array.array('H')
105+
# create the PulseIn object using context manager
106+
with pulseio.PulseIn(self._pin, 81, True) as pulse_in:
107+
# The DHT type device use a specialize 1-wire protocol
108+
# The microprocessor first sends a LOW signal for a
109+
# specific length of time. Then the device sends back a
110+
# series HIGH and LOW signals. The length the HIGH signals
111+
# represents the device values.
112+
pulse_in.pause()
113+
pulse_in.clear()
114+
pulse_in.resume(self._trig_wait)
115+
# loop until we get the return pulse we need or
116+
# 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()
124+
return pulses
125+
126+
def _get_pulses_bitbang(self):
127+
""" _get_pulses implements the communication protcol for
128+
DHT11 and DHT22 type devices. It sends a start signal
129+
of a specific length and listens and measures the
130+
return signal lengths.
105131
106-
if _USE_PULSEIO:
107-
# create the PulseIn object using context manager
108-
with pulseio.PulseIn(self._pin, 81, True) as pulse_in:
109-
110-
# The DHT type device use a specialize 1-wire protocol
111-
# The microprocessor first sends a LOW signal for a
112-
# specific length of time. Then the device sends back a
113-
# series HIGH and LOW signals. The length the HIGH signals
114-
# represents the device values.
115-
pulse_in.pause()
116-
pulse_in.clear()
117-
pulse_in.resume(self._trig_wait)
118-
119-
# loop until we get the return pulse we need or
120-
# time out after 1/4 second
121-
tmono = time.monotonic()
122-
while time.monotonic() - tmono < 0.25:
123-
pass # time out after 1/4 seconds
124-
pulse_in.pause()
125-
while pulse_in:
126-
pulses.append(pulse_in.popleft())
127-
pulse_in.resume()
128-
else:
129-
with DigitalInOut(self._pin) as dhtpin:
130-
# we will bitbang if no pulsein capability
131-
transitions = []
132-
# Signal by setting pin high, then low, and releasing
133-
dhtpin.direction = Direction.OUTPUT
134-
dhtpin.value = True
135-
time.sleep(0.1)
136-
dhtpin.value = False
137-
time.sleep(0.001)
138-
139-
timestamp = time.monotonic() # take timestamp
140-
dhtval = True # start with dht pin true because its pulled up
141-
dhtpin.direction = Direction.INPUT
142-
dhtpin.pull = Pull.UP
143-
while time.monotonic() - timestamp < 0.25:
144-
if dhtval != dhtpin.value:
145-
dhtval = not dhtval # we toggled
146-
transitions.append(time.monotonic()) # save the timestamp
147-
# convert transtions to microsecond delta pulses:
148-
# use last 81 pulses
149-
transition_start = max(1, len(transitions) - 81)
150-
for i in range(transition_start, len(transitions)):
151-
pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i-1]))
152-
pulses.append(min(pulses_micro_sec, 65535))
132+
return pulses (array.array uint16) contains alternating high and low
133+
transition times starting with a low transition time. Normally
134+
pulses will have 81 elements for the DHT11/22 type devices.
135+
"""
136+
pulses = array.array('H')
137+
with DigitalInOut(self._pin) as dhtpin:
138+
# we will bitbang if no pulsein capability
139+
transitions = []
140+
# Signal by setting pin high, then low, and releasing
141+
dhtpin.direction = Direction.OUTPUT
142+
dhtpin.value = True
143+
time.sleep(0.1)
144+
dhtpin.value = False
145+
time.sleep(0.001)
146+
timestamp = time.monotonic() # take timestamp
147+
dhtval = True # start with dht pin true because its pulled up
148+
dhtpin.direction = Direction.INPUT
149+
dhtpin.pull = Pull.UP
150+
while time.monotonic() - timestamp < 0.25:
151+
if dhtval != dhtpin.value:
152+
dhtval = not dhtval # we toggled
153+
transitions.append(time.monotonic()) # save the timestamp
154+
# convert transtions to microsecond delta pulses:
155+
# use last 81 pulses
156+
transition_start = max(1, len(transitions) - 81)
157+
for i in range(transition_start, len(transitions)):
158+
pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i-1]))
159+
pulses.append(min(pulses_micro_sec, 65535))
153160
return pulses
154161

155162
def measure(self):
@@ -168,7 +175,10 @@ def measure(self):
168175
(time.monotonic()-self._last_called) > delay_between_readings):
169176
self._last_called = time.monotonic()
170177

171-
pulses = self._get_pulses()
178+
if _USE_PULSEIO:
179+
pulses = self._get_pulses_pulseio()
180+
else:
181+
pulses = self._get_pulses_bitbang()
172182
#print(len(pulses), "pulses:", [x for x in pulses])
173183

174184
if len(pulses) >= 80:
@@ -179,14 +189,11 @@ def measure(self):
179189
if self._dht11:
180190
# humidity is 1 byte
181191
self._humidity = buf[0]
182-
else:
183-
# humidity is 2 bytes
184-
self._humidity = ((buf[0]<<8) | buf[1]) / 10
185-
186-
if self._dht11:
187192
# temperature is 1 byte
188193
self._temperature = buf[2]
189194
else:
195+
# humidity is 2 bytes
196+
self._humidity = ((buf[0]<<8) | buf[1]) / 10
190197
# temperature is 2 bytes
191198
# MSB is sign, bits 0-14 are magnitude)
192199
raw_temperature = (((buf[2] & 0x7f)<<8) | buf[3]) / 10

0 commit comments

Comments
 (0)