一,触摸校准算法 触摸屏校准通用方法。 (XL, YL是显示屏坐标,XT, YT是触摸屏坐标,) XL = XT*A+YT*B+C YL = XT*D+YT*E+F 由于具体计算是希望是整数运算,所以实际中保存的ABCDEF为整数,而增加一个参数Div XL = (XT*A+YT*B+C) / Div YL = (YT*D+YT*E+F) / Div TSLIB把以上的7个参数 ABCDEF Div 保存在 pointercal 文件中。 不校准的数据: A=1, B=0, C=0, D=0, E=1, F=0, Div=1
A |
B |
C |
D |
E |
F |
Div |
-411 |
37818 |
-3636780 |
-51325 |
39 |
47065584 |
65536 |
二,Android 事件处理机制 android 事件的传入是从EventHub开始的,EventHub是 事件的抽象结构,维护着系统设备的运行情况(设备文件放在/dev/input里),设备类型包括Keyboard、TouchScreen、TraceBall。它在系统启动的时候会通过 open_device方法将系统提供的输入设备都增加到这个抽象结构中,并维护一个所有输入设备的文件描述符,如果输入设备是键盘的话还会读取 /system/usr/keylayout/目录下对应键盘设备的映射文件(修改./development/emulator/keymaps/qwerty.kl来改变键值的映射关系),另外getEvent方法是对EventHub中的设备文件描述符使用poll操作等侍驱动层事件的发生,如果发生的事件是键盘事件,则调用Map函数按照映射文件转换成相应的键值并将扫描码和键码返回给KeyInputQueue. frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp 根据事件的类型以及事件值进行判断处理,从而确定这个事件对应的设备状态是否发生了改变并相应的改变对这个设备的描述结构InputDevice。 Windowmanager会创建一个线程(InputDispatcherThread),在这个线程里从事件队列中读取发生的事件 (QueuedEvent ev = mQueue.getEvent()),并根据读取到事件类型的不同分成三类(KEYBOARD、TOUCHSCREEN、TRACKBALL),分别进 行处理,例如键盘事件会调用dispatchKey((KeyEvent)ev.event, 0, 0)以将事件通过Binder发送给具有焦点的窗口应用程序,然后调用 mQueue.recycleEvent(ev)继续等侍键盘事件的发生;如果是触摸屏事件则调用dispatchPointer(ev, (MotionEvent)ev.event, 0, 0),这里会根据事件的种类(UP、DOWN、MOVE、OUT_SIDE等)进行判断并处理,比如Cancel或将事件发送到具有权限的指定的窗口中 去; 移植方案 Android本身并不带触摸屏校准。Android获取到的数据就是驱动上报的原始数据。 方案一 : 移植TSLIB,通过TSLIB产生 pointercal 校准参数文件。 方案二 : 从Android框架层获取OnTouch事件产生 pointercal 校准参数文件 方案一: 数据的校准在驱动中完成。 即把 pointercal 的参数数据通过某种方式(sysfs)传递给驱动程序进行校准。 方案二: 驱动上报原始点,原始点在框架层拦截后进行校验处理。 TSLIB移植过程 修改源码以适应android的文件结构。 设定Android.mk 编译选项,生成库即应用。 etc/ts.conf module_raw input src/ts_config.c #define TS_CONF "/system/etc/ts.conf" src/ts_load_module.c char *plugin_directory="/system/lib/ts/plugins/"; tests/fbutils.c char *defaultfbdevice = "/dev/graphics/fb0"; COPY ts.conf 到 /system/etc/ts.conf init.rc. mkdir /data/etc/pointercal 通过 ts_calibrate 产生pointercal 数据文件。 框架内获取参数文件制作APK 应用,仿效ts_calibrate采点并计算出各参数,产生 pointercal框架内实现触摸屏校准在 InputDevive.java 中 拦截触摸屏原始数据进行pointercal参数校验后再分发驱动内实现触摸屏校准 在init.rc 中添加event,在触摸屏加载后把 pointercal参数输送给驱动。 结果-效果 实现细节: 扩展init - proper_serivce 系统支持的属性权限,对自定义的特殊系统属性进行权限开放。 使用自定义系统属性在 init.rc 中 on property 事件中处理pointercal的读写权限。 使用自定义系统属性 触摸屏校准程序.apk 和 InputDevice.java 中的输入事件的同步。 (在触摸屏校准期间 inputDevice 在输入事件中不能采用算法。 校准程序完成有inputDevice重新启用校准算法) 模拟器中至今无法进入 device.absX/Y != null 的代码, 需要了解以下 inputDevice 被调用的步骤。 三,触摸屏的时间流程: 驱动层:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
static int abs_x[3] = {350, 3900, 5};
-
module_param_array(abs_x, int, NULL, 0);
-
MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
-
-
static int abs_y[3] = {320, 3750, 40};
-
module_param_array(abs_y, int, NULL, 0);
-
MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
-
-
static int abs_p[3] = {0, 150, 4};
-
module_param_array(abs_p, int, NULL, 0);
-
MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
-
-
-
-
- set_bit(EV_ABS, wm->input_dev->evbit);
- set_bit(ABS_X, wm->input_dev->absbit);
- set_bit(ABS_Y, wm->input_dev->absbit);
- set_bit(ABS_PRESSURE, wm->input_dev->absbit);
- input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
- abs_x[2], 0);
- input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
- abs_y[2], 0);
- input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
- abs_p[2], 0);
-
-
-
-
- input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
- input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
- input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
-
-
-
-
-
-
-
-
if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
-
- t = _IOC_NR(cmd) & ABS_MAX;
-
- abs.value = dev->abs[t];
- abs.minimum = dev->absmin[t];
- abs.maximum = dev->absmax[t];
- abs.fuzz = dev->absfuzz[t];
- abs.flat = dev->absflat[t];
/* * Touchscreen absolute values * * These parameters are used to help the input layer discard out of * range readings and reduce jitter etc. * * o min, max:- indicate the min and max values your touch screen returns * o fuzz:- use a higher number to reduce jitter * * The default values correspond to Mainstone II in QVGA mode * * Please read * Documentation/input/input-programming.txt for more details. */static int abs_x[3] = {350, 3900, 5};module_param_array(abs_x, int, NULL, 0);MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");static int abs_y[3] = {320, 3750, 40};module_param_array(abs_y, int, NULL, 0);MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");static int abs_p[3] = {0, 150, 4};module_param_array(abs_p, int, NULL, 0);MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");/** 对设备进行初始化设置*/ set_bit(EV_ABS, wm->input_dev->evbit); set_bit(ABS_X, wm->input_dev->absbit); set_bit(ABS_Y, wm->input_dev->absbit); set_bit(ABS_PRESSURE, wm->input_dev->absbit); input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1], abs_x[2], 0); input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1], abs_y[2], 0); input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1], abs_p[2], 0);/** 事件发生时,提供原始点*/input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff); input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff); input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);/** 提供给驱动外查询input_dev 的接口* struct input_absinfo info;* ioctl(fd, EVIOCGABS(axis), &info)* src file: evDev.c*/if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { t = _IOC_NR(cmd) & ABS_MAX; abs.value = dev->abs[t]; abs.minimum = dev->absmin[t]; abs.maximum = dev->absmax[t]; abs.fuzz = dev->absfuzz[t]; abs.flat = dev->absflat[t];
Android 底层驱动
- EventHub.cpp
-
-
static const char *device_path = "/dev/input";
-
-
openPlatformInput(void)
- scan_dir(device_path);
- open_device(devname);
- fd = open(deviceName, O_RDWR);
-
-
-
-
-
-
bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
- int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
- int32_t* outValue, nsecs_t* outWhen)
-
- JNI 部分: com_android_server_KeyInputQueue.cpp. 提供接口
-
-
static JNINativeMethod gInputMethods[] = {
-
-
{ "readEvent", "(Landroid/view/RawInputEvent;)Z",
-
(void*) android_server_KeyInputQueue_readEvent },
-
{ "getDeviceClasses", "(I)I",
-
(void*) android_server_KeyInputQueue_getDeviceClasses },
-
{ "getDeviceName", "(I)Ljava/lang/String;",
-
(void*) android_server_KeyInputQueue_getDeviceName },
-
{ "getAbsoluteInfo", "(IILcom/android/server/InputDevice$AbsoluteInfo;)Z",
-
(void*) android_server_KeyInputQueue_getAbsoluteInfo },
-
{ "getSwitchState", "(I)I",
-
(void*) android_server_KeyInputQueue_getSwitchState },
-
{ "getSwitchState", "(II)I",
-
(void*) android_server_KeyInputQueue_getSwitchStateDevice },
-
{ "getScancodeState", "(I)I",
-
(void*) android_server_KeyInputQueue_getScancodeState },
-
{ "getScancodeState", "(II)I",
-
(void*) android_server_KeyInputQueue_getScancodeStateDevice },
-
{ "getKeycodeState", "(I)I",
-
(void*) android_server_KeyInputQueue_getKeycodeState },
-
{ "getKeycodeState", "(II)I",
-
(void*) android_server_KeyInputQueue_getKeycodeStateDevice },
-
{ "hasKeys", "([I[Z)Z",
-
(void*) android_server_KeyInputQueue_hasKeys },
- };
EventHub.cppstatic const char *device_path = "/dev/input";openPlatformInput(void) scan_dir(device_path); open_device(devname); fd = open(deviceName, O_RDWR);/** 对外接口,getEvent, * inotify 监控device_path目录, 使用poll机制轮询 inotify 和各个输入设备的可用状态。 解析事件或输入信息,放入各个传出参数中。*/bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType, int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags, int32_t* outValue, nsecs_t* outWhen)JNI 部分: com_android_server_KeyInputQueue.cpp. 提供接口static JNINativeMethod gInputMethods[] = { /* name, signature, funcPtr */ { "readEvent", "(Landroid/view/RawInputEvent;)Z", (void*) android_server_KeyInputQueue_readEvent }, { "getDeviceClasses", "(I)I", (void*) android_server_KeyInputQueue_getDeviceClasses }, { "getDeviceName", "(I)Ljava/lang/String;", (void*) android_server_KeyInputQueue_getDeviceName }, { "getAbsoluteInfo", "(IILcom/android/server/InputDevice$AbsoluteInfo;)Z", (void*) android_server_KeyInputQueue_getAbsoluteInfo }, { "getSwitchState", "(I)I", (void*) android_server_KeyInputQueue_getSwitchState }, { "getSwitchState", "(II)I", (void*) android_server_KeyInputQueue_getSwitchStateDevice }, { "getScancodeState", "(I)I", (void*) android_server_KeyInputQueue_getScancodeState }, { "getScancodeState", "(II)I", (void*) android_server_KeyInputQueue_getScancodeStateDevice }, { "getKeycodeState", "(I)I", (void*) android_server_KeyInputQueue_getKeycodeState }, { "getKeycodeState", "(II)I", (void*) android_server_KeyInputQueue_getKeycodeStateDevice }, { "hasKeys", "([I[Z)Z", (void*) android_server_KeyInputQueue_hasKeys },};
java service 部分: KeyInputQueue.java. 循环查询输入设备信息或目录状态并处理
- Thread mThread = new Thread("InputDeviceReader") {
-
public void run() {
- android.os.Process.setThreadPriority(
- android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
-
-
try {
-
RawInputEvent ev = new RawInputEvent();
-
while (true) {
- InputDevice di;
-
-
-
- readEvent(ev);
-
-
boolean send = false;
-
boolean configChanged = false;
-
- ......
-
-
-
-
-
if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
-
synchronized (mFirst) {
- di = newInputDevice(ev.deviceId);
- mDevices.put(ev.deviceId, di);
-
configChanged = true;
- }
- }
-
-
- InputDevice.AbsoluteInfo absX;
- InputDevice.AbsoluteInfo absY;
- InputDevice.AbsoluteInfo absPressure;
- InputDevice.AbsoluteInfo absSize;
-
if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
-
absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X");
-
absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y");
-
absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure");
-
absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size");
-
} else {
-
absX = null;
-
absY = null;
-
absPressure = null;
-
absSize = null;
- }
-
-
return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
Thread mThread = new Thread("InputDeviceReader") { public void run() { android.os.Process.setThreadPriority( android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY); try { RawInputEvent ev = new RawInputEvent(); while (true) { InputDevice di; // block, doesn't release the monitor readEvent(ev); boolean send = false; boolean configChanged = false;...... //检测到新设备后if (ev.type == RawInputEvent.EV_DEVICE_ADDED) { synchronized (mFirst) { di = newInputDevice(ev.deviceId); mDevices.put(ev.deviceId, di); configChanged = true; } }//对触摸屏设备InputDevice.AbsoluteInfo absX; InputDevice.AbsoluteInfo absY; InputDevice.AbsoluteInfo absPressure; InputDevice.AbsoluteInfo absSize; if ((classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) { absX = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_X, "X"); absY = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_Y, "Y"); absPressure = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_PRESSURE, "Pressure"); absSize = loadAbsoluteInfo(deviceId, RawInputEvent.ABS_TOOL_WIDTH, "Size"); } else { absX = null; absY = null; absPressure = null; absSize = null; } return new InputDevice(deviceId, classes, name, absX, absY, absPressure, absSize);
我们对触摸屏的数据修订是在 InputDevice.java 中基于 absX, absY, absPressure != null 的状态下的,当绝对原始点数据从驱动报上来之后,传递到InputDevice.java 经过我们的校准后再dispatch出去到windowManager -> activity 。 这样就是起到了校准效果。 四,需要注意的补助说明 EventHub 中有使用IOCTL 对触摸屏的EVIOCGABS(axis)进行了采样,取出内容struct
- input_absinfo info;
-
struct input_absinfo {
- __s32 value;
- __s32 minimum;
- __s32 maximum;
- __s32 fuzz;
- __s32 flat;
- __s32 resolution;
- };
-
-
#define EVIOCGABS(abs) _IOR('E', 0x40 + abs, struct input_absinfo) /* get abs value/limits */
input_absinfo info;struct input_absinfo { __s32 value; __s32 minimum; __s32 maximum; __s32 fuzz; __s32 flat; __s32 resolution;};#define EVIOCGABS(abs) _IOR('E', 0x40 + abs, struct input_absinfo) /* get abs value/limits */
取出的是触摸屏的最大值,最小值等,这些值在com_android_server_KeyInputQueue.cpp 中会把这些值传递给InputDevice.cpp中使用,即把报上来的位置通过算法进行计算成绝对坐标值
- scaledX = ((scaledX-device.absX.minValue)
- / device.absX.range) * w;
scaledX = ((scaledX-device.absX.minValue) / device.absX.range) * w;
当然如果出现了X,Y轴相反或者坐标反向等问题都可通过改写这条语句来进行实际操作。
分
I have a resistive touchscreen with mechanical misalignments on my
board. The "5 point calibration method" solves errors caused by these
misalignments and converts Touchscreen controller data to actual
screen coordinates. A complete description of mechanical misalignments
and 5 point calibration method can be found at
http://focus.tij.co.jp/jp/lit/an/slyt277/slyt277.pdf.
5-point calibration method is part of the TSLIB touchscreen calibration commonly used for our QT based devices. We can extend Android's touchscreen Calibration parameters to include the 6 parameters that are part of the /etc/pointercal file ( calculated when we run ts_calibrate on TSLIB). Sample idc file below:
# See output of /etc/pointercal file after running ts_calibrate on normal Linux # # Before running ts_calibrate - # export TSLIB_TSDEVICE=/dev/input/event1 # export TSLIB_CALIBFILE=/etc/pointercal # export TSLIB_CONFFILE=/etc/ts.conf # export TSLIB_FBDEVICE=/dev/fb0 # # Pointercal file format - xscale, xymix, xoffset, yxmix, yscale, yoffset, divider. # Enter here the values after dividing xscale etc. with the divider. #
touch.5pointcalib.xscale = 0.199488 touch.5pointcalib.xymix = -0.000325 touch.5pointcalib.xoffset = -6.898865 touch.5pointcalib.yxmix = -0.000795 touch.5pointcalib.yscale = 0.124000 touch.5pointcalib.yoffset = -8.484802
I patched the Android Input subsystem to include the 5 point calibration method for generating actual screen coordinates. Gingerbread currently uses simple scaling of touchscreen X and Y data to CLCD screen X and Y coordinates. In frameworks/base made the following changes :
diff --git a/include/ui/InputReader.h b/include/ui/InputReader.h index 49351b0..bb3ec5e 100644 --- a/include/ui/InputReader.h +++ b/include/ui/InputReader.h @@ -589,6 +589,20 @@ protected:
// Immutable calibration parameters in parsed form. struct Calibration { + // 5 point calibration algorithm coefficients + bool havexscale; + float xscale; + bool havexymix; + float xymix; + bool havexoffset; + float xoffset; + bool haveyxmix; + float yxmix; + bool haveyscale; + float yscale; + bool haveyoffset; + float yoffset; + // Touch Size enum TouchSizeCalibration { TOUCH_SIZE_CALIBRATION_DEFAULT, diff --git a/libs/ui/InputReader.cpp b/libs/ui/InputReader.cpp index 3197ab2..9345dca 100644 --- a/libs/ui/InputReader.cpp +++ b/libs/ui/InputReader.cpp @@ -1682,6 +1682,20 @@ void TouchInputMapper::parseCalibration() { const InputDeviceCalibration& in = getDevice()->getCalibration(); Calibration& out = mCalibration;
+ out.havexscale = in.tryGetProperty(String8("touch.5pointcalib.xscale"), + out.xscale); + out.havexymix = in.tryGetProperty(String8("touch.5pointcalib.xymix"), + out.xymix); + out.havexoffset = in.tryGetProperty(String8("touch.5pointcalib.xoffset"), + out.xoffset); + out.haveyxmix = in.tryGetProperty(String8("touch.5pointcalib.yxmix"), + out.yxmix); + out.haveyscale = in.tryGetProperty(String8("touch.5pointcalib.yscale"), + out.yscale); + out.haveyoffset = in.tryGetProperty(String8("touch.5pointcalib.yoffset"), + out.yoffset); + + // Touch Size out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT; String8 touchSizeCalibrationString; @@ -2294,10 +2308,47 @@ void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags, uint32_t inIndex = touch->idToIndex[id];
const PointerData& in = touch->pointers[inIndex]; + float x,y;
// X and Y - float x = float(in.x - mLocked.xOrigin) * mLocked.xScale; - float y = float(in.y - mLocked.yOrigin) * mLocked.yScale; + if ( mCalibration.havexscale && mCalibration.havexymix && + mCalibration.havexoffset && mCalibration.haveyxmix && + mCalibration.haveyscale && mCalibration.haveyoffset) { + + x = float(in.x - mLocked.xOrigin) * mLocked.xScale; + y = float(in.y - mLocked.yOrigin) * mLocked.yScale; + LOGI("x_orig = %f, y_orig = %f\n", x, y); + float x_temp = float(in.x - mLocked.xOrigin); + float y_temp = float(in.y - mLocked.yOrigin); + + //float xscale = 0.199488; + //float xymix = -0.000325; + //float xoffset = -6.898865; + //float yxmix = -0.000795; + //float yscale = 0.124000; + //float yoffset = -8.484802; + + // 5 point Calibration coefficients - from tsc.idc + float xscale = mCalibration.xscale; + float xymix = mCalibration.xymix; + float xoffset = mCalibration.xoffset; + float yxmix = mCalibration.yxmix; + float yscale = mCalibration.yscale; + float yoffset = mCalibration.yoffset; + + float x_after_calib = (xscale * x_temp) + (xymix * y_temp) + xoffset; + float y_after_calib = (yxmix * x_temp) + (yscale * y_temp) + yoffset; + + + LOGI("x_after_calib = %f, y_after_calib = %f\n xscale = %f, yoffset = %f\n", x_after_calib, y_after_calib, xscale, yoffset); + x = x_after_calib; + y = y_after_calib; + + } + else { + x = float(in.x - mLocked.xOrigin) * mLocked.xScale; + y = float(in.y - mLocked.yOrigin) * mLocked.yScale; + }
// ToolMajor and ToolMinor float toolMajor, toolMinor;
Touchscreen soft-keyboard inputs were being mis-reported on my WVGA screen and with the patch, keypresses are more accurate.
Regards
|
|