/*
 * TicTacToe_project.c
 *
 * Created: 5/20/2017 11:06:55 PM
 * Author : Matic
 */ 

#include <avr/io.h>
#include <avr/eeprom.h>
#include "ugui.h"
#include "ugui_config.h"
#include "LCD_Ili9341.h"
#include "systime.h"
#include "kbd.h"
#include "BackgroundServicesv1.h"
#include "LED.h"
#include <util/delay.h>
#include <avr/interrupt.h>
#include <util/atomic.h>

#define SIN_SAMPLE_LENGTH	39
// sin((pi*x/2) - (pi/2))*neki + neki
uint8_t brightnessKrozc=0;
uint8_t brightnessKrizc=0;

volatile uint16_t sinOutKrozc[] = {0, 0, 0, 1, 2, 4, 6, 8, 10, 13, 15, 17, 20, 22, 24, 26, 28, 29, 30, 30, 31, 30, 30, 29, 28, 26, 24, 22, 20, 17, 15, 13, 10, 8, 6, 4, 2, 1, 0, 0};
volatile uint16_t sinOutKrizc[] = {0, 0, 32, 96, 192, 288, 384, 544, 672, 832, 992, 1152, 1312, 1440, 1600, 1696, 1792, 1888, 1952, 1984, 2016, 1984, 1952, 1888, 1792, 1696, 1600, 1440, 1312, 1152, 992, 832, 672, 544, 384, 288, 192, 96, 32, 0};

struct channelData
{
	uint8_t channelNum;
	uint16_t currentValue;
	uint16_t minValue;
	uint16_t maxValue;
}ch2, ch3;

void Init_IO();
void Init_T0();
void AD_Init();
void Game();
int WaitForStart();
int PlayTheGame();
int EndOfGame();
int GetPlayerNr();
int getXPercentage();
int getYPercentage();
int CheckForDraw();
int CheckForCombo();
uint32_t starttime;
uint16_t pos_xpercent;
uint16_t pos_ypercent;
int rezultat_x;
int rezultat_y;
int x_max=0, y_max=0;
int x_min;
int y_min;
char txt[8];
char player_nr;
volatile int xpercent;
volatile int ypercent;
int position;
int pos_cr;
int pos_c;
int state1;
char p_position;
int winner_nr;
int turn;
int x;
int y;
int key1;
int channelpercentage;
int win1=0;
int win2=0;
int cnt=0;
uint32_t zaznamekCasa;

// Calibration
void readChannel(struct channelData *channel);
int getChannelPercentage(struct channelData *channel);
uint8_t isNewMax(uint16_t value, uint16_t newMax);
uint8_t isNewMin(uint16_t value, uint16_t newMin);
void resetMinValue(struct channelData *channel);
void resetMaxValue(struct channelData *channel);

uint8_t searchMinMax = 1;

volatile uint32_t systick = 0;

int pad_pos;
int character;

char board[3][3] =  //0 - prazno, 1 - krozec(P1), 2 - krizec(P2)
{
	{0, 0, 0},
	{0, 0, 0},
	{0, 0, 0}
};


int CalibrateJoystick()
{
	static int xm,xM,ym,yM;
	static int xold,yold;
	static int state=0;
	int x,y,r,xr,yr,result=0;

	switch (state)
	{
		case 0:
			display = DISP_CAL_SCREEN;
			xm=1023;
			xM=0;
			ym=1023;
			yM=0;
			xold=-1;
			yold=-1;
			state=1;
			break;
		case 1:
			UG_FontSelect ( &RFONT_8X12 ) ;
			UG_SetForecolor ( ILI9341_GREEN ) ;
			xr=AD_convert(3);
			yr=AD_convert(2);
			//get extremes
			if (xr<xm) 
			{
				xm=xr;
				sprintf(txt,"%03d",xm);
				UG_PutString(10,120,txt);
			}
			if (xr>xM) 
			{
				xM=xr;
				sprintf(txt,"%03d",xM);
				UG_PutString(275,120,txt);
			}
			if (yr<ym) 
			{
				ym=yr;
				sprintf(txt,"%03d",ym);
				UG_PutString(163,17,txt);
			}
			if (yr>yM) 
			{
				yM=yr;
				sprintf(txt,"%03d",yM);
				UG_PutString(163,240-12,txt);
			}
		
			//calculate position
			x=((long)((xr-(xm+xM)/2))*MAX_R*2) / (xM-xm)+CENTERX;
			y=((long)((yr-(ym+yM)/2))*MAX_R*2) / (yM-ym)+CENTERY;
			
			if ((x!=xold) || (y!=yold))
			{
				UG_FontSelect ( &RFONT_8X12 ) ;
				UG_FillCircle(xold,yold,BALL_R,ILI9341_BLACK);
				r=sqrtf((long)(xold-CENTERX)*(xold-CENTERX)+(long)(yold-CENTERY)*(yold-CENTERY));
				if ((r>=MAX_R) && (r<=(MAX_R+2*BALL_R+2)))
				{
					UG_DrawCircle(CENTERX,CENTERY,MAX_R,C_CYAN);
				}
				sprintf(txt,"r=%03d",r);
				UG_PutString(250,17,txt);
				UG_FontSelect ( &RFONT_16X26 ) ;
				UG_FillCircle(x,y,BALL_R,ILI9341_CYAN);
				xold=x;
				yold=y;
			}
		
			if (KBD_GetKey() != 0)
			{
				eeprom_busy_wait();
				eeprom_write_word((uint16_t*)72,yM);
				eeprom_busy_wait();
				eeprom_write_word((uint16_t*)74,ym);
				eeprom_busy_wait();
				eeprom_write_word((uint16_t*)76,xM);
				eeprom_busy_wait();
				eeprom_write_word((uint16_t*)78,xm);
				state=0;
				result = 1;
			}
			break;
	}
	return result;
}

