/*
 * BackgroundServicesv1.c
 *
 * Created: 6/6/2017 12:22:13 AM
 *  Author: Matic
 */ 

#include "BackgroundServicesv1.h"

int score;
char display;
char x1_pad;
char x2_pad;
char y1_pad;
char y2_pad;
char x1_pad;
char x2_pad;
char y1_pad;
char y2_pad;
char x1_circle;
char y1_circle;
char xplus;
char yplus;
char joystick_position;
char player_nr;
char display_player;
char ci;
char cr;
char pad;
char txt[8];


void BackgroundServices()
{
	static uint32_t last_time;
	
	if (Has_X_MillisecondsPassed(10,&last_time))
	{
		/*UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_WHITE_SMOKE);
		UG_SetBackcolor(C_PURPLE);
		sprintf(txt, "X=%3d", pad);
		UG_PutString(231, 35, txt);*/
		KBD_Read();
	}
	RefreshLCD();
}

void RefreshLCD()
{
	switch (display)
	{
		case DISP_PAD1:
			LCD_DrawPad(1);
			pad=1;
			break;
		case DISP_PAD2:
			LCD_DrawPad(2);
			pad=2;
			break;
		case DISP_PAD3:
			LCD_DrawPad(3);
			pad=3;
			break;
		case DISP_PAD4:
			LCD_DrawPad(4);
			pad=4;
			break;
		case DISP_PAD5:
			LCD_DrawPad(5);
			pad=5;
			break;
		case DISP_PAD6:
			LCD_DrawPad(6);
			pad=6;
			break;
		case DISP_PAD7:
			LCD_DrawPad(7);
			pad=7;
			break;
		case DISP_PAD8:
			LCD_DrawPad(8);
			pad=8;
			break;
		case DISP_PAD9:
			LCD_DrawPad(9);
			pad=9;
			break;
		case DISP_PLAYER1:
			LCD_DrawPlayer(1);
			break;
		case DISP_PLAYER2:
			LCD_DrawPlayer(2);
			break;
		case DISP_WINNER1:
			LCD_DrawWinner(1);
			break;
		case DISP_WINNER2:
			LCD_DrawWinner(2);
			break;
		case DISP_DRAW:
			LCD_DrawDraw();
			break;
		case DISP_START_SCREEN:
			DrawStartScreen();
			break;
		case DISP_GAME_PLAY_SCREEN:
			DrawGamePlayScreen();
			break;
		case DISP_END_SCREEN:
			DrawGameOver();
			break;
		case DISP_CAL_SCREEN:
			DrawCalScreen();
			break;
		default:	//0 or unknown - do nothing
		
		break;
	}
	display=0;
}

void Refresh_position(char pos_nr)
{
 x1_pad = 10;
 y1_pad = 91;
 x2_pad = 69;
 y2_pad = 95;
 xplus=78;
 yplus=70;
 
	
	switch(pos_nr)
	{
		case 0:
			UG_FillFrame(x1_pad,y1_pad,x2_pad,y2_pad, C_BLACK);
			break;
		case 1:
			UG_FillFrame(x1_pad+xplus,y1_pad,x2_pad+xplus,y2_pad, C_BLACK);
			break;
		case 2:
			UG_FillFrame(x1_pad+2*xplus,y1_pad,x2_pad+2*xplus,y2_pad, C_BLACK);
			break;
		case 3:
			UG_FillFrame(x1_pad,y1_pad+yplus,x2_pad,y2_pad+yplus, C_BLACK);
			break;
		case 4:
			UG_FillFrame(x1_pad+xplus,y1_pad+yplus,x2_pad+xplus,y2_pad+yplus, C_BLACK);
			break;
		case 5:
			UG_FillFrame(x1_pad+2*xplus,y1_pad+yplus,x2_pad+2*xplus,y2_pad+yplus, C_BLACK);
			break;
		case 6:
			UG_FillFrame(x1_pad,y1_pad+2*yplus,x2_pad,y2_pad+2*yplus, C_BLACK);
			break;
		case 7:
			UG_FillFrame(x1_pad+xplus,y1_pad+2*yplus,x2_pad+xplus,y2_pad+2*yplus, C_BLACK);
			break;
		case 8:
			UG_FillFrame(x1_pad+2*xplus,y1_pad+2*yplus,x2_pad+2*xplus,y2_pad+2*yplus, C_BLACK);
			break;
		default:
		pos_nr=5;
			break;	
	}			
}

