触摸屏驱动分析之——ts_test.c

struct ts_button {
	int x, y, w, h;
	char *text;
	int flags;		/* button pressed ? */ 
#define BUTTON_ACTIVE 0x00000001
};

/* draw a button */
static void button_draw (struct ts_button *button)
{
	/* 按钮按下 s = 3,否则 s = 0 */
	int s = (button->flags & BUTTON_ACTIVE) ? 3 : 0;
	
	/* 画出边框 */
	rect (button->x, button-> y, button->x + button->w - 1,
	      button->y + button->h - 1, button_palette [s]);
	/* 按钮颜色填充 */	  
	fillrect (button->x + 1, button->y + 1,
			  button->x + button->w - 2,
		      button->y + button->h - 2, button_palette [s + 1]);
	/* 按钮标识 */	  
	put_string_center (button->x + button->w / 2,
					   button->y + button->h / 2,
			           button->text, button_palette [s + 2]);
}
/* 按钮处理程序,有触摸按到按钮返回0 */
static int button_handle (struct ts_button *button, struct ts_sample *samp)
{
	/* 触摸位置在按钮框内 inside = 1 */
	int inside = (samp->x >= button->x) && (samp->y >= button->y) &&
				 (samp->x < button->x + button->w) &&
		         (samp->y < button->y + button->h);

	/* 有触摸按下 */
	if ( samp->pressure > 0 ) 
	{
		if (inside) 	/* 按下位置在按钮框范围内 */
		{
			if ( !(button->flags & BUTTON_ACTIVE) ) 
			{
				button->flags |= BUTTON_ACTIVE;
				button_draw (button);
			}
		} else if (button->flags & BUTTON_ACTIVE) 
		{
			/* 触摸位置不在按钮框范围内 */
			button->flags &= ~BUTTON_ACTIVE;
			button_draw (button);
		}
	} /* 没有触摸 */
	else if (button->flags & BUTTON_ACTIVE) 
	{
		button->flags &= ~BUTTON_ACTIVE;
		button_draw (button);
        return 1;
	}
    return 0;
}
/* 刷新LCD屏幕 */
static void refresh_screen ()
{
	int i;

	fillrect (0, 0, xres - 1, yres - 1, 0);
	put_string_center (xres/2, yres/4,   "TSLIB test program", 1);
	put_string_center (xres/2, yres/4+20,"Touch screen to move crosshair", 2);

	for (i = 0; i < NR_BUTTONS; i++)
		button_draw (&buttons [i]);
}

/************* 主程序 *****************/
int main()
{
	struct tsdev *ts;		/* define a ts device */ 
	int x, y;			
	unsigned int i;
	unsigned int mode = 0;

	char *tsdevice=NULL;	/* ts device name */

	signal(SIGSEGV, sig);	/* tty singal */
	signal(SIGINT, sig);
	signal(SIGTERM, sig);

	/* read ts device from env */
	if ( (tsdevice = getenv("TSLIB_TSDEVICE")) == NULL ) 
	{
#ifdef USE_INPUT_API
		tsdevice = strdup ("/dev/input/event0");
#else
		/* default ts dev */
		tsdevice = strdup ("/dev/touchscreen/ucb1x00");
#endif /* USE_INPUT_API */
    }
	/* open ts device */
	ts = ts_open (tsdevice, 0);
	/* open error */
	if (!ts) {
		perror (tsdevice);
		exit(1);
	}
	/* config ts from ts.conf */
	if ( ts_config(ts) ) 
	{
		perror("ts_config");
		exit(1);
	}
	/* open lcd  */
	if (open_framebuffer()) {
		close_framebuffer();
		exit(1);
	}
	/* lcd center position */
	x = xres/2;
	y = yres/2;
	
	/* set palette color */
	for (i = 0; i < NR_COLORS; i++)
		setcolor (i, palette [i]);

	/* Initialize buttons */
	memset (&buttons, 0, sizeof (buttons));
	buttons [0].w = buttons [1].w = xres / 4;
	buttons [0].h = buttons [1].h = 20;
	buttons [0].x = xres / 4 - buttons [0].w / 2;
	buttons [1].x = (3 * xres) / 4 - buttons [0].w / 2;
	buttons [0].y = buttons [1].y = 10;
	buttons [0].text = "Drag";
	buttons [1].text = "Draw";

	refresh_screen ();

	while (1) 
	{
		struct ts_sample samp;     /* define the ts_sample */
		int ret;

		/* Show the cross */
		if ((mode & 15) != 1)
			put_cross(x, y, 2 | XORMODE);
		/* read the ts sample */
		ret = ts_read(ts, &samp, 1);

		/* Hide it */
		if ((mode & 15) != 1)
			put_cross(x, y, 2 | XORMODE);
		/* ts read error */
		if (ret < 0) {
			perror("ts_read");
			close_framebuffer();
			exit(1);
		}

		if (ret != 1)
			continue;
		/* 检测两个按钮是否按下,按下的时候刷屏 */
		for (i = 0; i < NR_BUTTONS; i++)
			if (button_handle (&buttons [i], &samp))
				switch (i) 
				{
					case 0:
						mode = 0;
						refresh_screen ();
						break;
					case 1:
						mode = 1;
						refresh_screen ();
						break;
				}

		printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec,
			samp.x, samp.y, samp.pressure);
		/* 有触摸按下,则两点连线方式画出触摸效果 */
		if (samp.pressure > 0) 
		{
			if (mode == 0x80000001)
				line (x, y, samp.x, samp.y, 2);
			x = samp.x;
			y = samp.y;
			mode |= 0x80000000;
		} else
			mode &= ~0x80000000;
	}
	close_framebuffer();
}

	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值