#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "hd44780.h"
#include "hd44780_settings_example.h"
//#include<avr/io162.h> 

#define F_CPU 8000000UL
#define BAUD 9600 

#define LED1_PORT PORTA
#define LED1_PIN  0
#define LED2_PORT	PORTA
#define LED2_PIN	1
#define LED3_PORT PORTA
#define LED3_PIN  2
#define LED4_PORT PORTA
#define LED4_PIN  3
 
#define LED1_low()  LED1_PORT&=~_BV(LED1_PIN)
#define LED1_high() LED1_PORT|=_BV(LED1_PIN)
#define LED2_low()  LED2_PORT&=~_BV(LED2_PIN)
#define LED2_high() LED2_PORT|=_BV(LED2_PIN)
#define LED3_low()  LED3_PORT&=~_BV(LED3_PIN)
#define LED3_high() LED3_PORT|=_BV(LED3_PIN)
#define LED4_low()  LED4_PORT&=~_BV(LED4_PIN)
#define LED4_high() LED4_PORT|=_BV(LED4_PIN)


void io_init();
void pwm_init();
void uart_init();
unsigned int UART_Receive();
void zobrazeni();
void test_kl();
void test_posun();
void grafika();
void prijem_data();


char znak_int [8];
int posun = 0;
char c;

char prijem;
int i=0,led1=0,led2=0,led3=0,led4=0;
	
unsigned int znak[3];

char OCR0_data[3];
char OCR2_data[3];
char OCR3B_data[3];
char OCR1B_data[3];


void main(void)
{

	io_init();
	pwm_init();
	uart_init();
	lcd_init();
	lcd_home();
	

	posun = 1;
	OCR0  = 30;
	OCR2  = 3;
	OCR3B = 0; 
	OCR1B = 30; 


	LED1_high();
	LED2_high();
	LED3_high();
	LED4_high();

while (1)
      {
	zobrazeni();
	test_kl();
	grafika();
	prijem_data();


	  
}
}


void uart_init()
{
	UBRR0H = (unsigned char) (((F_CPU/(BAUD*16UL))-1)>>8);
	UBRR0L = (unsigned char) (F_CPU/(BAUD*16UL))-1;
	UCSR0B |= (1<<TXEN0) | (1<<RXEN0) | (1<<UDRIE0) | (1<<RXCIE0) | (1<<TXCIE0);
	UCSR0C |= (1<<URSEL0) | (1<<UCSZ00) | (1<<UCSZ01); 
}

void pwm_init()
{
	TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00)|(1<<CS01);
	DDRB|=(1<<PB0);

	TCCR2|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00)|(1<<CS01);
	DDRB|=(1<<PB1);

	TCCR1A|=(1<<WGM10)|(1<<COM1A1)|(1<<COM1B1);
	TCCR1B|=(1<<WGM12)|(1<<CS10)|(1<<CS11);
	DDRE|=(1<<PE2);

	TCCR3A|=(1<<WGM30)|(1<<COM3A1)|(1<<COM3B1);
	TCCR3B|=(1<<WGM32)|(1<<CS30)|(1<<CS31);
	DDRB|=(1<<PB4);
}

void io_init()
{
	DDRA  = 0B11111111;
	PORTA = 0B11111110;

	DDRC  = 0B11111111;
	PORTC = 0B00000000;

	DDRD  = 0B00000010;
	PORTD = 0B11111101;
}

void UART_Transmit(unsigned char data) 
{ 
	while (!(UCSR0A & (1<<UDRE0))); 
	UDR0 = data; 
} 

unsigned int UART_Receive() 
{ 
   while(!(UCSR0A & (1<<UDRE0)));
   return UDR0; 
} 