void Board_Flush()
{
	board[0][0]=0;
	board[1][0]=0;
	board[2][0]=0;
	board[0][1]=0;
	board[1][1]=0;
	board[2][1]=0;
	board[0][2]=0;
	board[1][2]=0;
	board[2][2]=0;
}

void LCD_DrawPad(char pad_nr)
//default je na sredini
//pri pad_nr = 1 nariši levo zgoraj
//glej katera pozicija
//prištej dolžino tolikokrat kolikokrat je pove?ana pozicija
//nariši cross glede na pozicijo
{
	
x1_pad = 10;
y1_pad = 91;
x2_pad = 68;
y2_pad = 91;
xplus=78;
yplus=70;
int i;


	switch (pad_nr)
	{
		case 1:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad, y1_pad+i, x2_pad, y2_pad+i, C_RED);
			}
			break;
		case 2:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+xplus, y1_pad+i, x2_pad+xplus, y2_pad+i, C_RED);
			}
			break;
		case 3:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+2*xplus, y1_pad+i, x2_pad+2*xplus, y2_pad+i, C_RED);
			}
			break;
		case 4:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad, y1_pad+yplus+i, x2_pad, y2_pad+yplus+i, C_RED);
			}
			break;
		case 5:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+xplus, y1_pad+yplus+i, x2_pad+xplus, y2_pad+yplus+i, C_RED);
			}
			break;
		case 6:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+2*xplus, y1_pad+yplus+i, x2_pad+2*xplus, y2_pad+yplus+i, C_RED);
			}
			break;
		case 7:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad, y1_pad+2*yplus+i, x2_pad, y2_pad+2*yplus+i, C_RED);
			}
			break;
		case 8:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+xplus, y1_pad+2*yplus+i, x2_pad+xplus, y2_pad+2*yplus+i, C_RED);
			}
			break;
		case 9:
			for (i=0;i<4;i++)
			{
				UG_DrawLine( x1_pad+2*xplus, y1_pad+2*yplus+i, x2_pad+2*xplus, y2_pad+2*yplus+i, C_RED);
			}
			break;
		default:
		pad_nr=5;
		break;
	}
}

void LCD_DrawCross(char cross_nr)
//default je na sredini
//pri cross_nr = 1 nariši levo zgoraj
//glej katera pozicija
//prištej dolžino tolikokrat kolikokrat je pove?ana pozicija
//nariši cross glede na pozicijo
{
char x1 = 10;
char y1 = 40;
char x2 = 60;
char y2 = 90;
xplus=78;
yplus=70;
int i;


	switch (cross_nr)
	{
		case 1:
		cr=1;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+i, y1, x2+i, y2, C_LAWN_GREEN );
			UG_DrawLine( x1+i, y2, x2+i, y1, C_LAWN_GREEN );
		}
		break;
		
		case 2:
		cr=2;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+xplus+i, y1, x2+xplus+i, y2, C_LAWN_GREEN );
			UG_DrawLine( x1+xplus+i, y2, x2+xplus+i, y1, C_LAWN_GREEN );
		}
		break;
		
		case 3:
		cr=3;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+2*xplus+i, y1, x2+2*xplus+i, y2, C_LAWN_GREEN );
			UG_DrawLine( x1+2*xplus+i, y2, x2+2*xplus+i, y1, C_LAWN_GREEN );
		}
		break;
		
		case 4:
		cr=4;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+i, y1+yplus, x2+i, y2+yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+i, y2+yplus, x2+i, y1+yplus, C_LAWN_GREEN );
		}
		break;
		
		case 5:
		cr=5;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+xplus+i, y1+yplus, x2+xplus+i, y2+yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+xplus+i, y2+yplus, x2+xplus+i, y1+yplus, C_LAWN_GREEN );
		}
		break;
		
		case 6:
		cr=6;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+2*xplus+i, y1+yplus, x2+2*xplus+i, y2+yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+2*xplus+i, y2+yplus, x2+2*xplus+i, y1+yplus, C_LAWN_GREEN );
		}
		break;
		
		case 7:
		cr=7;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+i, y1+2*yplus, x2+i, y2+2*yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+i, y2+2*yplus, x2+i, y1+2*yplus, C_LAWN_GREEN );
		}
		break;
		
		case 8:
		cr=8;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+xplus+i, y1+2*yplus, x2+xplus+i, y2+2*yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+xplus+i, y2+2*yplus, x2+xplus+i, y1+2*yplus, C_LAWN_GREEN );
		}
		break;
		
		case 9:
		cr=9;
		for (i=0;i<9;i++)
		{
			UG_DrawLine( x1+2*xplus+i, y1+2*yplus, x2+2*xplus+i, y2+2*yplus, C_LAWN_GREEN );
			UG_DrawLine( x1+2*xplus+i, y2+2*yplus, x2+2*xplus+i, y1+2*yplus, C_LAWN_GREEN );
		}
		break;
		
		default:
		break;
	}
}

