/***************************************************
*                      GRAPHIC.C                   *
*               CK = B(fi) = 49,152MHz             *
***************************************************/

#include "graphics.h"
#include "globvar.h"
#include "fonts.h"
#include "tables.h"
#include "iodefine.h"
#include "pictures.h"
#include "gpla.h"
#include <math.h>

//---------------< GlobVar >--------------------
unsigned char OldMenu = 0;
/***************************************************
*                ---< LcdInit >---                 *
*                Inicializace LCD                  *
***************************************************/
void LcdInit(void)
{
  WriteCommand(0x56,0x02); // sleep
	WriteCommand(0x04,0x20);
	WriteCommand(0x06,0xf8);
	WriteCommand(0x08,0x80);
	WriteCommand(0x0a,0x28);
	WriteCommand(0x0c,0x00);
	WriteCommand(0x0e,0x26); // 38x
	WriteCommand(0x12,0x08); //0x09
	WriteCommand(0x14,0x00);
	WriteCommand(0x16,0x50);
	WriteCommand(0x18,0x7f);
	WriteCommand(0x1a,0xe0);
	WriteCommand(0x1C,0x01);
	WriteCommand(0x1E,0x15);
	WriteCommand(0x28,0x80);
	WriteCommand(0x2a,0x01);
	WriteCommand(0x56,0x00); // wake up
}

/***************************************************************
*                    ---<  WriteCommand >---                   *
*               Vstup: adresa - adresa registru                *
*                        data - data registru                  *
*  Vstupy jsou char protoze nastavovani registru je po bytech  *
***************************************************************/
void WriteCommand(char adresa, char sdata)
{
  LCD_cs = 0;
	LCD_dc = 0;
	LCD_wr = 0;
	LCD_io_byte = adresa;
	LCD_wr = 1;
	LCD_dc = 1;
	LCD_wr = 0;
	LCD_io_byte = sdata;
	LCD_wr = 1;
	LCD_cs = 1;
}

/***************************************************
*             ---< SetWindowsCoordinates >---      *
*			  Nastaveni registru v Epsonu pro kresleni   *
*        Vstup: xxstart - reg[38h,3ah]             *
*				        yystart - reg[3ch,3eh]             *
*							  xxlength - reg[40h,42h]            *
*							  yylength - reg[44h,46h]            *
***************************************************/
void SetWindowsCoordinates(unsigned int xstart, unsigned int ystart, unsigned int xlength, unsigned int ylength)
{
  LCD_cs = 0;
	LCD_dc = 0;
	LCD_wr = 0;
	LCD_io_byte = 0x38;                  // adresa
	LCD_wr = 1;
	LCD_dc = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xstart);       // start x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xstart >> 8);  // start x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(ystart);       // start y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(ystart >> 8);  // start y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xlength);      // stop x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xlength >> 8); // stop x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(ylength);      // stop y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(ylength >> 8); // stop y
	LCD_wr = 1;
}

/***************************************************
*                   ---< SetPixel >---             *
*               Vstup: xposition = pozice v x      *
*                      xposition = pozice v y      *
*											 color = barva               *
***************************************************/
void SetPixel(unsigned int xposition, unsigned int yposition,unsigned int color)
{
	LCD_cs = 0;
	LCD_dc = 0;
	LCD_wr = 0;
	LCD_io_byte = 0x38;  // adresa
	LCD_wr = 1;
	LCD_dc = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xposition);        // start x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xposition >> 8);   // start x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(yposition);        // start y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(yposition >> 8);   // start y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xposition);        // stop x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(yposition >> 8);   // stop x
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(xposition);       // stop y
	LCD_wr = 1;
	LCD_wr = 0;
	LCD_io_byte = (char)(yposition >> 8);  // stop y
	LCD_wr = 1;
	asm("nop");
	LCD_wr = 0;
	LCD_io = color;
	LCD_wr = 1;
	LCD_cs = 1;
}

/***************************************************
*                ---< CleanScreen >---             *
***************************************************/
void CleanScreen(unsigned int color)
{
	volatile float u;
	
	SetWindowsCoordinates(0,45,639,479);
	
	for(u=0; u<307200 ; u++){
		LCD_wr = 0;
		LCD_io = color;
		LCD_wr = 1;
	}
	LCD_cs = 1;
}

/***************************************************
*                     ---< DrawBox >---            *
*              Vstup: xstart - start v x           *
*                     ystart - start v y           *
*											xlength - sirka v x          *
*											ylength - sirka v y          *
*											color - barva                *
***************************************************/
void DrawBox(unsigned int xstart, unsigned int ystart, unsigned int xlength, unsigned int ylength, unsigned int color)
{
   unsigned long i;
  
	SetWindowsCoordinates(xstart,ystart,(xstart+xlength-1),(ystart+ylength));
  for(i=0; i<(long)(xlength*ylength);i++){
		LCD_wr = 0;
		LCD_io = color;
		LCD_wr = 1;
	}
	LCD_cs = 1;
}

/****************************************************
*                  ---< HorizontalLine >---         *
*               Vstup: xstart - start v x           *
*                      ystart - start v y           *
*                      xlength - delka cary         *
*                      width - sirka cary           *
****************************************************/
void HorizontalLine(signed int xstart, signed int ystart, unsigned int xlength, unsigned int width, unsigned int color)
{
	unsigned long i;
 
  SetWindowsCoordinates(xstart,ystart,(xstart+xlength-1),(ystart+width));
  for(i=0; i<(xlength*width);i++){
		LCD_wr = 0;
		LCD_io = color;
		LCD_wr = 1;
	}
	LCD_cs = 1;
}

/****************************************************
*                  ---< VerticalLine >---           *
*               Vstup: xstart - start v x           *
*                      ystart - start v y           *
*                      xlength - delka cary         *
*                      width - sirka cary           *
****************************************************/
void VerticalLine(signed int xstart, signed int ystart, unsigned int ylength, unsigned int width, unsigned int color)
{
	unsigned long i;
 
  SetWindowsCoordinates(xstart,ystart,(xstart+width-1),(ystart+ylength));
  for(i=0; i<(ylength*width);i++){
		LCD_wr = 0;
		LCD_io = color;
		LCD_wr = 1;
	}
	LCD_cs = 1;
}

/****************************************************
*						  	---< LoadPicture >---               *
*                  Vykresli obrazek                 *
*    Vstup: picture - Cislo obrazku                 * 
*           xstart - start v ose x                  *
*						ystart - start v ose y                  *
*						color - barva obrazku                   *
****************************************************/
unsigned int LoadPicture(unsigned int xstart,unsigned int ystart,unsigned char picture,unsigned int color)
{
  unsigned int i;
	
	SetWindowsCoordinates(xstart,ystart,xstart+picture_width-1,ystart+picture_height);
	
  for(i=0; i<(picture_width*picture_height);i++){
		if(Pictures[(picture *(picture_width*picture_height))+i]==0xf81f){
		  LCD_wr = 0;
		  LCD_io = color;
		  LCD_wr = 1;
	  }
	  else{	
	   LCD_wr = 0;
	   LCD_io = Pictures[(picture *(picture_width*picture_height))+i];
	   LCD_wr = 1;
	  }
	}
	LCD_cs = 1;
	return 0;
}

