# 树莓派2B+7寸触摸屏折腾记录

Source: https://blog.ferstar.org/posts/raspberry-pi-2b-7-inch-touchscreen-setup/






# 硬件

1.  raspberry pi2B+8G tf卡，系统lubuntu14.04.3
2.  某宝7寸800x480电容式触摸屏，厂商信息如下：

    `0eef:0005 D-WAV Scientific Co., Ltd`

# 过程

## 1. 触屏驱动+显示分辨率适配

<!--more-->

商家提供了预编译的内核，内容如下：

    ├── config.txt
    ├── kernel7.img
    ├── kernel.img
    ├── modules
    │   └── 3.18.6-v7+
    ├── USB_TOUCH_CAP_7.0_RASPBIAN
    └── xinput-calibrator_0.7.5-1_armhf.deb


    其中`USB_TOUCH_CAP_7.0_RASPBIAN`是自动安装脚本，但其实路径指定完全是错的，我修改如下：
```
    #!/bin/sh -e
    #
    # modified by ferstar
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will &quot;exit 0&quot; on success or any other
    # value on error.
    #
    sudo mv /boot/firmware/kernel7.img /boot/firmware/kernel7.img.`uname -r`
    sudo cp kernel7.img /boot/firmware/kernel7.img

    sudo mv /boot/firmware/initrd7.img /boot/firmware/initrd7.img.`uname -r`
    sudo cp kernel.img /boot/firmware/initrd7.img

    sudo cp /boot/firmware/config.txt /boot/firmware/config.txt.`uname -r`
    # 这个是树莓派启动配置文件，里面一个参数比较重要max_usb_current=1，解除USB最大供电电流限制
    sudo cp config.txt /boot/firmware/config.txt

    sudo mv /lib/modules/`uname -r` /lib/modules/`uname -r`.bak
    sudo cp -r  ./modules /lib/
    # 下面的这一步其实没必要，触屏校准xinput-calibrator软件在软件源里有提供，貌似还比这个版本新，不过用法都是一样的
    sudo dpkg -i xinput-calibrator_0.7.5-1_armhf.deb
    reboot
```
    重启后内核版本为`3.18.6-v7+`，以后不能从源里更新官方内核，会花屏，商家并没有提供触屏驱动源码

## 2. 触屏校准

    在设备开终端，运行`xinput-calibrator`，按提示戳四下，然后把类似如下输出字段
```
    Section InputClass
            Identifier      calibration
            MatchProduct    RPI_TOUCH By ZH851
            Option  Calibration 3 790 9 490
            Option  SwapAxes
    EndSection
```

    写入到`/usr/share/X11/xorg.conf.d/99-calibration.conf`中，没有就新建之

## 3. 触屏模拟右键

    编辑`/usr/share/X11/xorg.conf.d/99-calibration.conf`，加入三行：
```
    Option EmulateThirdButton 1
    Option EmulateThirdButtonTimeout 750
    Option EmulateThirdButtonThreshold 30
```

    最终结果像这样：
```
    Section &quot;InputClass&quot;
            Identifier     calibration
            MatchProduct    RPI_TOUCH By ZH851
            Option  Calibration   3 790 9 490
            Option  SwapAxes 0
            Option EmulateThirdButton 1
            Option EmulateThirdButtonTimeout 750
            Option EmulateThirdButtonThreshold 30
    EndSection
```

    保存重启后，长按屏幕==鼠标右键效果，模拟右键完成

## 4. 禁用系统bug报告弹窗

    bug报告本来是好事，但无奈弹的太多了些，多数情况除了影响心情外，并没有什么卵用，所以需要禁用之：

    `sudo vi /etc/default/apport`
```
    # set this to 0 to disable apport, or to 1 to enable it
    # you can temporarily override this with
    # sudo service apport start force_start=1
    enabled=0
```

## 5. 虚拟键盘`matchbox-keyboard`

    起先照这里的说明[http://ozzmaker.com/2014/06/30/virtual-keyboard-for-the-raspberry-pi/](http://ozzmaker.com/2014/06/30/virtual-keyboard-for-the-raspberry-pi/)编译了下，发现键盘布局蛮丑的，后来手欠apt-get，发现原来有编译好的=。=，而且还长的略好看。

    虚拟键盘装上后需要考虑按需弹出的问题，作者帖子里写的适用于自行编译的版本，deb包安装的不适用，需要略做修改：

    `sudo vi /usr/bin/toggle-matchbox-keyboard.sh`
```
    #!/bin/bash
    #This script toggle the virtual keyboard

    PID=`pidof matchbox-keyboard`
    if [ ! -e $PID ]; then
      killall matchbox-keyboard
    else
     matchbox-keyboard extended &amp;
    fi
```

    `sudo chmod +x /usr/bin/toggle-matchbox-keyboard.sh`

    `sudo vi /usr/share/applications/inputmethods/matchbox-keyboard.desktop`
```
    [Desktop Entry]
    Name=Keyboard
    Comment=Virtual Keyboard
    # Exec=matchbox-keyboard 原本是这行，需要指向我们自己的脚本
    Exec=toggle-matchbox-keyboard.sh
    Type=Application
    Icon=matchbox-keyboard.png
    Categories=Panel;Utility;MB
    X-MB-INPUT-MECHANSIM=True
```

    `vi .config/lxpanel/Lubuntu/panels/panel`这个路径也跟作者说的不太一致
```
    Plugin {
        type = launchbar
        Config {
            Button {
                id=/usr/share/applications/inputmethods/matchbox-keyboard.desktop
            }
        }
    }

    Plugin {
        type = volumealsa
    }
    # 我把键盘按钮放在音量键前，并把右下角的logout键去掉了，因为不小心触到会弹出很长的一个对话框，屏幕略小，显示不全
```

    这样，注销后重登陆就会看到右下角键盘图标了，按下弹出，再按关闭，很方便～

## 6. 配置自动登陆

    默认每次登陆都是要输入密码，但又没法弹出虚拟键盘，所以自动登陆很需要

    `sudo vi /etc/lightdm/lightdm.conf`没有就新建，内容

[SeatDefaults]
    # 这里填写用于自动登录的用户名
    autologin-user=ubuntu
    autologin-user-timeout=0
    # Check https://bugs.launchpad.net/lightdm/+bug/854261 before setting a timeout
    user-session=Lubuntu
    greeter-session=lightdm-gtk-greeter
    