int main(void)
{
	x_max=0, y_max=0;
	x_min = 850;
	y_min = 850;
	Init_IO();
	Init_T0();
	LCD_Init();
	Systime_Init();
	AD_Init();
	sei();
	
	ch2.channelNum = 2;
	ch3.channelNum = 3;
	resetMinValue(&ch2);
	resetMinValue(&ch3);
	
	starttime=GetSysTick();
	UG_FontSelect(&RFONT_5X8);
	
    while (1)
    {
		// Read Channels
		readChannel(&ch2);
		readChannel(&ch3);
		
		/*if(Has_X_MillisecondsPassed(100, &starttime))
		{
			rezultat_y = AD_convert(2);
			if(rezultat_y > y_max)
			{
				y_max = rezultat_y;
			}
			if(rezultat_y < y_min)
			{
				y_min = rezultat_y;
			}
			ypercent = 100 - (float)(rezultat_y-y_min)*100/(y_max - y_min);
		
			rezultat_x = AD_convert(3);
			if(rezultat_x > x_max)
			{
				x_max = rezultat_x;
			}
			if(rezultat_x < x_min)
			{
				x_min = rezultat_x;
			}
			xpercent = (float)(rezultat_x-x_min)*100/(x_max-x_min);
			/ *sprintf(txt, "X=%3d", xpercent);
			UG_PutString(231, 175, txt);
			sprintf(txt, "Y=%3d", ypercent);
			UG_PutString(231, 200, txt);* /
		}*/
		
		if (HasOneMillisecondPassed())
		{
			Game();  //se zgodi usako ms
			
			/*UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "S2=%3d", state2);
			UG_PutString(231, 137, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "S1=%3d", state1);
			UG_PutString(231, 146, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "S3=%3d", state3);
			UG_PutString(231, 155, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "S3=%3d", key1);
			UG_PutString(275, 65, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "Z=%4u", zaznamekCasa);
			UG_PutString(265, 74, txt);*/
		}
		BackgroundServices(); //bere tipke, išče po displayu
    }
	cli();
}

void readChannel(struct channelData *channel)
{
	channel->currentValue = AD_convert(channel->channelNum);
	if(searchMinMax)
	{
		if(isNewMax(channel->currentValue, channel->maxValue))channel->maxValue = channel->currentValue;
		if(isNewMin(channel->currentValue, channel->minValue))channel->minValue = channel->currentValue;
	}
}

int getChannelPercentage(struct channelData *channel)
{
	if(channel == &ch3)
	{
		channelpercentage = ((100*(float)(channel->currentValue - channel->minValue))/(channel->maxValue - channel->minValue));
	}
	if(channel == &ch2)
	{
		channelpercentage = (100 - ((100*(float)(channel->currentValue - channel->minValue))/(channel->maxValue - channel->minValue)));
	}
	
	return channelpercentage;
}

uint8_t isNewMax(uint16_t value, uint16_t newMax)
{
	if(value > newMax)return 1;
	return 0;
}

uint8_t isNewMin(uint16_t value, uint16_t newMin)
{
	if(value < newMin)return 1;
	return 0;
}

void resetMinValue(struct channelData *channel)
{
	channel->minValue = 0;
	channel->minValue--;
}

