Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Tiny6410 移植 tslib 1.4 觸控庫完整實戰記錄

編輯:Linux 系統常見問題解答

環境與工具鏈

  • 宿主機:Ubuntu 11.04
  • 交叉編譯器:arm-linux-gcc 4.3.2
  • 目標板:Tiny6410 (單點電阻式觸控)
  • 目標庫:tslib 1.4

關鍵修改:適配單點觸控原始資料格式

Tiny6410 觸控驅動輸出為單一 32-bit 整數 (jjts) 封裝 X/Y/壓力,非標準 input_event 結構。需修改 plugins/input-raw.c 中的 ts_input_read。

修改前 (標準 Linux input_event)

static int ts_input_read(struct tslib_module_info *inf,
                         struct ts_sample *samp, int nr) {
    struct tslib_input *i = (struct tslib_input*)inf;
    struct tsdev *ts = inf->dev;
    struct input_event ev;
    int ret = nr, total = 0;
    while (total < nr) {
        ret = read(ts->fd, &ev, sizeof(ev));
        if (ret != sizeof(ev)) return -1;
        // 標準事件解析...
    }
    return total;
}

修改後 (Tiny6410 專用格式)

static int ts_input_read(struct tslib_module_info *inf,
                         struct ts_sample *samp, int nr) {
    struct tslib_input *i = (struct tslib_input*)inf;
    struct tsdev *ts = inf->dev;
    int ret = nr, total = 0;
    unsigned long jjts;

    while (total < nr) {
        ret = read(ts->fd, &jjts, sizeof(jjts));
        if (ret == -1) return 0;  // 關鍵:非 continue,避免 Qt 點擊無效
        samp->pressure = (jjts >> 31) & 0x01;
        samp->x = (jjts >> 16) & 0xFFFF;
        samp->y = jjts & 0xFFFF;
        samp++;
        total++;
    }
    return total;
}

關鍵點:ret == -1 時 return 0 而非 continue,否則 Qt 程式雖能移動游標但點擊無回應。

交叉編譯完整流程

# 1. 解壓並進入目錄
tar xf tslib-1.4.tar.gz
cd tslib-1.4

# 2. 安裝建構依賴
sudo apt-get install autoconf automake libtool

# 3. 設定安裝前綴
export PREFIX=/opt/tslib

# 4. 生成 configure 腳本
./autogen.sh

# 5. 建立交叉編譯快取 (欺騙 configure)
echo "ac_cv_func_malloc_0_nonnull=yes" > arm-linux.cache

# 6. 配置 (指定 host、prefix、cache)
CC=arm-linux-gcc ./configure --host=arm-linux --prefix=$PREFIX --cache-file=arm-linux.cache

# 7. 編譯安裝
make
make install

成功後 /opt/tslib 下生成 bin/、etc/、include/、lib/ 四目錄。

執行端配置

1. 修改 /opt/tslib/etc/ts.conf

module_raw input
module pthres pmin=1
module variance delta=30
module dejitter delta=100
module linear

模組說明:

  • input:從 Linux input 子系統讀取原始資料
  • pthres:壓力閾值濾波
  • variance:方差濾波平滑抖動
  • dejitter:去抖動
  • linear:線性校正 (依據 pointercal 校準參數)

2. 部署至目標板

# 宿主機打包
tar czf tslib.tar.gz -C /opt tslib

# 目標板解壓
tar xzf tslib.tar.gz -C /opt

3. 環境變數腳本 tslib.sh

#!/bin/sh
export TSLIB_ROOT=/opt/tslib
export TSLIB_TSDEVICE=/dev/touchscreen-1wire   # 依實際裝置節點調整
export TSLIB_CALIBFILE=$TSLIB_ROOT/etc/pointercal
export TSLIB_CONFFILE=$TSLIB_ROOT/etc/ts.conf
export TSLIB_PLUGINDIR=$TSLIB_ROOT/lib/ts
export TSLIB_CONSOLEDEVICE=/dev/tty
export TSLIB_FBDEVICE=/dev/fb0

4. 校準觸控

# 目標板執行
source tslib.sh
cd /opt/tslib/bin
./ts_calibrate
# 依畫面十字準星點擊 5 點,生成 pointercal

5. 系統整合

# 讓 pointercal 系統可見
ln -s /opt/tslib/etc/pointercal /etc/pointercal

# 可將 source tslib.sh 加入 /etc/profile 或啟動腳本

驗證與測試

# 測試原始讀取
./ts_test
# 測試校準效果
./ts_calibrate
# Qt 程式測試
export QWS_MOUSE_PROTO=Tslib:/dev/touchscreen-1wire
./your_qt_app -qws

常見問題

  • 校準後點擊偏移:確認 ts.conf 中 module_raw 為 input,且裝置節點正確
  • Qt 無法載入 tslib 插件:檢查 TSLIB_PLUGINDIR 指向 lib/ts,且 libts-*.so 存在
  • 多點觸控不支援:tslib 1.x 僅支援單點,多點需升級 tslib 2.x 或使用 libinput
Copyright © Windows教程網 All Rights Reserved