Using Kivy with the official Raspberry Pi Touch Display
The guide below and example code will get you started setting up the Raspberry Pi touch display and getting Kivy working with it. Kivy is an "open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps." This guide assumes you have a fresh install of Raspbian (2015-05-05 release), an Internet connection, and the displaysc is connected and working. It also assumes that you're already familiar with Raspberry Pi enough to get yourself to the command line. You can run this code locally or via SSH. The command line is where you'll start.
-
Update your software. This is required with the 2015-05-05 Raspbian image in order to get touch working. This step will take a few minutes:
pi@raspberrypi ~ $ sudo apt-get update && sudo apt-get -y upgrade
-
Reboot:
pi@raspberrypi ~ $ sudo reboot
-
Back on the command line, check that touch input works by trying it in X11:
pi@raspberrypi ~ $ startx
-
If touch works, exit out of X11 and go back to the command line.
-
The next few steps will install Kivy. These instructions are based on the Kivy User’s Guide. Firstly, open the APT sources list:
pi@raspberrypi ~ $ sudo nano /etc/apt/sources.list
-
At the end of file, add the APT sources for Gstreamer:
deb http://vontaene.de/raspbian-updates/ . main
-
Type
Control+X
to exit nano. Then pressY
andEnter
to save the file. You'll be back on the command line. -
Download and add the GPG key for the Gstreamer sources (If you get an error,
gpg: keyserver receive failed: bad URI
after the first command, just try to run that command again. You should seegpg: imported: 1
):pi@raspberrypi ~ $ gpg --recv-keys 0C667A3E pi@raspberrypi ~ $ gpg -a --export 0C667A3E | sudo apt-key add -
-
Install the dependencies:
pi@raspberrypi ~ $ sudo apt-get update pi@raspberrypi ~ $ sudo apt-get -y install pkg-config libgl1-mesa-dev libgles2-mesa-dev \ python-pygame python-setuptools libgstreamer1.0-dev git-core \ gstreamer1.0-plugins-{bad,base,good,ugly} \ gstreamer1.0-{omx,alsa} python-dev
-
Install pip from source, as the version that is in the Raspbian apt repository is too old. You can ignore any messages about
InsecurePlatformWarning
:pi@raspberrypi ~ $ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py pi@raspberrypi ~ $ sudo python get-pip.py
-
Install Cython, Pygments, and docutils. The Pygments and docutils packages are not actually required for Kivy, but the example code you'll execute uses them. This step will take a few minutes:
pi@raspberrypi ~ $ sudo pip install cython pygments docutils
-
Download Kivy and install it globally (this step will take quite a few minutes):
pi@raspberrypi ~ $ git clone https://github.com/kivy/kivy pi@raspberrypi ~ $ cd kivy pi@raspberrypi ~/kivy $ python setup.py build pi@raspberrypi ~/kivy $ sudo python setup.py install
-
To enable touch, you'll need to make a modification to the Kivy configuration file:
pi@raspberrypi ~/kivy $ nano ~/.kivy/config.ini
-
Go into the
[input]
section, remove the lines that are in there and put in:mouse = mouse mtdev_%(name)s = probesysfs,provider=mtdev hid_%(name)s = probesysfs,provider=hidinput
-
Launch the multi touch pictures demo. Tap, drag, pinch, and rotate should all work like a dream:
pi@raspberrypi ~ $ python ~/kivy/examples/demo/pictures/main.py
-
Type
Control+C
to exit the pictures demo. -
Launch the UI showcase. This shows you all the different UI elements that Kivy makes available to you:
pi@raspberrypi ~ $ python ~/kivy/examples/demo/showcase/main.py
-
If you'd like, explore the other examples in
~/kivy/examples/
. -
To try Kivy and GPIO together, download this repo to your Raspberry Pi if you haven't already:
pi@raspberrypi ~ $ git clone https://github.com/mrichardson23/rpi-kivy-screen.git
-
The example uses BCM GPIO pins 17 as a piezo buzzer, 27 and 10 as LEDs, and 22 as a button (with internal pullups set HIGH, so connect one leg of the button to 22 and the other to ground.)
-
First try to run the example as root (root access is required for the GPIO library):
pi@raspberrypi ~ $ cd rpi-kivy-screen/ pi@raspberrypi ~/rpi-kivy-screen $ sudo python main.py
-
As you'll see, touch doesn't work. To fix this, you need to make the same change to
config.ini
you made before, but to the root account's config file. Exit (Control+C
) and copy over your home directory's Kivy configuration file to overwrite the root account's:pi@raspberrypi ~/rpi-kivy-screen $ sudo cp ~/.kivy/config.ini /root/.kivy/config.ini
-
Run the example again and you'll be able to control the LED and buzzer. You'll also be able to see the state of the physical button!
pi@raspberrypi ~/rpi-kivy-screen $ sudo python main.py
'강좌 > RaspberryPI 활용' 카테고리의 다른 글
라즈베리파이 FrameBuffer 스트리밍 (0) | 2016.01.13 |
---|---|
Raspberry Pi Zero 출시 (0) | 2015.11.26 |
날씨 예보문 가져오기 (0) | 2015.10.21 |
라즈베리파이 공식 7인치 LCD (DSI) 멀티 터치 사용하기 (5) | 2015.09.23 |
유튜브 다운로드 (0) | 2015.09.01 |
라즈베리파이 Telegram - PHP를 이용하여 메시지 보내기 (7) | 2015.08.13 |
라즈베리파이 Telegram BOT 만들기 5부 - Telegram CLI BOT 최종 (27) | 2015.07.22 |
라즈베리파이 Telegram BOT 만들기 4부 - Telegram CLI 데몬 실행 및 서비스 등록 (35) | 2015.07.21 |