Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9152102
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2013-08-07 12:56:12

使用ts_lib包自带的ts_calibrate校准触摸屏非常简单实用,但在基于Xsever的GUI应用环境下,有两个问题存在:

  1.校准后必须重新启动X,应用端才能生效。这样处理用户肯定不能接受,因为启动一次机器毕竟耗时。

  2.在使用ts_calibrate校准触摸屏时,要触摸5个点,这时如果GUI应用端在运行其他响应触摸事件(鼠标事件)的程序就会出现错乱。所以安全的做法应该是在校准触摸屏时进行锁屏操作。

  解决这两个问题之前来看看tslib校准方面的原理,如果将原理搞清楚,剩下就是方法实现的问题了。

  Tslib 是触摸屏驱动和应用层之间的适配层,它从触摸屏驱动处获得原始的设备坐标数据,通过一系列的去噪、去抖、坐标变换等操作,来去除噪声并将原始的设备坐标转 换为相应的屏幕坐标。通过tslib/src/tslib.h文件可以看出,在tslib中为应用层提供了2个主要的接口 ts_open(),ts_close();ts_read()和ts_read_raw(),其中ts_read()为正常情况下的接 口,ts_read_raw()为校准情况下使用的接口。从tslib默认的ts.conf文件中可以看出包括如下基本插件:

  pthres 为Tslib 提供的触摸屏灵敏度门槛插件;

  variance 为Tslib提供的触摸屏滤波算法插件;

  dejitter 为Tslib 提供的触摸屏去噪算法插件;

  linear为Tslib 提供的触摸屏坐标变换插件。

  tslib 从触摸屏驱动采样到的设备坐标进行处理再提供给应用端的过程大体如下:

  raw device --> variance --> dejitter --> linear --> application

  module         module       module      module

  再来看看ts_calibrate主要做了哪些事情,校准情况下,tslib对驱动采样到的数据进行处理的一般过程如下:

  1。读取屏上5个点的坐标(Top Left,Top Right,Bottom Left,Bottom Right,Center),在进行一系列的变换,取样的5个点,实际上是包含3个不同的X值,3个不同的Y值。和scaling 值一共7个值,一起保存到/etc/pointercal中.

  2.这个/etc/pointercal文件主要是供linear插件使用。而我们每次的触摸的操作都进行多次触摸坐标变换。

  至此已经找到解决问题的大体的方法了。在校准触摸屏后只需及时的让linear插件再次读取新的/etc/pointeracal文件,这样新校准的坐标信息就及时的更新到上层应用。下面就要考虑具体实现的问题了。

  1。从linear.c文件可以看出在该模块初始化时读取了/etc/pointercal文件。只要在linear_read()中读取新的/etc/pointercal文件即可。



如何锁屏?这需要从内核入手了,查看linux 2.6 内核 /drivers/input/evdev.c从该驱动提供的ioctl中看到对基于evdev的输入设备都提供EVIOCGRAB实现。顾名思 义,grab就是将当前的输入操作抓取到当前的操作中,让当前操作之外的所有应用端读不到触摸屏的触摸操作。由驱动 就很容易知道该如何实现锁屏解锁操作了。 如下:

  truct tsdev *ts;

  char *tsdevice = "/dev/input/event0";

  ts = ts_open(tsdevice, 0);

  int ts_tmpfd = ts_fd(ts);

  if (ts_tmpfd== -1)

  {

  perror("ts_open");

  exit(1);

  }

  unsigned long val =1;

  int ioctl_ret=ioctl(ts_tmpfd,EVIOCGRAB,&val);

  printf("now lock the ts ioctl ret is:%dn",ioctl_ret);

  if (ioctl_ret!=0)

  {

  printf("Error: %sn", strerror(errno));

  exit(1);

  }

  printf("lock the ts success n");



