Selaa lähdekoodia

vuelta a cambiarlo con un uint32_t

dacowars 1 kuukausi sitten
vanhempi
sitoutus
c66e68cd43
1 muutettua tiedostoa jossa 18 lisäystä ja 19 poistoa
  1. 18 19
      main/main.c

+ 18 - 19
main/main.c

@@ -18,6 +18,7 @@ static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
 #define CHECK_LOGE(x, msg, ...) do { \
         esp_err_t __; \
         if ((__ = x) != ESP_OK) { \
+            portEXIT_CRITICAL(&mux); \
             ESP_LOGE(TAG, msg, ## __VA_ARGS__); \
             return __; \
         } \
@@ -48,7 +49,7 @@ static esp_err_t get_change_time(gpio_num_t pin, uint8_t timeout_us, bool expect
     return ESP_ERR_TIMEOUT;
 }
 
-static inline esp_err_t dht_communication(gpio_num_t pin, uint8_t data[DHT_DATA_BITS/8]){
+static inline esp_err_t dht_communication(gpio_num_t pin, uint32_t *data){
     uint32_t high_time;
     uint32_t low_time;
 
@@ -62,23 +63,28 @@ static inline esp_err_t dht_communication(gpio_num_t pin, uint8_t data[DHT_DATA_
     CHECK_LOGE(get_change_time(pin, 88, 1, &high_time), "Failed to get dht response"); // Wait for the sensor to pull high 80us
     CHECK_LOGE(get_change_time(pin, 88, 0, &low_time), "Failed to get dht response"); // Wait for the sensor to pull low 80us
     
+    uint8_t checksum = 0;
 
     for (uint8_t i = 0; i < DHT_DATA_BITS; i++){
         
+        
         // Wait for the sensor to pull low 50us
         CHECK_LOGE(get_change_time(pin, 65, 1, &low_time), "Failed dht transmission %d h", i); 
         
         // Wait for the sensor to pull high 26-28us for 0 or 70us for 1
         CHECK_LOGE(get_change_time(pin, 80, 0, &high_time), "Failed dht transmission %d l", i); 
 
-        uint8_t b = i / 8;
-        uint8_t m = i % 8;
-        if (!m) data[b] = 0; // Clear the byte at the start of each new byte
-
-        data[b] |= (high_time > low_time) << (7 - m); // Set the bit if high_time is greater than low_time
+        if (i < 32){
+            *data |= ((high_time > low_time) << (31 - i)); // Set the bit if high_time is greater than low_time
+        }
+        else checksum |= (high_time > low_time) << (39 - i); // Calculate checksum in the last 8 bits
 
     }
     
+    if (checksum != (uint8_t)((*data >> 24) + (*data >> 16 & 0xFF) + (*data >> 8 & 0xFF) + (*data & 0xFF))) {
+        ESP_LOGE(TAG, "DHT22 checksum mismatch: calculated %02X, received %02X", checksum, *data & 0xFF);
+        return ESP_ERR_INVALID_CRC;
+    }
 
     return ESP_OK;
 }
@@ -89,21 +95,14 @@ static inline int16_t data_to_int16(uint8_t MSB, uint8_t LSB){
 
 
 esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
-    uint8_t data[DHT_DATA_BITS/8] = {0};
+    uint32_t data = 0;
 
     gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
     gpio_set_level(pin, 1); 
 
-    //portENTER_CRITICAL(&mux);
-    esp_err_t err = dht_communication(pin, data);
-    if (err == ESP_OK) {
-       // portEXIT_CRITICAL(&mux);
-    }
-
-    if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
-        ESP_LOGE(TAG, "DHT22 checksum mismatch");
-        return ESP_ERR_INVALID_CRC;
-    }
+    portENTER_CRITICAL(&mux);
+    esp_err_t err = dht_communication(pin, &data);
+    portEXIT_CRITICAL(&mux);
 
     gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
     gpio_set_level(pin, 1); 
@@ -111,8 +110,8 @@ esp_err_t dht_read(gpio_num_t pin, float *temperature, float *humidity){
     if (err != ESP_OK) return err;
 
 
-    *humidity = data_to_int16(data[0], data[1]) / 10.0;
-    *temperature = data_to_int16(data[2], data[3]) / 10.0;
+    *humidity = data_to_int16(data >> 24 & 0xFF, data >> 16 & 0xFF) / 10.0;
+    *temperature = data_to_int16(data >> 8 & 0xFF, data & 0xFF) / 10.0;
     return ESP_OK;
 }