Refactor DHT driver

Refactor DHT driver (#7717)
This commit is contained in:
Theo Arends 2020-02-23 21:26:58 +01:00
parent 9b758350b5
commit 8969877502
1 changed files with 17 additions and 0 deletions

View File

@ -100,6 +100,7 @@ bool DhtRead(uint32_t sensor)
break; break;
} }
/*
bool error = false; bool error = false;
noInterrupts(); noInterrupts();
if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) { if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) {
@ -127,6 +128,22 @@ bool DhtRead(uint32_t sensor)
} }
interrupts(); interrupts();
if (error) { return false; } if (error) { return false; }
*/
uint32_t i = 0;
noInterrupts();
if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) {
for (i = 0; i < 40; i++) {
if (!DhtWaitState(sensor, 1)) { break; }
delayMicroseconds(35); // Was 30
if (digitalRead(Dht[sensor].pin)) {
dht_data[i / 8] |= (1 << (7 - i % 8));
}
if (!DhtWaitState(sensor, 0)) { break; }
}
}
interrupts();
if (i < 40) { return false; }
uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF; uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF;
if (dht_data[4] != checksum) { if (dht_data[4] != checksum) {