点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <sys/ioctl.h>
  6. #include <linux/input.h>

  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <pthread.h>

  12. #include <tslib.h>

  13. #define TS_DEV_FAKE        "/dev/fake_event1"
  14. #define TSDEV_MAJOR        13
  15. #define TS_DEV_MINOR    65

  16. typedef struct {
  17.     int x[5], xfb[5];
  18.     int y[5], yfb[5];
  19.     int a[7];
  20. } calibration;

  21. static calibration cal;

  22. static struct tsdev *fake_ts = NULL;

  23. void fake_tsdev_grab(int bEnabled)
  24. {
  25.     int ts_tmpfd = ts_fd(fake_ts);

  26.     ioctl(ts_tmpfd, EVIOCGRAB, &bEnabled);
  27. }

  28. void fake_tsdev_close(void)
  29. {    
  30.     if (fake_ts != NULL) {
  31.         fake_tsdev_grab(0);
  32.         ts_close(fake_ts);
  33.     }
  34.         
  35.     fake_ts = NULL;
  36. /*    
  37.     if (0 == access( TS_DEV_FAKE, F_OK | R_OK))
  38.         remove(TS_DEV_FAKE);
  39. */        
  40. }
  41. /*
  42. * 复制一个 tsdev 的设备节点并打开。
  43. * < 0: 失败。
  44. * > 0:返回的设备句柄。
  45. */

  46. int fake_tsdev_dup(void)
  47. {
  48. /*
  49.     int iret;
  50.     dev_t dev_id;
  51.     const char* tsdevice ;
  52.     struct stat stat_buf;    
  53. */    
  54.     if (fake_ts != NULL) {
  55.         fake_tsdev_close();
  56.     }
  57. /*    //all the fake device is create on system init process in script.

  58.     tsdevice = getenv ("TSLIB_TSDEVICE");
  59.     if (tsdevice == NULL)
  60.     {
  61.         perror("ENV TSLIB_TSDEVICE Not Found.");
  62.         return -1;
  63.     }
  64.     
  65.     if ( 0 != (stat(tsdevice, &stat_buf)))
  66.     {
  67.         perror("stat TSLIB_TSDEVICE error.");
  68.         return -1;
  69.     }    
  70.     
  71.     //dev_id = makedev(TSDEV_MAJOR, TS_DEV_MINOR);
  72.     dev_id = stat_buf.st_rdev;
  73.     fprintf(stderr, "fake touchscreen. char dev %d-%d\n", major(dev_id), minor(dev_id));

  74.     if (access(TS_DEV_FAKE, F_OK) != 0) {    
  75.         iret = mknod(TS_DEV_FAKE, stat_buf.st_mode, dev_id);
  76.         if (-1 == iret) {
  77.             perror("mknod fake tsdev");
  78.             fake_tsdev_close();
  79.             return -1;
  80.         }
  81.     }
  82. */        
  83.     fake_ts = ts_open (TS_DEV_FAKE, 0);
  84.     if (!fake_ts) {
  85.         perror("fake ts_open");
  86.         fake_tsdev_close();
  87.         return -1;
  88.     }
  89.     
  90.     fake_tsdev_grab(1);
  91.     
  92.     if (ts_config(fake_ts)) {
  93.         perror("fake ts_config");
  94.         fake_tsdev_close();
  95.         return -1;
  96.     }    

  97.     return ts_fd (fake_ts);
  98. }

  99. static int sort_by_x(const void* a, const void *b)
  100. {
  101.     return (((struct ts_sample *)a)->x - ((struct ts_sample *)b)->x);
  102. }

  103. static int sort_by_y(const void* a, const void *b)
  104. {
  105.     return (((struct ts_sample *)a)->y - ((struct ts_sample *)b)->y);
  106. }

  107. /*
  108. * 开始获取某个点的坐标原始信息。0-4
  109. * 0: get OK
  110. * <0: failed.
  111. */
  112. int fake_tsdev_get_sample (int index, int origin_x, int origin_y)
  113. {    
  114. #define MAX_SAMPLES 128
  115.     struct ts_sample samp[MAX_SAMPLES];
  116.     int idx, middle;
  117.     int fs_fd, iret;
  118.     fd_set rfds;
  119.     struct timeval timeout;
  120.     
  121.     if (index == 0) {
  122.         memset(&cal, 0, sizeof(cal));
  123.     }
  124.     
  125.     printf("Took %d samples...\n",index);
  126.     
  127.     if ((index<0) | (index>4)) return -1;
  128.     
  129.     do {            
  130.         fs_fd = ts_fd (fake_ts);
  131.         FD_ZERO (&rfds);
  132.         FD_SET(fs_fd, &rfds);
  133.         timeout.tv_sec = 0;
  134.         timeout.tv_usec = 10000;
  135.         iret = select (fs_fd + 1, &rfds, NULL, NULL, &timeout);
  136.         
  137.         if (0 > iret) goto err; //some error
  138.         if (0 == iret) continue;
  139.  
  140.         if (ts_read_raw(fake_ts, &samp[0], 1) < 0) {
  141.             perror("fake ts_read 0");
  142.             goto err;
  143.         }        
  144.     } while (samp[0].pressure == 0);
  145.     
  146.     /* Now collect up to MAX_SAMPLES touches into the samp array. */
  147.     idx = 0;
  148.     do {
  149.         if (idx < MAX_SAMPLES-1)
  150.             idx++;
  151.             
  152.         if (NULL == fake_ts) return -2;
  153.         if (ts_read_raw(fake_ts, &samp[idx], 1) < 0) {
  154.             perror("fake ts_read 1");
  155.             return -2;
  156.         }
  157.     } while (samp[idx].pressure > 0);
  158.     
  159.     middle = idx/2;
  160.     {
  161.         qsort(samp, idx, sizeof(struct ts_sample), sort_by_x);
  162.         if (idx & 1)
  163.             cal.x[index] = samp[middle].x;
  164.         else
  165.             cal.x[index] = (samp[middle-1].x + samp[middle].x) / 2;
  166.     }
  167.     {
  168.         qsort(samp, idx, sizeof(struct ts_sample), sort_by_y);
  169.         if (idx & 1)
  170.             cal.y [index] = samp[middle].y;
  171.         else
  172.             cal.y [index] = (samp[middle-1].y + samp[middle].y) / 2;
  173.     }
  174.     
  175.     cal.xfb[index] = origin_x;
  176.     cal.yfb[index] = origin_y;
  177.     
  178.     fprintf(stderr, "should[%d-%d] -> real[%d-%d]\n", cal.xfb[index], cal.yfb[index], cal.x[index], cal.y[index]);
  179.     
  180.     return 0;
  181. err:
  182.     return -2;
  183. }

  184. /*
  185. * 计算
  186. */
  187. int perform_calibration(calibration *cal) {
  188.     int j;
  189.     float n, x, y, x2, y2, xy, z, zx, zy;
  190.     float det, a, b, c, e, f, i;
  191.     float scaling = 65536.0;

  192. // Get sums for matrix
  193.     n = x = y = x2 = y2 = xy = 0;
  194.     for(j=0;j<5;j++) {
  195.         n += 1.0;
  196.         x += (float)cal->x[j];
  197.         y += (float)cal->y[j];
  198.         x2 += (float)(cal->x[j]*cal->x[j]);
  199.         y2 += (float)(cal->y[j]*cal->y[j]);
  200.         xy += (float)(cal->x[j]*cal->y[j]);
  201.     }

  202. // Get determinant of matrix -- check if determinant is too small
  203.     det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
  204.     if(det < 0.1 && det > -0.1) {
  205.         printf("ts_calibrate: determinant is too small -- %f\n",det);
  206.         return 0;
  207.     }

  208. // Get elements of inverse matrix
  209.     a = (x2*y2 - xy*xy)/det;
  210.     b = (xy*y - x*y2)/det;
  211.     c = (x*xy - y*x2)/det;
  212.     e = (n*y2 - y*y)/det;
  213.     f = (x*y - n*xy)/det;
  214.     i = (n*x2 - x*x)/det;

  215. // Get sums for x calibration
  216.     z = zx = zy = 0;
  217.     for(j=0;j<5;j++) {
  218.         z += (float)cal->xfb[j];
  219.         zx += (float)(cal->xfb[j]*cal->x[j]);
  220.         zy += (float)(cal->xfb[j]*cal->y[j]);
  221.     }

  222. // Now multiply out to get the calibration for framebuffer x coord
  223.     cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
  224.     cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
  225.     cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));

  226.     printf("%f %f %f\n",(a*z + b*zx + c*zy),
  227.                 (b*z + e*zx + f*zy),
  228.                 (c*z + f*zx + i*zy));

  229. // Get sums for y calibration
  230.     z = zx = zy = 0;
  231.     for(j=0;j<5;j++) {
  232.         z += (float)cal->yfb[j];
  233.         zx += (float)(cal->yfb[j]*cal->x[j]);
  234.         zy += (float)(cal->yfb[j]*cal->y[j]);
  235.     }

  236. // Now multiply out to get the calibration for framebuffer y coord
  237.     cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
  238.     cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
  239.     cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));

  240.     printf("%f %f %f\n",(a*z + b*zx + c*zy),
  241.                 (b*z + e*zx + f*zy),
  242.                 (c*z + f*zx + i*zy));

  243. // If we got here, we're OK, so assign scaling to a[6] and return
  244.     cal->a[6] = (int)scaling;    
  245.     
  246.     return 0;
  247. }

  248. /*
  249. * fake_tsdev_get_sample获得5个点之后调用,用户计算最终结果并保存。
  250. * 0:成功. <0 failed。
  251. */
  252. int fake_tsdev_update_tscalibrate(void)
  253. {
  254.     char *calfile = NULL;
  255.     int cal_fd, iret=0;
  256.     char cal_buffer[256];
  257.     
  258.     iret = perform_calibration(&cal);
  259.     if (iret != 0) return iret;
  260.     
  261.     if ((calfile = getenv("TSLIB_CALIBFILE")) != NULL) {
  262.         cal_fd = open (calfile, O_CREAT | O_RDWR);
  263.     } else {
  264.         cal_fd = open ("/etc/pointercal", O_CREAT | O_RDWR);
  265.     }
  266.     
  267.     memset(cal_buffer, 0, sizeof(cal_buffer));
  268.     sprintf (cal_buffer,"%d %d %d %d %d %d %d",
  269.              cal.a[1], cal.a[2], cal.a[0],
  270.              cal.a[4], cal.a[5], cal.a[3], cal.a[6]);
  271.             
  272.     printf("fake touch offset [%s]\n", cal_buffer);
  273.     write (cal_fd, cal_buffer, strlen (cal_buffer) + 1);
  274.     close (cal_fd);
  275.     return 0;
  276. }

阅读(2935) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~