/*
    ViewPAC.c :  Test the LCD and KeyPAD

    Compiler: BC++ 3.1,
              Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum)
              MSC 6.0,
              MSVC 1.52.

    Compile mode: large

    Project: ViewPAC.c
             ..\Lib\(8000E.Lib,7188XAL.Lib,7188XBL.Lib,7188XL.Lib or 7188EL.Lib)

    Detail description:
        User can use MiniOs utility to load these following files to ViewPAC.
        a.viewpac.exe
        b.icpdas.bmp
        c.index.bmp
        d.info.bmp

    Hardware: ViewPAC

[16 Apr,2009] by Spike
*/

//#include <viewpac.h>
#include "..\..\lib\new8k.h"
#include "..\..\lib\vp2k.h"
#include "..\..\lib\vp2k_lcd.h"

char *keyname[]=
{
"UP:","LEFT:","DOWN:","CR:","RIGHT:","BS:","3:","6:","9:","0:","SHIFT:","DOT(.):","1:",
"4:","7:","8:","5:","2:","F1:","F2:","F3:","F6:","F5:","F4:"
};

char *keyCH[]=
{
"A:","B:","C:","D:","E:","F:","G:","H:","I:","J:","K:","L:","M:",
"N:","O:","P:","Q:","R:","S:","T:","U:","V:","W:","X:","Y:","Z:"
};

char *bk_str[]=
{
"00", "01",  "02",  "03",  "04",  "05",  "06",  "07",  "08",  "09",  "10",
"11", "12",  "13",  "14",  "15",  "16",  "17",  "18",  "19",  "20"
};

int inc_flag[]=
{
18,19,20,23,22,21,12,13,14,17,16,15,6,7,8,11,10,9,0,1,2,5,4,3
};

void Test_Bright(void)
{
	int quit_Test_Bright=0,key;
  uchar upkey_[2]={0x1E,0}, downkey_[2]={0x1F,0};

	LCD_PutsAt(0,0,upkey_,0);
	LCD_PutsAt(2,0,":Bright",0);
	LCD_PutsAt(0,1,downkey_,0);
	LCD_PutsAt(2,1,":Dark",0);
	LCD_PutsAt(0,2,"'Q':Quit",0);
	LCD_PutsAt(0,3,"LCD Bright : ",0);
	LCD_PutsAt(13,3,bk_str[*LCD_BackLight],0);

	while(!quit_Test_Bright)
	{
	  if(IsKey())
	  {
	 		key=ReadKey();
	 		switch(key)
	 		{
	    	case UP_KEY:
          if(*LCD_BackLight < LCD_MAX_BACK_LIGHT) ++(*LCD_BackLight);
          else 	*LCD_BackLight =LCD_MAX_BACK_LIGHT;
          break;//brighter
	   	 	case DOWN_KEY:
	   	 	  if(*LCD_BackLight == 0) *LCD_BackLight = 0;
	   	 		else --(*LCD_BackLight);
          break;//darker
	    	case 'Q':
	    		quit_Test_Bright=1;
          break;
	 		}//switch

	 		LCD_PutsAt(13,3,bk_str[*LCD_BackLight],0);
    }//kbhit
  }
}