void resetMaxValue(struct channelData *channel)
{
	channel->maxValue = 0;
}

void Game()
{
	static int state; 
	int result;

	switch (state)
	{
		case 1:
		result = WaitForStart();
		if (result == 1) state=20;
		if (result == 2) state=21;
		break;
		case 20:
		result = PlayTheGame();
		if (result == 1) state=3;
		break;
		case 21:
		result = CalibrateJoystick();
		if (result == 1) state=1;
		break;
		case 3:
		result = EndOfGame();
		if (result == 1) state=0;
		break;
		default: //0 or unknown value; reset the game
		state = 1;
		break;
	}
}

int WaitForStart()
{
	static int state; 
	int result=0;
	winner_nr=0;
	zaznamekCasa=0;
	volatile uint8_t color_krozc;
	volatile uint16_t color_krizc;
	char key; 
	
	
	// Flashing startscreen
	cnt++;
	if(cnt >= 4)
	{
		if(brightnessKrozc >= SIN_SAMPLE_LENGTH)
		{
			brightnessKrozc = 0;
			
		}
		else
		{
			brightnessKrozc++;
		}
		
		if(brightnessKrizc >= SIN_SAMPLE_LENGTH)
		{
			brightnessKrizc = 0;
			
		}
		else
		{
			brightnessKrizc++;
		}
		
		color_krozc = sinOutKrozc[brightnessKrozc];
		color_krizc = sinOutKrizc[brightnessKrizc];
		
		// Krožec
		UG_DrawCircle( 199, 160, 23, color_krozc );
		UG_DrawCircle( 199, 160, 24, color_krozc );
		UG_DrawCircle( 199, 160, 25, color_krozc );
		UG_DrawCircle( 199, 160, 26, color_krozc );
		UG_DrawCircle( 199, 160, 27, color_krozc );
		UG_DrawCircle( 199, 160, 28, color_krozc );
		UG_DrawCircle( 199, 160, 29, color_krozc );
		
		// Križec
		UG_DrawLine( 90, 135, 140, 185, color_krizc );
		UG_DrawLine( 89, 135, 139, 185, color_krizc );
		UG_DrawLine( 91, 135, 141, 185, color_krizc );
		UG_DrawLine( 88, 135, 138, 185, color_krizc );
		UG_DrawLine( 92, 135, 142, 185, color_krizc );
		UG_DrawLine( 87, 135, 137, 185, color_krizc );
		UG_DrawLine( 93, 135, 143, 185, color_krizc );
		UG_DrawLine( 86, 135, 136, 185, color_krizc );
		UG_DrawLine( 94, 135, 144, 185, color_krizc );
		
		UG_DrawLine( 90, 185, 140, 135, color_krizc );
		UG_DrawLine( 89, 185, 139, 135, color_krizc );
		UG_DrawLine( 91, 185, 141, 135, color_krizc );
		UG_DrawLine( 88, 185, 138, 135, color_krizc );
		UG_DrawLine( 92, 185, 142, 135, color_krizc );
		UG_DrawLine( 87, 185, 137, 135, color_krizc );
		UG_DrawLine( 93, 185, 143, 135, color_krizc );
		UG_DrawLine( 86, 185, 136, 135, color_krizc );
		UG_DrawLine( 94, 185, 144, 135, color_krizc );
		
		/*UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_WHITE_SMOKE);
		UG_SetBackcolor(C_PURPLE);
		sprintf(txt, "D=%4d", color_krozc);
		UG_PutString(170, 83, txt);
		
		UG_FontSelect(&RFONT_16X26);
		UG_SetForecolor(C_WHITE_SMOKE);
		UG_SetBackcolor(C_PURPLE);
		sprintf(txt, "L=%4d", color_krizc);
		UG_PutString(10, 83, txt);*/
		
		cnt = 0;
	}
	
	switch (state)
	{
		case 0 :  
			display = DISP_START_SCREEN;
			KBD_flush();
			state++;
			break;
		
		case 1 :
			key = KBD_GetKey();
			if (key == BTN_OK)
			{
				state=0;
				result=1;
				break;
			} 
			else if (key == BTN_ESC)
			{
				state=0;
				result=2;
				break;
			}
			break;
		
		default : //konec - resetiraj avtomat stanj
			result = 1; 
			state = 0;
			break;
	}
	return result;
}

