/*
    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 <stdio.h>
#include "..\..\lib\vh2k.h"
#include "..\..\lib\vp2k_lcd.h"



void Test_Bright(void)
{
    int quit=0,key;
    char string[3];

    LCD_Clear();
    LCD_Putch(0x1E,0);//0x1E=up 
    LCD_PutsAt(2,0,":brightend",0);
    
    LCD_Gotoxy(0,1);
    LCD_Putch(0x1F,0);//0x1F=down
    LCD_PutsAt(2,1,":darkend",0);
    LCD_PutsAt(0,2,"F1:Quit",0);
    LCD_PutsAt(0,3,"back light:",0);
    LCD_PutsAt(12,3,"00",0);
    while(!quit)
    {
        if(IsKey())
        {
            key=ReadKey();
            switch(key)
            {
                case UP_KEY:
                    if(*LCD_BackLight < LCD_MAX_BACK_LIGHT) (*LCD_BackLight)++;
                    break;
                case DOWN_KEY:
                    if(*LCD_BackLight > 0) (*LCD_BackLight)--;
                    break;
                case F1_KEY:
                    quit=1;
                    break;
            }//switch
            sprintf(string,"%02d",*LCD_BackLight);
            LCD_PutsAt(12,3,string,0);
        }//kbhit
    }//while loop
}

void Test_LCD(void)
{
    int quit_Test_LCD =0,quit=0;
    int key;
    int x,y;
    char string[3];  

    while(!quit_Test_LCD)
    {
        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:Quit",0);
		quit = 0;
        while(!quit)
        {
            if(IsKey())
            {
                key =ReadKey();
                switch(key)
                {
                    case F1_KEY:
                        LCD_Clear();
                        LCD_PutsAt(0,0,"Bright : ",0);
                        for(*LCD_BackLight =0;*LCD_BackLight <= LCD_MAX_BACK_LIGHT; (*LCD_BackLight)++)
                        {
                            sprintf(string,"%02d",*LCD_BackLight);
                            LCD_PutsAt(9,0,string,0);
                            Delay(250);
                        }
                        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(50);
                        }
                        for(x=0;x < 128;x++)
                        {
                            for(y =0; y< 64;y++) LCD_Pixel(x,y,0);
                            LCD_UpdateGraphic();
                            Delay(50);
                        }
                        quit=1;
                        break;
          
                    case F2_KEY:
                        LCD_Clear();
                        Test_Bright();
                        quit=1;
                        break;
          
                    case F3_KEY:
                        LCD_Clear();
                        LCD_PutsAt(0,0,"Detect Pixel",0);
                        LCD_PutsAt(0,1,"Press Any Key to",0);
                        LCD_PutsAt(0,2,"detect next Line",0);
                        LCD_PutsAt(0,3,"F4:Back",0);
                        LCD_PutsAt(0,2,"",0);
                        for(x=0;x < 128;x++)
                        {
                            if(quit == 1) break ;
                            for(y =0; y< 64;y++) LCD_Pixel(x,y,1);
                            while(1)
                            {
                                if(IsKey())
                                {
                                    if(ReadKey() == F4_KEY)  quit =1;
                                    else LCD_UpdateGraphic();
                                    break ;
                                }
                            }
                        }
                        for(x=0;x < 128;x++)
                        {
                            if(quit == 1) break ;
                            for(y =0; y< 64;y++) LCD_Pixel(x,y,0);
                            while(1)
                            {
                                if(IsKey())
                                {
                                    if(ReadKey() == F4_KEY)  quit =1;
                                    else LCD_UpdateGraphic();
                                    break ;
                                }
                            }
                        }
                        quit =1;
                        break;
                    case F4_KEY:
                        quit =1;
                        quit_Test_LCD =1;
                        break;
                }//end of: switch
            }//end of: if(IsKey())
        }//end of: while(!quit)
    }
}

void Test_KEY(void)
{
    unsigned cursor_x = 0,cursor_y =0;
    int quit =0;
    int key;

    LCD_Clear();
    LCD_PutsAt(0,0,"Press F6 to quit",0);
    LCD_Hline(0,127,14,1);
    LCD_Hline(0,127,15,1);
    LCD_UpdateGraphic();
    cursor_x = 0;
    cursor_y = 1;
    LCD_Gotoxy(cursor_x,cursor_y);
    LCD_SetCursor(1);
    LCD_BlinkCursor(1);

    while(!quit)
    {
        LCD_DisplayCursor();
        if(IsKey())
        {
            key = ReadKey();
            LCD_Putch((uchar)key,0);
            cursor_x++;
            if(cursor_x > 15)
            {
                cursor_x =0;
                cursor_y++;
                if(cursor_y > 3) cursor_y =1;
            }
            LCD_Gotoxy(cursor_x,cursor_y);
        }
        if(key == F6_KEY) quit = 1;
    }
    LCD_SetCursor(0);
}

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

void viewPAC_info(void)
{
    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(2000);
    LCD_Clear();
    while(!quit)
    {
        LCD_Clear();
        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_info();
                        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();
}
