static BOOL perform_calibration(CALIBRATION_t *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;
// Get sums for matrix
n = x = y = x2 = y2 = xy = 0;
for (j = 0; j < 5; j++)
{
n += 1.0;
x += (float)cal->x[j];
y += (float)cal->y[j];
x2 += (float)(cal->x[j] * cal->x[j]);
y2 += (float)(cal->y[j] * cal->y[j]);
xy += (float)(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)
{
return (FALSE);
}
// 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;
// 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));
// 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));
// If we got here, we're OK, so assign scaling to a[6] and return
cal->a[6] = (int)scaling;
return (TRUE);
}
/*
功能:读取触摸屏读坐标,该坐标未做转换,不能直接使用
返回:0=无效坐标
1=有效坐标
说明:本函数连续采样2次,2次采样结果+-5范围内才算有效
*/
static BOOL TOUCH_MeasureXY(int *x, int *y)
{
int x1, y1;
int x2, y2;
int i;