More cleanup and seems to be working

This commit is contained in:
2023-10-31 20:42:29 +00:00
parent 45d7001bb0
commit 03b4bc7e5a
2 changed files with 20 additions and 18 deletions

View File

@@ -34,17 +34,11 @@ void dht22_handle_error(dht22_error e) {
uint32_t wait_for_signal_to_change_from(int signal_level) {
uint64_t start_time = esp_timer_get_time();
uint64_t next_time = start_time;
/*
A vague attempt at "waiting" until the pin comes some kind of level
if (gpio_get_level(CONFIG_DHT22_DATA_GPIO) != signal_level) {
ets_delay_us(15);
}
*/
while (gpio_get_level(DATA_GPIO) == signal_level) {
next_time = esp_timer_get_time();
ets_delay_us(2); // just incase of lagging/debouncing/jitter/whatever
// TODO think about a way to pass in a max time - although 10ms isn't bad
if ((next_time - start_time) >= 1000 * 10) {
return DHT22_TIMEOUT_ERROR;
}
@@ -58,8 +52,6 @@ uint32_t wait_for_signal_to_change_from(int signal_level) {
}
dht22_error dht22_read() {
uint32_t elapsed_time;
portDISABLE_INTERRUPTS();
/*
* From datasheet:
@@ -74,25 +66,35 @@ dht22_error dht22_read() {
gpio_set_level(DATA_GPIO, 1);
ets_delay_us(25);
gpio_set_direction(DATA_GPIO, GPIO_MODE_INPUT);
// Wait here until it goes high DHT "responds" after being pulled high
// The 40 is pretty arbitrary - but there's probably a problem if it goes higher
uint32_t elapsed_time;
elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time == DHT22_TIMEOUT_ERROR) {
return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time > 40) {
return DHT22_TIMING_ERROR;
}
/*
* From datasheet:
* - DHT pulls low 80us
* - DHT pulls high 80us
*/
gpio_set_direction(DATA_GPIO, GPIO_MODE_INPUT);
// Maybe wait here until it goes high?
while (gpio_get_level(DATA_GPIO) == 1) {
// wait for a while - should only be 20 uS
}
elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time < 70 || elapsed_time > 90) {
if (elapsed_time == DHT22_TIMEOUT_ERROR) {
return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR;
}
elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time < 70 || elapsed_time > 90) {
if (elapsed_time == DHT22_TIMEOUT_ERROR) {
return DHT22_TIMEOUT_ERROR;
} else if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR;
}