int PlayTheGame()
{
	uint16_t pos_xpercent;
	uint16_t pos_ypercent;
	int result=0;
	char key;

	
	switch (state1)
	{
		case 0 :
			winner_nr = 0;
			Board_Flush();
			display = DISP_GAME_PLAY_SCREEN;
			pad_pos=5;
			state1++;
			break;
		
		case 1 :  // Določi playerja
			player_nr = GetPlayerNr();
			
			/*UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "P=%3d", pad_pos);
			UG_PutString(231, 65, txt);*/
			
			/*UG_FontSelect(&RFONT_8X12);
			UG_SetForecolor(C_DODGER_BLUE);
			UG_SetBackcolor(C_BLACK);
			UG_PutString(211,5,"Player1 turn");
			
			UG_FontSelect(&RFONT_8X12);
			UG_SetForecolor(C_RED);
			UG_SetBackcolor(C_BLACK);
			UG_PutString(/ *161* /18,25,"Position already taken!");*/
	
			/*UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "G=%3d", player_nr);
			UG_PutString(231, 74, txt);*/
			
			// Preverjaj playerja
			if(player_nr==1)
			{
				UG_FillFrame(208,3,319,18,C_BLACK);
				display = DISP_PLAYER1;
				state1++;
				break;
			}
			if(player_nr==2)
			{
				UG_FillFrame(1,5,150,18,C_BLACK);
				display = DISP_PLAYER2;
				state1++;
				break;
			}
			break;
			
			
		case 2:
		
			eeprom_busy_wait();
			ch2.maxValue = eeprom_read_word((uint16_t*)72);
			eeprom_busy_wait();
			ch2.minValue = eeprom_read_word((uint16_t*)74);
			eeprom_busy_wait();
			ch3.maxValue = eeprom_read_word((uint16_t*)76);
			eeprom_busy_wait();
			ch3.minValue = eeprom_read_word((uint16_t*)78);
			
			pos_xpercent = getChannelPercentage(&ch3);
			pos_ypercent = getChannelPercentage(&ch2);
			
			/*sprintf(txt, "X=%3d", pos_xpercent);
			UG_PutString(231, 175, txt);
			sprintf(txt, "Y=%3d", pos_ypercent);
			UG_PutString(231, 200, txt);*/
			
			
		switch (pad_pos)
		{
			case 10:
			key=KBD_GetKey();

			_delay_ms(120);
			if(pad==1) pad_pos=1;
			if(pad==2) pad_pos=2;
			if(pad==3) pad_pos=3;
			if(pad==4) pad_pos=4;
			if((pad==5) || (pad==0)) pad_pos=5;
			if(pad==6) pad_pos=6;
			if(pad==7) pad_pos=7;
			if(pad==8) pad_pos=8;
			if(pad==9) pad_pos=9;
			break;


			case 1:
					 key=KBD_GetKey();
					 Refresh_position(3);
					 Refresh_position(1);
					
					_delay_ms(150);
					display=DISP_PAD1;
					
				
				if(pos_xpercent>60)
				{
					pad_pos=2;
					break;
				}
				if(pos_ypercent<42)
				{
					pad_pos=4;
					break;
				}
				if(key==BTN_A) 
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK); // Erases WARNING if any
					break;
				}
				break;
	
				
			case 2:
					 key=KBD_GetKey();
					 Refresh_position(0);
					 Refresh_position(2);
					 Refresh_position(4);		
			
					_delay_ms(150);
					display=DISP_PAD2;
					
	
				if(pos_xpercent>60)
				{
					pad_pos=3;
					break;
				}
				if(pos_ypercent<42)
				{
					pad_pos=5;
					break;
				}
				if(pos_xpercent<35)
				{
					pad_pos=1;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK); 
					break;
				}
				break;
	
			
			case 3:
					 key=KBD_GetKey();
					 Refresh_position(1);
					 Refresh_position(5);
				
					_delay_ms(150);
					display=DISP_PAD3;
					
	
				if(pos_xpercent<35)
				{
					pad_pos=2;
					break;
				}
				if(pos_ypercent<42)
				{
					pad_pos=6;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 4:
					 key=KBD_GetKey();
					 Refresh_position(0);
					 Refresh_position(6);
					 Refresh_position(4);
			
					_delay_ms(150);
					display=DISP_PAD4;
					
				
				if(pos_xpercent>60)
				{
					pad_pos=5;
					break;
				}
				if(pos_ypercent<42)
				{
					pad_pos=7;
					break;
				}
				if(pos_ypercent>63)
				{
					pad_pos=1;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 5:
					 key=KBD_GetKey();
					 Refresh_position(1);
					 Refresh_position(3);
					 Refresh_position(5);
					 Refresh_position(7);
			
					_delay_ms(150);
					display=DISP_PAD5;
					
					
				if(pos_xpercent>60)
				{
					pad_pos=6;
					break;
				}
				if(pos_ypercent<42)
				{
					pad_pos=8;
					break;
				}
				if(pos_xpercent<35)
				{
					pad_pos=4;
					break;
				}
				if(pos_ypercent>63)
				{
					pad_pos=2;
					break;
				}
				if(key==BTN_A)
				{
				state1=3;
				UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 6:
					 key=KBD_GetKey();
					 Refresh_position(8);
					 Refresh_position(2);
					 Refresh_position(4);
			
					_delay_ms(150);
					display=DISP_PAD6;
					
	
				if(pos_ypercent<42)
				{
					pad_pos=9;
					break;
				}
				if(pos_xpercent<35)
				{
					pad_pos=5;
					break;
				}
				if(pos_ypercent>63)
				{
					pad_pos=3;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 7:
					key=KBD_GetKey();
					Refresh_position(3);
					Refresh_position(7);
					
					_delay_ms(150);
					display=DISP_PAD7;
					
	
				if(pos_xpercent>60)
				{
					pad_pos=8;
					break;
				}
				if(pos_ypercent>63)
				{
					pad_pos=4;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 8:
					 key=KBD_GetKey();
					 Refresh_position(8);
					 Refresh_position(6);
					 Refresh_position(4);
			
					_delay_ms(150);
					display=DISP_PAD8;
					
	
				if(pos_ypercent>63)
				{
					pad_pos=5;
					break;
				}
				if(pos_xpercent<35)
				{
					pad_pos=7;
					break;
				}
				if(pos_xpercent>60)
				{
					pad_pos=9;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				
			case 9:
					 key=KBD_GetKey();
					 Refresh_position(7);
					 Refresh_position(5);
					
			
					_delay_ms(150);
					display=DISP_PAD9;
					
					
				if(pos_ypercent>63)
				{
					pad_pos=6;
					break;
				}
				if(pos_xpercent<35)
				{
					pad_pos=8;
					break;
				}
				if(key==BTN_A)
				{
					state1=3;
					UG_FillFrame(14,19,228,35,C_BLACK);
					break;
				}
				break;
	
				

			default:
			pad_pos=10;
			break;
		}
		break;
			
		
		case 3 :  //Preverja ?e je mesto prosto
	
	
		if(pad==1)
		{
			//_delay_ms(100);
			if(board[0][0]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE1;
					board[0][0]=1;
				}
					
				if(player_nr==2) 
				{
					DISP_CROSS1;
					board[0][0]=2;
				}
					
				state1=4;
				break;
			}
		
			if((board[0][0]==1) || (board[0][0]==2))
			{
				DISP_WARNING1; //(position already taken)
				state1=2;
				break;
			}
			break;
		}


		if(pad==2)
		{
			//_delay_ms(100);
			if(board[1][0]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE2;
					board[1][0]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS2;
					board[1][0]=2;
				}
				
				state1=4;
				break;
			}

			if ((board[1][0]==1) || (board[1][0]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		

		if(pad==3)
		{
			//_delay_ms(100);
			if(board[2][0]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE3;
					board[2][0]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS3;
					board[2][0]=2;
				}
				
				state1=4;
				break;
			}


			if((board[2][0]==1) || (board[2][0]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}


		if(pad==4)
		{
			//_delay_ms(100);
			if(board[0][1]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE4;
					board[0][1]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS4;
					board[0][1]=2;
				}
				
				state1=4;
				break;
			}

			if((board[0][1]==1) || (board[0][1]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		
		
		if(pad==5)
		{
			//_delay_ms(100);
			if(board[1][1]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE5;
					board[1][1]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS5;
					board[1][1]=2;
				}

				state1=4;
				break;
			}

			if((board[1][1]==1) || (board[1][1]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		
		
		if(pad==6)
		{
			//_delay_ms(100);
			if(board[2][1]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE6;
					board[2][1]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS6;
					board[2][1]=2;
				}
	
				state1=4;
				break;
			}

			if((board[2][1]==1) || (board[2][1]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		

		if(pad==7)
		{
			//_delay_ms(100);
			if(board[0][2]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE7;
					board[0][2]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS7;
					board[0][2]=2;
				}
				
				state1=4;
				break;
			}

			if((board[0][2]==1) || (board[0][2]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		
		
		if(pad==8)
		{
			//_delay_ms(100);
			if(board[1][2]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE8;
					board[1][2]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS8;
					board[1][2]=2;
				}

				state1=4;
				break;
			}

			if((board[1][2]==1) || (board[1][2]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		
		
		if(pad==9)
		{
			//_delay_ms(100);
			if(board[2][2]==0)
			{
				if(player_nr==1)
				{
					DISP_CIRCLE9;
					board[2][2]=1;
				}
				
				if(player_nr==2)
				{
					DISP_CROSS9;
					board[2][2]=2;
				}

				state1=4;
				break;
			}

			if((board[2][2]==1) || (board[2][2]==2))
			{
				DISP_WARNING1;
				state1=2;
				break;
			}
			break;
		}
		break;
			

		case 4: 
		// Glej ?e je zmagovalna kombincija
				//?e je display winner_nr
				//?e ne state=1
				
			/*UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "B1=%3d", board[0][0]);
			UG_PutString(231, 92, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "B2=%3d", board[1][0]);
			UG_PutString(231, 101, txt);
			
			UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "B9=%3d", board[2][2]);
			UG_PutString(231, 110, txt);*/
			
			
			winner_nr=Winner_combos();
			
			/*UG_FontSelect(&RFONT_5X8);
			UG_SetForecolor(C_WHITE_SMOKE);
			UG_SetBackcolor(C_PURPLE);
			sprintf(txt, "WNR=%3d", winner_nr);
			UG_PutString(231, 119, txt);*/
			
			
			
			if ((winner_nr == 1) || (winner_nr == 2) || (winner_nr == 3))
			{
				state1=0;
				result=1;
				break;
			}
			if(winner_nr==0) state1=1;
			break;
		
		default:
			state1=1;
			result=1;
			break;
	}
	return result;
}

int EndOfGame()
{
	int static state;
	int result=0;
	
		switch (state)
		{
			case 0:
				
				if (winner_nr == 1)
				{
					win1++;
					display=DISP_WINNER1;
					state++;
					break;
				}
		
				if(winner_nr == 2)
				{
					win2++;
					display=DISP_WINNER2;
					state++;
					break;			
				}
		
				if(winner_nr == 3)
				{
					display=DISP_DRAW;
					state++;
					break;
				}
				zaznamekCasa=GetSysTick();
				break;
			
			case 1:
				key1=KBD_GetKey();
				
				if(key1 == BTN_ESC)
				{
					key1=0;
					state++;
					break;
				}
				
				if(Has_X_MillisecondsPassed(15000, &zaznamekCasa))
				{
					state++;
					break;
				}
				break;
				
			default:
			state=0;
			result=1;
			break;
		
	
	/*UG_FontSelect(&RFONT_5X8);
	UG_SetForecolor(C_WHITE_SMOKE);
	UG_SetBackcolor(C_PURPLE);
	sprintf(txt, "R=%3d", result);
	UG_PutString(231, 128, txt);*/
	}
	return result;
}
 
int GetPlayerNr()
{
	
	char player=1;
	
	while (turn>=0)
		{
			if(turn == 2*x)
			{
				player = 1;
				x++;
				break;
			}
			else if(turn == 2*y + 1)
			{
				player = 2;
				y++;
				break;
			}
			break;
		}
	turn++;
	return player;
}
	
int Winner_combos()
{
	int winner=0;
	//char txt[8];
	//preveri kot character ali cross/circle
	//1.stolpec
	//2.stolpec
	//3. stolpec
	//1.vrstica
	//2.
	//3.
	//po?ez 1	
	//po?ez 2
	
		
		// IF NOT ALL POSITIONS TAKEN
		if ((board[0][0]==1)&&(board[0][1]==1)&&(board[0][2]==1))
		{
			winner = 1;
		}
		if ((board[1][0]==1)&&(board[1][1]==1)&&(board[1][2]==1))
		{
			winner = 1;
		}
		if ((board[2][0]==1)&&(board[2][1]==1)&&(board[2][2]==1))
		{
			winner = 1;
		}
		if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1))
		{
			winner = 1;
		}
		if ((board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==1))
		{
			winner = 1;
		}
		if ((board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==1))
		{
			winner = 1;
		}
		if ((board[0][0]==1)&&(board[1][1]==1)&&(board[2][2]==1))
		{	
			winner = 1;
		}
		if ((board[0][2]==1)&&(board[1][1]==1)&&(board[2][0]==1))
		{
			winner = 1;
		}
		if ((board[0][0]==2)&&(board[0][1]==2)&&(board[0][2]==2))
		{
			winner = 2;
		}
		if ((board[1][0]==2)&&(board[1][1]==2)&&(board[1][2]==2))
		{
			winner = 2;
		}
		if ((board[2][0]==2)&&(board[2][1]==2)&&(board[2][2]==2))
		{
			winner = 2;
		}
		if ((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==2))
		{
			winner = 2;
		}
		if ((board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==2))
		{
			winner = 2;
		}
		if ((board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==2))
		{
			winner = 2;
		}
		if ((board[0][0]==2)&&(board[1][1]==2)&&(board[2][2]==2))
		{
			winner = 2;
		}
		if ((board[0][2]==2)&&(board[1][1]==2)&&(board[2][0]==2))
		{
			winner = 2;
		}
		
		//IF ALL POSITIONS TAKEN
		if ((board[0][0]!=0) && (board[1][0]!=0) && (board[2][0]!=0) && (board[1][1]!=0) && (board[2][1]!=0) && (board[0][1]!=0) && (board[0][2]!=0) && (board[1][2]!=0) && (board[2][2]!=0))
		{
			winner=CheckForDraw();
			
			//ORDINARY COMBOs 1
			if ((board[1][0]==1)&&(board[1][1]==1)&&(board[1][2]==1)&&(board[0][1]==1)&&(board[2][1]==1)&&(board[0][0]==2)&&(board[2][0]==2)&&(board[0][2]==2)&&(board[2][2]==2))
			{
				winner=1;
			}
			if ((board[0][0]==1)&&(board[2][2]==1)&&(board[0][2]==1)&&(board[1][1]==1)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][2]==2)&&(board[2][1]==2)&&(board[1][0]==2))
			{
				winner=1;
			}
			
			
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]!=0)&&(board[1][1]!=0)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner =1;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==1)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=1;
			}
			if ((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]==1)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner=1;
			}
			if ((board[0][0]==1)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]==1)&&(board[1][1]!=0)&&(board[2][1]!=0)&&(board[0][2]==1)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=1;
			}
			if ((board[0][0]!=0)&&(board[1][0]==1)&&(board[2][0]!=0)&&(board[0][1]!=0)&&(board[1][1]==1)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]==1)&&(board[2][2]!=0))
			{
				winner=1;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]==1)&&(board[0][1]!=0)&&(board[1][1]!=0)&&(board[2][1]==1)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]==1))
			{
				winner=1;
			}
			if ((board[0][0]==1)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]!=0)&&(board[1][1]==1)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]==1))
			{
				winner=1;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]==1)&&(board[0][1]!=0)&&(board[1][1]==1)&&(board[2][1]!=0)&&(board[0][2]==1)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=1;
			}
			
			//ORDINARY COMBOs 2
			if ((board[1][0]==2)&&(board[1][1]==2)&&(board[1][2]==2)&&(board[0][1]==2)&&(board[2][1]==2)&&(board[0][0]==1)&&(board[2][0]==1)&&(board[2][2]==1)&&(board[0][2]==1))
			{
				winner=2;
			}
			if ((board[0][0]==2)&&(board[2][2]==2)&&(board[0][2]==2)&&(board[1][1]==2)&&(board[2][0]==2)&&(board[0][1]==1)&&(board[1][2]==1)&&(board[2][1]==1)&&(board[1][0]==1))
			{
				winner=2;
			}
			
			if ((board[0][0]==12)&&(board[1][0]==2)&&(board[2][0]==2)&&(board[0][1]!=0)&&(board[1][1]!=0)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner =2;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==2)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=2;
			}
			if ((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]==2)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner=2;
			}
			if ((board[0][0]==2)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]==2)&&(board[1][1]!=0)&&(board[2][1]!=0)&&(board[0][2]==2)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=2;
			}
			if ((board[0][0]!=0)&&(board[1][0]==2)&&(board[2][0]!=0)&&(board[0][1]!=0)&&(board[1][1]==2)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]==2)&&(board[2][2]!=0))
			{
				winner=2;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]==2)&&(board[0][1]!=0)&&(board[1][1]!=0)&&(board[2][1]==2)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]==2))
			{
				winner=2;
			}
			if ((board[0][0]==2)&&(board[1][0]!=0)&&(board[2][0]!=0)&&(board[0][1]!=0)&&(board[1][1]==2)&&(board[2][1]!=0)&&(board[0][2]!=0)&&(board[1][2]!=0)&&(board[2][2]==2))
			{
				winner=2;
			}
			if ((board[0][0]!=0)&&(board[1][0]!=0)&&(board[2][0]==2)&&(board[0][1]!=0)&&(board[1][1]==2)&&(board[2][1]!=0)&&(board[0][2]==2)&&(board[1][2]!=0)&&(board[2][2]!=0))
			{
				winner=2;
			}
			
			
			
			//EXTRAORDINARY COMBOs 1
			//1
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==1)&&(board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==1))
			{
				winner =1;
			}//2
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==1)&&(board[1][1]==2)&&(board[2][1]==2)&&(board[0][2]==1)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner =1;
			}//3
			if ((board[0][0]==1)&&(board[1][0]==2)&&(board[2][0]==2)&&(board[0][1]==1)&&(board[1][1]==2)&&(board[2][1]==2)&&(board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner =1;
			}//4
			if ((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==1)&&(board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner =1;
			}//5
			if ((board[0][0]==2)&&(board[1][0]==1)&&(board[2][0]==2)&&(board[0][1]==2)&&(board[1][1]==1)&&(board[2][1]==2)&&(board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner =1;
			}//6
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][1]==1)&&(board[2][1]==2)&&(board[0][2]==2)&&(board[1][2]==1)&&(board[2][2]==2))
			{
				winner =1;
			}//7
			if ((board[0][0]==1)&&(board[1][0]==2)&&(board[2][0]==2)&&(board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==1)&&(board[0][2]==1)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner =1;
			}//8
			if ((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==1)&&(board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==1)&&(board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==1))
			{
				winner =1;
			}
			
			
			//EXTRAORDINARY COMBOs 2
			//1
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==2)&&(board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==2))
			{
				winner =2;
			}//2
			if ((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==2)&&(board[0][1]==2)&&(board[1][1]==1)&&(board[2][1]==1)&&(board[0][2]==2)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner =2;
			}//3
			if ((board[0][0]==2)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][1]==1)&&(board[2][1]==1)&&(board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner =2;
			}//4
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==2)&&(board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==2)&&(board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner =2;
			}//5
			if ((board[0][0]==1)&&(board[1][0]==2)&&(board[2][0]==1)&&(board[0][1]==1)&&(board[1][1]==2)&&(board[2][1]==1)&&(board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==2))
			{
				winner =2;
			}//6
			if ((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==2)&&(board[0][1]==1)&&(board[1][1]==2)&&(board[2][1]==1)&&(board[0][2]==1)&&(board[1][2]==2)&&(board[2][2]==1))
			{
				winner =2;
			}//7
			if ((board[0][0]==2)&&(board[1][0]==1)&&(board[2][0]==1)&&(board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==2)&&(board[0][2]==2)&&(board[1][2]==1)&&(board[2][2]==1))
			{
				winner =2;
			}//8
			if ((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==2)&&(board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==2)&&(board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==2))
			{
				winner =2;
			}
			
			  // UPDATE: DELA BREZ ELSE-A!!
			//else winner = 3;
		}
		
		/*UG_FontSelect(&RFONT_5X8);
		UG_SetForecolor(C_WHITE_SMOKE);
		UG_SetBackcolor(C_PURPLE);
		sprintf(txt, "W=%3d", winner);
		UG_PutString(231, 83, txt);*/
		return winner;
}

