📄 6_5548_4.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#ifdef _SMDK2410_IAL
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/kd.h>
#include <sys/time.h> /* i add it here */
#include <math.h>
#include <sys/poll.h>
#include <sys/types.h>
#include "ial.h"
#include "2410.h"
typedef struct mPoint {
int x,
y ;
} mPOINT ;
static mPOINT ts_position[4]={ {940,926},{940,102},{96,104},{120,940}
};
static mPOINT display_position[4]={ {0,0},{0,319},{239,319},{239,0}
};
typedef struct Matrix {
/* This arrangement of values facilitates
* calculations within getDisplayPoint()
*/
int An, /* A = An/Divider */
Bn, /* B = Bn/Divider */
Cn, /* C = Cn/Divider */
Dn, /* D = Dn/Divider */
En, /* E = En/Divider */
Fn, /* F = Fn/Divider */
Divider ;
} mMATRIX ;
static mMATRIX m_matrix;
int setCalibrationMatrix( mPOINT * display,
mPOINT * screen,
mMATRIX * matrix) ;
int getDisplayPoint( mPOINT * display,
mPOINT * screen,
mMATRIX * matrix ) ;
/* for data reading from /dev/ts */
typedef struct {
unsigned short pressure;
unsigned short x;
unsigned short y;
unsigned short pad;
} TS_EVENT;
static unsigned char state [NR_KEYS];
static int ts = -1;
static int mousex = 0;
static int mousey = 0;
static TS_EVENT ts_event;
#undef _DEBUG
/************************ Low Level Input Operations **********************/
/*
* Mouse operations -- Event
*/
static int mouse_update(void)
{
return 1;
}
static void mouse_getxy(int *x, int* y)
{
#ifdef _DEBUG
printf ("mousex = %d, mousey = %d\n", mousex, mousey);
#endif
if (mousex < 0) mousex = 0;
if (mousey < 0) mousey = 0;
if (mousex > 239) mousex = 239;
if (mousey > 319) mousey = 319;
*x = mousex;
*y = mousey;
}
static int mouse_getbutton(void)
{
return ts_event.pressure;
}
#ifdef _LITE_VERSION
static int wait_event (int which, int maxfd, fd_set *in, fd_set *out, fd_set *except,
struct timeval *timeout)
#else
static int wait_event (int which, fd_set *in, fd_set *out, fd_set *except,
struct timeval *timeout)
#endif
{
fd_set rfds;
int retvalue = 0;
int e;
static int last_pressure=0;
if (!in) {
in = &rfds;
FD_ZERO (in);
}
if ((which & IAL_MOUSEEVENT) && ts >= 0) {
FD_SET (ts, in);
#ifdef _LITE_VERSION
if (ts > maxfd) maxfd = ts;
#endif
}
#ifdef _LITE_VERSION
e = select (maxfd + 1, in, out, except, timeout) ;
#else
e = select (FD_SETSIZE, in, out, except, timeout) ;
#endif
if (e > 0) {
if (ts >= 0 && FD_ISSET (ts, in) ) {
FD_CLR (ts, in);
ts_event.x=0;
ts_event.y=0;
read (ts, &ts_event, sizeof (TS_EVENT));
if(last_pressure==0)
{
read(ts,&ts_event,sizeof(TS_EVENT));
read(ts,&ts_event,sizeof(TS_EVENT));
}
if (ts_event.pressure > 0 ) {
int new_x;
int new_y;
mPOINT ts_point,display_point;
ts_point.x=ts_event.x;
ts_point.y=ts_event.y;
getDisplayPoint(&display_point,&ts_point,&m_matrix);
new_x = display_point.x;
new_y = display_point.y;
if(last_pressure==0 || (last_pressure>0 && abs(new_x-mousex)<7))
mousex=new_x;
if(last_pressure==0 || (last_pressure>0 && abs(new_y-mousey)<7))
mousey=new_y;
/*
printf("ts_x=%d,ts_y=%d\n",ts_event.x,ts_event.y);
printf("mounsex=%d mousey=%d\n",mousex,mousey);
*/
}
#ifdef _DEBUG
if (ts_event.pressure > 0) {
printf ("mouse down: ts_event.x = %d, ts_event.y = %d\n", ts_event.x, ts_event.y);
}
#endif
ts_event.pressure = ( ts_event.pressure > 0 ? IAL_MOUSE_LEFTBUTTON : 0);
last_pressure=ts_event.pressure;
/*
printf("pressure=%d\n",ts_event.pressure);
*/
retvalue |= IAL_MOUSEEVENT;
}
}
else if (e < 0) {
return -1;
}
return retvalue;
}
BOOL Init2410Input (INPUT* input, const char* mdev, const char* mtype)
{
ts = open ("/dev/ts", O_RDONLY);
if (ts < 0) {
fprintf (stderr, "2410: Can not open touch screen!\n");
return FALSE;
}
input->update_mouse = mouse_update;
input->get_mouse_xy = mouse_getxy;
input->set_mouse_xy = NULL;
input->get_mouse_button = mouse_getbutton;
input->set_mouse_range = NULL;
input->wait_event = wait_event;
mousex = 0;
mousey = 0;
ts_event.x = ts_event.y = ts_event.pressure = 0;
setCalibrationMatrix(&display_position,&ts_position,&m_matrix);
return TRUE;
}
void Term2410Input(void)
{
if (ts >= 0)
close(ts);
}
#endif /* _SMDK2410_IAL */
int setCalibrationMatrix( mPOINT * displayPtr,
mPOINT * screenPtr,
mMATRIX * matrixPtr)
{
int retValue = 0 ;
matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
if( matrixPtr->Divider == 0 )
{
retValue = -1 ;
}
else
{
matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
(screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
(screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
(screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
(screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
}
return( retValue ) ;
} /* end of setCalibrationMatrix() */
int getDisplayPoint( mPOINT * displayPtr,
mPOINT * screenPtr,
mMATRIX * matrixPtr )
{
int retValue = 0 ;
if( matrixPtr->Divider != 0 )
{
/* Operation order is important since we are doing integer */
/* math. Make sure you add all terms together before */
/* dividing, so that the remainder is not rounded off */
/* prematurely. */
displayPtr->x = ( (matrixPtr->An * screenPtr->x) +
(matrixPtr->Bn * screenPtr->y) +
matrixPtr->Cn
) / matrixPtr->Divider ;
displayPtr->y = ( (matrixPtr->Dn * screenPtr->x) +
(matrixPtr->En * screenPtr->y) +
matrixPtr->Fn
) / matrixPtr->Divider ;
}
else
{
retValue = -1 ;
}
return( retValue ) ;
} /* end of getDisplayPoint() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -