I have taken some time off from writing about my Raspberry Pi prototyping but I have been busy prototyping and making a GUI program for the Raspberry Pi. I started with the wonderful Touch Pi design from the wonderful people at Adafruit that adds a Resistive Touchscreen to the Pi! I made many changes to this system which I will describe in future posts. But essentially, this is what the prototype looks like:
So for my particular setup, where I wanted to use a large HDMI screen to write my program and then use the Touch Screen to test it, I needed to figure out a bunch of things that I will explain in this post. Please note that this is a fairly technical post that is aimed to help others who are running into similar challenges. I will have more application focused posts in the future (once I actually figure out the rest of my issues!).
For my implementation, I am using a Raspbian Jessie installation. For implementing my particular GUI (which I will discuss in a future post) I used pygame to interact both with the touch screen and the GPIO pins.
Detecting HDMI vs. Touchscreen
In any case, the first issue I had to figure out was how to automatically detect if the Pi was connected to the HDMI screen and then run the GUI from there. If an HDMI cable was not detected then I wanted the GUI to run on the touchscreen. One thing to note is to the best of my knowledge, you can’t run the GUI simultaneously on the touch screen and the HDMI TV because the system can’t handle input from two sources.
The solution I came up with involves modifying (or creating if it doesn’t exist) the rc.local file in the /etc/ folder:
cd etc sudo nano rc.local
Then add these lines to the script:
if (/usr/bin/tvservice -s | /bin/egrep 'HDMI|DVI); then sudo cp /home/pi.displays/HDMI/99-fbdev.conf /etc/X11/xorg.conf.d con2fbmap 1 0 echo "rc.local HDMI selected" else sudo cp /home/pi/displays/TFT/99-fbdev.conf /etc/X11/xorg.conf.d con2fbmap 1 1 echo "rc.local TFT selected" fi exit 0
Thanks to these forum discussions!
Note: This might cause a problem if you plan to use a Raspberry Pi Zero. In case your RPi Zero cannot use the display, comment out the added lines above and also copy the 99-fbdev.conf file form the HDMI folder to the X11 folder and everything should work on startup.
Starting programs on GUI startup
The next interesting challenge was to run my custom python code (or the shell script running it) on startup. I particularly wanted the program to run once the GUI is loaded. If you want your script to run at the beginning (with no graphical support), please see my previous post here.
There are many forum posts about where and how to change files to make this happen but it is important that you locate and change the right file for your particular setup. In Raspbian Jessie, you want to change the autostart file in the .config/lxsession/LXDE-pi directory:
cd .config/lxsession/LXDE-pi sudo nano autostart
In this file you have to add your particular program path to the end of the list. So it will look something like this:
@lxpanel --profile LXDE-pi @pcmanfm --desktop --profile LXDE-pi @xscreensaver -no-spalsh @lxterminal -e /home/pi/Foad/myProgramLaunch.sh
The last command specifies that I want my script to be called from a terminal. Depending on your needs you might or might not need to call from a terminal.
Making Desktop Shortcut
A final bonus tip (thanks to the information here)! If you would like to make a desktop shortcut with an icon that you can double click to start your program, you need to create a file in the Desktop directory:
sudo nano mydesktop.desktop
In this file, you specify your icon, path, …:
[Desktop Entry] Name=reMixer Comment=This is the reMixer program Icon=/home/pi/Foad/myProgramLaunch.sh Type=Application Encoding=UTF-8 Terminal=false Categories=None;
You have to make sure that your script has execution privileges (set them with chmod) and you are set!