
int touchscreen_calibrate(void)
{
	unsigned int fb_base;
	int xres, yres, bpp;

	int x, y, pressure;
	int x1, y1,temp1,temp2;

	unsigned char buf[64];//buf[0] 等于100则表明flash 存有触摸屏数据
	int ts_flag;
	
	/* 获得LCD的参数: fb_base, xres, yres, bpp */
	get_lcd_params(&fb_base, &xres, &yres, &bpp);

	touchscreen_init();

	/* 清屏 */
	clear_screen(0);

	nand_read((unsigned int)1024*1024, buf, 7);
	ts_flag = buf[0];
	
	if(ts_flag == 100)
	{
		fb_print_string(70, 70, "Whether the touch screen needs to be recalibrated", 0xffffff);

		/* 画圆 */

		draw_circle(xres/2-60,  yres/2+60 , 40, 0x800000);
		fb_print_string(xres/2-60-24, yres/2+60-8, "  YES", 0xA0);

		/* 画圆 */

		draw_circle(xres/2+60,  yres/2+60 , 40, 0x800000);
		fb_print_string(xres/2+60-24, yres/2+60-8, "  NO", 0xA0);

		while (1)
		{
			ts_calibrate_nand(); //从flash 加载
		
			if (ts_read(&x, &y, &pressure) == 0)
			{

				if (pressure)
				{
					do {
						ts_read_raw(&x1, &y1, &pressure); 
					} while (pressure);
			

					temp1 = x - (xres/2-60);
					temp2 = y - ( yres/2+60);
					if( (temp1 * temp1 + temp2 * temp2) - 40*40 < 0 )//yes 重新校准
					{
						clear_screen(0);
						fb_print_string(70, 70, "Touch cross to calibrate touchscreen", 0xffffff);
						ts_calibrate();
						return 0;
					}

					temp1 = x - (xres/2+60);
					temp2 = y - ( yres/2+60);
					if( (temp1 * temp1 + temp2 * temp2) - 40*40 < 0 )
					{
						return 0;
					}
				}
			}
		}		
			
	}
	else
	{
			/* 显示文字提示较准 */
		fb_print_string(70, 70, "Touch cross to calibrate touchscreen", 0xffffff);
		ts_calibrate();
	}

}

