Tested on:Raspberry Pi B+ and 2; Kivy 0.7; Official Touchscreen
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:RPi B+ or 2, SD-card and power source
Host computer with linux
official RPi touchscreen
3-color-LED and jumper cables
0

Kivy on a Raspberry Pi: Control light color with a touch interface

Say hello to Kivy, a gorgeous python framework for touch applications and a natural fit for the Raspberry Pi. Kivy allows setting up polished touch interfaces in no time!

Kivy Installation
Download the image and burn it onto a SD card. Alternatively, follow the manual installation guide (which is necessary for running Kivy on a Raspberry Pi 3).
The username is sysop instead of pi, which is important for logging in over ssh. The corresponding password is posys. Determine the ip address for example with the Angry IP Scanner.

Update: Fixing the Keyerror
Kivy 1.9.1 has a minor bug: it crashes sometimes giving a keyerror. Here is a patch for fixing that.

Connectivity
Follow the manufacturers guide for setting up the touchscreen.
Connect the 3-color-LED: the red pin needs to be connected to pin 11, the green pin to pin 12, the blue pin to pin 13 and ground to pin 6 of the Raspberry Pi in BOARD numbering.

Run the script
After connecting everything, download and run a Kivy sample script with

On the official touchscreen, the interface will look like this:

Kivy Touchscreen Interface

Kivy Touchscreen Interface

Code explained
Luckily, Kivy comes with an own language for layouting touch interfaces, the KV Language. The layout could be seperated into .kv files or integrated in a python file by using the builder. This is the complete KV language layout for the app in this tutorial:

This says: make a vertical box and add three modified sliders. The sliders will have a variable mycolor with their specific color. If the slider position changes, the built-in function on_value is called, which then calls the custom function set_color with the new slider position.

Creating custom touch interfaces involves subclassing widgets from the Kivy framework for enhancing functionality. The subclassed widgets are then integrated in the interface with the KV language as shown above.
Subclass the kivy slider and give it defaults for min, max and current value:

A custom function for updating the color with regard to the slider position follows (value is the slider position and comes from on_value):

The LED color is changed with software-Pulse-Width-Modulation. They are setup for software-PWM with

This creates an sPWM of 100 values and starts with 50, like the sliders.

The complete script
Click on Full Script to show all of the code.

Sample Video

A semitransparent LED instead of the transparent one used here would give mixed colors…

Some good sources
Kivy Introduction
Kivy Blog
Google Groups
Kivy Layouts Documentation

Links
Kivy Download: http://kivypie.mitako.eu/kivy-download.html
Kivy manuall installation: https://github.com/mrichardson23/rpi-kivy-screen/blob/master/README.md
Remote access with SSH: http://www.knight-of-pi.org/remote-control-your-raspberry-pi/
Angry IP Scanner usage: http://www.knight-of-pi.org/sniff-a-raspberry-angry-ip-scanner
Official touchscreen assembly guide: https://thepihut.com/blogs/raspberry-pi-tutorials/45295044-raspberry-pi-7-touch-screen-assembly-guide
Kivy layouting with .kv: https://kivy.org/docs/guide/lang.html
Software PWM tutorial: http://www.knight-of-pi.orgsimple-dc-motor-board-for-the-raspberry-pi-with-ic-l293-and-software-pulse-width-modulation/
Opensourceforu on Kivy: http://opensourceforu.efytimes.com/2016/02/developing-python-based-android-apps-using-kivy/
Karanbalkar Kivy Blog: http://karanbalkar.com/tag/kivy/
Kivy on Google Groups: https://groups.google.com/forum/embed/?place=forum/kivy-users#!forum/kivy-users
Kivy Layouts Documentation: https://kivy.org/docs/gettingstarted/layouts.html
Official touchscreen assembly guide: https://thepihut.com/blogs/raspberry-pi-tutorials/45295044-raspberry-pi-7-touch-screen-assembly-guide
Tutorial: Burn a disk image to a SD card: http://www.knight-of-pi.org/installation/
Patch: Kivy Keyerror: http://www.knight-of-pi.org/kivy-on-the-raspberry-pi-bugfix-prevent-keyerror-y-crash/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

Comment

Time limit is exhausted. Please reload CAPTCHA.