#include "../s3c2440_soc.h"
#include "../led.h"
#include "../interrupt.h"
#include "../sensors/photoresistor/photoresistor.h"
#include "../sensors/ds18b20/ds18b20.h"
#include "../sensors/dht11/dht11.h"




#define NULL  ((void *)0)

#define ADC_INT_BIT (10)
#define TC_INT_BIT  (9)

#define INT_ADC_TC   (31)


/* ADCTSC's bits */
#define WAIT_PEN_DOWN    (0<<8)
#define WAIT_PEN_UP      (1<<8)

#define YM_ENABLE        (1<<7)
#define YM_DISABLE       (0<<7)

#define YP_ENABLE        (0<<6)
#define YP_DISABLE       (1<<6)

#define XM_ENABLE        (1<<5)
#define XM_DISABLE       (0<<5)

#define XP_ENABLE        (0<<4)
#define XP_DISABLE       (1<<4)

#define PULLUP_ENABLE    (0<<3)
#define PULLUP_DISABLE   (1<<3)

#define AUTO_PST         (1<<2)

#define WAIT_INT_MODE    (3)
#define NO_OPR_MODE      (0)


void lcd_close(void)
{
	printf("return\n\r");
}


static volatile int g_ts_timer_enable = 0;

static int g_ts_x = 0;
static int g_ts_y = 0;
static int g_ts_pressure;
static int g_ts_data_valid = 0;

static int test_x_array[16];
static int test_y_array[16];

typedef void(*return_func)(void);


void report_ts_xy(int x, int y, int pressure);

void enter_wait_pen_down_mode(void)
{
	ADCTSC = WAIT_PEN_DOWN | PULLUP_ENABLE | YM_ENABLE | YP_DISABLE | XP_DISABLE | XM_DISABLE | WAIT_INT_MODE;
}

void enter_wait_pen_up_mode(void)
{
	ADCTSC = WAIT_PEN_UP | PULLUP_ENABLE | YM_ENABLE | YP_DISABLE | XP_DISABLE | XM_DISABLE | WAIT_INT_MODE;
}

void enter_auto_measure_mode(void)
{
	ADCTSC = AUTO_PST | NO_OPR_MODE;
}

int is_in_auto_mode(void)
{
	return ADCTSC & AUTO_PST;
}

void Isr_Tc(void)
{
	//printf("ADCUPDN = 0x%x, ADCDAT0 = 0x%x, ADCDAT1 = 0x%x, ADCTSC = 0x%x\n\r", ADCUPDN, ADCDAT0, ADCDAT1, ADCTSC);
	
	if (ADCDAT0 & (1<<15))
	{
		//printf("pen up\n\r");
		enter_wait_pen_down_mode();
		report_ts_xy(0, 0, 0);
		
	}
	else	
	{
		//printf("pen down\n\r");

		/* 进入"自动测量"模式 */
		enter_auto_measure_mode();

		/* 启动ADC */
		ADCCON |= (1<<0);
	}
}

static void ts_timer_enable(void)
{
	g_ts_timer_enable = 1;
}

static void ts_timer_disable(void)
{
	g_ts_timer_enable = 0;
}

static int get_status_of_ts_timer(void)
{
	return g_ts_timer_enable;
}

void report_ts_xy(int x, int y, int pressure)
{
	//printf("x = %08d, y = %08d\n\r", x, y);

	if (g_ts_data_valid == 0)
	{
		g_ts_x = x;
		g_ts_y = y;
		g_ts_pressure = pressure;
		g_ts_data_valid = 1;
	}
}

void ts_read_raw(int *px, int *py, int *ppressure)
{
	while (g_ts_data_valid == 0);
	*px = g_ts_x;
	*py = g_ts_y;
	*ppressure = g_ts_pressure;
	g_ts_data_valid = 0;
}

