#define F_CPU 16000000UL // 16 MHz clock speed

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <stdlib.h>
#include <util/delay.h>
#include "global.h"
#include "uart.h"
#include "rf12.h"
#include "i2cmaster.h"

#define Dev1621 0x90
#define MY_SDA PC0
#define MY_SCL PC1

//Programs for RFM12

void send(void);
void receive(void);
void ireceive(unsigned int *re_wert, unsigned int *half_wert );

//Programs for I2C

void delay();
void wait(unsigned int ms);

unsigned int TempIn, TempInhalf,Show=0, received=0;
float	TempInFloat;
unsigned int OffsetIn=0;
int main(void)
{
	  
    uart_init(UART_BAUD_SELECT(19200, F_CPU));
	sei();
	uart_puts_P("Atmega laeuft !\n");
// Initialisierung RFM
{
	rf12_init();					// ein paar Register setzen (z.B. CLK auf 10MHz)
	rf12_setfreq(RF12FREQ(433.92));	// Sende/Empfangsfrequenz auf 433,92MHz einstellen
	rf12_setbandwidth(4, 1, 4);		// 200kHz Bandbreite, -6dB Verstärkung, DRSSI threshold: -79dBm
	rf12_setbaud(19200);			// 19200 baud
	rf12_setpower(0, 6);			// 1mW Ausgangangsleistung, 120kHz Frequenzshift
	rf12_rxmode();
}
// Inititalisierung für PWM
{
	DDRD = 0xff;                      // Set Port as Output
	TCCR1A = (1<<WGM10)|(1<<COM1A1)   // Set up the two Control registers of Timer1.
	|(1<<COM1B1);             // Wave Form Generation is Fast PWM 8 Bit,
	TCCR1B = (1<<WGM12)|(1<<CS12)     // OC1A and OC1B are cleared on compare match
	|(1<<CS10);               // and set at BOTTOM. Clock Prescaler is 1024.

	OCR1A = 0;                       // Dutycycle of OC1A = 0%
	OCR1B = 0;                      // Dutycycle of OC1B = 0%

	sei();
	uart_puts_P("Empfaenger laeuft !\n");
}

// Initialisierung for LED-Outputs
 DDRA |= (1 << DDA5) | (1 << DDA0)| (1 << DDA1)| (1 << DDA2)| (1 << DDA3)| (1 << DDA4)| (1 << DDA5)| (1 << DDA6)| (1 << DDA7);
 DDRC |= (1 << DDC5) | (1 << DDC6)| (1 << DDC7);
// Initialisierung für I2C
{	
		//I²C
				i2c_init();
				i2c_start(Dev1621 + I2C_WRITE);	
				i2c_write(0xEE);
				i2c_stop();
				i2c_start(Dev1621+I2C_WRITE);
				i2c_write(0xAA);
				i2c_stop();
				uart_puts_P("I2c Init finished!\n");
				_delay_ms(700);
				
}

// Start-Up Show
//LEDs Rot			
					OCR1A=41;
					OCR1B=41;
			PORTA &= ~(1<<PC4); //LED BLAU IN 1
			PORTA &= ~(1<<PA2); //LED BLAU IN 2
			PORTA |= (1<<PA0); //LEDs Rot IN

			PORTC &= ~(1<<PC6); //LED BLAU OUT 1
			PORTA &= ~(1<<PA7); //LED BLAU OUT 2
			PORTA |= (1<<PA5); //LEDs Rot OUT
			_delay_ms(1500);

//LEDs Blau
					OCR1A=170;
					OCR1B=170;
			PORTA &= ~(1<<PA0);	//LEDs Rot IN
			PORTA |= (1<<PA4); //LED BLAU IN 1
			PORTA |= (1<<PA2); //LED BLAU IN 2
			
			PORTA &= ~(1<<PA5);	//LEDs Rot OUT
			PORTC |= (1<<PC6); //LED BLAU OUT 1
			PORTA |= (1<<PA7); //LED BLAU OUT 2
			_delay_ms(1500);
//LEDs Grün
					OCR1A=254;
					OCR1B=254;
			PORTA &= ~(1<<PA2); //LED BLAU IN 1
			PORTA &= ~(1<<PA4); //LED BLAU IN 2
			PORTA |= (1<<PA3); //LEDs Grün IN 1			
			PORTA |= (1<<PA1); //LEDs Grün IN 2
			
			PORTC &= ~(1<<PC6); //LED BLAU Out 1
			PORTA &= ~(1<<PA7); //LED BLAU Out 2
			PORTC |= (1<<PC7); //LEDs Grün Out 1
			PORTA |= (1<<PA6); //LEDs Grün Out 2
			_delay_ms(1500);
for (;;)    //MainLoop
{
	//************************Temp from Inside****************************************************************
				i2c_start(Dev1621+I2C_READ);

				TempIn=i2c_readAck();
				TempInhalf=i2c_readNak();
				i2c_stop();
			uart_putc(TempIn);
			uart_putc(TempInhalf);			
// Berechnung TempIn
		//If Temperture positiv but smaller than the Offset -> Special calculation
		if (TempIn<(OffsetIn))
		{
			TempIn=OffsetIn-TempIn;   //Example: Offset:3° re_wert:1° -> Temperture at real negativ
			if (TempInhalf>0)
			{
				TempIn=TempIn-0.5;
			}
			//Insert here Output for positiv-LED
	
		}
		
		//If Temperature negativ
		else if (TempIn & 0b10000000)
		{
			TempIn=TempIn-OffsetIn;
			TempIn= 256-TempIn;
			if (TempIn>0)
			{
				TempIn=TempIn-0.5;
			}
		}
		//Else it is positiv. If positiv and 0.5-Bit is true add 0.5°
		else
		{
			TempIn=TempIn-OffsetIn;
			if (TempInhalf>0)
			{
				TempIn=TempIn+0.5;
			}
		}
		
		//LED claculation for positiv or negativ Temp IN
		if (TempIn & 0b10000000)
		{
			PORTA &= ~(1<<PA1);	//LED Grün IN 1		
			PORTA &= ~(1<<PA3);	//LED Grün IN 2		
			PORTA &= ~(1<<PA0);	//LEDs Rot IN
			PORTA |= (1<<PA4); //LED BLAU IN 1
			PORTA |= (1<<PA2); //LED BLAU IN 2
		}
		else
		{
			PORTA &= ~(1<<PA1);	//LED Grün IN 1
			PORTA &= ~(1<<PA3);	//LED Grün IN 2
			PORTA &= ~(1<<PC4); //LED BLAU IN 1
			PORTA &= ~(1<<PA2); //LED BLAU IN 2
			PORTA |= (1<<PA0); //LEDs Rot IN
		}
//************************Temp from Outside***************************************************************
{
		unsigned int	temp, re_wert, half_wert,Offset=0; //Offset always negativ
		float	calctemp;
		
		if (rf12_data())
		{	
			received =1;
			ireceive(&re_wert, &half_wert);
		//If Temperture positiv but smaller than the Offset -> Special calculation
			if (re_wert<(Offset))
			{
				temp=Offset-re_wert;   //Example: Offset:3° re_wert:1° -> Temperture at real negativ
				if (half_wert>0)
				{
					temp=temp-0.5;
				}
			}
			
				
		//If Temperature negativ
			else if (re_wert & 0b10000000)
			{
				temp=re_wert-Offset;
				temp= 256-temp;
				if (half_wert>0)
				{
					temp=temp-0.5;
				}
			}
		//Else it is positiv. If positiv and 0.5-Bit is true add 0.5°
			else
			{
				temp=re_wert-Offset;
				if (half_wert>0)
				{
					temp=temp+0.5;
				}
			}
			
		//LED claculation for positiv or negativ Temp OUT
			if (re_wert & 0b10000000)
			{
		PORTA &= ~(1<<PA6);	//LED Grün IN 1
		PORTC &= ~(1<<PC7);	//LED Grün IN 2
 		PORTA &= ~(1<<PA5);	//LEDs Rot OUT			
		PORTC |= (1<<PC6); //LED BLAU OUT 1
		PORTA |= (1<<PA7); //LED BLAU OUT 2			
			}
			else 
			{
		PORTA &= ~(1<<PA6);	//LED Grün IN 1
		PORTC &= ~(1<<PC7);	//LED Grün IN 2
		PORTC &= ~(1<<PC6); //LED BLAU OUT 1
		PORTA &= ~(1<<PA7); //LED BLAU OUT 2
		PORTA |= (1<<PA5); //LEDs Rot OUT				
			}
}	

//Berechnung für Ausgabe an PWM
		{	
		calctemp=temp*214.04/40+40.96;
		OCR1A=calctemp;
		TempInFloat=TempIn*214.04/40+40.96;
		OCR1B=TempInFloat;
																	
	}
			//Bar-Show until something was received	



		
		_delay_ms(1000);
		uart_putc(temp);
		uart_putc(TempIn);
}
}
}

//RFM
void ireceive(unsigned int	*re_wert, unsigned int *half_wert)
{	unsigned char test[5];
	unsigned char i=0;
	rf12_irxdata(test,5);
	*re_wert=test[0];
	*half_wert=test[1];
	for (i=0; i<2; i++)
 	uart_putc(test[i]);	

}
void receive(void)
{	unsigned char test[5];
	unsigned char i=0;
	rf12_rxdata(test,5);

	for (i=0; i<5; i++)
	uart_putc(test[i]);

}

void send(void)
{	unsigned char test[]="11111111";
	rf12_txdata(test,10);
}

