/*
 * File:   DHT22.c
 * Author: mhanc
 *
 * Created on ?tvrtok, 2025, októbra 9, 16:14
 */
#include "settings.h"
#include "xc.h"
#include "stdint.h"
#include "stdio.h"
#include "mcc_generated_files/system.h"
#include "DHT22.h"


#define MAX_TIMINGS 85
#define DHT_PIN 7  // wiringPi pin 7 = BCM_GPIO 4

int data[5] = {0, 0, 0, 0, 0};

int read_dht22_data(char *humidity_str, char *temperature_str) {
    uint8_t laststate = 1;
    uint8_t counter = 0;
    uint8_t j = 0, i;

    data[0] = data[1] = data[2] = data[3] = data[4] = 0;
    
    volatile int humidity = 0; 
    volatile int temperature = 0;

    // pull pin down for 18 milliseconds
    DHT_PIN_SetDigitalOutput();
    DHT_PIN_SetHigh();
    __delay_ms(10);
    DHT_PIN_SetLow();
    __delay_ms(18);

    // then pull it up for 40 microseconds
    DHT_PIN_SetHigh();
    __delay_us(40);

    // prepare to read the pin
    DHT_PIN_SetDigitalInput();
    Nop();

    // read the data (DHT22 sends 40 bits)
    for (i = 0; i < MAX_TIMINGS; i++) 
    {
        counter = 0;
        while (laststate == DHT_PIN_GetValue()) {
            counter++;
            __delay_us(1);
            if (counter == 255)
                break;
        }
        laststate = DHT_PIN_GetValue();

        if (counter == 255)
            break;

        // skip first 3 transitions
        if ((i > 2) && (i % 2 == 0)) {
            data[j / 8] <<= 1;
            if (counter > 16) // if high pulse >50us, bit = 1
                data[j / 8] |= 1;
            j++;
        }
    }

    // check if we received 40 bits (5 bytes) and verify checksum
    if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) 
    {
        humidity = ((data[0] << 8) + data[1]);
        temperature = (((data[2] & 0x7F) << 8) + data[3]);
        if(humidity>=600||humidity<=300)
            hum_col = 0xF800;
        else
            hum_col = 0;
        for(i=0;i<8;i++)
        {
            temperature_str[i] = ' ';
            humidity_str[i] = ' ';
        }
        
        if (data[2] & 0x80)
        {
            temperature_str[0] = '-';
        }
        else
        {
            temperature_str[0] = ' ';
        }
        
        
        i = 3;
        while (temperature > 0) 
        {
            // Convert integer digit to character and store
            // it in the str
            temperature_str[i--] = temperature % 10 + '0';
            temperature /= 10;
        }
        temperature_str[4] = temperature_str[3];
        temperature_str[3] = ',';
        if(temperature_str[1]=='0')
            temperature_str[1] = ' ';
        temperature_str[5] = 0;
        
        i = 3;
        while (humidity > 0) 
        {
            // Convert integer digit to character and store
            // it in the str
            humidity_str[i--] = humidity % 10 + '0';
            humidity /= 10;
        }
        humidity_str[4] = humidity_str[3];
        humidity_str[3] = ',';
        if(humidity_str[0]=='0')
        {
            humidity_str[0] = ' ';
            if(humidity_str[1]=='0')
                humidity_str[1] = ' ';
        }
        humidity_str[5] = 0;   
        Nop();
    }
    else
    {
        for(i=0;i<5;i++)
        {
            humidity_str[i] = '-';
            temperature_str[i] = '-';
        }
        humidity_str[3] = ',';
        temperature_str[3] = ',';
    }
    
    Nop();
    Nop();
}