void LCD_DrawCircle(char circle_nr)
//default je na sredini
//pri circle_nr = 0 nariši levo zgoraj
//glej katera pozicija
//prištej dolžino tolikokrat kolikokrat je pove?ana pozicija
//nariši circle glede na pozicijo
{
x1_circle=39;
y1_circle=63;
xplus=78;
yplus=70;
int i;


	switch (circle_nr)
	{
	
		case 1:
		ci=1;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle, y1_circle, 20+i, C_DODGER_BLUE );
		}
		break;
		case 2:
		ci=2;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+xplus, y1_circle, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 3:
		ci=3;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+2*xplus, y1_circle, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 4:
		ci=4;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle, y1_circle+yplus, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 5:
		ci=5;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+xplus, y1_circle+yplus, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 6:
		ci=6;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+2*xplus, y1_circle+yplus, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 7:
		ci=7;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle, y1_circle+2*yplus, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 8:
		ci=8;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+xplus, y1_circle+2*yplus, 20+i, C_DODGER_BLUE );
		}
		break;
	
		case 9:
		ci=9;
		for (i=0;i<7;i++)
		{
			UG_DrawCircle( x1_circle+2*xplus, y1_circle+2*yplus, 20+i, C_DODGER_BLUE );
		}
		break;
		
		default:
		break;
	}
}

void LCD_DrawPlayer(char draw_player)
{
	switch (draw_player)
	{
		
		case 1:
			UG_FontSelect(&RFONT_8X12);
			UG_SetForecolor(C_DODGER_BLUE);
			UG_SetBackcolor(C_BLACK);
			UG_PutString(5,5,"Player1 turn");
			break;
			
		case 2:
			UG_FontSelect(&RFONT_8X12);
			UG_SetForecolor(C_LAWN_GREEN);
			UG_SetBackcolor(C_BLACK);
			UG_PutString(211,5,"Player2 turn");
			break;
			
		default:
			draw_player=1;
	}
}

void LCD_DrawWarning()
{
	UG_FontSelect(&RFONT_8X12);
	UG_SetForecolor(C_RED);
	UG_SetBackcolor(C_BLACK);
	UG_PutString(15,22,"Position already taken!");
}

void LCD_DrawWinner(char draw_winner)
{
	switch(draw_winner)
	{
	case 1:
		UG_FillFrame(0,0,320,240, C_DODGER_BLUE);
		UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_WHITE);
		UG_SetBackcolor(C_DODGER_BLUE); 
		UG_PutString(69,40,"PLAYER1 WON");
		
		_delay_ms(1000);
		UG_DrawFrame( 15, 85, 298, 159, C_BLACK );
		UG_DrawFrame( 14, 84, 299, 160, C_BLACK );
		UG_DrawFrame( 13, 83, 300, 161, C_BLACK );
		
		UG_FillFrame(16,86,298,159, C_WHITE);
		UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_RED);
		UG_SetBackcolor(C_WHITE);
		UG_PutString(22,90,"PLAYER1  PLAYER2");
		sprintf(txt, "%d", win1);
		UG_PutString(70,130, txt);
		UG_PutString(148,130, ":");
		sprintf(txt, "%d", win2);
		UG_PutString(222,130, txt);
		
		_delay_ms(2000);
		UG_FontSelect(&RFONT_8X12);
		UG_SetForecolor(C_BLACK);
		UG_SetBackcolor(C_DODGER_BLUE);
		UG_PutString(81,225,"Press ESC to restart");
		break;
				
	case 2:
		UG_FillFrame(0,0,320,240, C_LAWN_GREEN);
		UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_WHITE);
		UG_SetBackcolor(C_LAWN_GREEN);
		UG_PutString(69,40,"PLAYER2 WON");
		
		_delay_ms(1000);
		UG_DrawFrame( 15, 85, 298, 159, C_BLACK );
		UG_DrawFrame( 14, 84, 299, 160, C_BLACK );
		UG_DrawFrame( 13, 83, 300, 161, C_BLACK );
		
		UG_FillFrame(16,86,298,159, C_WHITE);
		UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_RED);
		UG_SetBackcolor(C_WHITE);
		UG_PutString(22,90,"PLAYER1  PLAYER2");
		sprintf(txt, "%d", win1);
		UG_PutString(70,130, txt);
		UG_PutString(148,130, ":");
		sprintf(txt, "%d", win2);
		UG_PutString(222,130, txt);
		
		_delay_ms(2000);
		UG_FontSelect(&RFONT_8X12);
		UG_SetForecolor(C_BLACK);
		UG_SetBackcolor(C_LAWN_GREEN);
		UG_PutString(81,225,"Press ESC to restart");
		break;
		
	default:
	draw_winner=1;
	break;
	}
}

