//--------< Funkce pouzute v menu >---------
#include "menu_function.h"
#include "globvar.h"
#include "math.h"
#include "graphics.h" 
#include "tables.h"
#include "gpla.h"


/******************************************************************************************
*                            ---< ShowJednotky >---                                       *
*      Funkce zobrazi jednotky a jejich rady a vrati cislo v                              * 
*                        prislusnem radu k zobrazeni                                      *
*          Vstup: xstart - start v ose x                                                  *
*                 ystart - start v ose y                                                  *
*									ZakJed - zakladni jednotka                                              *
*									cislo  - Cislo,ktere se ma zobrazit                                     *
*									color  - barva pisma                                                    *
*                 size   - Velikost pisma                                                 *
*                 funct  - 0 - vrati cislo v prislusnem radu a zobrazi jednotky           *
*                          1 - Zobrazi pouze jednotky a vrati odkud se ma dal psat        *
*                          2 - Vrati Cislo v radu                                         *
*                          3 - Napise jednotky ze konci v xstart vrati kde zacinaji       *
*******************************************************************************************/
float ShowJednotky(unsigned int xstart, unsigned int ystart, unsigned char *line,float number, unsigned int color,unsigned int color_screen,unsigned char size,unsigned char funct)
{
	
	unsigned char jednotky[5];
	unsigned char m = 0,i;
	unsigned int from;

	if(fabs(number) >= 1000000){ // mega
		number/=1000000;
		jednotky[m] = 'M';  
		m++;
	}
	else{
	  if(fabs(number) >= 1000){    // kilo
		  number/=1000;
		  jednotky[m] = 'k';  
		  m++;
	  }
	}
	while(*line){
		jednotky[m] = *line;  // napisu zakladni jednotku
		line++;
		m++;
	}
  if(!funct){
  	for(i=m;i<4;i++){      // Udelam korekci kdyby se cislo zkratilo
	  	jednotky[i] =' ';
	  }
	  jednotky[4] = 0; // Zarazka pro zobrazeni 
    WriteText(&jednotky[0],xstart,ystart,color,color_screen,size); // Na danou pozici napisu jednotky
  	return(number);
 	}
	else{
	  if(funct == 1){
    	jednotky[m] = 0; // Zarazka pro zobrazeni 
      from=WriteTextB(&jednotky[0],xstart,ystart,color,color_screen,size); // Na danou pozici napisu jednotky
    	return(from);
		}
		else{
			if(funct == 3){
    	  jednotky[m] = 0; // Zarazka pro zobrazeni 
        from=WriteTextAligR(&jednotky[0],xstart,ystart,color,color_screen,size); // Na danou pozici napisu jednotky
        return(from);
			}
			return(number);
		} 	
	}
}
/****************************************************************************
*                            ---< MaxMinAvg >---                            *
* Funkce nakresli hodnotu zadanou v numer v prislusnym radu s jednotkou     *
*         Vyuzito pri zobrazeni MAX, MIN,AVG hodnoty v numeric              *
*           vstup: xstart                                                   *
*                   ystart                                                  *
*									 jednotky - zakladni jednotka [V,A...]                    *
*									 color                                                    *
*									 color_screen                                             *
*									 use      - 0/1 1 - zobrazi se cislo 0 - zobrazi se -,--  * 
*									 PlatCisl - Pocet platnych cislic 3 nebo 4                *
****************************************************************************/
void MaxMinAvg(unsigned int xstart, unsigned int ystart, unsigned char *jednotky,float number, unsigned int color, unsigned int color_screen,unsigned char use,unsigned char PlatCisl)
{
  unsigned int pomocna,TextFrom;
	float NumberShow;

  pomocna = xstart+(11*PlatCisl)+6; // 11- sirka cislic 6 sirka des. carky
  NumberShow = ShowJednotky(0,0,0,number,0,0,0,2);  // Vrati cislo v radu
	ShowNumber(pomocna,ystart,xstart,NumberShow,0,color,color_screen,4,use,PlatCisl);
  if(!use){
    TextFrom = ShowJednotky(pomocna,ystart,jednotky,number,color,color_screen,4,1);    // Zobrazi jednotky a rekne odkud psat
	}
	else{
	  TextFrom = WriteTextB(jednotky,pomocna,ystart,color,color_screen,4);	
	} 
	DrawBox(TextFrom,ystart,640-TextFrom,19,color_screen);
}
/****************************************************************************
*                             ---< ShowNumber >---                          *
*              Pise hodnotu z prava do leva(xstart,ystart konec cisla)      *
*          Vstup: xstart   - kde ma text konci v x                          *
*                 ystart   - kde ma text koncit v y                         *
*                 xend     - kde zacina okno textu                          *
*                 znamenko - hodnota je znamenkova                          *
*                 color    - barva pisma                                    *
*                 color_screen - barva pozadi                               *
*                 size         - velikost pisma                             *
*                 use          - 0/1 0 - zobrazi se cislo                   *
*                                    1 - zobrazi se hodnota nepouzita       *
*                 PlatCis      - 3/4 Pocet platnzch cislic(jen 3 nebo 4)    *																		 
*****************************************************************************/
void ShowNumber(unsigned int xstart, unsigned int ystart,unsigned int xend, float number,unsigned char znamenko,unsigned int color,unsigned int color_screen, unsigned char size,unsigned char use,unsigned char PlatCis)
{

  unsigned char i,j=0;
	unsigned int TextEnd,pomocna;

  if(!use){
    if(!number){
	    j=1;	
	  }
	  else{ 
	    if(number<1)j=2;
	    else{
	      if(number<10)j=PlatCis-1;
		    else{
		      if(number<100)j=PlatCis-2;
			    else{
			      j=PlatCis-3;
			    }	
		    }	
	    }
	  }
  	NumToStr(number,5,j,znamenko,0,0);
	  TextEnd = WriteTextAligR(&cislo[0],xstart,ystart,color,color_screen,size); 
    j = FontHigh(size);
		if(TextEnd>xend){  // Kdyby byl text delsi nez vymezena oblast tak aby nedoslo k zaseku
      DrawBox(xend,ystart,TextEnd-xend,j,white);
		}
	}
	else{
	  TextEnd = WriteTextAligR("-,--  ",xstart,ystart,color,color_screen,size); 
    j = FontHigh(size);
		if(TextEnd>xend){  // Kdyby byl text delsi nez vymezena oblast tak aby nedoslo k zaseku
      DrawBox(xend,ystart,TextEnd-xend,j,color_screen);
		}
	} 
}  
/*****************************************************************************
*                             ---< TextIndex >---                            *
*                 Napise text s indexem. Pr. U1: ( 1 je mala )               *
*****************************************************************************/
void TextIndex(unsigned int xstart, unsigned int ystart, unsigned char *text, unsigned char cislo,unsigned int color, unsigned int color_screen)
{
  unsigned int TextFrom;
	
	TextFrom = WriteTextB(text,xstart,ystart,color,color_screen,2);  // Napisu text (U)
	TextFrom = WriteCharacterB(cislo,TextFrom, ystart+40,color,color_screen,0);  // Napisu cislu(U1)
	WriteCharacter(':',TextFrom, ystart, color, color_screen,2);  // Napisu delitko  (U1:) 
}
/*****************************************************************************
*                          ---< ShowHarmonic >---                            *
*                    Zobrazeni harmonickych v numeric                        * 
*          Vstup: StartLich - Kde v tabulce SumVal zacinaji lich. har.       *
*                 StartSud  - Kde v tabulce SumVal zacinaji Sud. har.        *
*									faze - urcuje ktera faze se ma zobrazit                    *
*****************************************************************************/
void ShowHarmonic(unsigned char StartLich,unsigned char StartSud, unsigned char faze)
{
	unsigned char i,m=0;
	unsigned int TextFrom;

	if(flag.Change){  // harm
    CleanScreen(white);
	  DrawBox(0,45,100,435,blue2);  // Modre pruhy
	  DrawBox(310,45,100,435,blue2);
	  for(i=0;i<10;i++){
	    WriteText("Ha",10,60+40*i,black,blue2,0);  // Napisu static text
		  WriteText("Ha",320,60+40*i,black,blue2,0);
		  WriteText("%",515,60+40*i,black,white,0);
			WriteText("%",205,60+40*i,black,white,0);
		}
	}
  if(flag.Scroll){
	  ScrollH(max_scroll,menu_scroll,1);   // Scroll lista aktivni
	}
	else{
	  ScrollH(max_scroll,menu_scroll,0);  // Scroll lista neaktivni
	}
	// Leva cast harmonickych
	for(i=2+menu_scroll*2;i<22+menu_scroll*2;i+=2){  // pisu cisla a hodnoty a scrolluju
	  if(i >= 10){                                   // Cislo harmonicke je >10 trochu jinak se zobrazuje nez kdyz je <10
		  NumToStr(i,1,0,0,0,1);
      TextFrom = WriteTextB(&cislo[0],52,60+40*m,black,blue2,0);
			WriteText(":",TextFrom,60+40*m,black,blue2,0);	
		}
		else{                                          // Cislo je >10
		  NumToStr(i,0,0,0,0,1);
			TextFrom = WriteTextB(&cislo[0],52,60+40*m,black,blue2,0);
			WriteText(":  ",TextFrom,60+40*m,black,blue2,0);	
		}
		NumToStr(ShowVal[(StartSud-2)+i][faze],4,2,0,0,1);  // Sude harmonicke v leve casti
		WriteText(&cislo[0],125,60+40*m,black,white,0);
    if(((22+menu_scroll*2)<=50) && (i != 50)){  // 50 harmonicka vyjde na samostatny radek
		  WriteText("Ha",320,420,black,blue2,0);
			WriteText("%",515,420,black,white,0);  	 
		}
	  if(i != 50){
		// Prava cast harmonickych
		  if(i+1>=10){
			  NumToStr(i+1,1,0,0,0,1);
				TextFrom = WriteTextB(&cislo[0],360,60+40*m,black,blue2,0);
				WriteText(":",TextFrom,60+40*m,black,blue2,0);
			}
			else{
			  NumToStr(i+1,0,0,0,0,1);
				TextFrom = WriteTextB(&cislo[0],360,60+40*m,black,blue2,0);
				WriteText(":  ",TextFrom,60+40*m,black,blue2,0);
			}
			NumToStr(ShowVal[(StartLich-2)+i][faze],4,2,0,0,1);  // liche harmonicke v prave casti
			WriteText(&cislo[0],435,60+40*m,black,white,0);
		}
		else{  // 50 harmonicka je sama na radku proto smazu pravou cast 
		  DrawBox(310,420,100,60,blue2);
			DrawBox(410,420,150,60,white);
		}	
    m++;	
	}
}

