Fixes #81 "signal already there" != "timeout"

If expectPulse(LOW) sees the port is already LOW (result == 0), this is not timeout!
This commit is contained in:
adams13x13 2017-09-02 20:15:19 +02:00 committed by GitHub
parent c978977718
commit 761f4f4fb9

11
DHT.cpp
View File

@ -7,6 +7,7 @@ written by Adafruit Industries
#include "DHT.h" #include "DHT.h"
#define MIN_INTERVAL 2000 #define MIN_INTERVAL 2000
#define TIMEOUT -1
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) { DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin; _pin = pin;
@ -161,12 +162,12 @@ boolean DHT::read(bool force) {
// First expect a low signal for ~80 microseconds followed by a high signal // First expect a low signal for ~80 microseconds followed by a high signal
// for ~80 microseconds again. // for ~80 microseconds again.
if (expectPulse(LOW) == 0) { if (expectPulse(LOW) == TIMEOUT) {
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse.")); DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
_lastresult = false; _lastresult = false;
return _lastresult; return _lastresult;
} }
if (expectPulse(HIGH) == 0) { if (expectPulse(HIGH) == TIMEOUT) {
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse.")); DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
_lastresult = false; _lastresult = false;
return _lastresult; return _lastresult;
@ -191,7 +192,7 @@ boolean DHT::read(bool force) {
for (int i=0; i<40; ++i) { for (int i=0; i<40; ++i) {
uint32_t lowCycles = cycles[2*i]; uint32_t lowCycles = cycles[2*i];
uint32_t highCycles = cycles[2*i+1]; uint32_t highCycles = cycles[2*i+1];
if ((lowCycles == 0) || (highCycles == 0)) { if ((lowCycles == TIMEOUT) || (highCycles == TIMEOUT)) {
DEBUG_PRINTLN(F("Timeout waiting for pulse.")); DEBUG_PRINTLN(F("Timeout waiting for pulse."));
_lastresult = false; _lastresult = false;
return _lastresult; return _lastresult;
@ -242,7 +243,7 @@ uint32_t DHT::expectPulse(bool level) {
uint8_t portState = level ? _bit : 0; uint8_t portState = level ? _bit : 0;
while ((*portInputRegister(_port) & _bit) == portState) { while ((*portInputRegister(_port) & _bit) == portState) {
if (count++ >= _maxcycles) { if (count++ >= _maxcycles) {
return 0; // Exceeded timeout, fail. return TIMEOUT; // Exceeded timeout, fail.
} }
} }
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266 // Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
@ -250,7 +251,7 @@ uint32_t DHT::expectPulse(bool level) {
#else #else
while (digitalRead(_pin) == level) { while (digitalRead(_pin) == level) {
if (count++ >= _maxcycles) { if (count++ >= _maxcycles) {
return 0; // Exceeded timeout, fail. return TIMEOUT; // Exceeded timeout, fail.
} }
} }
#endif #endif