void Test_LCD(void)
{
  int quit_Test_LCD =0,quit_F4 =0;
  int Test_LCD_key;
  int x,y;

  
    LCD_Clear();
    LCD_PutsAt(0,0,"F1:Auto Detect",0);
    LCD_PutsAt(0,1,"F2:Set Bright",0);
    LCD_PutsAt(0,2,"F3:Detect Pixel",0);
    LCD_PutsAt(0,3,"F4:Back",0);
    quit_F4 = 0;
    
      
        
         LCD_Clear();
         for(*LCD_BackLight =0;*LCD_BackLight <= LCD_MAX_BACK_LIGHT; (*LCD_BackLight)++)
         {
           LCD_PutsAt(0,0,"Bright : ",0);
           LCD_PutsAt(9,0,bk_str[*LCD_BackLight],0);
           Delay(200);
         }
         
         LCD_PutsAt(0,0,"Detect Pixel",0);
         for(x=0;x < 128;x++)
         {
           for(y =0; y< 64;y++) LCD_Pixel(x,y,1);
           LCD_UpdateGraphic();
           //Delay(100);
         }
         Delay(5000);
         for(x=0;x < 128;x++)
         {
           for(y =0; y< 64;y++) LCD_Pixel(x,y,0);
           LCD_UpdateGraphic();
           //Delay(100);
         }         
             
}
void Test_KEY(void)
{
  int y=1,i,idx=0,sts;
  int j=0,z=0;
  int status[24]={0},temp;
  
  
  LCD_Clear();
  LCD_PutsAt(0,0,"Press KEY test",0);
	LCD_Hline(0,127,14,1);
	LCD_Hline(0,127,15,1);
	LCD_UpdateGraphic();
  while(1)
  {
    //for(i=0 ;i <24 ;i++)
    for(j=0 ;j <24 ;j++)
    {
      LCD_PutsAt(0,y,"                ",0);
      LCD_Gotoxy(0,y);
      i=inc_flag[j];
      //idx=i;
      LCD_Puts(keyname[i],0);
      //*i=inc_flag[j];
      //LCD_Puts(keyname[i],0);
      while(1)
      {
         z=inc_flag[idx];
        if((sts=ReadKeyStatus(z)) != status[z])
        {
     			status[z]=sts;
    			if(sts)
		    	{
		    	  //if(idx == inc_flag[j]) break;
               if(idx == inc_flag[i]) break;
            }
        }
        idx++;
        if(idx > 23) idx =0;
        temp = ReadKey();
      }
      LCD_Puts(" OK",0);
      y++;
      if(y > 3) y =1;
    }
    LCD_Clear();
    LCD_PutsAt(0,0,"KEY test OK",0);
    break;
    /*
    for(i=0 ;i <26 ;i++)
    {
      LCD_PutsAt(0,y,"                ",0);
      LCD_Gotoxy(0,y);
      LCD_Puts(keyCH[i],0);
      while(1)
      {
        if(IsKey())
        {
          temp = ReadKey();
          if(temp == 65+i)
          {
            LCD_Putch(65+i,0);
            break;
          }
        }
      }
      LCD_Puts(" OK",0);
      y++;
      if(y > 3) y =1;
    }
    break;
    */
  }
}

void Show_BMP(void)
{
  LCD_Bmp(0,0,"icpdas.BMP",1);
  LCD_UpdateGraphic();
}

void viewPAC_tradion_Chinese(void)
{
uchar COMp[]={"泓格科技股份有限公司"};
  uchar TEL[]={"電話:03-5973366"};
  LCD_PutsAt(0,0,"www.icpdas.com",0); 
  LCD_PutsAt(0,1,COMp,0);
  LCD_PutsAt(0,3,TEL,1);
  //LCD_PutsAt(0,0,"web:www.icpdas.com",0); 
  //LCD_Bmp(0,0,"info.BMP",1);
  LCD_UpdateGraphic();

}

void viewPAC_simp_Chinese(void)
{
uchar COMp[]={"裼跡褪撮嘖爺衄癹鼠侗"};
  uchar TEL[]={"萇趕:03-5973366"};
  LCD_PutsAt(0,0,"www.icpdas.com",0); 
  LCD_PutsAt(0,1,COMp,0);
  LCD_PutsAt(0,3,TEL,1);
  //LCD_PutsAt(0,0,"web:www.icpdas.com",0); 
  //LCD_Bmp(0,0,"info.BMP",1);
  LCD_UpdateGraphic();

}


void main(void)
{ 
	int quit=0,quit_page=0,quit_Show_BMP =0;
  int Get_Fkey;
  char *adr;
  
  InitLib();
  LCD_Init();
  Show_BMP();
  Delay(3000);
  LCD_Clear();
  while(!quit)
  {
    LCD_Clear();
    //LCD_Bmp(0,0,"index.BMP",1);
    LCD_Bmp(0,0,"index.BMP",1);
    
    LCD_UpdateGraphic();
    while(!quit_page)
    {
  		if(IsKey())
  		{
        Get_Fkey =ReadKey();
        switch(Get_Fkey)
        {
          case F1_KEY:
            Test_LCD();
            quit_page =1;
          break;
          case F2_KEY:
            Test_KEY();
            quit_page =1;
          break;
          case F3_KEY:
            LCD_Clear();
            Show_BMP();
            quit_Show_BMP = 0;
            while(!quit_Show_BMP)
            {
		          if(IsKey())  quit_Show_BMP = 1;
            }
            LCD_Clear();
            quit_page =1;
          break;
          case F4_KEY:
            LCD_Clear();
            viewPAC_tradion_Chinese();
            quit_Show_BMP = 0;
            while(!quit_Show_BMP)
            {
		          if(IsKey())  quit_Show_BMP = 1;
            }
            LCD_Clear();
            quit_page =1;
          break;
          case F5_KEY:
          	LCD_Clear();
            viewPAC_simp_Chinese();
            quit_Show_BMP = 0;
            while(!quit_Show_BMP)
            {
		          if(IsKey())  quit_Show_BMP = 1;
            }
            LCD_Clear();
            quit_page =1;
          break;
          case F6_KEY:
            quit =1;
            quit_page =1;
          break;
        }
      }
    }
    quit_page = 0;
  }
  StopOutWave1();
  LCD_Clear();

}
