#include "touchscreen.h"
 #define     MENU                  0
 #define     LED                   1
 #define     BUTTON                2
 #define     LCD                   3
 #define     TS                    4
 #define     I2C                   5
 #define     SPI                   6
 #define     RES                   7
 #define     DHT11                 8
 #define     DS18                  9
 #define     HS0038                10

unsigned int fb_base;
int xres, yres, bpp;

 int draw_return_rectangle(void)
 {
 		clear_screen(0);
		
 		draw_line(xres/3, yres/4*3, xres/3*2, yres/4*3, 0x23ff77);
		draw_line(xres/3, yres-1,   xres/3*2, yres-1, 0x23ff77);
		draw_line(xres/3, yres/4*3, xres/3,   yres - 1, 0xff45);
		draw_line(xres/3*2, yres/4*3, xres/3*2, yres - 1, 0xff45);

		fb_print_string(xres/3+50, yres/4*3+50, "return", 0xff);
		return 0;
 }

 int draw_menu(void)
 {
 		clear_screen(0);
		
        /* 横*/
		draw_line(0, 0,        xres - 1, 0,        0x23ff77);
		draw_line(0, yres/4,   xres - 1, yres/4,   0x23ff77);
		draw_line(0, yres/4*2, xres - 1, yres/4*2, 0x23ff77);
		draw_line(0, yres/4*3, xres - 1, yres/4*3, 0x23ff77);
		draw_line(0, yres-1,   xres - 1, yres-1,   0x23ff77);
		
        /*竖*/
		draw_line(0,        0, 0,        yres - 1, 0xff45);
		draw_line(xres/3,   0, xres/3,   yres - 1, 0xff45);
		draw_line(xres/3*2, 0, xres/3*2, yres - 1, 0xff45);
		draw_line(xres-1,   0, xres-1,   yres - 1, 0xff45);
		
		fb_print_string(50,          yres/8,          "LED", 0xff);
		fb_print_string(xres/3+50,   yres/8,          "BUTTON", 0xff);
		fb_print_string(xres/3*2+50, yres/8,          "LCD", 0xff);
		fb_print_string(50,          yres/4+yres/8,   "Touch Screen", 0xff);
		fb_print_string(xres/3+50,   yres/4+yres/8,   "I2C", 0xff);
		fb_print_string(xres/3*2+50, yres/4+yres/8,   "SPI", 0xff);
		fb_print_string(50,          yres/4*2+yres/8, "Photoresistor", 0xff);
		fb_print_string(xres/3+50,   yres/4*2+yres/8, "DHT11", 0xff);
		fb_print_string(xres/3*2+50, yres/4*2+yres/8, "DS18B20", 0xff);
		fb_print_string(50,          yres/4*3+yres/8, "HS0038", 0xff);

		return 0;
 }

 void touchscreen_test(void)
 	{
 	/* 清屏 */
	clear_screen(0);

	/* 显示文字提示较准 */
	fb_print_string(70, 70, "Touch cross to calibrate touchscreen", 0xffffff);
	ts_calibrate();

	/* 显示文字提示较准完成 */
	fb_print_string(70, yres - 70, "OK! Touch screen test complete !", 0xffffff);
	mdelay(1000);
 	}
 