void ts_read_raw_h(int *px, int *py, int *ppressure)
{
	if(g_ts_data_valid == 0)
		return -1;
	*px = g_ts_x;
	*py = g_ts_y;
	*ppressure = g_ts_pressure;
	g_ts_data_valid = 0;
}


/* 每10ms该函数被调用一次 
 */
void touchscreen_timer_irq(void)
{
	/* 如果触摸屏仍被按下, 进入"自动测量模式", 启动ADC */
	if (get_status_of_ts_timer() == 0)
		return;

	if (is_in_auto_mode())
		return;

	/* 只有在"等待中断模式"下才可以使用ADCDAT0'BIT 15来判断触摸笔状态 */

	if (ADCDAT0 & (1<<15)) /* 如果松开 */
	{
	//	printf("timer set pen down\n\r");
		ts_timer_disable();
	enter_wait_pen_down_mode();
		report_ts_xy(0, 0, 0);
		return;
	}
	else  /* 按下状态 */
	{
	//	printf("timer set \n\r");
		/* 进入"自动测量"模式 */
		enter_auto_measure_mode();

		/* 启动ADC */
		ADCCON |= (1<<0);
	}
}

void Isr_Adc(void)
{
	int x = ADCDAT0;
	int y = ADCDAT1;

	static int adc_cnt = 0;
	static int adc_x = 0;
	static int adc_y = 0;

	/* 进入ADC中断时, TS处于"自动测量模式" */

	/* 只有在"等待中断模式"下才可以使用ADCDAT0'BIT 15来判断触摸笔状态 */

	enter_wait_pen_up_mode();
	if (!(ADCDAT0 & (1<<15))) /* 如果仍然按下才打印 */
	{
		
#if 0		
		x &= 0x3ff;
		y &= 0x3ff;
		
		//printf("x = %08d, y = %08d\n\r", x, y);
		report_ts_xy(x, y, 1);

		/* 启动定时器以再次读取数据 */
		ts_timer_enable();
#endif
		/* 第1次启动ADC后:
		 *   a. 要连续启动N次, 获得N个数据, 求平均值并上报
		 *   b. 得到N次数据后, 再启动TIMER 
		 */
		adc_x += (x & 0x3ff);
		adc_y += (y & 0x3ff);

		test_x_array[adc_cnt] = (x & 0x3ff);
		test_y_array[adc_cnt] = (y & 0x3ff);
		
		adc_cnt++;

		if (adc_cnt == 16)
		{
			adc_x >>= 4;
			adc_y >>= 4;
			report_ts_xy(adc_x, adc_y, 1);

			adc_cnt = 0;
			adc_x = 0;
			adc_y = 0;
			
			/* 启动定时器以再次读取数据 */
			/* 先设置TS进入"等待中断模式" */
			enter_wait_pen_up_mode();
			ts_timer_enable();
		}
		else
		{
			/* 再次启动ADC */
			/* 进入"自动测量"模式 */
			enter_auto_measure_mode();
			
			/* 启动ADC */
			ADCCON |= (1<<0);
		}
		
	}
	else
	{
		adc_cnt = 0;
		adc_x = 0;
		adc_y = 0;
	//	printf("adc report pen down\n\r");
		ts_timer_disable();
		enter_wait_pen_down_mode();
		report_ts_xy(0, 0, 0);
	}
}

void adc_ts_reg_init(void)
{
	/* [15] : ECFLG,  1 = End of A/D conversion
	 * [14] : PRSCEN, 1 = A/D converter prescaler enable
	 * [13:6]: PRSCVL, adc clk = PCLK / (PRSCVL + 1)
	 * [5:3] : SEL_MUX, 000 = AIN 0
	 * [2]   : STDBM
	 * [0]   : 1 = A/D conversion starts and this bit is cleared after the startup.
	 */
	ADCCON = (1<<14) | (49<<6) | (0<<3);

	/*  按下触摸屏, 延时一会再发出TC中断
	 *  延时时间 = ADCDLY * 晶振周期 = ADCDLY * 1 / 12000000 = 5ms
	 */
	ADCDLY = 60000;	
}

