//ATMEGA 328p
#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include "spi.h"
#include "wl_module.h"
#include "nRF24L01.h"
#include "dht.h"
#include "dht.c"
#include "bmp085.h"
#include "bmp085.c"
#include "spi.h"
#include "uart.h"

uint16_t adc_read(uint8_t ch)
{
	ch &= 0b00000111;
	ADMUX  |=  (ADMUX & 0xF8)|ch;
	ADCSRA |= (1 << ADSC );
	while (ADCSRA & (1 << ADSC));
	return (ADC);
}

int main(void)
{
	float temperature,humidity;
	double t;
	long p;
	uint16_t l;
	char txth[10],txthn[10],txtt[10],txttn[10],txtp[10],txtpn[10],txtl[10],txtln[10],ans[32];
	
	//UART initialisieren
	uart_init( UART_BAUD_SELECT(2400,F_CPU) );
	
	//bmp085 initialisieren
	bmp085_init();
	
	//Interrupt konfigurieren
	EICRA  = ((1<<ISC11)|(0<<ISC10)|(0<<ISC01)|(0<<ISC00));	
	EIMSK  = ((1<<INT1) |(0<<INT0));
	
	//ADC konfigurieren
	DDRC   &= ~(1 << PC1);
	ADMUX  |=  (0 << REFS1 ) | (1 << REFS0)   | (0 << ADLAR);
	ADCSRA |=  (1 << ADEN )  | (1 << ADPS2 ) | (1 << ADPS1 ) | (1 << ADPS0 );
	
	while(1)
    {	
		//Initialisierung von NRF24L01 
		wl_module_init();							
		_delay_ms(50);								
		sei();
		wl_module_tx_config(wl_module_TX_NR_0);		
		
		//ADC auslesen
		l = adc_read(1);
		
		//DHT22 auslesen
		if(dht_gettemperaturehumidity(&temperature, &humidity) != -1) {
		}
		else if (dht_gettemperaturehumidity(&temperature, &humidity) ==-1) {
			temperature = 66;
			humidity = 66;
		}
		
		//bmp085 auslesen
		t = bmp085_gettemperature();
		p = bmp085_getpressure();
		
		//Mittelwert der Temperaturen
		temperature = ((temperature + t)/2);
		
		//Werte in String schreiben
		dtostrf(humidity, 3,1, txth);
		dtostrf(temperature, 3,1, txtt);
		ltoa(p, txtp, 10);
		itoa(l, txtl, 10);
				
		/*
		//Temperaturstring anpassen
		if (temperature<-10)
		{
			txttn[4]=txtt[4];
			txttn[3]=',';
			txttn[2]=txtt[2];
			txttn[1]=txtt[1];
			txttn[0]=txtt[0];
		} 
		
		else if (temperature<0)
		{
			txttn[4]=txtt[3];
			txttn[3]=',';
			txttn[2]=txtt[1];
			txttn[1]=txtt[0];
			txttn[0]=' ';
		}

		else if  (temperature<10)
		{
			txttn[4]=txtt[2];
			txttn[3]=',';
			txttn[2]=txtt[0];
			txttn[1]=' ';
			txttn[0]=' ';
		}

		else if (temperature<100)
		{
			txttn[4]=txtt[3];
			txttn[3]=',';
			txttn[2]=txtt[1];
			txttn[1]=txtt[0];
			txttn[0]=' ';
		}
		
		//Feuchtigkeitsstring anpassen
		if (humidity<10)
		{
			txthn[4]=txth[2];
			txthn[3]=',';
			txthn[2]=txth[0];
			txthn[1]=' ';
			txthn[0]=' ';
		}

		else if  (humidity<100)
		{
			txthn[4]=txth[3];
			txthn[3]=',';
			txthn[2]=txth[1];
			txthn[1]=txth[0];
			txthn[0]=' ';
		}

		else if (humidity<1000)
		{
			txthn[4]=txth[4];
			txthn[3]=',';
			txthn[2]=txth[2];
			txthn[1]=txth[1];
			txthn[0]=txth[0];
		}
		
		//Druckstring anpassen
		if (p<100000)
		{
			txtpn[0] = ' ';
			txtpn[1] = txtp[0];
			txtpn[2] = txtp[1];
			txtpn[3] = txtp[2];
			txtpn[4] = ',';
			txtpn[5] = txtp[3];
			txtpn[6] = txtp[4];
		}	
		
		else if (p<1000000)
		{
			txtpn[0] = txtp[0];
			txtpn[1] = txtp[1];
			txtpn[2] = txtp[2];
			txtpn[3] = txtp[3];
			txtpn[4] = ',';
			txtpn[5] = txtp[4];
			txtpn[6] = txtp[5];
		}
		
		//Lichtstring anpassen
		if (l<10)
		{
			txtln[3]=txtl[0];
			txtln[2]=' ';
			txtln[1]=' ';
			txtln[0]=' ';
		}

		else if  (l<100)
		{
			txtln[3]=txtl[1];
			txtln[2]=txtl[0];
			txtln[1]=' ';
			txtln[0]=' ';
		}

		else if (l<1000)
		{
			txtln[3]=txtl[2];
			txtln[2]=txtl[1];
			txtln[1]=txtl[0];
			txtln[0]=' ';
		}
		*/
		
		strcpy(ans, "TM");
		strcat(ans, txtt);
		strcat(ans, " RH");
		strcat(ans, txth);
		strcat(ans, " P");
		strcat(ans, txtp);
		strcat(ans, " L");
		strcat(ans, txtl);
		
		//Daten mit nrf24l01 versenden
		wl_module_send(ans,wl_module_PAYLOAD);
		
		//Daten über uart ausgeben
		uart_puts(ans);
		
		_delay_ms(1500);
	}
}

ISR(INT1_vect)
{
    uint8_t status;   
       
        //Auslesen des wl_module status 
        wl_module_CSN_lo;                               		// Pull down chip select
        status = spi_fast_shift(NOP);							// Read status register
        wl_module_CSN_hi;                               		// Pull up chip select
		
		
		if (status & (1<<TX_DS))								// IRQ: Package has been sent
		{
			wl_module_config_register(STATUS, (1<<TX_DS));		//Clear Interrupt Bit
			PTX=0;
		}
		
		if (status & (1<<MAX_RT))								// IRQ: Package has not been sent, send again
		{
			wl_module_config_register(STATUS, (1<<MAX_RT));	// Clear Interrupt Bit
			wl_module_CE_hi;									// Start transmission
			_delay_us(10);								
			wl_module_CE_lo;
		}
		
		if (status & (1<<TX_FULL))								//TX_FIFO Full <-- this is not an IRQ
		{
			wl_module_CSN_lo;                             	  	// Pull down chip select
			spi_fast_shift(FLUSH_TX);							// Flush TX-FIFO
			wl_module_CSN_hi;                               	// Pull up chip select
		}
		
}