管理提醒: 本帖被 kasim 设置为精华(2010-10-24)
因为MiniGUI的轻巧,灵活,而且还是我们国产的嵌入式GUI,我弃QT的强大首选了它来学习。由于minigui的文档挺详细的而且是汉语编写的,学习起来挺容易的。
可是学习了这么久还是用鼠标和键盘进行操作,感觉触摸屏挺委屈的,于是从网上搜罗了一大堆的IAL移植资料,大部分都是 minigui+tslib,而且内容都基本上相同。
按照这些方法很快的就移植好了(我没有修改tslib的源代码),可是问题也就来了:按下去不能抬起,直到下一次按下才抬起下:
难道是我漏掉了那一节?前辈们认为tslib与mingui的压力感应相反,minigui是1为按下,而tslib为0;所以要将tslib的源代码的压力输出部分修改,可是我用程序测试tslib输出没问题啊?按下为1啊;
这是测试代码:
复制代码- #include<tslib.h>
- #include<stdio.h>
- #include <stdlib.h>
- int main()
- {
- int i,x,y,re;
- char *ts_device,input;
- static struct tsdev *ts;
- static struct ts_sample spl;
-
- if((ts_device = getenv("TSLIB_TSDEVICE")) != NULL)
- {
- ts = ts_open(ts_device, 0);
- if(ts!=NULL)
- printf("tslib opened!\n");
- re=ts_config(ts);
- if(re==0)
- printf("ts config ok\n");
- }
- while(1)
- {
- re=-1;
- printf("continue or cancel?\n");
- input=getchar();
- if(input=='n')
- {
- ts_close(ts);
- return 0;
- }
- // for(i=0;i<10;i++)
- // {
- re=ts_read(ts,&spl,1);
- if(re<0) printf("erro\n");
- printf("x=%d y=%d pressure=%d \n",spl.x,spl.y,spl.pressure);
- // }
- }
- return 0;
- }
|
从这个测试程序我发现
tslib会缓存最近的数据,一松开设备将处于阻塞状态,当ts_open以阻塞方式打开时将0读完后,ts_read将进入阻塞状态。!
我再分析IAL的核心函数wait_event
我贴上前辈的函数:
复制代码- static int wait_event (int which, fd_set *in, fd_set *out, fd_set *except, struct timeval *timeout)
- {
- struct ts_sample sample;
- int ret = 0;
- int fd;
- fd_set rfds;
- int e;
- if (!in)
- {
- in = &rfds;
- FD_ZERO (in);
- }
- fd = ts_fd(ts);
- if ((which & IAL_MOUSEEVENT) && fd >= 0)
- {
- printf("which is %d ",which);
- FD_SET (fd, in);
- }
- e = select (FD_SETSIZE, in, out, except, timeout) ;
- if (e > 0)
- {
- if (fd > 0 && FD_ISSET (fd, in))
- {
- FD_CLR (fd, in);
- ts_event.x=0;
- ts_event.y=0;
- ret = ts_read(ts, &sample, 1);
- if (ret < 0)
- {
- perror("ts_read()");
- exit(-1);
- }
- ts_event.x = sample.x;
- ts_event.y = sample.y;
- ts_event.pressure = (sample.pressure > 0 ? 4:0);
- if((ts_event.x >= 0 && ts_event.x <= 239) &&
- (ts_event.y >= 0 && ts_event.y <= 319))
- {
- mousex = ts_event.x;
- mousey = ts_event.y;
- }
- ret |= IAL_MOUSEEVENT;
- return (ret);
- }
- }
- else if (e < 0)
- {
- return -1;
- }
- return (ret);
- }
|
当你按下去的时候可以顺利的读取数据,即wait_event返回4(鼠标左击事件)
当你松开时,设备阻塞,select将会超时,e将会等于零,导致不能用ts_read读取松开时的值即sample.pressure,所以你要是不按下去将会永远保持上次按下的状态,直到你下次的按下才会读取上次缓存松开的值即sample.pressure=0;所以导致了按下去抬起依旧保持按下去得状态。
解决的办法是:去掉select函数!直接用ts_read读取判断!
去掉select后的代码为:
复制代码- static int wait_event (int which, fd_set *in, fd_set *out, fd_set *except,struct timeval *timeout)
- {
- struct ts_sample sample;
- int ret = 0;
- ts_event.x=0;
- ts_event.y=0;
- ret = ts_read(ts, &sample, 1);
- if (ret < 0)
- {
- perror("ts_read()");
- exit(-1);
- }
- ts_event.x = sample.x;
- ts_event.y = sample.y;
- ts_event.pressure = (sample.pressure > 0 ? 4:0);
- if((ts_event.x >= 0 && ts_event.x <= 239) &&(ts_event.y >= 0 && ts_event.y <= 319))
- {
- mousex = ts_event.x;
- mousey = ts_event.y;
- }
- ret |= IAL_MOUSEEVENT;
- return (ret);
- }
|
注意:用这种方式ts_open的第二个参数要为0!