结构体设置
static T_InputOpr g_tTouchScreenOpr = {
.name = "touchscreen",
.DeviceInit = TouchScreenDevInit,
.DeviceExit = TouchScreenDevExit,
.GetInputEvent = TouchScreenGetInputEvent,
};
结构体注册
int TouchScreenInit(void)
{
return RegisterInputOpr(&g_tTouchScreenOpr);
}
触摸屏初始化
参考tslib中的ts_printf.c
/* 注意: 由于要用到LCD的分辨率, 此函数要在SelectAndInitDisplay之后调用 */
static int TouchScreenDevInit(void)
{
char *pcTSName = NULL;
//如果有TSLIB_TSDEVICE这个环境变量,则open 环境变量设置的TSLIB_TSDEVICE这个设备(pcTSName),else 打开 /dev/event0
if ((pcTSName = getenv("TSLIB_TSDEVICE")) != NULL )
{
//这里ts_open的第二个形参是非阻塞设置(为1是非阻塞)
g_tTSDev = ts_open(pcTSName, 1);
}
else
{
g_tTSDev = ts_open("/dev/event0", 1);
}
if (!g_tTSDev) {
DBG_PRINTF("ts_open error!\n");
return -1;
}
if (ts_config(g_tTSDev)) {
DBG_PRINTF("ts_config error!\n");
return -1;
}
//获得lcd屏的尺寸,这里是调用了disp_opr里的init函数获得fb的var信息,该函数在draw.c里定义
if (GetDispResolution(&giXres, &giYres))
{
return -1;
}
return 0;
}
触摸屏event获取解析
//判断触摸屏这次事件是否距离上次次触摸事件500ms
static int isOutOf500ms(struct timeval *ptPreTime, struct timeval *ptNowTime)
{
int iPreMs;
int iNowMs;
iPreMs = ptPreTime->tv_sec * 1000 + ptPreTime->tv_usec / 1000;
iNowMs = ptNowTime->tv_sec * 1000 + ptNowTime->tv_usec / 1000;
return (iNowMs > iPreMs + 500);
}
static int TouchScreenGetInputEvent(PT_InputEvent ptInputEvent)
{
struct ts_sample tSamp;
int iRet;
static struct timeval tPreTime;
iRet = ts_read(g_tTSDev, &tSamp, 1);
if (iRet < 0) {
return -1;
}
/* 处理数据 上一次和这一次触摸事件有500ms,则处理数据*/
if (isOutOf500ms(&tPreTime, &tSamp.tv))
{
/* 如果此次触摸事件发生的时间, 距上次事件超过了500ms */
tPreTime = tSamp.tv;
ptInputEvent->tTime = tSamp.tv;
ptInputEvent->iType = INPUT_TYPE_TOUCHSCREEN;
//这里tSamp.y是触摸点在LCD屏是的y坐标,与giYres/3(触摸屏1/3位置)比较,就可以判断是否是上翻event
if (tSamp.y < giYres/3)
{
ptInputEvent->iVal = INPUT_VALUE_UP;
}
//这里tSamp.y是触摸点在LCD屏是的y坐标,与giYres * 2/3(触摸屏2/3位置)比较,就可以判断是否是下翻event
else if (tSamp.y > 2*giYres/3)
{
ptInputEvent->iVal = INPUT_VALUE_DOWN;
}
//else是判断点按了触摸屏中间
else
{
ptInputEvent->iVal = INPUT_VALUE_UNKNOWN;
}
return 0;
}
else
{
return -1;
}
return 0;
}