Skip to content

Commit 024c454

Browse files
committed
Reduce number of branches to make pylint happy
1 parent ced0804 commit 024c454

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

adafruit_dht.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -184,46 +184,45 @@ def measure(self):
184184
pulses = self._get_pulses_bitbang()
185185
#print(len(pulses), "pulses:", [x for x in pulses])
186186

187-
if len(pulses) >= 80:
188-
buf = array.array('B')
189-
for byte_start in range(0, 80, 16):
190-
buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16))
191-
192-
if self._dht11:
193-
# humidity is 1 byte
194-
new_humidity = buf[0]
195-
# temperature is 1 byte
196-
new_temperature = buf[2]
197-
else:
198-
# humidity is 2 bytes
199-
new_humidity = ((buf[0]<<8) | buf[1]) / 10
200-
# temperature is 2 bytes
201-
# MSB is sign, bits 0-14 are magnitude)
202-
raw_temperature = (((buf[2] & 0x7f)<<8) | buf[3]) / 10
203-
# set sign
204-
if buf[2] & 0x80:
205-
raw_temperature = -raw_temperature
206-
new_temperature = raw_temperature
207-
# calc checksum
208-
chk_sum = 0
209-
for b in buf[0:4]:
210-
chk_sum += b
211-
212-
# checksum is the last byte
213-
if chk_sum & 0xff != buf[4]:
214-
# check sum failed to validate
215-
raise RuntimeError("Checksum did not validate. Try again.")
216-
217-
if new_humidity < 0 or new_humidity > 100:
218-
# We received unplausible data
219-
raise RuntimeError("Received unplausible data. Try again.")
220-
221-
elif len(pulses) >= 10:
187+
if len(pulses) < 10:
188+
# Probably a connection issue!
189+
raise RuntimeError("DHT sensor not found, check wiring")
190+
191+
if len(pulses) < 80:
222192
# We got *some* data just not 81 bits
223193
raise RuntimeError("A full buffer was not returned. Try again.")
194+
195+
buf = array.array('B')
196+
for byte_start in range(0, 80, 16):
197+
buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16))
198+
199+
if self._dht11:
200+
# humidity is 1 byte
201+
new_humidity = buf[0]
202+
# temperature is 1 byte
203+
new_temperature = buf[2]
224204
else:
225-
# Probably a connection issue!
226-
raise RuntimeError("DHT sensor not found, check wiring")
205+
# humidity is 2 bytes
206+
new_humidity = ((buf[0]<<8) | buf[1]) / 10
207+
# temperature is 2 bytes
208+
# MSB is sign, bits 0-14 are magnitude)
209+
new_temperature = (((buf[2] & 0x7f)<<8) | buf[3]) / 10
210+
# set sign
211+
if buf[2] & 0x80:
212+
new_temperature = -new_temperature
213+
# calc checksum
214+
chk_sum = 0
215+
for b in buf[0:4]:
216+
chk_sum += b
217+
218+
# checksum is the last byte
219+
if chk_sum & 0xff != buf[4]:
220+
# check sum failed to validate
221+
raise RuntimeError("Checksum did not validate. Try again.")
222+
223+
if new_humidity < 0 or new_humidity > 100:
224+
# We received unplausible data
225+
raise RuntimeError("Received unplausible data. Try again.")
227226

228227
self._temperature = new_temperature
229228
self._humidity = new_humidity

0 commit comments

Comments
 (0)