void _2440_test(void)
{
	//unsigned int fb_base;
	//int xres, yres, bpp;
	 //int g_ts_data_valid;

	int x, y, pressure;
	int testitem=MENU;

	/* 获得LCD的参数: fb_base, xres, yres, bpp */
	get_lcd_params(&fb_base, &xres, &yres, &bpp);

	touchscreen_init();

	/* 清屏 */
	clear_screen(0);

	/* 显示文字提示较准 */
	fb_print_string(70, 70, "Touch cross to calibrate touchscreen", 0xffffff);
	ts_calibrate();
	clear_screen(0);

	/* 显示文字提示较准完成 */
	fb_print_string(70, yres/2, "OK! Please connect UART and choose a test item !", 0xffffff);
	mdelay(4000);
	
    while (1)
	{
		if (testitem == MENU)

		{
		draw_menu();
		
		if (ts_read(&x, &y, &pressure) == 0)
			{
			
			if ( 0<x&&x<(xres/3)&&0<y&&y<(yres/4))
					{
					draw_return_rectangle();
					fb_print_string(10, yres/2, "LED TEST ...", 0xff);
					led_init();
					testitem=LED;
					}
			else if( (xres/3)<x&&x<(xres/3*2)&&0<y&&y<(yres/4))
					{
					key_led_init();
					key_eint_init();
					draw_return_rectangle();
					fb_print_string(10, yres/2, "BUTTON TEST ...", 0xff);
					testitem=BUTTON;
					}
			else if( (xres/3*2)<x&&x<(xres-1)&&0<y&&y<(yres/4))
					{
					lcd_test();
					draw_return_rectangle();
					fb_print_string(10, yres/2, "LCD TEST Complete !", 0xff);
					testitem=LCD;
					}
			else if( 0<x&&x<(xres/3)&&(yres/4)<y&&(yres/4)<y&&y<(yres/4*2))
					{
					touchscreen_test();
					draw_return_rectangle();
					fb_print_string(10, yres/2, "Touch screen test complete !", 0xff);
					testitem=TS;
					}
			else if( (xres/3)<x&&x<(xres/3*2)&&(yres/4)<y&&y<(yres/4*2))
					{
					i2c_init();
					draw_return_rectangle();
					fb_print_string(10, yres/2, "I2C test:Please connect UART to get massage", 0xff);
					testitem=I2C;
					}
			else if( (xres/3*2)<x&&x<(xres-1)&&(yres/4)<y&&y<(yres/4*2))
					{
					SPIInit();
    				OLEDInit();
   					OLEDPrint(0,0,"SPI OK!");
					draw_return_rectangle();
					fb_print_string(10, yres/2, "SPI TEST ...", 0xff);
					testitem=SPI;
					}
			else if( 0<x&&x<(xres/3)&&(yres/4*2)<y&&y<(yres/4*3))
					{
					draw_return_rectangle();
					fb_print_string(10, yres/2, "Photoresistor TEST: Please connect UART to get data ", 0xff);
					testitem=RES;
					}
			else if( (xres/3)<x&&x<(xres/3*2)&&(yres/4*2)<y&&y<(yres/4*3))
					{
					draw_return_rectangle();
					fb_print_string(10, yres/2, "DHT11 TEST: Please connect UART to get data", 0xff);
					testitem=DHT11;
					}
			else if( (xres/3*2)<x&&x<(xres-1)&&(yres/4*2)<y&&y<(yres/4*3))
					{
					draw_return_rectangle();
					fb_print_string(10, yres/2, "DS18b20 TEST: Please connect UART to get data", 0xff);
					testitem=DS18;
					}
			else if( 0<x&&x<(xres/3)&&(yres/4*3)<y&&y<(yres-1))
					{
					draw_return_rectangle();
					fb_print_string(10, yres/2, "HS0038 TEST: Please connect UART to get data", 0xff);
					testitem=HS0038;
					}
			else 
					{
					
					testitem=MENU;
					}
			}
		}

		

		
		
		if (testitem == LED)
		{
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			unregister_timer("led");
			led_close();
			testitem =MENU;
			}
		}
		if (testitem == BUTTON)
		{
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
			
		}
		if (testitem == LCD)
		{
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3))
			{
			testitem =MENU;
			}
		}
		if (testitem == TS)
		{
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3))
			{
			testitem =MENU;
			}
		}
		if (testitem == I2C)
		{
			i2c_test();
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
		}
		if (testitem == SPI)
		{
			
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
		}
		if (testitem == RES)
		{
		static int i;
		for(;i<5;i++)
			{
			photoresistor_test();
			mdelay(500);
			touchscreen_init();
			}
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			i=0;
			}
			
		}
		if (testitem == DHT11)
		{
			dht11_test();
			mdelay(500);
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
		}
		if (testitem == DS18)
		{
			ds18b20_test();	
			mdelay(500);
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
		}
		if (testitem == HS0038)
		{
			irda_nec_test();
			if((ts_read(&x, &y, &pressure) == 0)&&y>(yres/4*3)&&(xres/3)<x&&x<(xres/3*2))
			{
			testitem =MENU;
			}
		}
		
	}
}
