1. 程式人生 > >樹莓派(Linux 系統)觸控式螢幕翻轉顯示以及觸控翻轉

樹莓派(Linux 系統)觸控式螢幕翻轉顯示以及觸控翻轉

問題:未使用的Pi官方螢幕,所以使用HDMI螢幕顯示的時候,顯示旋轉的同時,觸控沒有旋轉
目的:旋轉螢幕顯示的同時,使得觸控也跟隨旋轉
操作總流程:
    旋轉90度操作
翻轉顯示:
1.sudo nano /boot/config.txt
2.在檔案末尾加入
display_rotate=0
display_rotate=1//90°
display_rotate=2//180°
display_rotate=3//270°
ctrl + o 儲存文件 重啟即可。

注:以下操作皆為SSH操作,所以每條指令前都加了DISPLAY=:0,如果本機操作刪掉即可。
1.安裝xinput
sudo apt-get install xinput
2.列出所有輸入裝置資訊 遠端操作時,使用xinput指令需要加入DISPLAY=:0

DISPLAY=:0 xinput --list
3.列出目標裝置屬性
DISPLAY=:0 xinput --list-props 7
4.旋轉觸控的座標軸
1)所以先交換x、y軸
DISPLAY=:0 xinput --set-prop '7' 'Evdev Axes Swap' 1
2)然後反轉y軸
DISPLAY=:0 xinput --set-prop '7' 'Evdev Axis Inversion' 0 1
顯示旋轉修改之後需要重啟。而觸控旋轉不需要重啟。
詳細操作方法:
1.安裝xinput
sudo apt-get install xinput
描述:
xinput - utility to configure and test X input devices  

xinput-用於配置和測試X輸入裝置的實用程式
xinput is a utility to list available input devices,
xinput是一個實用程式,可以列出可用的輸入裝置、
query information about a device and change input device settings.
關於裝置的查詢資訊和更改輸入裝置設定
2.列出所有輸入裝置資訊
xinput --list
如果遠端操作記得在命令前加DISPLAY=:0
得到以下資訊:
[email protected]:~ $ DISPLAY=:0 xinput --list
⎡ Virtual core pointer                          id=2    [master pointer  (3)]

⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Logitech USB Optical Mouse                id=6    [slave  pointer  (2)]
⎜   ↳ WaveShare WaveShare Touchscreen           id=7    [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Logitech USB Keyboard                     id=8    [slave  keyboard (3)]
    ↳ Logitech USB Keyboard                     id=9    [slave  keyboard (3)]


3.列出目標裝置屬性
由於所使用的觸控式螢幕幕是微雪7寸螢幕,由以上資訊可以得到微雪的觸控式螢幕對應的ID為7
所以,接下來看這款輸入裝置的屬性,
[email protected]:~ $ DISPLAY=:0 xinput --list-props 7
Device 'WaveShare WaveShare Touchscreen':
        Device Enabled (115):   1
        Coordinate Transformation Matrix (116): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
        Device Accel Profile (240):     0
        Device Accel Constant Deceleration (241):       1.000000
        Device Accel Adaptive Deceleration (242):       1.000000
        Device Accel Velocity Scaling (243):    10.000000
        Device Product ID (244):        3823, 5
        Device Node (245):      "/dev/input/event3"
        Evdev Axis Inversion (246):     0, 0
        Evdev Axis Calibration (247):   <no items>
        Evdev Axes Swap (248):  0
        Axis Labels (249):      "Abs MT Position X" (267), "Abs MT Position Y" (268), "Abs MT Pressure" (269), "None" (0), "None" (0), "None" (0)
        Button Labels (250):    "Button Unknown" (233), "Button Unknown" (233), "Button Unknown" (233), "Button Wheel Up" (121), "Button Wheel Down" (122)
        Evdev Scrolling Distance (251): 0, 0, 0
        Evdev Middle Button Emulation (252):    0
        Evdev Middle Button Timeout (253):      50
        Evdev Third Button Emulation (254):     0
        Evdev Third Button Emulation Timeout (255):     1000
        Evdev Third Button Emulation Button (256):      3
        Evdev Third Button Emulation Threshold (257):   20
        Evdev Wheel Emulation (258):    0
        Evdev Wheel Emulation Axes (259):       0, 0, 4, 5
        Evdev Wheel Emulation Inertia (260):    10
        Evdev Wheel Emulation Timeout (261):    200
        Evdev Wheel Emulation Button (262):     4
        Evdev Drag Lock Buttons (263):  0
這塊螢幕正常顯示和觸控的資訊如上。
如果執行到這一步,發現並沒有以上的Evdev等屬性項,請跳轉第2種方法。
現在需要達到的目的是在螢幕顯示反轉的同時,使得觸控也隨顯示翻轉。
螢幕顯示為翻轉90度。/boot/config.txt設定為display_rotate=1
上述資訊中Evdev Axis Inversion 項是每條軸的旋轉設定項,後面第一個引數是x,第二個引數是y.
Evdev Axes Swap項對應的是兩條軸的交換。
0為不翻轉,1為翻轉 
例如。x軸原本是朝向右的,當把Evdev Axis Inversion的第一個引數
設定為1,即x軸朝向左。
4.旋轉觸控的座標軸
現在目的是要觸控旋轉90度,從座標軸理解:
即目的x軸正向為初始y軸的反向。目的y軸的正方向為初始x軸的正向。
1)所以先交換x、y軸
DISPLAY=:0 xinput --set-prop '7' 'Evdev Axes Swap' 1
2)然後反轉y軸
DISPLAY=:0 xinput --set-prop '7' 'Evdev Axis Inversion' 0 1
這樣即可完成觸控旋轉90度。若要旋轉其他角度,推理一下即可。
顯示旋轉修改之後需要重啟。而觸控旋轉不需要重啟。