/****************************************************
*							  ---< WriteCharacter >---            *
*                   Napise pismeno                  *
*    Vstup: Character - pismeno Pr. 'A'             * 
*           xstart - start v ose x                  *
*						ystart - start v ose y                  *
*						color - barva pismene                   *
*						color_screen - barva pozadi             *
****************************************************/
unsigned int WriteCharacter(unsigned char character,unsigned int xstart,unsigned int ystart,unsigned int color,unsigned int color_screen,unsigned char size)
{
  unsigned int i,j,k;
	unsigned char pismeno;
	
	SetFont(size);
  pismeno = character-32;
	SetWindowsCoordinates(xstart,ystart,xstart+(*(width_table+pismeno)-1),ystart+font_height);
  for(k=0; k<sloupec_bytu ; k++){
 	  for(j=0;j<8;j++){ // sirka dat (asi vzdy zustane nastavene 8)
      for(i=0;i<(int)(*(width_table+pismeno));i++){  // 
				if((((*(character_table+(*(offset_table+pismeno)+k+(i*sloupec_bytu))))>>j)&0x1)==0x1){
			    LCD_wr = 0;
	        LCD_io = color;
	        LCD_wr = 1;	
	      }
			  else{
				  LCD_wr = 0;
	        LCD_io = color_screen;
	        LCD_wr = 1;	
			  }
	    }
	  }
	}
  return *(width_table+pismeno);
}
/****************************************************
*							  ---< WriteCharacterB >---            *
*                   Napise pismeno                  *
*    Vstup: Character - pismeno Pr. 'A'             * 
*           xstart - start v ose x                  *
*						ystart - start v ose y                  *
*						color - barva pismene                   *
*						color_screen - barva pozadi             *
****************************************************/
unsigned int WriteCharacterB(unsigned char character,unsigned int xstart,unsigned int ystart,unsigned int color,unsigned int color_screen,unsigned char size)
{
  unsigned int i,j,k;
	unsigned char pismeno;
	
	SetFont(size);
  pismeno = character-32;
	SetWindowsCoordinates(xstart,ystart,xstart+(*(width_table+pismeno)-1),ystart+font_height);
  for(k=0; k<sloupec_bytu ; k++){
 	  for(j=0;j<8;j++){ // sirka dat (asi vzdy zustane nastavene 8)
      for(i=0;i<(int)(*(width_table+pismeno));i++){  // 
				if((((*(character_table+(*(offset_table+pismeno)+k+(i*sloupec_bytu))))>>j)&0x1)==0x1){
			    LCD_wr = 0;
	        LCD_io = color;
	        LCD_wr = 1;	
	      }
			  else{
				  LCD_wr = 0;
	        LCD_io = color_screen;
	        LCD_wr = 1;	
			  }
	    }
	  }
	}
  return (*(width_table+pismeno))+xstart;
}

/****************************************************
*				 ---< WriteTranspaCharacterB >---            *
*            Napise pruhledne pismeno               *
*    Vstup: Character - pismeno Pr. 'A'             * 
*           xstart - start v ose x                  *
*						ystart - start v ose y                  *
*						color - barva pismene                   *
****************************************************/
unsigned int WriteTranspaCharacterB(unsigned char character,unsigned int xstart,unsigned int ystart,unsigned int color,unsigned char size)
{
  unsigned int i,j,k;
	unsigned char pismeno;
	
	SetFont(size);
  pismeno = character-32;
	SetWindowsCoordinates(xstart,ystart,xstart+(*(width_table+pismeno)-1),ystart+font_height);
  for(k=0; k<sloupec_bytu ; k++){
 	  for(j=0;j<8;j++){ // sirka dat (asi vzdy zustane nastavene 8)
      for(i=0;i<(int)(*(width_table+pismeno));i++){  // 
				if((((*(character_table+(*(offset_table+pismeno)+k+(i*sloupec_bytu))))>>j)&0x1)==0x1){
			    SetPixel(xstart+i,ystart+j+(k*8),color);
	      }
	    }
	  }
	}
  return (*(width_table+pismeno))+xstart;
}
/**************************************************
*             ---< WriteTextAligR >---            *
*           Pise text kterz konci v X             *
*            Proste zarovnani v pravo             *
*           vstup: line - slovo                   *
*                  size - velikost pisma          *
*           vraci: delku slova v pixlech          *
**************************************************/
unsigned int WriteTextAligR(unsigned char* line, unsigned short x, unsigned short y, unsigned int color,unsigned int color_screen, unsigned char size)
{
  unsigned int DelkaSlova = 0;
	unsigned char pismeno;
	unsigned char *text;

  text = (unsigned char*)line;
	SetFont(size);
  while(*line){
    pismeno = *line-32;
    DelkaSlova += width_table[pismeno]; 
    line++;
	}
  WriteText(text,x-DelkaSlova,y,color,color_screen,size);
  return((unsigned int)x-DelkaSlova);
}
/**************************************************
*                  ---< JoinText >---             *
*              Spojidva texty v jeden             *
*              Vrati text v poli cislo[]          *
**************************************************/
void JoinText(unsigned char* Text1, unsigned char* Text2)
{
  unsigned char i=0;
	
	while(*Text1){
	  cislo[i] = *Text1;	
		i++;
		Text1++;
	}
	while(*Text2){
	  cislo[i] = *Text2;	
		i++;
		Text2++;
	}
}

/***************************************************
*                ---< WriteText >---               *
*             Napise nekolik pismen                *
*        Vstup: line - slovo Pr. "ahoj. jak je"    *
*			          x - pozice textu v ose x           *
*							  y - pozice texte v ose y           *
*							  color - barva pisma                *
*							  color_screen - barva pozadi        *
***************************************************/
void WriteText(unsigned char* line, unsigned short x, unsigned short y, unsigned int color,unsigned int color_screen, unsigned char size)
{
	unsigned short dx=x;

  while(*line){
		dx+=WriteCharacter(*line,dx,y,color,color_screen,size);
		line++;
	}
}

/***************************************************
*                ---< WriteTextB >---               *
*            To same co WriteText akorat           *
*               vrati kde bylo dopsano             *
*        Vstup: line - slovo Pr. "ahoj. jak je"    *
*			          x - pozice textu v ose x           *
*							  y - pozice texte v ose y           *
*							  color - barva pisma                *
*							  color_screen - barva pozadi        *
***************************************************/
unsigned int WriteTextB(unsigned char* line, unsigned short x, unsigned short y, unsigned int color,unsigned int color_screen, unsigned char size)
{
	unsigned int dx=x;

  while(*line){
		dx+=WriteCharacter(*line,dx,y,color,color_screen,size);
		line++;
	}
	return(dx); 
}