void zobrazeni()
{
	if (posun == 1) 
	{
			lcd_goto(0);
			lcd_puts("LED1:");
			lcd_goto(6);
			lcd_putc(OCR0_data[0]);
			lcd_putc(OCR0_data[1]);
			lcd_putc(OCR0_data[2]);


			lcd_goto(64);
			lcd_puts("LED2:");
			lcd_goto(70);
			lcd_putc(OCR2_data[0]);
			lcd_putc(OCR2_data[1]);
			lcd_putc(OCR2_data[2]);

        	lcd_goto(13);
			lcd_puts("<--");
			lcd_goto(77);
			lcd_puts("   ");

			if (led1 == 0) 
			{
				lcd_goto(10);
				lcd_puts("off");
			}

			if (led1 == 1) 
			{
				lcd_goto(10);
				lcd_puts("on ");
			}

			if (led2 == 0) 
			{
				lcd_goto(74);
				lcd_puts("off");
			}

			if (led2 == 1) 
			{
				lcd_goto(74);
				lcd_puts("on ");
			}
	}

	if (posun == 2) 
	{
			lcd_goto(0);
			lcd_puts("LED1:");
			lcd_goto(6);
			lcd_putc(OCR0_data[0]);
			lcd_putc(OCR0_data[1]);
			lcd_putc(OCR0_data[2]);


			lcd_goto(64);
			lcd_puts("LED2:");
			lcd_goto(70);
			lcd_putc(OCR2_data[0]);
			lcd_putc(OCR2_data[1]);
			lcd_putc(OCR2_data[2]);

        	lcd_goto(13);
			lcd_puts("   ");
			lcd_goto(77);
			lcd_puts("<--");

			if (led1 == 0) 
			{
				lcd_goto(10);
				lcd_puts("off");
			}

			if (led1 == 1) 
			{
				lcd_goto(10);
				lcd_puts("on ");
			}

			if (led2 == 0) 
			{
				lcd_goto(74);
				lcd_puts("off");
			}

			if (led2 == 1) 
			{
				lcd_goto(74);
				lcd_puts("on ");
			}
	}

	if (posun == 3) 
	{
			lcd_goto(0);
			lcd_puts("LED3:");
			lcd_goto(6);
			lcd_putc(OCR3B_data[0]);
			lcd_putc(OCR3B_data[1]);
			lcd_putc(OCR3B_data[2]);


			lcd_goto(64);
			lcd_puts("LED4:");
			lcd_goto(70);
			lcd_putc(OCR1B_data[0]);
			lcd_putc(OCR1B_data[1]);
			lcd_putc(OCR1B_data[2]);

        	lcd_goto(13);
			lcd_puts("<--");
			lcd_goto(77);
			lcd_puts("   ");

			if (led3 == 0) 
			{
				lcd_goto(10);
				lcd_puts("off");
			}

			if (led3 == 1) 
			{
				lcd_goto(10);
				lcd_puts("on ");
			}

			if (led4 == 0) 
			{
				lcd_goto(74);
				lcd_puts("off");
			}

			if (led4 == 1) 
			{
				lcd_goto(74);
				lcd_puts("on ");
			}
	}

	if (posun == 4) 
	{
			lcd_goto(0);
			lcd_puts("LED3:");
			lcd_goto(6);
			lcd_putc(OCR3B_data[0]);
			lcd_putc(OCR3B_data[1]);
			lcd_putc(OCR3B_data[2]);


			lcd_goto(64);
			lcd_puts("LED4:");
			lcd_goto(70);
			lcd_putc(OCR1B_data[0]);
			lcd_putc(OCR1B_data[1]);
			lcd_putc(OCR1B_data[2]);

        	lcd_goto(13);
			lcd_puts("   ");
			lcd_goto(77);
			lcd_puts("<--");

			if (led3 == 0) 
			{
				lcd_goto(10);
				lcd_puts("off");
			}

			if (led3 == 1) 
			{
				lcd_goto(10);
				lcd_puts("on ");
			}

			if (led4 == 0) 
			{
				lcd_goto(74);
				lcd_puts("off");
			}

			if (led4 == 1) 
			{
				lcd_goto(74);
				lcd_puts("on ");
			}
	}
		
}

