#include "touchscreen_test.h"
#include "../lcd/lcd.h"
#include "touchscreen.h"
#include "../lcd/framebuffer.h"
#include "../lcd/font.h"
#include "tslib.h"
#include "../my_printf.h"
#include "../app/functional_selection.h"
#include "../app/display.h"
#include "../lcd/geometry.h"

volatile int complete_flag = 0;

void touchscreen_init(void)
{
	unsigned int fb_base;
	int xres, yres, bpp;

	int x, y, pressure;

	/* 获得LCD的参数: fb_base, xres, yres, bpp */
	get_lcd_params(&fb_base, &xres, &yres, &bpp);

	touchscreenInit();

	/* 清屏 */
	clear_screen(0);

	/* 显示文字提示较准 */
	//fb_print_string(70, 70, "Touch cross to calibrate touchscreen", 0xffffff);
	
	/* 触摸屏校准 */
	//ts_calibrate();
	ts_data_from_nand();

	/* 显示文字提示绘画 */
	fb_print_string(70, yres - 70, "OK! Calibration completion!", 0xffffff);

	/* 画轨迹测试 */
   	// while (1)
	// {
	// 	if (ts_read(&x, &y, &pressure) == 0)
	// 	{
	// 		printf(" x = %d, y = %d\n\r", x, y);

	// 		if (pressure)
	// 		{
	// 			fb_put_pixel(x, y, 0xff00);
	// 		}
	// 	}
	// }   
}

void touchscreen_test(void)
{
	unsigned int fb_base;
	int xres, yres, bpp;

	int x, y, pressure;
	int index;

	/* 获得LCD的参数: fb_base, xres, yres, bpp */
	get_lcd_params(&fb_base, &xres, &yres, &bpp);
	/* 清屏 */
	clear_screen(0);

	/* 显示文字提示绘画 */
	fb_print_string(70, yres - 70, "OK! To draw!!!", 0xffffff);
	/* return框显示 */
	fb_disp_rectangle(340,200,110,40,0xffffff);
	fb_print_string(350,215,"Return",0xffffff);

	/* 画轨迹测试 */
   	while (1)
	{
		index = index_read();
		if (ts_read(&x, &y, &pressure) == 0)
		{
			printf(" x = %d, y = %d\n\r", x, y);

			if (pressure)
			{
				fb_put_pixel(x, y, 0xff00);
			}
		}
		
		if (index == 11)
		{
			display_init();
			break;
		}
		
	}   
}
