#include "functional_selection.h"
#include "../lcd/framebuffer.h"
#include "../lcd/geometry.h"
#include "../lcd/font.h"
#include "../my_printf.h"
#include "../adc_touchscreen/tslib.h"
#include "display.h"
#include "../led/led.h"

static int index;

/* 求绝对值 */
int abs(int a)
{
	if (a < 0)
		return -a;
	return a;
}

/*******************************************************************************
* Function Name  : index_read
* Description    : Reads index from touchscreen.
* Input          : None
* Output         : None
* Return         : Return the number of index
*******************************************************************************/
int index_read(void)
{
	//printf("\n\r\n\r");
	//printf("-----------------------------\n\r");
	int x, y;
	int pressure;
	int row, col;
	
	int x1, y1;
	int x2, y2;
	int er1,er2;

	if (ts_read(&x, &y, &pressure) == 0)
	{
		printf(" x = %d, y = %d\n\r", x, y);
		if (pressure)
		{
			fb_put_pixel(x, y, 0);
			if (x > 0 && x < 500 && y > 0 && y<300)
			{
				col = x / 160;
				row = y / 65;
				index = row * 3 + col;
				printf("col = %d, row = %d\n\r", col, row);
				printf("index = %d\n\r",index);
				return index;
			}
		}
	}
	/* 触摸屏没有按下， pressure=0*/
	else
	{
		// printf("no touch!!!\n\r");
		return -1;	/* 没有按下，return index=-1 */
	}
}