void test_kl()
{
	if (bit_is_clear(PIND,2)) 
	{	
		posun = posun + 1;
		if (posun > 4) {posun = 1;}		
		while (bit_is_clear(PIND,2));
	}

	
		if ((bit_is_clear(PIND,3)) && (posun == 1)) 
		{	
			OCR0 = OCR0 + 2;
			if (OCR0 == 255) {OCR0 = 0;}
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,3)) && (posun == 2)) 
		{	
			OCR2 = OCR2 + 2;
			if (OCR2 == 255) {OCR2 = 0;}	
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,3)) && (posun == 3)) 
		{	
			OCR3B = OCR3B + 2;
			if (OCR3B == 255) {OCR3B = 0;}	
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,3)) && (posun == 4)) 
		{	
			OCR1B = OCR1B + 2;
			if (OCR1B == 255) {OCR1B = 0;}	
			_delay_ms(100);	
		}


		if ((bit_is_clear(PIND,4)) && (posun == 1)) 
		{	
			OCR0 = OCR0 - 2;
			if (OCR0 < 1) {OCR0 = 255;}	
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,4)) && (posun == 2)) 
		{	
			OCR2 = OCR2 - 2;
			if (OCR2 < 1) {OCR2 = 255;}	
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,4)) && (posun == 3)) 
		{	
			OCR3B = OCR3B - 2;
			if (OCR3B < 1) {OCR3B = 255;}	
			_delay_ms(100);	
		}

		if ((bit_is_clear(PIND,4)) && (posun == 4)) 
		{	
			OCR1B = OCR1B - 2;
			if (OCR1B < 0) {OCR1B = 255;}	
			_delay_ms(100);	
		}


		if (bit_is_clear(PIND,5) && (posun == 1)) 
		{		
			if (led1 == 0) {led1 = 1; LED1_low();}
			else {led1 = 0; LED1_high();}
		while (bit_is_clear(PIND,5));
		}

		if (bit_is_clear(PIND,5) && (posun == 2)) 
		{		
			if (led2 == 0) {led2 = 1; LED2_low();}
			else {led2 = 0; LED2_high();}
		while (bit_is_clear(PIND,5));
		}

		if (bit_is_clear(PIND,5) && (posun == 3)) 
		{		
			if (led3 == 0) {led3 = 1; LED3_low();}
			else {led3 = 0; LED3_high();}
		while (bit_is_clear(PIND,5));
		}

		if (bit_is_clear(PIND,5) && (posun == 4)) 
		{		
			if (led4 == 0) {led4 = 1; LED4_low();}
			else {led4 = 0; LED4_high();}
		while (bit_is_clear(PIND,5));
		}

		if (led1 == 0) { LED1_high();}
		else { LED1_low();}

		if (led2 == 0) { LED2_high();}
		else { LED2_low();}

		if (led3 == 0) { LED3_high();}
		else { LED3_low();}

		if (led4 == 0) { LED4_high();}
		else { LED4_low();}
}

void grafika()
{
	OCR0_data[0] = (OCR0 / 100) + '0';
	OCR0_data[1] = ((OCR0 % 100)/10) + '0';
	OCR0_data[2] = ((OCR0 % 100)%10) + '0';

	OCR2_data[0] = ((int)OCR2 / 100) + '0';
	OCR2_data[1] = ((OCR2 % 100)/10) + '0';
	OCR2_data[2] = ((OCR2 % 100)%10) + '0';

	OCR3B_data[0] = (OCR3B / 100) + '0';
	OCR3B_data[1] = ((OCR3B % 100)/10) + '0';
	OCR3B_data[2] = ((OCR3B % 100)%10) + '0';

	OCR1B_data[0] = (OCR1B / 100) + '0';
	OCR1B_data[1] = ((OCR1B % 100)/10) + '0';
	OCR1B_data[2] = ((OCR1B % 100)%10) + '0';
}

void prijem_data()
{
	//UART_Receive();
	prijem = UART_Receive();
	if (prijem != 0)
	{
		if (prijem == 'A'){led1 = 1;prijem = UART_Receive();}
		if (prijem == 'S'){led2 = 1;prijem = UART_Receive();}
		if (prijem == 'D'){led3 = 1;prijem = UART_Receive();}
		if (prijem == 'F'){led4 = 1;prijem = UART_Receive();}
		if (prijem == 'Q'){led1 = 0;prijem = UART_Receive();}
		if (prijem == 'W'){led2 = 0;prijem = UART_Receive();}
		if (prijem == 'E'){led3 = 0;prijem = UART_Receive();}
		if (prijem == 'R'){led4 = 0;prijem = UART_Receive();}
		if (prijem == 'Y'){OCR0 = (UART_Receive() - '0');prijem = UART_Receive();}
		if (prijem == 'X'){OCR2 = UART_Receive();prijem = UART_Receive();}
		if (prijem == 'C'){OCR3B =UART_Receive();prijem = UART_Receive();}
		if (prijem == 'V'){OCR1B =UART_Receive();prijem = UART_Receive();}
	}
}
