#include <avr/io.h>
#include "lcd_ch.h"
#include <avr/interrupt.h>
#include <util/delay.h>


#define SW1  PD0	//zabudovaný switch
#define SW2  PD2
#define LED1  PD6	//zadudovane diody cervena
#define LED2  PD7	//zlta
#define LED3  PD5	//zelena


#define LCD_CTRL_PORT DDRD	//LCD define
#define LCD_DATA_PORT DDRB

#define LCD_RS_pin 2
#define LCD_RW_pin 3
#define LCD_EN_pin 4

#define LCD_D4_pin 1
#define LCD_D5_pin 2
#define LCD_D6_pin 3
#define LCD_D7_pin 4		//LCD define2

#define LCD_CLR               0      /* DB0: clear display                  */
#define LCD_HOME              1      /* DB1: return to home position        */



int main(void) 
{  
	 DDRD = 0b00100000;       // PORTB: LED1 na PB5  je output, SW1 (PB4) input
   PORTD = 0b00010000;       // LED Active low, LED off, pull-up ON
	
	
	DDRD |= (1 << LED1);
	  DDRD |= (1 << LED2);
	  DDRD |= (1 << LED3);
	// LCD display
	
	DDRD |= (1<<LCD_EN_pin);	// Pin D4 (Enable)  PORTD  output  
	DDRD |= (1<<LCD_RW_pin);	// Pin D3 (RW)      PORTD  output  
	DDRD |= (1<<LCD_RS_pin);	// Pin D2 (RS)      PORTD  output  
  
	
	
	

	   LCD_DATA_PORT |= (1<<LCD_D4_pin)|(1<<LCD_D5_pin)|(1<<LCD_D6_pin)|(1<<LCD_D7_pin);	// Piny 1,2,3,4, PORTB ako output (Data pre display)
		lcd_init();  // definiciu pozri v -> lcd_ch.h resp. lcd_ch.c
	lcd_puts("Stlac tlacitka,");
	lcd_command(0xC0);
	lcd_puts("aby sa hra zacala");
	
		
    while(1)   /* stop here */   
   { 
                // 0b00010000
     if ( !(PIND & (1<<SW1)) & !(PIND & (1<<SW2)) )    // ZAPNUTIE PROGRAMU
       {  	
		  lcd_init();									
		lcd_puts("Spustam semafor");
		   								
	   PORTD |=  (1<<LED1);								//SEMAFOR 
		_delay_ms(1000);
		
		 PORTD &= ~(1<<LED1); 			
	   PORTD |=  (1<<LED2); 
		_delay_ms(1000);

			PORTD &= ~(1<<LED2);
	   PORTD |=  (1<<LED3); 
	   _delay_ms(1000);
	    PORTD &= ~(1<<LED3); 
		
		lcd_init();									//ODPOCITAVANIE 10 SEC.
		lcd_puts("Odpocitavaj 10 sek.");
		lcd_command(0xC0 + 1);
	lcd_puts("stlac tlacitko ");		
	   
	   
	   _delay_ms(1000);
	   
	   int cas=0;   // premenná, ktorej hodnotu by sme chceli zobrazi? na LCD
	
		 while ((PIND & (1<<SW1))) {
        _delay_ms(100);
        cas++;
		 }   
   
   if (cas >= 90 && cas <= 110) 
   {		
		lcd_command(1<<LCD_CLR);   /* Clear screen */
 lcd_command(1<<LCD_HOME);  /* Set cursor to home position */
		 char riadok[]= {"                "};
		sprintf(riadok,"Vyhral si"); 
lcd_puts(riadok);
		}		
		
		else 
		{
			lcd_init();									
		lcd_puts("nevyhral si");
		}
	
		 }	
	   
	   		
		

	
		}} 
		   	   
   
	    	
       /* end of while */
