#include "KK_dht11.h"

int KK_dht11::read(const int pin) {
    uint8_t bits[5];
    uint8_t cnt = 7;
    uint8_t idx = 0;

    for (int i=0; i< 5; i++) bits[i] = 0;

    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
    delay(18);
    digitalWrite(pin, HIGH);
    delayMicroseconds(40);
    pinMode(pin, INPUT);

    unsigned int loopCnt = 10000;
    while(digitalRead(pin) == LOW) { if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; }
    loopCnt = 10000;
    while(digitalRead(pin) == HIGH) { if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; }

    for (int i=0; i<40; i++) {
        loopCnt = 10000;
        while(digitalRead(pin) == LOW) { if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; }
        unsigned long t = micros();
        loopCnt = 10000;
        while(digitalRead(pin) == HIGH) { if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; }
        if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
        if (cnt == 0) { cnt = 7; idx++; }
        else cnt--;
    }

    humidity    = bits[0];
    temperature = bits[2];

    uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
    if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
    
    return DHTLIB_OK;
}