void AdcTsIntHandle(int irq)
{
	adc_ts_reg_init();
	if (SUBSRCPND & (1<<TC_INT_BIT))  /* 如果是触摸屏中断 */
		Isr_Tc();

	if (SUBSRCPND & (1<<ADC_INT_BIT))  /* ADC中断 */
		Isr_Adc();
	SUBSRCPND = (1<<TC_INT_BIT) | (1<<ADC_INT_BIT);
}

void adc_ts_int_init(void)
{
	SUBSRCPND = (1<<TC_INT_BIT) | (1<<ADC_INT_BIT);

	/* 注册中断处理函数 */
	register_irq(31, AdcTsIntHandle);	

	/* 使能中断 */
	INTSUBMSK &= ~((1<<ADC_INT_BIT) | (1<<TC_INT_BIT));
	//INTMSK    &= ~(1<<INT_ADC_TC);
}


void touchscreen_init(void)
{
	/* 设置触摸屏接口:寄存器 */
	adc_ts_reg_init();

	printf("ADCUPDN = 0x%x, SUBSRCPND = 0x%x, SRCPND = 0x%x\n\r", ADCUPDN, SUBSRCPND, SRCPND);

	/* 设置中断 */
	adc_ts_int_init();

	/* 注册定时器处理函数 */
	register_timer("touchscreen", touchscreen_timer_irq);

	/* 让触摸屏控制器进入"等待中断模式" */
	enter_wait_pen_down_mode();
}

void print_test_array(void)
{
	int i;

	printf("test array x : ");
	for (i = 0; i < 16; i++)
		printf("%08d ", test_x_array[i]);
	printf("\n\r");

	printf("test array y : ");
	for (i = 0; i < 16; i++)
		printf("%08d ", test_y_array[i]);
	printf("\n\r");
}

void ts_read_raw_test(int *px, int *py, int *ppressure)
{
	while (g_ts_data_valid == 0);
	*px = g_ts_x;
	*py = g_ts_y;
	*ppressure = g_ts_pressure;
	print_test_array();
	g_ts_data_valid = 0;
}

 

void Waiting_for_click(return_func re)
{
	int x,y,pressure;
	int p_x,p_y;
	int tmp = 0;
	disp_rectangle(235,208,0xffffffff);
	fb_print_string(210, 200, "return", 0xffffffff);
	while (1)
	{
		if (ts_read(&x, &y, &pressure) == 0 && pressure)
		{	
			printf("x = %d , y = %d , tmp = %d\n\r",x,y,tmp);
			printf("px = %d , py = %d , tmp = %d\n\n\r",p_x,p_y,tmp);

			if(tmp == 0 || juedui(p_x,p_y,x,y) == 0)
			{
				p_x = x;
				p_y = y;
			}
			tmp++;
		}
		else
		{
			printf("x=%d, y=%d , pr=%d\n\r",p_x,p_y,pressure);
			if (175 <= p_x && p_x <= 295 && 181 <= p_y && p_y <= 235)
			{
				printf("return\n\r");
				re();
				Test_module_menu();
			}
			p_x=0,p_y=0;
			tmp = 0;
		}
	}
}

void test_touchscreen(void)
{
	int x, y, pressure;
	if (ts_read(&x, &y, &pressure) == 0)
	{
		fb_print_string(160, 100, "Module testing", 0xffffff);
	}
}

int juedui(int px,int py,int x,int y)
{
	if((py - y) > 30 || (y - py) > 30 || (px - x) > 50 ||(x - px) > 50)
		return -1;
	return 0;
}

