Skip to content

Commit efda9b8

Browse files
committed
Reduce number of branches to make pylint happy
1 parent e6880d7 commit efda9b8

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

adafruit_dht.py

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -189,48 +189,45 @@ def measure(self):
189189
pulses = self._get_pulses_bitbang()
190190
# print(len(pulses), "pulses:", [x for x in pulses])
191191

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

235232
self._temperature = new_temperature
236233
self._humidity = new_humidity

0 commit comments

Comments
 (0)