/*****************************************************************************
*                                ---< OsaX >---                              *
*  Funkce zobrazi osu X/ Vstupem je cislo ktere bude nad osou zobrazeno      *
*	         Funkce vrati pocet pixlu reprezentujicich vstupni cislo           *
*              Vstup: xstart - start v ose x                                 *
*                      ystart - start v ose y                                *
*											DelkaOsy - Delka osy                                   *
*											Jednotky - Jednotkz osy (V,A)                          *
*											number   - cislo k prevodu                             *
*											osa      - ktera to je osa 1-4                         *
*											rad      - 1,10,100                                    *
*											krok     - kolik ma osa bodu(zadavat licha cisla 7,9)  *
*****************************************************************************/
unsigned int OsaX(unsigned int xstart,unsigned int ystart,unsigned int DelkaOsy, unsigned char *jednotky,float number,unsigned char osa,unsigned char rad,unsigned char krok)
{
	
	
	unsigned char i;
	unsigned int okno,LengthX,KrokRozsah,ZobrazHodnota,BeginRozsah,TextFrom;
  float hodnota;
	
	number = fabs(number); 
  DrawAxisX(xstart,ystart,DelkaOsy,krok);  // Nakreslim osu
  okno = (krok-3)*rad;
	KrokRozsah = okno/2;
  hodnota = ShowJednotky(xstart+DelkaOsy+30,ystart+15,(unsigned char*)jednotky,number,blue1,white,0,0);  // Jednotky
  if(hodnota > okno){  // Najdu mez osy
    while(hodnota>PoleRozsahu[osa]){
      PoleRozsahu[osa] += KrokRozsah;
	  }
		while(hodnota<(PoleRozsahu[osa]-okno)){ // Musim porovnavat se spodni hranou okna proto 40!!!
		  PoleRozsahu[osa] -= KrokRozsah;
		}
		hodnota -= PoleRozsahu[osa]-okno;
    hodnota = (hodnota*((float)DelkaOsy/((okno+KrokRozsah))))+(DelkaOsy/(krok-1));
    BeginRozsah = (PoleRozsahu[osa]-okno)-rad;
	}
  else{
		PoleRozsahu[osa] = 0;
    BeginRozsah = 0;
		hodnota = (hodnota*((float)DelkaOsy/((okno+KrokRozsah))));
	}
	for(i=0;i<krok;i++){  // Ocisluju osu
    ZobrazHodnota = BeginRozsah+rad*i; 
    NumToStr(ZobrazHodnota,2,0,0,0,0);
		if(ZobrazHodnota < 10){
			TextFrom = WriteTextB(&cislo[0],((xstart+(DelkaOsy/(krok-1))*i)-40),ystart+15,blue1,white,0);
      DrawBox(TextFrom,ystart+15,20,35,white);
		}
		else{
      if(ZobrazHodnota < 100){
        TextFrom = WriteTextB(&cislo[0],((xstart+(DelkaOsy/(krok-1))*i)-32),ystart+15,blue1,white,0);
				DrawBox(TextFrom,ystart+15,10,35,white);
	    }
	    else{
	      WriteText(&cislo[0],((xstart+(DelkaOsy/(krok-1))*i)-23),ystart+15,blue1,white,0);
	    }
		}
	} 
	return((unsigned int) hodnota); 
}
/***********************************************************************
*                         ---< ZoomHarmVol >---                        *
*        Dela zoom a zobrazuje scroll a zoom u harmonickych napeti     *
*                        Vraci Nasobitel napeti                        *
***********************************************************************/
void ZoomHarmVol(unsigned char faze)
{
	float Zoom,Step_Zoom;
	unsigned char i,m=0,j;
	unsigned int TextFrom; 

  if(flag.Change){  // Harm U1
	  CleanScreen(white);
		DrawAxisXT2(70,395,520,13,10);
		DrawAxisY(55,69,330,11);
		WriteText("[%]",610,420,black,white,3);
		Screen_Zoom = 9; // Vychozi hodnota pro zoom
	}
 	// Zoom
	if(Screen_Zoom == 0){
	  Zoom = 3.25;  // 100%
		for(i=1;i<11;i++){
		  NumToStr(10*i,2,0,0,0,1);
			TextFrom = WriteTextB(&cislo[0],10,60+(10-i)*33,black,white,3);
			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
		}
	}
	else{
	  if(Screen_Zoom<=9){
		  Step_Zoom = (100-Screen_Zoom*10);
			Zoom = 325/Step_Zoom;
			for(i=1;i<11;i++){
			  NumToStr((Step_Zoom/10)*i,2,0,0,0,1);
				TextFrom = WriteTextB(&cislo[0],10,60+(10-i)*33,black,white,3);
  			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
  		}
		}
		else{
		  if(Screen_Zoom<=13){
		    Step_Zoom = (10-(2*(Screen_Zoom-9)));
				Zoom = 325/Step_Zoom;
				for(i=1;i<11;i++){
				  NumToStr((Step_Zoom/10)*i,3,1,0,0,1);
					TextFrom = WriteTextB(&cislo[0],5,60+(10-i)*33,black,white,3);
    			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
				}
			}
			else{
  		  Step_Zoom = (2-(0.5*(Screen_Zoom-13)));
				Zoom = 325/Step_Zoom;
	  	  for(i=1;i<11;i++){
				  NumToStr((Step_Zoom/10)*i,4,2,0,0,1);
					TextFrom = WriteTextB(&cislo[0],2,60+(10-i)*33,black,white,3);
     			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
				}
			}	
		}						 
	}
  if(flag.Zoom){
	  ShowZoom(600,70,1,Screen_Zoom);
		ScrollV(max_scroll,menu_scroll,2);  // Je zoom scroll neaktivni
	}
	else{
    ShowZoom(600,70,0,Screen_Zoom);     // Neaktivni zoom
  	if(flag.Scroll){                    // Neaktivni zoom povoluje se scroll
  	  ScrollV(max_scroll,menu_scroll,1);
	  }
		else{
		  ScrollV(max_scroll,menu_scroll,0);
		}
  }
	for(i=menu_scroll*3;i<13+menu_scroll*3;i++){
 	  if((ShowVal[9+i][faze]*Zoom)< 325){  // Zobrazuju sloupce
	    DrawBox(70+m*42.5,70,12,325-(ShowVal[9+i][faze]*Zoom),white);
		  DrawBox(70+m*42.5,395-(ShowVal[9+i][faze]*Zoom),12,ShowVal[9+i][faze]*Zoom,ColorPhase[faze]);
	  }
	  else{
		  DrawBox(70+m*42.5,70,12,324,black);
		}
		//--- Ocisluju osu X
    NumToStr((i+2),1,0,0,0,1);
    if(i+2<10){
		  TextFrom = WriteTextB(&cislo[0],63+m*42.5,405,black,white,3);
		 	DrawBox(TextFrom,405,5,16,white);
		}
		else{
		  WriteText(&cislo[0],68+m*42.5,405,black,white,3);
		}	
		NumToStr(ShowVal[9+i][faze],4,2,0,0,1);
		if(ShowVal[9+i][faze] < 10){
		  WriteText(&cislo[0],55+m*42.5,420,black,white,3);
    }
		else{
	    WriteText(&cislo[0],60+m*42.5,405,black,white,3);
    }	
    m++;
	}
}