void LCD_DrawDraw()
{
	UG_FillFrame(0,0,320,240, C_TURQUOISE);
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_BLACK);
	UG_SetBackcolor(C_TURQUOISE);
	UG_PutString(59,40,"IT IS A DRAW");
	
	_delay_ms(1000);
	UG_DrawFrame( 15, 85, 298, 159, C_BLACK );
	UG_DrawFrame( 14, 84, 299, 160, C_BLACK );
	UG_DrawFrame( 13, 83, 300, 161, C_BLACK );
	
	UG_FillFrame(16,86,298,159, C_WHITE);
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_RED);
	UG_SetBackcolor(C_WHITE);
	UG_PutString(22,90,"PLAYER1  PLAYER2");
	sprintf(txt, "%d", win1);
	UG_PutString(70,130, txt);
	UG_PutString(148,130, ":");
	sprintf(txt, "%d", win2);
	UG_PutString(222,130, txt);
	
	_delay_ms(2000);
	UG_FontSelect(&RFONT_8X12);
	UG_SetForecolor(C_BLACK);
	UG_SetBackcolor(C_TURQUOISE);
	UG_PutString(81,225,"Press ESC to restart");
}

void DrawStartScreen()
{	//&RFONT_8X12 &RFONT_16X26 &RFONT_22X36
	UG_FillFrame(0,0,106,240,C_RED);
	UG_FillFrame(214,0,106,240,C_DARK_KHAKI);
	UG_FillFrame(320,0,214,240,C_LIME_GREEN);
	UG_FillFrame(243,200,77,120,C_BLACK);
	
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_WHITE);
	UG_SetBackcolor(C_RED);
	UG_PutString(26,40,"TIC");
	
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_WHITE);
	UG_SetBackcolor(C_DARK_KHAKI);
	UG_PutString(133,40,"TAC");
	
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_WHITE);
	UG_SetBackcolor(C_LIME_GREEN);
	UG_PutString(241,40,"TOE");

	UG_FillFrame(243+30,237,77-30,208,C_BLACK);
	
	UG_FontSelect(&RFONT_8X12);
	UG_SetForecolor(C_WHITE);
	UG_SetBackcolor(C_BLACK);
	UG_PutString(83-30,209,"OK  - start");
	UG_PutString(83-30,223,"ESC - calibrate Joystick");	
}