void ts_tc(void)
{
	int x, y, pressure,tmp = 0;
	int p_x=0,p_y=0;
	
	disp_rectangle(235,208,0xbfbfff);
	fb_print_string(210, 200, "return", 0xbfbfff);
	
	ts_printf(50,150,0xbfbfff,"x : 000 | y : 000");
	while(1)
	{
		if (ts_read(&x, &y, &pressure) == 0 && pressure)
		{
			fb_put_pixel(x,y,0xbfbfff);
			
			ts_printf(82,150,0xbfbfff,"%03d",x);
			ts_printf(162,150,0xbfbfff,"%03d",y);

			if(tmp == 0 || juedui(p_x,p_y,x,y) == 0)
			{
				p_x = x;
				p_y = y;
			}
			tmp++;
		}
		else
		{
			printf("x=%d, y=%d , pr=%d\n\r",p_x,p_y,pressure);
			if (175 <= p_x && p_x <= 295 && 181 <= p_y && p_y <= 235)
			{
				printf("hahahaha\n\r");
				Test_module_menu();
			}
			p_x=0,p_y=0;
			tmp = 0;
		}
	}
}


void Module_waiting_for_click(int *px,int *py)
{
	int x, y, pressure,tmp = 0;
	int p_x=0,p_y=0;
	char m[10];
	char *modules = m;
	while (1)
	{
		if (ts_read(&x, &y, &pressure) == 0 && pressure)
		{	
			printf("x = %d , y = %d , tmp = %d\n\r",x,y,tmp);
			printf("px = %d , py = %d , tmp = %d\n\n\r",p_x,p_y,tmp);

			if(tmp == 0 || juedui(p_x,p_y,x,y) == 0)
			{
				p_x = x;
				p_y = y;
			}
			tmp++;
		}
		else
		{
			printf(" x = %d, y = %d  tmp = %d \n\n\r", p_x, p_y,tmp);
			
			if(20 <= p_x && p_x <= 140)
				{
					if(13 <= p_y && p_y <= 67)
					{
						printf("LED\n\r");
						modules = "LED";
						led_init();
						Test_module_interface(led_close,modules);
						
					}
					else if(78 <= p_y && p_y <= 132)
					{
						printf("TS\n\r");
						modules = "TS";
						Test_module_interface_i(modules);
						ts_tc();
					}
					else if(143 <= p_y && p_y <= 197)
					{
						printf("RES\n\r");
						modules = "RES";
						register_timer("photoresistor",photoresistor_test);
						Test_module_interface(photoresistor_close,modules);
					}
					else if(208 <= p_y && p_y <= 252)
					{
						printf("HS0038\n\r");
						modules = "HS0038";
						Test_module_interface_i(modules);
						irda_nec_test();
					}
				}
				else if(175 <= p_x && p_x <= 295)
				{
					if(13 <= p_y && p_y <= 67)
					{
						printf("Button\n\r");
						modules = "Button";
						key_eint_init();
						Test_module_interface(key_eint_close,modules);
					}
					else if(78 <= p_y && p_y <= 132)
					{
						printf("I2C\n\r");
						modules = "I2C";
						Test_module_interface_i(modules);
						i2c_test();
						Waiting_for_click(led_close);
					}
					else if(143 <= p_y && p_y <= 197)
					{
						printf("DHT11\n\r");
						modules = "DHT11";
						dht11_test();
						Test_module_interface(dht11_close,modules);
					}
				}
				else if(330 <= p_x && p_x <= 450)
				{
					if(13 <= p_y && p_y <= 67)
					{
						printf("LCD\n\r");
						modules = "LCD";
						lcd_test();
						Test_module_interface(lcd_close,modules);
					}
					else if(78 <= p_y && p_y <= 132)
					{
						printf("SPI\n\r");
						modules = "SPI";
						Test_module_interface_i(modules);
						Test_Adc_oled();
					}
					else if(143 <= p_y && p_y <= 197)
					{
						printf("DS18\n\r");
						modules = "DS18";
						
						register_timer("ds18b20",ds18b20_test);
						Test_module_interface(ds18b20_close,modules);
					}
				}
			p_x=0,p_y=0;
			tmp = 0;
		}
	}
}


