#define F_CPU 3686000UL  //nastaveni frekvence oscilátoru
#include <avr\io.h>
#include <stdio.h>
#include <util/delay.h>
#include "ovladac.h"

const int t = 120;
const int m = 5500;
const int b = 415;


void uart0_init(void){
 
 UCSRB |=(1<<TXEN);
 UBRRL = 0x5F; // Set baud rate
 UBRRH = 0b00000000; // Set baud rate

}

void uart0_stop(void){
 
 bit_clr(UCSRB,TXEN);
 UBRRL = 0b00000000; // Set baud rate
 UBRRH = 0b00000000; // Set baud rate

}


void send(unsigned char data){
	unsigned char i;	

	for(i=0;i<7;i++){
		while ( !( UCSRA & (1<<UDRE)) )
		;
		UDR=0b10101010;		//trening sequence 170
		_delay_ms(7);
	}
	while ( !( UCSRA & (1<<UDRE)) )
		;
	UDR = data;

}

//--------------------------------------------
//Hlavní program
//--------------------------------------------
void main(void){ 
	
	unsigned char play=0,aut=0,man=1;

//	void init_peripherals(void);
	DDRC|=0b0;
	PORTC|=0xFF;
	DDRD|=0b00010111;
	PORTD|=0b11101000;
	uart0_init();
	LED_G=1;
	for (;;){
		if((!SWR)&&(man)){	//všude negativní logika pro tlačítka
			LED_R=1;
			send('r');
			LED_R=0;
		}
		if(SWL==0&&(man)){
			LED_R=1;
			send('l');
			LED_R=0;
		}
		if(SWU==0&&(man)){
			LED_R=1;
			send('u');
			LED_R=0;
		}
		if(SWD==0&&(man)){
			LED_R=1;
			send('d');
			LED_R=0;
		}
		if(SWP==0&&(man)){
			LED_R=1;
			_delay_ms(35);
			if((SWP==0)&&(play==0)){
				send('p');
				play=!play;
				_delay_ms(800);
			}
			if((SWP==0)&&(play==1)){
				send('s');
				play=!play;
				_delay_ms(500);
			}
			LED_R=0;
		}
		if(SWA==0&&(!aut)){
			send('a');	//auto mode
			aut=1;
			man=0;
			LED_Y=1;
		}
		if((SWA==1)&&(aut)){
			send('m');  //manual mode
			aut=0;
			man=1;
			LED_Y=0;
		}
	}
}
