01 - 作业所在路径
ARM裸机1期加强版\源码文档图片\文档图片\第018课_ADC和触摸屏
02 - 作业描述
- 百度搜索下载tslib库
- 分析它的较准函数(不需要理解原理)、移植
- tslib还有其他功能,比如判断连续点之间的距离,如果过大就丢弃。
这可以解决我们程序中出现的问题:画线时突然有某个点在很远的距离出现
03 - 作业解答
tslib库的运算量太大,有大量的浮点数乘除法运算,目前的裸版代码不支持,移植没有问题,只是计算出错,除法运算库不够强大
ts_calibrate.h
#ifndef _TSCALIBRATE_H
#define _TSCALIBRATE_H
typedef struct {
int x[5], xfb[5];
int y[5], yfb[5];
int a[7];
} calibration;
int perform_calibration(calibration *cal);
#endif /* _TSCALIBRATE_H */
ts_calibrate.c
#include "../uart.h"
#include "ts_calibrate.h"
int perform_calibration(calibration *cal)
{
int j;
double n, x, y, x2, y2, xy, z, zx, zy;
double det, a, b, c, e, f, i;
double scaling = 65536.0;
/* Get sums for matrix */
n = x = y = x2 = y2 = xy = 0;
for (j = 0; j < 5; j++) {
n += 1.0;
x += (double)cal->x[j];
y += (double)cal->y[j];
x2 += (double)(cal->x[j]*cal->x[j]);
y2 += (double)(cal->y[j]*cal->y[j]);
xy += (double)(cal->x[j]*cal->y[j]);
}
/* Get determinant of matrix -- check if determinant is too small */
det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
if (det < 0.1 && det > -0.1) {
printf("ts_calibrate: determinant is too small -- %d\n\r", (int)det);
return 0;
}
/* Get elements of inverse matrix */
a = (x2*y2 - xy*xy)/det;
b = (xy*y - x*y2)/det;
c = (x*xy - y*x2)/det;
e = (n*y2 - y*y)/det;
f = (x*y - n*xy)/det;
i = (n*x2 - x*x)/det;
printf("abcefi:%d %d %d %d %d %d \r\n",a,b,c,e,f,i);
/* Get sums for x calibration */
z = zx = zy = 0;
for (j = 0; j < 5; j++) {
z += (double)cal->xfb[j];
zx += (double)(cal->xfb[j]*cal->x[j]);
zy += (double)(cal->xfb[j]*cal->y[j]);
}
/* Now multiply out to get the calibration for framebuffer x coord */
cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));
printf("%d %d %d\n\r", (cal->a[0]),
(cal->a[1]),
(cal->a[2]));
/* Get sums for y calibration */
z = zx = zy = 0;
for (j = 0; j < 5; j++) {
z += (double)cal->yfb[j];
zx += (double)(cal->yfb[j]*cal->x[j]);
zy += (double)(cal->yfb[j]*cal->y[j]);
}
/* Now multiply out to get the calibration for framebuffer y coord */
cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));
printf("%d %d %d\n\r", (cal->a[3]),
(cal->a[4]),
(cal->a[5]));
/* If we got here, we're OK, so assign scaling to a[6] and return */
cal->a[6] = (int)scaling;
return 1;
}
然后在原来代码tslib.c的ts_calibrate()
函数中调用
……
if (g_ts_xy_swap)
{
/* 对调所有点的XY坐标 */
swap_xy(&(cal.x[0]), &(cal.y[0]));
swap_xy(&(cal.x[1]), &(cal.y[1]));
swap_xy(&(cal.x[2]), &(cal.y[2]));
swap_xy(&(cal.x[3]), &(cal.y[3]));
swap_xy(&(cal.x[4]), &(cal.y[4]));
}
print_cal(&cal);
/* 执行校准 */
perform_calibration(&cal);
//print_cal(&cal); //出错,计算错误
……