/***********************************************************************
*                         ---< ZoomHarmCur >---                        *
*        Dela zoom a zobrazuje scroll a zoom u harmonickych napeti     *
*                        Vraci Nasobitel napeti                        *
***********************************************************************/
void ZoomHarmCur(unsigned char faze)
{
	float Zoom,Step_Zoom;
	unsigned char i,m=0,j;
	unsigned int TextFrom; 

  if(flag.Change){  // Harm I3
    CleanScreen(white);
	  DrawAxisXT2(70,395,520,13,10);
	  DrawAxisY(55,69,330,11);
	  WriteText("[%]",610,420,black,white,3);
	  Screen_Zoom = 5; // Vychozi hodnota pro zoom
	}
 	// Zoom
	if(Screen_Zoom == 0){
	  Zoom = 1.083;  // 300%
		for(i=1;i<11;i++){
		  NumToStr(30*i,2,0,0,0,1);
			TextFrom = WriteTextB(&cislo[0],13,60+(10-i)*33,black,white,3);
			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
		}
	}
	else{
	  if(Screen_Zoom<=5){
		  Step_Zoom = (300-Screen_Zoom*40);
			Zoom = 325/Step_Zoom;
			for(i=1;i<11;i++){
			  NumToStr((Step_Zoom/10)*i,3,0,0,0,1);
				TextFrom = WriteTextB(&cislo[0],5,60+(10-i)*33,black,white,3);
  			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
  		}
		}
		else{
		  if(Screen_Zoom<=11){
		    Step_Zoom = (100-(15*(Screen_Zoom-5)));
				Zoom = 325/Step_Zoom;
				for(i=1;i<11;i++){
				  NumToStr((Step_Zoom/10)*i,3,1,0,0,1);
					TextFrom = WriteTextB(&cislo[0],10,60+(10-i)*33,black,white,3);
    			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
				}
			}
			else{
  		  Step_Zoom = (10-(2.25*(Screen_Zoom-11)));
				Zoom = 325/Step_Zoom;
	  	  for(i=1;i<11;i++){
				  NumToStr((Step_Zoom/10)*i,4,2,0,0,1);
					TextFrom = WriteTextB(&cislo[0],2,60+(10-i)*33,black,white,3);
     			DrawBox(TextFrom,60+(10-i)*33,45-TextFrom,16,white);
				}
			}	
		}						 
	}
  if(flag.Zoom){
	  ShowZoom(600,70,1,Screen_Zoom);
		ScrollV(max_scroll,menu_scroll,2);  // Je zoom scroll neaktivni
	}
	else{
    ShowZoom(600,70,0,Screen_Zoom);     // Neaktivni zoom
  	if(flag.Scroll){                    // Neaktivni zoom povoluje se scroll
  	  ScrollV(max_scroll,menu_scroll,1);
	  }
		else{
		  ScrollV(max_scroll,menu_scroll,0);
		}
  }
  for(i=menu_scroll*3;i<13+menu_scroll*3;i++){
 	  if((ShowVal[59+i][faze]*Zoom)< 325){  // Zobrazuju sloupce
	    DrawBox(70+m*42.5,70,12,325-(ShowVal[59+i][faze]*Zoom),white);
		  DrawBox(70+m*42.5,395-(ShowVal[59+i][faze]*Zoom),12,ShowVal[59+i][faze]*Zoom,ColorPhase[4+faze]);
	  }
	  else{
		  DrawBox(70+m*42.5,70,12,324,black);
		}
		//--- Ocisluju osu X
    NumToStr((i+2),1,0,0,0,1);
    if(i+2<10){
		  TextFrom = WriteTextB(&cislo[0],63+m*42.5,405,black,white,3);
		 	DrawBox(TextFrom,405,5,16,white);
		}
		else{
		  WriteText(&cislo[0],68+m*42.5,405,black,white,3);
		}	
		NumToStr(ShowVal[59+i][faze],4,2,0,0,1);
		if(ShowVal[59+i][faze] < 10){
		  WriteText(&cislo[0],55+m*42.5,420,black,white,3);
    }
		else{
	    WriteText(&cislo[0],60+m*42.5,405,black,white,3);
    }	
    m++;
	}
}
/**********************************************************************************************
*                              ---< Scope >---                                                *
*                            Nakresli prubeh Scope                                            *
**********************************************************************************************/
void DrawScope(unsigned char Typ)
{ 
	unsigned char To,Cyklu,Posun,CasZak;
	unsigned char PoleMereni[5]; // Bude mi rikat, ktere faze mam merit	
  signed int x[5][2],y[5][2];
  signed int startx[5][2],starty[5][2],DyMax[2];
  unsigned int i,j,k,l,m = 0,color;
	unsigned int MaxHodnota = 0;
	float hodnota,prevod;
	float ScopeRozsah[4];
  
	CasZak = 2;        // Casova zakladna
  PoleMereni[0] = 1; // zakladni cara je vzdy	
	if(PoleRozsahu[0]){  // Urceni stareho rozsahu
    ScopeRozsah[1] = 150/((float)PoleRozsahu[0]);
	}
	else{
		ScopeRozsah[1] = 0; // Pokud je PoleRozsahu = 0, jdu tudy poprve
	}
	//---< Def.Par >---	 
  dy[0][0] = 0;   // dy je pro osu X vzdy 0
  dy[0][1] = 0;
	starty[0][1] = 208; // pro osu y je start vzdy stejny
	starty[0][0] = 208;

//----------------< Najdu rozsahy >-----------------		
	switch(Typ){  // Podle typu Scope musim posouvat "ukazatel" v PoleScope
	  case 0: Posun=0;
		        prevod = PrevodU;
	      	  for(i=0;i<4;i++){
		          if((ParamChar[0]&(0x80>>i))){
                PoleMereni[i+1] =1;
			          for(j=0;j<257;j++){
                  if(MaxHodnota < fabs(PoleScope[j][i]))MaxHodnota = fabs(PoleScope[j][i]);
			          } 			
		          }
		          else{
	              PoleMereni[i+1] = 0;		
		          }
            }
//---< Najdu rozsah napeti >----
            hodnota = ShowJednotky(580,45,"V",(float)(MaxHodnota*prevod),white,white,4,3);  // Jednotky (V PoleRozsahu[7] Max hodnota vsech 3 fazi. Rozsah se urcuje podle max)
           	if(hodnota > 40){  // Najdu mez osy
              while(hodnota>=PoleRozsahu[0]){
                PoleRozsahu[0] += 20;
	            }
	            while(hodnota<=(PoleRozsahu[0]-40)){ // Musim porovnavat se spodni hranou okna proto 40!!!
	              PoleRozsahu[0] -= 20;
	            }
		          ScopeRozsah[0] = 150/((float)PoleRozsahu[0]);
	          }
            else{// Pokud je mensi nez 50 nebo 0
	          }					
	        	break;
		case 1: Posun=4;
		        prevod = PrevodI;
	      	  for(i=0;i<4;i++){
		          if((ParamChar[1]&(0x80>>i))){
                PoleMereni[i+1] =1;
			          for(j=0;j<257;j++){
                  if(MaxHodnota < fabs(PoleScope[j][Posun+i]))MaxHodnota = fabs(PoleScope[j][Posun+i]);
			          } 			
		          }
		          else{
	              PoleMereni[i+1] = 0;		
		          }
            }
//---< Najdu rozsah proudu >----
            hodnota = ShowJednotky(580,45,"A",(float)(MaxHodnota*prevod),white,white,4,3);  // Jednotky (V PoleRozsahu[7] Max hodnota vsech 3 fazi. Rozsah se urcuje podle max)
            if(hodnota > 4){  // Najdu mez osy
              while(hodnota>PoleRozsahu[0]){
                PoleRozsahu[0] += 2;
	            }
		          while(hodnota<(PoleRozsahu[0]-4)){ // Musim porovnavat se spodni hranou okna proto 6!!!
		            PoleRozsahu[0] -= 2;
		          }
		          ScopeRozsah[0] = 150/((float)PoleRozsahu[0]);
	          }
            else{// Pokud je mensi nez 6 nebo 0
		          ScopeRozsah[0] = 150/(float)4;
			        PoleRozsahu[0] = 4;
            }
        		break;
	}
//-----------------------< Zobrazeni prubehu >---------------------------
  while(m<258){
	  DyMax[0] = 0;	
	  DyMax[1] = 0;	
		if(m>1){
	    for(i=0;i<4;i++){
  	    dy[i+1][1] = ((PoleScope[m-2][Posun+i]-PoleScope[m-1][Posun+i])*prevod)*ScopeRozsah[0];
	  	  startx[i+1][1] = 74+(m-2)*2;
  	    starty[i+1][1] = 208 - (PoleScope[(m-2)][Posun+i]*prevod)*ScopeRozsah[0];
  		  x[i+1][1]	= 0;
	  	  y[i+1][1] = 0;
			  To = 2; 
				if(DyMax[1]<=fabs(dy[i+1][1]))DyMax[1]=fabs(dy[i+1][1]);  // Najdu Max dy pozdeji z toho urcim kolem mam udelat cyklu
			}
  	  startx[0][1] = 74+(m-2)*2;
 		  x[0][1]	= 0;
  	  y[0][1] = 0;
		}
		else{
			To = 1;  // Mazu o dva cykli drive(aby v sinu nebyli mezery) proto tento nesmysl
		}
		if(m<257){
			for(i=0;i<4;i++){
        dy[i+1][0] = ((ScopeOld[m][i]-ScopeOld[m+1][i])*prevod)*ScopeRozsah[1];
   		  startx[i+1][0] = 74+m*2;
		    starty[i+1][0] = 208 -(ScopeOld[m][i]*prevod)*ScopeRozsah[1];  
  		  x[i+1][0]	= 0;
	  	  y[i+1][0] = 0;
				if(DyMax[0]<=fabs(dy[i+1][0]))DyMax[0]=fabs(dy[i+1][0]); 
  	    ScopeOld[m][i] = PoleScope[m][Posun+i];  // Ulozim si starou hodnotu
			}
  	  startx[0][0] = 74+m*2;
 		  x[0][0]	= 0;
  	  y[0][0] = 0;
		}
  	m++;
    j=0;
		if(DyMax[0]<=DyMax[1])DyMax[0]=DyMax[1];  // Najdu Max dy a pak urcim kolikrat se proleti vykresleni car.
		if(DyMax[0]<=2)Cyklu = 2;
		else Cyklu = DyMax[0]; 
	  while(j<Cyklu){
		  j++;
      for(k=0;k<To;k++){
        for(i=0;i<5;i++){
					if(!k || PoleMereni[i]){  
 		        if(!k)color = white;  // pri prvnim pruchodu se maze
			      else{
						  if(!i){
							  color=grey2;
						  } 
						  else{
						    color = ColorPhase[Posun+(i-1)];  // barva napeti
						  }
					  }
			      if(dy[i][k]){		
	            if(dy[i][k]>0){  
		            if(CasZak>dy[i][k]){
			            if(x[i][k]<CasZak){
				            y[i][k]=((dy[i][k]+1)*x[i][k])/(CasZak);
                    VerticalLine(x[i][k]+startx[i][k],y[i][k]+starty[i][k],2,2,color);
				            x[i][k]++;
			            }
		            }
		            else{
			            if(y[i][k]<dy[i][k]){
				            x[i][k]=((CasZak+1)*y[i][k])/(dy[i][k]);
                    HorizontalLine(x[i][k]+startx[i][k],y[i][k]+starty[i][k],2,2,color);
							      y[i][k]++;
			            }
		            }
	            }//
//----------------------------------------------------	
              else{
					      if(dy[i][k]<0){  //roste
                  if(CasZak>(-dy[i][k])){
	                  if(x[i][k]<CasZak){
		                  y[i][k]=(((-dy[i][k])+1)*x[i][k])/(CasZak);
                      VerticalLine(startx[i][k]+x[i][k],starty[i][k]-y[i][k],2,2,color);
  		                x[i][k]++;
			              }
		              }
        	        else{
			              if(y[i][k]<(-dy[i][k])){
				              x[i][k]=((CasZak+1)*y[i][k])/(-dy[i][k]);
                      HorizontalLine(startx[i][k]+x[i][k],starty[i][k]-y[i][k],2,2,color);
			                y[i][k]++;
			              }
		              }
	              }//
				      }
					  }
				    else{
//--------------< Vodorovne a svisle cary >------------------------	
  	          if(CasZak > 0){
	              if(x[i][k]<CasZak){
                  HorizontalLine(startx[i][k]+x[i][k],starty[i][k],2,2,color);
			            x[i][k]++;
			          }
		          }
	          }
				  }
			  }
		  }
	  }
	}
}
/**********************************************************************************************
*                              ---< ScopeUI >---                                                *
*                            Nakresli prubeh Scope                                            *
**********************************************************************************************/
void DrawScopeUI(void)
{ 
	unsigned char To,Cyklu,Posun = 0xff,CasZak;
  signed int x[5][2],y[5][2];
  signed int startx[3][2],starty[3][2],DyMax[2];
	float prevod[2];
  unsigned int i,j,k,l,m = 0,color;
	unsigned int MaxHodnotaU = 0,MaxHodnotaI = 0;
	float hodnota;
	float ScopeRozsah[4];
  
	prevod[0] = PrevodU;
	prevod[1] = PrevodI;
	CasZak = 2;        // Casova zakladna
	if(PoleRozsahu[0] || PoleRozsahu[1]){  // Urceni stareho rozsahu
    ScopeRozsah[2] = 150/((float)PoleRozsahu[0]);  // Nap
    ScopeRozsah[3] = 150/((float)PoleRozsahu[1]);  // Pro
	}
	else{
		ScopeRozsah[2] = 0; // Pokud je PoleRozsahu = 0, jdu tudy poprve
		ScopeRozsah[3] = 0; // Pokud je PoleRozsahu = 0, jdu tudy poprve
	}
	//---< Def.Par >---	 
  dy[0][0] = 0;   // dy je pro osu X vzdy 0
  dy[0][1] = 0;
	starty[0][1] = 208; // pro osu y je start vzdy stejny
	starty[0][0] = 208;

//----------------< Najdu rozsahy >-----------------		
  for(i=0;i<4;i++){
    if((ParamChar[2]&(0x80>>i))){
      Posun = i;
		  for(j=0;j<257;j++){
        if(MaxHodnotaU < fabs(PoleScope[j][i]))MaxHodnotaU = fabs(PoleScope[j][i]);
        if(MaxHodnotaI < fabs(PoleScope[j][i+4]))MaxHodnotaI = fabs(PoleScope[j][i+4]);
			}
		}
  }
//---< Najdu rozsah napeti >----
  hodnota = ShowJednotky(580,45,"V",(float)(MaxHodnotaU*PrevodU),white,white,4,3);  // Jednotky (V PoleRozsahu[7] Max hodnota vsech 3 fazi. Rozsah se urcuje podle max)
  if(hodnota > 40){  // Najdu mez osy
    while(hodnota>=PoleRozsahu[0]){
      PoleRozsahu[0] += 20;
	  }
	  while(hodnota<=(PoleRozsahu[0]-40)){ // Musim porovnavat se spodni hranou okna proto 40!!!
	    PoleRozsahu[0] -= 20;
	  }
	  ScopeRozsah[0] = 150/((float)PoleRozsahu[0]);
	}
  else{// Pokud je mensi nez 50 nebo 0
	}					
//---< Najdu rozsah proudu >----
  hodnota = ShowJednotky(580,45,"A",(float)(MaxHodnotaI*PrevodI),white,white,4,3);  // Jednotky (V PoleRozsahu[7] Max hodnota vsech 3 fazi. Rozsah se urcuje podle max)
  if(hodnota > 4){  // Najdu mez osy
    while(hodnota>PoleRozsahu[1]){
      PoleRozsahu[1] += 2;
	  }
		while(hodnota<(PoleRozsahu[1]-4)){ // Musim porovnavat se spodni hranou okna proto 6!!!
		  PoleRozsahu[1] -= 2;
		}
		ScopeRozsah[1] = 150/((float)PoleRozsahu[1]);
	}
  else{// Pokud je mensi nez 6 nebo 0
	  ScopeRozsah[1] = 150/(float)4;
		PoleRozsahu[1] = 4;
  }
//-----------------------< Zobrazeni prubehu >---------------------------

  while(m<258){
	  DyMax[0] = 0;	
	  DyMax[1] = 0;	
		if(m>1){
	    for(i=0;i<2;i++){
  	    dy[i+1][1] = ((PoleScope[m-2][Posun+(4*i)]-PoleScope[m-1][Posun+(4*i)])*prevod[i])*ScopeRozsah[i];
    	  startx[i+1][1] = 74+(m-2)*2;
  	    starty[i+1][1] = 208 - (PoleScope[(m-2)][Posun+(4*i)]*prevod[i])*ScopeRozsah[i];
  		  x[i+1][1]	= 0;
	  	  y[i+1][1] = 0;
			  To = 2; 
				if(DyMax[1]<=fabs(dy[i+1][1]))DyMax[1]=fabs(dy[i+1][1]);  // Najdu Max dy pozdeji z toho urcim kolem mam udelat cyklu
			}
  	  startx[0][1] = 74+(m-2)*2;
 		  x[0][1]	= 0;
  	  y[0][1] = 0;
		}
		else{
			To = 1;  // Mazu o dva cykli drive(aby v sinu nebyli mezery) proto tento nesmysl
		}
		if(m<257){
			for(i=0;i<2;i++){
        dy[i+1][0] = ((ScopeOld[m][i]-ScopeOld[m+1][i])*prevod[i])*ScopeRozsah[i+2];
   		  startx[i+1][0] = 74+m*2;
		    starty[i+1][0] = 208 -(ScopeOld[m][i]*prevod[i])*ScopeRozsah[i+2];  
  		  x[i+1][0]	= 0;
	  	  y[i+1][0] = 0;
				if(DyMax[0]<=fabs(dy[i+1][0]))DyMax[0]=fabs(dy[i+1][0]); 
  	    ScopeOld[m][i] = PoleScope[m][Posun+(4*i)];  // Ulozim si starou hodnotu
			}
  	  startx[0][0] = 74+m*2;
 		  x[0][0]	= 0;
  	  y[0][0] = 0;
		}
  	m++;
    j=0;
		if(DyMax[0]<=DyMax[1])DyMax[0]=DyMax[1];  // Najdu Max dy a pak urcim kolikrat se proleti vykresleni car.
		if(DyMax[0]<=2)Cyklu = 2;
		else Cyklu = DyMax[0]; 
	  while(j<Cyklu){
		  j++;
      for(k=0;k<To;k++){
        for(i=0;i<3;i++){
					if(!k || Posun !=0xff){  
 		        if(!k)color = white;  // pri prvnim pruchodu se maze
			      else{
						  if(!i){
							  color=grey2;
						  } 
						  else{
						    color = ColorPhase[Posun+(4*(i-1))];  // barva napeti
						  }
					  }
			      if(dy[i][k]){		
	            if(dy[i][k]>0){  
		            if(CasZak>dy[i][k]){
			            if(x[i][k]<CasZak){
				            y[i][k]=((dy[i][k]+1)*x[i][k])/(CasZak);
                    VerticalLine(x[i][k]+startx[i][k],y[i][k]+starty[i][k],2,2,color);
				            x[i][k]++;
			            }
		            }
		            else{
			            if(y[i][k]<dy[i][k]){
				            x[i][k]=((CasZak+1)*y[i][k])/(dy[i][k]);
                    HorizontalLine(x[i][k]+startx[i][k],y[i][k]+starty[i][k],2,2,color);
							      y[i][k]++;
			            }
		            }
	            }//
//----------------------------------------------------	
              else{
					      if(dy[i][k]<0){  //roste
                  if(CasZak>(-dy[i][k])){
	                  if(x[i][k]<CasZak){
		                  y[i][k]=(((-dy[i][k])+1)*x[i][k])/(CasZak);
                      VerticalLine(startx[i][k]+x[i][k],starty[i][k]-y[i][k],2,2,color);
  		                x[i][k]++;
			              }
		              }
        	        else{
			              if(y[i][k]<(-dy[i][k])){
				              x[i][k]=((CasZak+1)*y[i][k])/(-dy[i][k]);
                      HorizontalLine(startx[i][k]+x[i][k],starty[i][k]-y[i][k],2,2,color);
			                y[i][k]++;
			              }
		              }
	              }//
				      }
					  }
				    else{
//--------------< Vodorovne a svisle cary >------------------------	
  	          if(CasZak > 0){
	              if(x[i][k]<CasZak){
                  HorizontalLine(startx[i][k]+x[i][k],starty[i][k],2,2,color);
			            x[i][k]++;
			          }
		          }
	          }
				  }
			  }
		  }
	  }
	}
}