int CheckForDraw()
{
	int draw=0;
	
	if ((((board[0][0]==1)||(board[0][0]==2)) && ((board[1][0]==1)||(board[1][0]==2)) && ((board[2][0]==1)||(board[2][0]==2)) && ((board[1][1]==1)||(board[1][1]==2)) && ((board[2][1]==1)||(board[2][1]==2)) && ((board[0][1]==1)||(board[0][1]==2)) && ((board[0][2]==1)||(board[0][2]==2)) && ((board[1][2]==1)||(board[1][2]==2)) && ((board[2][2]==1)||(board[2][2]==2))))
	{
		draw = 3;
	}
	
	return draw;
}

int CheckForCombo()
{
	int combo=0;
	
	if ((board[1][0]==1)&&(board[1][1]==1)&&(board[1][2]==1)&&(board[0][1]==1)&&(board[2][1]==1))
	{
		combo=1;
	}
	if ((board[0][0]==1)&&(board[2][2]==1)&&(board[0][2]==1)&&(board[1][1]==1)&&(board[2][0]==1))
	{
		combo=1;
	}
	
	return combo;
}

void Init_T0()
{
	//Init timer0
	OCR0A = *sinOutKrozc;
	TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0A1) | (1 << COM0A0);
	TCCR0B= (1 << CS00) | (1 << CS01);
	//TIMSK0 = (1 << OCIE0A);
	//Init timer1
	/*OCR1A = 16000;
	TCCR1B = (1 << WGM12);
	TIMSK1 = (1 << OCIE1A);
	TCCR1B |= (1 << CS10);*/
}