现在的位置: 首页 > 综合 > 正文

TQ2440触模屏事件的读取和转换

2014年08月07日 ⁄ 综合 ⁄ 共 1671字 ⁄ 字号 评论关闭

/*
触模屏事件的处理和转换,适合TQ2440上面使用
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/input.h>   
#include <time.h>
#include <stdlib.h>
int touch_x,touch_y;//点击按下的X和Y坐标
static int event0_fd = -1;  //打开触模设备文析的文件描述符
struct input_event ev0[64];  //存放读取到的触模数值
char envent_buf[1024];  //用于清除触模缓存

//触模屏初始化
int initEvent(void)
{
    event0_fd = open("/dev/event0", O_RDWR);  
if(event0_fd < 0)
    {  
    printf("open input device error\n");  
return -1; 
    }
return 0;
}

//触模屏数据的读取
int handle_event(void)  
{  
 char buf[4096];
    int realx = 0, realy = 0, i = 1, rd;  
  
    rd = read(event0_fd, ev0, sizeof(struct input_event)* 256);  
    if(rd < sizeof(struct input_event)) 
    {
rd = read(event0_fd, buf, 4096);
return 0;  
    }
for(i = 0; i < rd/sizeof(struct input_event); i++)  
    {  
        if(EV_ABS == ev0[i].type) //首先判断类型是不是绝对坐标 
        {  
            if(ev0[i].code == 0) 
{  //接着判断是不是X轴的坐标
                realy = ev0[i].value;  
            } 
            else if(ev0[i].code == 1) 
{  //是不是Y轴的坐标,这两个坐标不能同时得到,一次只能得到一个
                realx = ev0[i].value;  
            }  
        }  
       printf("event(%d):type:%d; code:%3d; value:%3d; realx:%3d; realy:%3d\n",i,ev0[i].type,ev0[i].code,ev0[i].value,realx,realy);
    } 
//读到的数据,要进行一个校准,转换成我需要的XY坐标
#if 1
touch_x = (int)((realx-525)*0.551) + 240;
if(touch_x < 0)
touch_x = -1;
touch_y = (int)((realy - 511)*0.362) + 136;
if(touch_y < 0)
touch_y = -1;

#else
touch_x = 480 - (360 - (((float)realx - 326)/(696 - 326))*240); 
touch_y = 272 - (204 - (((float)realy - 746)/(317 - 746))*136);
#endif

  printf("##realx=%d______##realy=%d\n", realx, realy);
printf("########%d______%d########\n", touch_x, touch_y);
  usleep(200*1000);  
return 0;   
}
/*
int main(void)
{
//初始化触模函数
initEvent();
while(1)
{
handle_event();

}

}
*/

抱歉!评论已关闭.