/***************************************************
*                ---< WriteTextMenu >---           *
*                Napise text z pole Menu           *
*        Vstup: line - slovo Pr. "ahoj. jak je"    *
*			          x - pozice textu v ose x           *
*							  y - pozice texte v ose y           *
*							  color - barva pisma                *
*							  color_screen - barva pozadi        *
***************************************************/
void WriteTextMenu(const unsigned char* line, unsigned short x, unsigned short y, unsigned int color,unsigned int color_screen, unsigned char size)
{
	unsigned short dx=x;

	line+=12;
	while(*line){
		dx+=WriteCharacter(*line,dx,y,color,color_screen,size);
		line++;
	}
}
/***************************************************
*                ---< WriteTextMenuB >---          *
*                Napise text z pole Menu           *
*        Vstup: line - slovo Pr. "ahoj. jak je"    *
*			          x - pozice textu v ose x           *
*							  y - pozice texte v ose y           *
*							  color - barva pisma                *
*							  color_screen - barva pozadi        *
***************************************************/
unsigned int WriteTextMenuB(const unsigned char* line, unsigned short x, unsigned short y, unsigned int color,unsigned int color_screen, unsigned char size)
{
	unsigned short dx=x;

	line+=12;
	while(*line){
		dx+=WriteCharacter(*line,dx,y,color,color_screen,size);
		line++;
	}
	return(dx);
}
/*************************************************
*               ---< SetFont >---                *
*          Vstup: font - cislo fontu             *
*************************************************/
void SetFont(unsigned char font)
{
	switch(font){

		case 0:  sloupec_bytu = font3_sloupec_bytu;
						 font_height = font3_height; 
		         character_table = &font3_data_table[0];
						 offset_table = &font3_offset_table[0];
						 width_table = &font3_width_table[0];
 		      	 break;

		case 1:  sloupec_bytu = font1_sloupec_bytu;
						 font_height = font1_height;
		         character_table = &font1_data_table[0];
						 offset_table = &font1_offset_table[0];
						 width_table = &font1_width_table[0];
		         break;

		case 2:  sloupec_bytu = font2_sloupec_bytu;
						 font_height = font2_height; 
		         character_table = &font2_data_table[0];
						 offset_table = &font2_offset_table[0];
						 width_table = &font2_width_table[0];
 		      	 break;

		case 3:  sloupec_bytu = font4_sloupec_bytu;
						 font_height = font4_height; 
		         character_table = &font4_data_table[0];
						 offset_table = &font4_offset_table[0];
						 width_table = &font4_width_table[0];
 		      	 break;
						 
		case 4:  sloupec_bytu = font5_sloupec_bytu;
						 font_height = font5_height; 
		         character_table = &font5_data_table[0];
						 offset_table = &font5_offset_table[0];
						 width_table = &font5_width_table[0];
 		      	 break;							 				 
	}
}
/*************************************************
*               ---< FontHigh >---               *
*          Vstup: font - cislo fontu             *
*          Vystup: Velikost fontu v pixlech      *
*************************************************/
unsigned char FontHigh(unsigned char font)
{
	switch(font){
		case 0:  return(font3_height); 
 		      	 break;
		case 1:	 return(font1_height);
		         break;
		case 2:	 return(font2_height); 
 		      	 break;
		case 3:	 return(font4_height); 
 		      	 break;
		case 4:  return(font5_height); 
 		      	 break;							 				 
	}
}        

