在电阻式的触摸屏上开发嵌入式应用时,Qt+tslib的组合很常见,可谓经典组合。tslib库中提供了一些测试程序,比如ts_calibrate,ts_test,可以用来对触摸屏进行校准,测试。在Qt中也提供了一个例程,也是用来触屏校准的(http://doc.qt.io/qt-4.8/qt-qws-mousecalibration-calibration-cpp.html) ,Qt库中把触摸屏模拟成鼠标设备来实现,我试了这种方法,发现校准后在/etc/pointercal产生的数据不大对,和ts_calibrate程序产生的数据相差很大,校准后触屏无法正常使用,而且用鼠标点点点也能触发校准完成。由于我的项目应用中是鼠标和触屏混用的,这种校准方式不可取,于是就想到了利用tslib库中的方法,来实现Qt上的触摸屏校准应用。
首先分下下tslib库中的test/ts_calibrate.c程序,去除显示部分(显示校准点十字框),代码如下。
typedef struct {
int x[5], xfb[5];
int y[5], yfb[5];
int a[7];
} calibration;
int perform_calibration(calibration *cal) {
int j;
float n, x, y, x2, y2, xy, z, zx, zy;
float det, a, b, c, e, f, i;
float scaling = 65536.0;
...
}
static void get_sample (struct tsdev *ts, calibration *cal,
int index, int x, int y, char *name)
{
...
put_cross(x, y, 2 | XORMODE);
...
getxy (ts, &cal->x [index], &cal->y [index]);
cal->xfb [index] = x;
cal->yfb [index] = y;
}
int main()
{
struct tsdev *ts; calibration cal;
int cal_fd; char cal_buffer[256];
char *tsdevice = NULL; char *calfile = NULL;
if( (tsdevice = getenv("TSLIB_TSDEVICE")) != NULL ) {
ts = ts_open(tsdevice,0);
} else {
...
}
if (ts_config(ts)) {
perror("ts_config");
exit(1);
}
get_sample (ts, &cal, 0, 50, 50, "Top left");
get_sample (ts, &cal, 1, xres - 50, 50, "Top right");
get_sample (ts, &cal, 2, xres - 50, yres - 50, "Bot right");
get_sample (ts, &cal, 3, 50, yres - 50, "Bot left");
get_sample (ts, &cal, 4, xres / 2, yres / 2, "Center");
if (perform_calibration (&cal)) {
if ((calfile = getenv(