第二種方法
如果查詢到的資訊如下
DISPLAY=:0 xinput --list-props 6
Device 'WaveShare WaveShare Touchscreen':
        Device Enabled (114):   1
        Coordinate Transformation Matrix (115): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
        libinput Calibration Matrix (246):      0.000000, 1.000000, 0.000000, -1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000
        libinput Calibration Matrix Default (247):      1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
        libinput Send Events Modes Available (248):     1, 0
        libinput Send Events Mode Enabled (249):        0, 0
        libinput Send Events Mode Enabled Default (250):        0, 0
        Device Node (251):      "/dev/input/event0"
        Device Product ID (252):        3823, 5

可以看到該驅動方式採用的是libinput,
檢視/usr/share/X11/xorg.conf.d/目錄下是否有40-libinput.conf這個檔案。
無 則需要安裝 
sudo apt-get install xserver-xorg-input-libinput
下一步安裝完成後ls一下,就可以看到在/usr/share/X11/xorg.conf.d/目錄下存在該檔案,
下一步複製該檔案到/etc/X11/xorg.conf.d/目錄下。
一開始xorg.conf.d這個目錄在/etc/X11可能沒有,需要自己建立。
sudo mkdir xorg.conf.d
下一步複製到xorg.conf.d 目錄下即可。
sudo cp /usr/share/X11/xorg.conf.d/40-libinput.conf /etc/X11/xorg.conf.d/
下一步進入/etc/X11/xorg.conf.d/目錄下修改40-libinput.conf 檔案
cd /etc/X11/xorg.conf.d/
sudo nano 40-libinput.conf
找到touchscreen section
Section "InputClass"
        Identifier "libinput touchscreen catchall"
        MatchIsTouchscreen "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
EndSection
新增一行  Option "CalibrationMatrix" "0 1 0 -1 0 1 0 0 1
結果為
Section "InputClass"
        Identifier "libinput touchscreen catchall"
        Option "CalibrationMatrix" "0 1 0 -1 0 1 0 0 1
        MatchIsTouchscreen "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"

EndSection

然後重啟生效

這樣的修改也是同樣修改為翻轉90度,如果需要修改為其他角度,請參考libinput的演算法
https://wayland.freedesktop.org/libinput/doc/latest/absolute_axes.html

第三種方法是微雪官方給的建議,修改input子系統的程式碼,然後重新編譯核心,即可完成
詳細操作沒有給出,給了一個操作文件,在此也寫出來,希望有操作成功的可以分享下。