/**************************************************
*              ---< DrawFreeLine >---             *
*                    Volna usecka                 *
*     Vstup: startx - zacatek usecky v ose x      *
*		         starty - zacatek usecky v ose z      *
*						 endx - konec usecky v ose x          *
*						 endy - konec usecky v ose y          *
*            width - sirka cary                   *       
*						 color - barva                        *
***************************************************/
void DrawFreeLine(unsigned int startx, unsigned int starty, unsigned int endx, unsigned int endy,unsigned char width, unsigned int color)
{
	unsigned int x,y;
	signed int dx,dy;
	unsigned char l;
	
	dx=endx-startx;
	dy=endy-starty;
  if(dx && dy){
	if(dx>0 && dy>0){
		if(dx>dy){
			for(x=0;x<dx;x++){
				y=((dy+1)*x)/(dx);
				for(l=0;l<width;l++){
				  SetPixel(x+startx,(y+starty)+l,color);
				}
			}
		}
		else{
			for(y=0;y<dy;y++){
				x=((dx+1)*y)/(dy);
				for(l=0;l<width;l++){
  				SetPixel((x+startx)+l,y+starty,color);
				}
			}
		}
	}//
	else if(dx<0 && dy>0){
		if((-dx)>dy){
			for(x=0;x<(-dx);x++){
				y=((dy+1)*x)/(-dx);
				for(l=0;l<width;l++){
				  SetPixel(startx-x,(y+starty)+l,color);
				}
			}
		}
		else{
			for(y=0;y<dy;y++){
				x=(((-dx)+1)*y)/(dy);
				for(l=0;l<width;l++){
  				SetPixel((startx-x)+l,y+starty,color);
				}
			}
		}
	}//
	else if(dx>0 && dy<0){
		if(dx>(-dy)){
			for(x=0;x<dx;x++){
				y=(((-dy)+1)*x)/(dx);
				for(l=0;l<width;l++){
  				SetPixel(x+startx,(starty-y)+l,color);
				}
			}
		}
		else{
			for(y=0;y<(-dy);y++){
				x=((dx+1)*y)/(-dy);
 				for(l=0;l<width;l++){
  				SetPixel((x+startx)+l,starty-y,color);
				}
			}
		}
	}//
	else {
		if((-dx)>(-dy)){
			for(x=0;x<(-dx);x++){
				y=(((-dy)+1)*x)/(-dx);
				for(l=0;l<width;l++){
  				SetPixel(startx-x,(starty-y)+l,color);
				}
			}
		}
		else{
			for(y=0;y<(-dy);y++){
				x=(((-dx)+1)*y)/(-dy);
				for(l=0;l<width;l++){
  				SetPixel((startx-x)+l,starty-y,color);
				}
			}
		}
	}//
	}
//---------------------------------------------------------------	
	else{
		if(dx>0){
			for(x=0;x<dx;x++){
				for(l=0;l<width;l++){
  				SetPixel(x+startx,(starty)+l,color);
				}
			}//x
		}
		else{
			for(x=0;x<(-dx);x++){
				for(l=0;l<width;l++){
	  			SetPixel(startx-x,(starty)+l,color);
				}
			}//x
		}
		if(dy>0){
			for(y=0;y<dy;y++){
				for(l=0;l<width;l++){
  				SetPixel((startx)+l,starty+y,color);
				}
			}//y
		}
		else{
			for(y=0;y<(-dy);y++){
  			for(l=0;l<width;l++){
    			SetPixel((startx)+l,starty-y,color);
				}
			}//y
		}
	}	
}
/*****************************************************
*                   ---< DrawLine >---               *
*          Vstup: xstart - stred v ose x             *
*                  ystart - stred v ose y            *
*									length - delka                     *
*									uhel   - uhel (kladna osa x je 0)  *
*									color  - barva cary                *
*****************************************************/
void DrawLine(unsigned int xstart,unsigned int ystart, unsigned char length,unsigned int uhel,unsigned char width, unsigned int color)
{
  double ux,uh;
	double dx,dy;
	unsigned int bx,by;
	int i;
	
	ux= uhel*0.017453292;
  uh = cos(ux);
  dx = uh*length;
	if(uhel==0 || uhel==180 || uhel== 360){
	  dy = 0;
	}
	else{
	  dy = sqrt((length*length) - (dx*dx));
	}
  if(uhel>180){
	  bx = xstart+dx;
    by = ystart-dy;
	}
	else{
	  bx = xstart+dx;
    by = ystart+dy;
	}
  DrawFreeLine(xstart,ystart,(unsigned int)bx,(unsigned int)by,width,color);
}
/*****************************************************
*                   ---< DrawLineEnd >---               *
*          Vstup: xstart - stred v ose x             *
*                  ystart - stred v ose y            *
*									length - delka                     *
*									uhel   - uhel (kladna osa x je 0)  *
*									color  - barva cary                *
*****************************************************/
//void graphics_draw_free_line(unsigned int startx, unsigned int starty, unsigned int endx, unsigned int endy, unsigned short color){
void DrawLineEnd(signed int startx,signed int starty, unsigned int color)
{

	signed int x[9][2]={0,0,0,0,0,0,0,0,0},y[9][2]={0,0,0,0,0,0,0,0,0};
	signed int l;
	unsigned int BarvaFaze[9],ColorVol,ColorCur,TextFrom;
	unsigned int i,j,k,m;
	unsigned char TextOs;
	float dxx,dyy,ux,uh;

//----< Preskladam si barvy aby se mi lepe pracovalo >---
  for(i=0;i<3;i++){
		BarvaFaze[i*3] = grey; // Barva zakladnich car 0-120-240
		BarvaFaze[1+i*3] = ColorPhase[i];
		BarvaFaze[2+i*3] = ColorPhase[i+4];
	}
//---< Parametry zakladnich car >---
  dx[0][1] = 165;   // 0
  dy[0][1] = 0;     // 0
  dx[3][1] = -82;  // 120
	dy[3][1] =  143;  // 120
  dx[6][1] = -82;  // 240
	dy[6][1] = -143;  // 240
  dx[0][0] = 0;     // Old 0
	dy[0][0] = 0;     // Old 0
  dx[3][0] = 0;     // Old 120
	dy[3][0] = 0;     // Old 120
  dx[6][0] = 0;     // Old 240
	dy[6][0] = 0;     // Old 240		

//---< Parametry Nove cary(Napeti)>---
  for(i=0;i<3;i++){
	  if(PoleVector[i] > 12){
      dyy = ShowVal[121][i]*PoleVector[i];  // sin*hodnota napeti  
		  if(!ShowVal[121]){
			  dxx = PoleVector[i];
		  }
		  else{
        dxx = sqrt((pow(PoleVector[i],2) - (fabs(dyy)*fabs(dyy))));
		  }
      if(ShowVal[120][i]<0){  // pokud je cos<0 -> uhel>180 otacim osu 
	      dx[1+(i*3)][1] = (signed int)(-dxx);
        dy[1+(i*3)][1] = (signed int)dyy;
	    }
	    else{
	      dx[1+(i*3)][1] = (signed int)dxx;
        dy[1+(i*3)][1] = (signed int)dyy;
  	  }
	  }
	  else{
      dx[1+(i*3)][1] = 0;
      dy[1+(i*3)][1] = 0;
	  }
//----< Parametry nove cary napeti >------
    if(ShowVal[3][i] > 12){
      dyy = ShowVal[123][i]*ShowVal[3][i]; // sin*hodnota proudu
		  if(!ShowVal[123][i]){
		    dxx = ShowVal[3][i];
		  }
		  else{
        dxx = sqrt((pow(ShowVal[3][i],2) - (fabs(dyy)*fabs(dyy))));
		  }
      if(ShowVal[122][i] < 0){  // pokud je cos<0 -> uhel>180 otacim osu 
	      dx[2+(i*3)][1] = (signed int)(-dxx);
        dy[2+(i*3)][1] = (signed int)dyy;
	    }
	    else{
	      dx[2+(i*3)][1] = (signed int)dxx;
        dy[2+(i*3)][1] = (signed int)dyy;
  	  }
	  }
	  else{
      dx[2+(i*3)][1] = 0;
      dy[2+(i*3)][1] = 0;
	  }
  }

//------------< Parametry pro pozici popisek >-----------------	
//---< Napeti >---
	for(i=0;i<3;i++){
		if(PoleVector[i] > 12){
      dyy = ShowVal[121][i]*(PoleVector[i]+20);  // sin*hodnota napeti  
		  if(!ShowVal[121]){
			  dxx = PoleVector[i]+20;
		  }
		  else{
        dxx = sqrt((pow(PoleVector[i]+20,2) - (fabs(dyy)*fabs(dyy))));
		  }
      if(ShowVal[120][i]<0){  // pokud je cos<0 -> uhel>180 otacim osu 
	      TextX[i*2][1] = (signed int)(-dxx);
        TextY[i*2][1] = (signed int)dyy;
	    }
	    else{
	      TextX[i*2][1] = (signed int)dxx;
        TextY[i*2][1] = (signed int)dyy;
  	  }
	  }
	  else{
		  TextX[i*2][1] = 0;
			TextY[i*2][1] = 0;
		}
//---< Proudu >---
		if(ShowVal[3][i] > 12){
      dyy = ShowVal[123][i]*(ShowVal[3][i]+20); // sin*hodnota proudu
		  if(!ShowVal[123][i]){
			  dxx = ShowVal[3][i]+20;
		  }
		  else{
        dxx = sqrt((pow(ShowVal[3][i]+20,2) - (fabs(dyy)*fabs(dyy))));
		  }
      if(ShowVal[122][i] < 0){  // pokud je cos<0 -> uhel>180 otacim osu 
	      TextX[1+(i*2)][1] = (signed int)(-dxx);
        TextY[1+(i*2)][1] = (signed int)dyy;
	    }
	    else{
	      TextX[1+(i*2)][1] = (signed int)dxx;
        TextY[1+(i*2)][1] = (signed int)dyy;
  	  }
	  }
	 	else{
		  TextX[1+(i*2)][1] = 0;
			TextY[1+(i*2)][1] = 0;
		}
  }
//---< Kresleni kruhu >---
	Circle(430,262,6,black);      // stredovy		
  DrawCircle(430,262,55,grey);
	DrawCircle(430,262,110,grey);
	DrawCircle(430,262,165,grey);
	
//---< Kresleni car >---
  for(m=0;m<3;m++){
//---< Koncova kolecka >---
		if((!dx[1+m*3][1]) && !(dy[1+m*3][1])){ // Aby pri vypadku napeti neblikalo stredni kolecko
      ColorVol = black;
		}
		else{
			ColorVol = BarvaFaze[1+m*3];
		}
		if((!dx[2+m*3][1]) && (!dy[2+m*3][1])){  // Aby pri vypadku proudu neblikalo stredni kolecko
      ColorCur = black;
		}
		else{
      ColorCur = BarvaFaze[2+m*3];
		}
    CircleClear(startx+dx[1+m*3][0],starty+dy[1+m*3][0],startx+dx[1+m*3][1],starty+dy[1+m*3][1],startx+dx[2+m*3][0],starty+dy[2+m*3][0],startx+dx[2+m*3][1],starty+dy[2+m*3][1],6,ColorVol,ColorCur);
//---< Cara >---
    j=165;
	  while(j){
		  j--;
 	    for(k=0;k<2;k++){
        for(i=(m*3);i<((m*3)+3);i++){  // Vykresluju proud a nap jedne faze soucasne v tabulce to je navzajem o 3 posonute
 		      if(!k)color = white;  // pri prvnim pruchodu se maze
			    else color = BarvaFaze[i];  // barva napeti
			    if(dx[i][k] && dy[i][k]){		
	          if(dx[i][k]>0 && dy[i][k]>0){  
		          if(dx[i][k]>=dy[i][k]){
			          if(x[i][k]<dx[i][k]){
				          y[i][k]=((dy[i][k]+1)*x[i][k])/(dx[i][k]);
 					        for(l=0;l<3;l++){
  							    if(((pow(x[i][k]-(dx[1+m*3][1]),2)+pow(((y[i][k]-1)+l)-(dy[1+m*3][1]),2))>36) && ((pow(x[i][k]-(dx[2+m*3][1]),2)+pow(((y[i][k]-1)+l)-(dy[2+m*3][1]),2))>36) && ((pow(x[i][k],2)+pow(((y[i][k]-1)+l),2))>36)){
	                    SetPixel(x[i][k]+startx,(y[i][k]+starty-1)+l,color);
						        }
								  }
				          x[i][k]++;
			          }
		          }
		          else{
			          if(y[i][k]<dy[i][k]){
				          x[i][k]=((dx[i][k]+1)*y[i][k])/(dy[i][k]);
 					        for(l=0;l<3;l++){
  							    if(((pow((((x[i][k])-1)+l)-(dx[1+m*3][1]),2)+pow((y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow((((x[i][k])-1)+l)-(dx[2+m*3][1]),2)+pow((y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow((((x[i][k])-1)+l),2)+pow((y[i][k]),2))>36)){
    	                SetPixel((x[i][k]+startx-1)+l,(y[i][k]+starty),color);
						        }
								  }
							    y[i][k]++;
			          }
		          }
	          }//
//----------------------------------------------------	
					  else{
						  if(dx[i][k]<0 && dy[i][k]>0){  //roste
		            if((-dx[i][k])>dy[i][k]){
			            if(x[i][k]<(-dx[i][k])){
				            y[i][k]=((dy[i][k]+1)*x[i][k])/(-dx[i][k]);
 					          for(l=0;l<3;l++){
   							      if(((pow((-x[i][k])-(dx[1+m*3][1]),2)+pow((((-1)+y[i][k])+l)-(dy[1+m*3][1]),2))>36) && ((pow((-x[i][k])-(dx[2+m*3][1]),2)+pow((((-1)+y[i][k])+l)-(dy[2+m*3][1]),2))>36) && ((pow((-x[i][k]),2)+pow((((-1)+y[i][k])+l),2))>36)){
    					          SetPixel(startx-x[i][k],((starty-1)+y[i][k])+l,color);
						          }
								    }									 
				            x[i][k]++;
			            }
		            }
		            else{
			            if(y[i][k]<dy[i][k]){
  			            x[i][k]=(((-dx[i][k])+1)*y[i][k])/(dy[i][k]);
    			          for(l=0;l<3;l++){
  							      if(((pow((((-x[i][k])-1)+l)-(dx[1+m*3][1]),2)+pow((y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow((((-x[i][k])-1)+l)-(dx[2+m*3][1]),2)+pow((y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow((((-x[i][k])-1)+l),2)+pow((y[i][k]),2))>36)){
                        SetPixel(((-x[i][k])+startx-1)+l,(y[i][k]+starty),color);
						          }
								    }									
				            y[i][k]++;
			            }
		            }
	            }//
//----------------------------------------------------	
              else{
					      if(dx[i][k]>0 && dy[i][k]<0){  //roste
                  if(dx[i][k]>(-dy[i][k])){
	                  if(x[i][k]<dx[i][k]){
		                  y[i][k]=(((-dy[i][k])+1)*x[i][k])/(dx[i][k]);
 			                for(l=0;l<3;l++){
  			  		          if(((pow((x[i][k])-(dx[1+m*3][1]),2)+pow((((-1)-y[i][k])+l)-(dy[1+m*3][1]),2))>36) && ((pow((x[i][k])-(dx[2+m*3][1]),2)+pow((((-1)-y[i][k])+l)-(dy[2+m*3][1]),2))>36) && ((pow((x[i][k]),2)+pow((((-1)-y[i][k])+l),2))>36)){
      		                SetPixel(x[i][k]+startx,((starty-1)-y[i][k])+l,color);
						            }
							        }
			                x[i][k]++;
			              }
		              }
        	        else{
			              if(y[i][k]<(-dy[i][k])){
				              x[i][k]=((dx[i][k]+1)*y[i][k])/(-dy[i][k]);
				              for(l=0;l<3;l++){
  							        if(((pow((((x[i][k])-1)+l)-(dx[1+m*3][1]),2)+pow((-y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow((((x[i][k])-1)+l)-(dx[2+m*3][1]),2)+pow((-y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow((((x[i][k])-1)+l),2)+pow((-y[i][k]),2))>36)){
      			              SetPixel((x[i][k]+startx-1)+l,starty-y[i][k],color);
						            }
								      }									
			                y[i][k]++;
			              }
		              }
	              }//
//-----------------------------------------------------------
	              else {
		              if((-dx[i][k])>=(-dy[i][k])){
			              if(x[i][k]<(-dx[i][k])){
				              y[i][k]=(((-dy[i][k])+1)*x[i][k])/(-dx[i][k]);
 					            for(l=0;l<3;l++){
  						          if(((pow((-x[i][k])-(dx[1+m*3][1]),2)+pow((((-1)-y[i][k])+l)-(dy[1+m*3][1]),2))>36) && ((pow((-x[i][k])-(dx[2+m*3][1]),2)+pow((((-1)-y[i][k])+l)-(dy[2+m*3][1]),2))>36) && ((pow((-x[i][k]),2)+pow((((-1)-y[i][k])+l),2))>36)){
    					            SetPixel(startx-x[i][k],(((starty-1)-y[i][k])+l),color);
						            }
							        }									 
			                x[i][k]++;
			              }
									}
        		      else{
			              if(y[i][k]<(-dy[i][k])){
				              x[i][k]=(((-dx[i][k])+1)*y[i][k])/(-dy[i][k]);
 					            for(l=0;l<3;l++){
  					            if(((pow(((-x[i][k]-1)+l)-(dx[1+m*3][1]),2)+pow((-y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow(((-x[i][k]-1)+l)-(dx[2+m*3][1]),2)+pow((-y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow(((-x[i][k]-1)+l),2)+pow((-y[i][k]),2))>36)){
	  					            SetPixel((startx-x[i][k]-1)+l,starty-y[i][k],color);
						            }
						          }									
			                y[i][k]++;
			              }
		              }
		            }
				      }
					  }
					}	
				  else{
//--------------< Vodorovne a svisle cary >------------------------	
            if(dx[i][k] && dy[i][k]==0){//x
			        if(dx[i][k] > 0){
		            if(x[i][k]<dx[i][k]){
                  for(l=0;l<3;l++){
   			  		      if(((pow((x[i][k])-(dx[1+m*3][1]),2)+pow(((-1)+l)-(dy[1+m*3][1]),2))>36) && ((pow((x[i][k])-(dx[2+m*3][1]),2)+pow(((-1)+l)-(dy[2+m*3][1]),2))>36) && ((pow((x[i][k]),2)+pow(((-1)+l),2))>36)){
		                  SetPixel(x[i][k]+startx,(starty-1)+l,color);
			              }
							    }
				          x[i][k]++;
				        }
		          }
		          else{
		            if(x[i][k]<(-dx[i][k])){
                  for(l=0;l<3;l++){
   				  	      if(((pow((-x[i][k])-(dx[1+m*3][1]),2)+pow(((-1)+l)-(dy[1+m*3][1]),2))>36) && ((pow((-x[i][k])-(dx[2+m*3][1]),2)+pow(((-1)+l)-(dy[2+m*3][1]),2))>36) && ((pow((-x[i][k]),2)+pow(((-1)+l),2))>36)){
	      		          SetPixel(startx-x[i][k],(starty-1)+l,color);
						  	    }
					        }
					        x[i][k]++; 
			          }
 		          }
						}
				    if(dy[i][k] && dx[i][k]==0){//y
		          if(dy[i][k]>0){
				        if(y[i][k]<dy[i][k]){
   		            for(l=0;l<3;l++){
   					        if(((pow(((-1)+l)-(dx[1+m*3][1]),2)+pow((y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow(((-1)+l)-(dx[2+m*3][1]),2)+pow((y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow(((-1)+l),2)+pow((y[i][k]),2))>36)){
	    	              SetPixel((startx-1)+l,starty+y[i][k],color);
	 		 		          }
							    }
							  }
					      y[i][k]++;
			        } 
		          else{
		            if(y[i][k]<(-dy[i][k])){
   		            for(l=0;l<3;l++){
 	    	  		      if(((pow(((-1)+l)-(dx[1+m*3][1]),2)+pow((-y[i][k])-(dy[1+m*3][1]),2))>36) && ((pow(((-1)+l)-(dx[2+m*3][1]),2)+pow((-y[i][k])-(dy[2+m*3][1]),2))>36) && ((pow(((-1)+l),2)+pow((-y[i][k]),2))>36)){
		  	              SetPixel((startx-1)+l,starty-y[i][k],color);
		   		          }
								  }
					        y[i][k]++;
			          }
 		          }
					  }
	        }
				}
			}
	  }//
//---------------< Popisky vektoru >-------------------------------------------------
    for(k=0;k<2;k++){	
  		for(i=0;i<2;i++){
        if(k)color = BarvaFaze[(1+(m*3))+i];
		    else color = white;
		    if(!i) TextOs = 'U';
		    else TextOs ='I';
				if(TextX[i+m*2][k] || TextY[i+m*2][k]){
 		      TextFrom = WriteTranspaCharacterB(TextOs,startx+TextX[i+m*2][k]-8,starty+TextY[i+m*2][k]-8,color,3);
 				  TextFrom = WriteTranspaCharacterB(((1+m)+0x30),TextFrom,starty+TextY[i+m*2][k]-8,color,3);
				}
  		}
	  }
	}
//------------< ulozim si stare hodnoty >----------------------	
  for(i=0;i<9;i++){
	  dx[i][0] = dx[i][1];
		dy[i][0] = dy[i][1];
	}
  for(i=0;i<6;i++){
    TextX[i][0] = TextX[i][1]; 
		TextY[i][0] = TextY[i][1];
	}
}
/*****************************************************
*                      DrawAxisX                     *
*               Vstup: xstart - start v x            *
*                       ystart - start v y           *
*											 length - délka                *
*											 krok - krok                   *
*****************************************************/
void DrawAxisX(unsigned int xstart, unsigned int ystart,unsigned int length, unsigned char krok)
{
  unsigned char i;
	unsigned int kkrok = 0;
	
	HorizontalLine(xstart,ystart,length,4,blue1);
  for(i=0;i<krok;i++){
		VerticalLine(xstart+kkrok,ystart,12,4,blue1);
    kkrok += length/(krok-1);
	}
}
/*****************************************************
*                      DrawAxisXT2                   *
*         OsaX ale trochu jinak pro harmonicke       * 
*               Vstup: xstart - start v x            *
*                      ystart - start v y            *
*											 length - délka                *
*                      width  - sirka okna hodnoty   *
*											 krok - krok                   *
*****************************************************/
void DrawAxisXT2(unsigned int xstart, unsigned int ystart,unsigned int length, unsigned char krok,unsigned char width)
{
  unsigned char i;
	float kkrok = 0;

	HorizontalLine(xstart-15,ystart,15,4,blue1);  // Odsazeni od prvni zobrazovane harmonicke
	HorizontalLine(xstart,ystart,length,4,blue1);
  for(i=0;i<krok;i++){
		VerticalLine(xstart+kkrok,ystart,12,2,blue1);
		VerticalLine(xstart+kkrok+width,ystart,12,2,blue1);
    kkrok += (float)(length-width)/(float)(krok-1);
	}
}
/******************************************************
*                 ---< DrawAxisY >---                 *
*                   Namaluje osuY                     *
*               Vstup: xstart - start v x             *
*                       ystart - start v y            *
*											 length - délka                 *
*											 StartVal - Pocatecni hodnota   *
*											 StopVal  - koncová hodnota     *
*											 krok - krok                    *
*******************************************************/
void DrawAxisY(unsigned int xstart, unsigned int ystart,unsigned int length, unsigned char krok)
{
  unsigned char i;
	unsigned int kkrok = 0;
	
	VerticalLine(xstart,ystart,length,4,blue1);
  for(i=1;i<krok;i++){
		HorizontalLine(xstart-8,ystart+kkrok,8,2,blue1);
    kkrok += length/(krok-1);
	}
}
/******************************************************
*                 ---< DrawAxisYT2 >---               *
*                   Namaluje osuY                     *
*               Vstup: xstart - start v x             *
*                       ystart - start v y            *
*											 length - délka                 *
*											 StartVal - Pocatecni hodnota   *
*											 StopVal  - koncová hodnota     *
*											 krok - krok                    *
*******************************************************/
void DrawAxisYT2(unsigned int xstart, unsigned int ystart,unsigned int length, unsigned char krok)
{
  unsigned char i;
	float kkrok = 0;
	
	VerticalLine(xstart,ystart-2,length,2,grey2);
	ystart += (length/2)-2;
  for(i=1;i<(krok/2)+1;i++){
		HorizontalLine(xstart-8,ystart+kkrok,10,2,grey2);
		HorizontalLine(xstart-8,ystart-kkrok,10,2,grey2);
    kkrok += (float)(length/2)/(float)((krok/2)-1);
	}
}



/***************************************************
*              ---< ReadCommand >---               *
*              Vystup: Data z adresy               *
*              Vstup : Adresa                      *
***************************************************/
char ReadCommand(int adresa){

unsigned char rdata; 

  LCD_cs = 0;
	LCD_dc = 0;
	LCD_wr = 0;
	LCD_io = adresa;
	LCD_wr = 1;
	LCD_dc = 1;
	LCD_io_pfc = 0x0000;  // Ctu port jako in
	LCD_rd = 0;
  delay(10);
	rdata = LCD_io_byte;
	LCD_rd = 1;
	LCD_io_pfc = 0xffff;  // Def. Port jako out
	LCD_cs = 1;
return rdata;	
}

/***************************************************************
*                     ---< DrawTroju >---                      *
*              Vstup: xstart - start v ose x                   *
*                      ystart - start v ose y                  *                  
*											xlenght - delka prepony                  *
*											ylenght - vyska                          *
*											color   - barva                          *
***************************************************************/

void Troj(unsigned int xstart, unsigned int ystart, signed char xlenght, unsigned char ylenght,unsigned int color)
{
  unsigned int i,m;
	if(xlenght > 0){
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      HorizontalLine(xstart,ystart+i,m,1,color);
	  }
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      HorizontalLine(xstart,ystart+(ylenght-i),m,1,color);
	  }
	}
	else{
		xlenght*=-1;
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      HorizontalLine(xstart-m,ystart+i,m,1,color);
	  }
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      HorizontalLine(xstart-m,ystart+(ylenght-i),m,1,color);
	  }
	}	
}
/***************************************************************
*                     ---< DrawTrojuB >---                     *
*              Vstup: xstart - start v ose x                   *
*                      ystart - start v ose y                  *                  
*											xlenght - vyska                          *
*											ylenght - delka prepony                  *
*											color   - barva                          *
***************************************************************/

void TrojB(unsigned int xstart, unsigned int ystart, signed char xlenght, unsigned char ylenght,unsigned int color)
{
  unsigned int i,m;
	
	if(xlenght > 0){
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      VerticalLine(xstart+i,ystart,m,1,color);
	  }
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      VerticalLine(xstart+(ylenght-i),ystart,m,1,color);
	  }
	}
	else{
		xlenght *=-1;
		for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      VerticalLine(xstart+i,ystart-m,m,1,color);
	  }
	  for(i=0;i<=(ylenght/2);i++){
	    m=((xlenght*i)/(ylenght/2));
      VerticalLine(xstart+(ylenght-i),ystart-m,m,1,color);
	  }
	}
}


/***************************************************************
*                       ---< LoadIcon >---                     *
*                 Vstup: xstart - start v ose x                * 
*                        ystart - start v ose y                *
*												 icon   - cislo ikony                  *
*												 activ  - aktivni neaktivni 1/0	       *
***************************************************************/
void LoadIcon(unsigned int xstart, unsigned int ystart, unsigned char icon, unsigned char activ)
{
	
	unsigned char *ukazatel;
	
	ukazatel=(unsigned char*)&(IconText[icon][0]);
  if(activ){
	  DrawBox(xstart,ystart,305,80,blue2);
	  Troj(xstart+7,ystart+5,15,70,blue1);
	  LoadPicture(xstart+40,ystart+15,icon,blue2);
	  WriteText(ukazatel,xstart+100,ystart+16,blue1,blue2,1);

	}
	else{
    DrawBox(xstart,ystart,305,80,white);
	  Troj(xstart+7,ystart+5,15,70,white);
	  LoadPicture(xstart+40,ystart+15,icon,white);
	  WriteText(ukazatel,xstart+100,ystart+16,blue1,white,1);
	}
}
/**************************************************************
*                     ---< LoadMainMenu >---                  *
*                      Nahraje hlavni menu                    *
*           vstup: aktiv - ktery prvek menu ma byt aktivni    *
**************************************************************/	
void LoadMainMenu(unsigned char activ)
{
	unsigned char i,act;
	unsigned int y = 95;


	for(i=0; i<8 ; i++){
	 if((activ-1) == i)act = 1;
	   else act = 0;
	    if((i%2)==0){
		   LoadIcon(0,y,i,act);
		  }	
		  else{
		   LoadIcon(335,y,i,act);
		   y +=85; 
		  }
	}
	OldMenu = activ;
}
/*******************************************************************
*                           ChangeMenu                             *
*         Rozsviti aktualni polozku munu a zhasne starou           *
*******************************************************************/
void ChangeMenu(unsigned char activ)
{
  if((activ%2) != 0) LoadIcon(0,95+(85*((activ-1)/2)),activ-1,1);         // aktivni menu
	else LoadIcon(335,95+(85*((activ-1)/2)),activ-1,1);                     // aktivni menu
  if(OldMenu != activ){
	  if((OldMenu%2) != 0) LoadIcon(0,95+(85*((OldMenu-1)/2)),OldMenu-1,0);   // deaktivuju menu
	  else LoadIcon(335,95+(85*((OldMenu-1)/2)),OldMenu-1,0);                 // deaktivuju menu
	}
	OldMenu = activ;
}

/******************************************************************
*                          ---< ScrollH >---                      *
*     Vstup: MaxHodnot - maximalni pocet rolovani                 *
*            ActualHodnota - aktualni hodnota rolovani            *
******************************************************************/
void ScrollH(unsigned char MaxHodnot,unsigned char ActualHodnota,unsigned char act)
{
 float DelkaScroll;
 
 DelkaScroll = 383/((float)MaxHodnot+1);
 if(act){  // aktivni scroll(po stisknuti set)
	 DrawBox(611,45,29,25,blue3);
	 DrawBox(611,455,29,25,blue3);
   TrojB(613,464,10,23,white);
   TrojB(613,62,-10,23,white);
	 DrawBox(611,71+DelkaScroll*ActualHodnota,29,DelkaScroll,blue1);
   WriteText("Esc",614,(71+DelkaScroll*ActualHodnota)+(DelkaScroll/2-8),white,blue1,3);
   DrawBox(611,(71+DelkaScroll*ActualHodnota)+DelkaScroll,29,454-((71+DelkaScroll*ActualHodnota)+DelkaScroll),blue2);
   DrawBox(611,71,29,DelkaScroll*ActualHodnota,blue2);

 }
 else{  // neaktivni scroll(scroll je videt ale neni aktivni(nebyl stisknut set))
	 DrawBox(611,45,29,25,grey2);
	 DrawBox(611,455,29,25,grey2);
   TrojB(613,464,10,23,black);
   TrojB(613,62,-10,23,black);
	 DrawBox(611,71+DelkaScroll*ActualHodnota,29,DelkaScroll,grey1);
   WriteText("Set",614,(71+DelkaScroll*ActualHodnota)+(DelkaScroll/2-8),white,grey1,3);
   DrawBox(611,(71+DelkaScroll*ActualHodnota)+DelkaScroll,29,455-((71+DelkaScroll*ActualHodnota)+DelkaScroll),grey);
   DrawBox(611,71,29,DelkaScroll*ActualHodnota,grey);

 }	
} 
/******************************************************************
*                          ---< ScrollHT2 >---                      *
*     Vstup: MaxHodnot - maximalni pocet rolovani                 *
*            ActualHodnota - aktualni hodnota rolovani            *
******************************************************************/
void ScrollH2(unsigned char MaxHodnot,unsigned char ActualHodnota,unsigned char act)
{
 float DelkaScroll;
 
 ActualHodnota-=1;
 DelkaScroll = 383/((float)MaxHodnot);
 if(act){  // aktivni scroll(po stisknuti set)
	 DrawBox(611,45,29,25,blue3);
	 DrawBox(611,455,29,25,blue3);
   TrojB(613,464,10,23,white);
   TrojB(613,62,-10,23,white);
	 DrawBox(611,71+DelkaScroll*ActualHodnota,29,DelkaScroll,blue1);
   DrawBox(611,(71+DelkaScroll*ActualHodnota)+DelkaScroll,29,454-((71+DelkaScroll*ActualHodnota)+DelkaScroll),blue2);
   DrawBox(611,71,29,DelkaScroll*ActualHodnota,blue2);

 }
 else{  // neaktivni scroll(scroll je videt ale neni aktivni(nebyl stisknut set))
	 DrawBox(611,45,29,25,grey2);
	 DrawBox(611,455,29,25,grey2);
   TrojB(613,464,10,23,black);
   TrojB(613,62,-10,23,black);
	 DrawBox(611,71+DelkaScroll*ActualHodnota,29,DelkaScroll,grey1);
   DrawBox(611,(71+DelkaScroll*ActualHodnota)+DelkaScroll,29,455-((71+DelkaScroll*ActualHodnota)+DelkaScroll),grey);
   DrawBox(611,71,29,DelkaScroll*ActualHodnota,grey);

 }	
} 


/******************************************************************
*                          ---< ScrollV >---                      *
*     Vstup: MaxHodnot - maximalni pocet rolovani                 *
*            ActualHodnota - aktualni hodnota rolovani            *
*            ActualHodnota 1 - aktivni                            *
*                          0 - neaktivni                          *
*													 2 - neaktivni protoze je aktivni Zoom  *
******************************************************************/
void ScrollV(unsigned char MaxHodnot,unsigned char ActualHodnota,unsigned char act)
{
 float DelkaScroll;
 
 DelkaScroll = 588/((float)MaxHodnot+1);
 switch(act){

   case 0:
  	 DrawBox(0,451,25,29,grey2);
	   DrawBox(615,451,25,29,grey2);
     Troj(623,453,10,23,black);
     Troj(16,453,-10,23,black);
	   DrawBox(26+DelkaScroll*ActualHodnota,451,DelkaScroll,29,grey1);
     WriteText("Set",(26+DelkaScroll*ActualHodnota)+(DelkaScroll/2-11),457,white,grey1,3);
     DrawBox((26+DelkaScroll*ActualHodnota)+DelkaScroll,451,614-((26+DelkaScroll*ActualHodnota)+DelkaScroll),29,grey);
     DrawBox(26,451,DelkaScroll*ActualHodnota,29,grey);
     break;

	 case 1:
	   DrawBox(0,451,25,29,blue3);
	   DrawBox(615,451,25,29,blue3);
     Troj(623,453,10,23,white);
     Troj(16,453,-10,23,white);
	   DrawBox(26+DelkaScroll*ActualHodnota,451,DelkaScroll,29,blue1);
     WriteText("Esc",(26+DelkaScroll*ActualHodnota)+(DelkaScroll/2-11),457,white,blue1,3);
     DrawBox((26+DelkaScroll*ActualHodnota)+DelkaScroll,451,614-((26+DelkaScroll*ActualHodnota)+DelkaScroll),29,blue2);
     DrawBox(26,451,DelkaScroll*ActualHodnota,29,blue2);
     break;

   case 2:
  	 DrawBox(0,451,25,29,grey2);
	   DrawBox(615,451,25,29,grey2);
     Troj(623,453,10,23,black);
     Troj(16,453,-10,23,black);
	   DrawBox(26+DelkaScroll*ActualHodnota,451,DelkaScroll,29,grey1);
//     WriteText("Esc",(26+DelkaScroll*ActualHodnota)+(DelkaScroll/2-11),457,white,grey1,3);
     DrawBox((26+DelkaScroll*ActualHodnota)+DelkaScroll,451,614-((26+DelkaScroll*ActualHodnota)+DelkaScroll),29,grey);
     DrawBox(26,451,DelkaScroll*ActualHodnota,29,grey);
     break;
 }	
}

/*************************************************************
*                     ---< ShowZoom >---                     *
*************************************************************/
void ShowZoom(unsigned int xstart, unsigned int ystart,unsigned char act,unsigned char Actual_Zoom)
{
  if(act){
	  WriteText("Zoom",xstart,ystart,blue1,white,3);
 		if(Actual_Zoom < 15){	
  		TrojB(xstart-3,ystart,-21,42,blue3);
			WriteCharacter('+',xstart+15,ystart-17,white,blue3,3);	
		}
		else{
  		TrojB(xstart-3,ystart,-21,42,grey2);
			WriteCharacter('+',xstart+15,ystart-17,white,grey2,3);	
		}
		if(Actual_Zoom > 0){			
      TrojB(xstart-3,ystart+17,21,42,blue3);
  	  WriteCharacter('-',xstart+16,ystart+17,white,blue3,3);
  	}	
		else{
		  TrojB(xstart-3,ystart+17,21,42,grey2);
  	  WriteCharacter('-',xstart+16,ystart+17,white,grey2,3);
		}			
	}
	else{
	  WriteText("Zoom",xstart,ystart,grey1,white,3);
		TrojB(xstart-3,ystart,-21,42,grey2);
    TrojB(xstart-3,ystart+17,21,42,grey2);	
		WriteCharacter('+',xstart+15,ystart-17,white,grey2,3);	
		WriteCharacter('-',xstart+16,ystart+17,white,grey2,3);	

	}   	
}
/***************************************************************************
*                            ---< CircleClean >---                         *
***************************************************************************/
void CircleClear(signed int xstart1, signed int ystart1,signed int xstart2,signed int ystart2,signed int xstart3, signed int ystart3,signed int xstart4,signed int ystart4,  unsigned char polomer,unsigned int color1,unsigned color2)
{
 signed int m,j=0,i;
 signed int uh,uhh,uhhOld=0;
 float ux;

  for(i=0;i<=90;i++){
    ux= i*0.017453292;
    uh = (signed int)(cos(ux)*polomer);
	  uhh = (signed int)(sin(ux)*polomer);
	  if(uhh >= uhhOld){
		  if(j<=polomer){
        for(m=0;m<=(uh*2);m++){
  		    if((pow((((xstart1-uh)+m)-xstart2),2)+pow(((ystart1+uhh)-ystart2),2))>(signed int)pow(polomer,2)){
            SetPixel((xstart1-uh)+m,ystart1+uhh,white);
					}
   		    if((pow((((xstart1-uh)+m)-xstart2),2)+pow(((ystart1-uhh)-ystart2),2))>(signed int)pow(polomer,2)){
   		      SetPixel((xstart1-uh)+m,ystart1-uhh,white);
		      }
					if((pow((((xstart3-uh)+m)-xstart4),2)+pow(((ystart3+uhh)-ystart4),2))>(signed int)pow(polomer,2)){
            SetPixel((xstart3-uh)+m,ystart3+uhh,white);
					}
					if((pow((((xstart3-uh)+m)-xstart4),2)+pow(((ystart3-uhh)-ystart4),2))>(signed int)pow(polomer,2)){
			      SetPixel((xstart3-uh)+m,ystart3-uhh,white);
		      }
  	    
				  SetPixel((xstart2-uh)+m,ystart2-uhh,color1);
          SetPixel((xstart2-uh)+m,ystart2+uhh,color1);

		      SetPixel((xstart4-uh)+m,ystart4-uhh,color2);
          SetPixel((xstart4-uh)+m,ystart4+uhh,color2);
	      }
			  j++;
			}
			uhhOld = uhh+1; 
	  }
	}
}
/***************************************************************************
*                              ---< Circle >---                            *
***************************************************************************/
void Circle(unsigned int xstart, unsigned int ystart,unsigned char polomer,unsigned int color)
{
 signed int m,j=0,i;
 signed int uh,uhh,uhhOld=0;
 float ux;
 
	for(i=0;i<=90;i++){
    ux= i*0.017453292;
    uh = (signed int)(cos(ux)*polomer);
	  uhh = (signed int)(sin(ux)*polomer);
	  if(uhh >= uhhOld){
		  if(j<=polomer){
        for(m=0;m<=(uh*2);m++){
            SetPixel((xstart-uh)+m,ystart+uhh,color);
   		      SetPixel((xstart-uh)+m,ystart-uhh,color);
	      }
			  j++;
			}
			uhhOld = uhh+1; 
	  }
	}
}

/***************************************************************************
*                             ---< DrawCircle >                            *
***************************************************************************/
void DrawCircle(unsigned int xstart,unsigned int ystart,unsigned int polomer,unsigned int color)

{
  unsigned int startx,starty,endx,endy;
  float uh,ux;
	signed int dx,dy;
	unsigned int i;
	
	startx = xstart + polomer;
	starty = ystart;
	for(i=1;i<360;i++){
	  ux= i*0.017453292;
    dx = (unsigned int)(cos(ux)*polomer);
		if(i==0 || i == 180 || i == 360){
			dy = 0;
		}
		else{
      dy = sqrt((polomer*polomer) - (fabs(dx)*fabs(dx)));
		}
    if(i>=180){
      dy = -dy;
	  }
		endx = xstart+dx;
		endy = ystart+dy;
		DrawFreeLine(startx,starty,endx,endy,1,color);
		startx = endx;
		starty = endy;
	}
	DrawFreeLine(startx,starty,xstart + polomer,ystart,1,color);  // Dotahnu do nuly
}
/********************************************************************************
*                          ---< RadioButton >---                                *
********************************************************************************/
void RadioButton(unsigned int xstart, unsigned int ystart, unsigned int color,unsigned int color_screen,unsigned char act)
{
  unsigned int m,j=0,i;
  unsigned int uh,uhh,uhhOld=0;
  float ux;

  if(act){
    Circle(xstart, ystart,4,color);
	}
	else{
		Circle(xstart, ystart,4,color_screen);
	} 
	for(i=0;i<=90;i++){
    ux= i*0.017453292;
    uh = (unsigned int)(cos(ux)*8);
	  uhh = (unsigned int)(sin(ux)*8);
	  //---< Obrysove kolecko>--
		SetPixel(xstart+uh,ystart-uhh,color);
		SetPixel(xstart-uh,ystart-uhh,color);
		SetPixel(xstart+uh,ystart+uhh,color);
		SetPixel(xstart-uh,ystart+uhh,color);
	  if(uhh >= uhhOld){
		  if(j<=8){
        for(m=0;m<=uh;m++){
 		      if((((unsigned int)pow(m,2)+(unsigned int)pow(uhh,2))>16) && (((unsigned int)pow(m,2)+(unsigned int)pow(uhh,2))<49)){
            SetPixel(xstart+m,ystart+uhh,color_screen);
            SetPixel(xstart-m,ystart+uhh,color_screen);
				  }
   		    if((((unsigned int)pow(m,2)+(unsigned int)pow(uhh,2))>16) && (((unsigned int)pow(m,2)+(unsigned int)pow(uhh,2))<49)){
  		      SetPixel(xstart+m,ystart-uhh,color_screen);
            SetPixel(xstart-m,ystart-uhh,color_screen);
					}
		    }
	    }
			j++;
		}
		uhhOld = uhh+1; 
	}
}