void DrawGamePlayScreen()
{	//&RFONT_8X12 &RFONT_16X26
	
	//BACKGROUND
	UG_FillFrame(0,0,320,240,C_BLACK);
	
	//BOARD
	UG_DrawLine( 10, 95, 229, 95, C_WHITE );
	UG_DrawLine( 10, 96, 229, 96, C_WHITE );
	UG_DrawLine( 10, 97, 229, 97, C_WHITE );
	UG_DrawLine( 10, 98, 229, 98, C_WHITE );
	UG_DrawLine( 10, 99, 229, 99, C_WHITE );
	UG_DrawLine( 10, 100, 229, 100, C_WHITE );
	UG_DrawLine( 10, 101, 229, 101, C_WHITE );
	UG_DrawLine( 10, 102, 229, 102, C_WHITE );
	UG_DrawLine( 10, 103, 229, 103, C_WHITE );
	UG_DrawLine( 10, 104, 229, 104, C_WHITE );
	
	UG_DrawLine( 10, 165, 229, 165, C_WHITE );
	UG_DrawLine( 10, 166, 229, 166, C_WHITE );
	UG_DrawLine( 10, 167, 229, 167, C_WHITE );
	UG_DrawLine( 10, 168, 229, 168, C_WHITE );
	UG_DrawLine( 10, 169, 229, 169, C_WHITE );
	UG_DrawLine( 10, 170, 229, 170, C_WHITE );
	UG_DrawLine( 10, 171, 229, 171, C_WHITE );
	UG_DrawLine( 10, 172, 229, 172, C_WHITE );
	UG_DrawLine( 10, 173, 229, 173, C_WHITE );
	UG_DrawLine( 10, 174, 229, 174, C_WHITE );
	
	UG_DrawLine( 73, 40, 73, 230, C_WHITE );
	UG_DrawLine( 74, 40, 74, 230, C_WHITE );
	UG_DrawLine( 75, 40, 75, 230, C_WHITE );
	UG_DrawLine( 76, 40, 76, 230, C_WHITE );
	UG_DrawLine( 77, 40, 77, 230, C_WHITE );
	UG_DrawLine( 78, 40, 78, 230, C_WHITE );
	UG_DrawLine( 79, 40, 79, 230, C_WHITE );
	UG_DrawLine( 80, 40, 80, 230, C_WHITE );
	UG_DrawLine( 81, 40, 81, 230, C_WHITE );
	UG_DrawLine( 82, 40, 82, 230, C_WHITE );
	
	UG_DrawLine( 151, 40, 151, 230, C_WHITE );
	UG_DrawLine( 152, 40, 152, 230, C_WHITE );
	UG_DrawLine( 153, 40, 153, 230, C_WHITE );
	UG_DrawLine( 154, 40, 154, 230, C_WHITE );
	UG_DrawLine( 155, 40, 155, 230, C_WHITE );
	UG_DrawLine( 156, 40, 156, 230, C_WHITE );
	UG_DrawLine( 157, 40, 157, 230, C_WHITE );
	UG_DrawLine( 158, 40, 158, 230, C_WHITE );
	UG_DrawLine( 159, 40, 159, 230, C_WHITE );
	UG_DrawLine( 160, 40, 160, 230, C_WHITE );
}

void DrawGameOver()
{	//&RFONT_8X12 &RFONT_16X26
	UG_FillFrame(0,0,320,240, C_WHITE);
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_TURQUOISE);
	UG_SetBackcolor(C_WHITE);
	UG_PutString(90,100,"GAME OVER");
	//_delay_ms(1500);
}

void DrawCalScreen()
{
	UG_FillFrame(0,0,320,240, C_BLACK);
	UG_FontSelect(&RFONT_8X12);
	UG_SetForecolor(C_WHITE);
	UG_SetBackcolor(C_BLACK);
	UG_PutString(5,5,"Calibrate Joystick");
	UG_PutString(5,240-12*2,"press any");
	UG_PutString(5,240-12,"key to save");
	UG_PutString(5,108,"xmin:");
	UG_PutString(270,108,"xMax:");
	UG_PutString(160-40,17,"ymin:");
	UG_PutString(160-40,240-12,"yMax:");
	UG_DrawCircle(160,120+8,MAX_R,C_CYAN);
}

void Init_IO()
{
	//DDR: 1-out, 0-in
	PORTB = 0x12;	//0-LCD_DC, 1-buzzer, 2-LCD_CS,
	DDRB = 0x2D;	//3-MOSI, 4-MISO, 5-SCK, 6,7-not implemented
	
	PORTC = 0xF0;	//0..3-analog inputs
	DDRC = 0x00;	//4-SDA(not connected), 5-SCL(not connected), 6,7-not implemented
	
	PORTD = 0xBF;	//0-Rx, 1-Tx, 2..5-buttons
	DDRD = 0xC2;	//6-LCD_BACKLIGHT, 7-LCD_RESET
	
	PORTE = 0x00;	//LEDs
	DDRE = 0xFF;
}

void AD_Init()
{
	ADMUX = (1<<REFS0) | (2<<MUX0); //0x4f;
	ADCSRA = (1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (7<<ADPS0); //0x87;
	ADCSRB = 0;
	DIDR0 = (1<<2) | (1<<3);
}

int AD_convert(char channel)
{
	ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);//nastavi kanal
	_delay_us(20);
	ADCSRA |= (1<<ADSC);//poženi pretvorbo
	while (ADCSRA & (1<<ADSC)) {/*čakaj*/}//počakaj da je pretvorbe konec
	//  while (!(ADCSRA & (1<<ADIF)) {}
	//  ADCSRA |= (1<<ADIF);

	return ADC; //vrni rezultat
}