Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Swapping between PiTFT and HDMI display.

Fri Feb 20, 2015 2:21 pm

Hi, I've got my PiTFT display working on my Pi and it's great (using the capacitive image: https://learn.adafruit.com/adafruit-2-8 ... h/overview).

I have two Raspberry Pi's, one connected to a HDMI monitor and the other working with this TFT display. What I would desire is to be able to swap my SD card, containing the image, between them and have it auto-detect which display to output to as normal (so I wont have to use 2 SD cards if I want to access Raspbian on the TFT or HDMI display).

Currently, if I swap my PiTFT image to the HDMI Pi the resolution is awkward and I'm quite sure it processes as if it's connected to the PiTFT still.

Is it possible, (and easy) to get this auto-detection? Or is the image permanently fixed to use the TFT display? I found a topic similar to what I want: http://www.raspberrypi.org/forums/viewt ... 44&t=91764

but it's rather convoluted, long, and quite technical for a beginner (and I'm not even sure if the solution came about in the end).
Last edited by Silversonic on Sat Feb 21, 2015 8:43 pm, edited 1 time in total.

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Swapping between PiTFT and HDMI display.

Fri Feb 20, 2015 4:12 pm

Hi i made a script that swaps the config file that has the monitor resolution in this case 1080p and 720p. So if my pi connects to a tv that has 720p the script will detect the resolution is not 1080p and reboot the pi to 720p. So in your case you can mod the script to detect the pitft display if it doesn't have the resolution of hdmi and so on.
http://www.raspberrypi.org/forums/viewt ... 72#p674072 :mrgreen:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 12:30 am

expandables wrote:Hi i made a script that swaps the config file that has the monitor resolution in this case 1080p and 720p. So if my pi connects to a tv that has 720p the script will detect the resolution is not 1080p and reboot the pi to 720p. So in your case you can mod the script to detect the pitft display if it doesn't have the resolution of hdmi and so on.
http://www.raspberrypi.org/forums/viewt ... 72#p674072 :mrgreen:
Thanks for the reply.

On the touchscreen guide (https://learn.adafruit.com/adafruit-2-8 ... re-install) it describes how the image automatically makes the command line / startX export to the PiTFT display on start-up, by installing some kernel modules to /etc/modules and modifying ~./boot/cmdline.txt.

I guess what needs to be done is to remove this automation, and instead get it to detect the resolution and, if it's not 1920x1080, then install those kernel modules and modifty /cmdline.txt for the PiTFT display. All of this would have to be done before the kernel is booted and cmdline.txt is read (is it even possible to do that, because the kernel is the first thing the system boots?)

Edit:

Code: Select all

#!/bin/bash
	
    tvservice -m CEA > tvservice2.sh

    tvservice=$(cat /home/pi/tvservice2.sh | awk '/(prefer)/ {print $4}' | tail -c 10)

    if [ $tvservice != "" ]; then
   sudo sed -i '/snd-bcm2835/d' /etc/modules
   sudo sed -i '/spi-bcm2708/d' /etc/modules
   sudo sed -i '/i2c-bcm2708/d' /etc/modules
   sudo sed -i '/fbtft_device/d' /etc/modules
   sudo sed -i 'fbcon=map:10 fbcon=font:VGA8x8' /boot/cmdline.txt
   sudo reboot
    fi
I'm very new to bash, this is ONLY TEMPORARY but this bashscript in /etc/rc.local comes close to what I need. If there is a "preferred" hdmi output then the PiTFT modules are removed (along with commandline interface use) and the system reboots.

I'll work on it tomorrow and post the solution. I just need a reverse (if no preferred is detected then the PiTFT modules are loaded), and to stop infinite loops of reboots occurring under certain conditions (this happened to me as I was testing, fairly certain I came to ruining my image).

DirkS
Posts: 10759
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 2:33 pm

I have been working with another user on something like this (http://www.raspberrypi.org/forums/viewt ... 44&t=91764)
In the end I came to the conclusion that it's much simpler to change display settings instead of trying to manipulate modules.
AFAICT this should also work on the new 3.18 kernel with device trees.
I'm not sure if that will be the case using your method.

The script checks the current output using 'tvservice -s'.
It changes the configuration file for X if necessary.
It also changes the the mapping between console and framebuffer (using con2fbmap)

The script (with some logging included)

Code: Select all

#!/bin/bash

echo "is it HDMI or DMT?" > /home/pi/tft.log
/usr/bin/tvservice -s >> /home/pi/tft.log
if (/usr/bin/tvservice -s | /bin/egrep 'HDMI|DVI') ; then
    echo "HDMI / DVI screen" >> /home/pi/tft.log
    if [ -f  /usr/share/X11/xorg.conf.d/99-fbdev.conf ]; then
      rm /usr/share/X11/xorg.conf.d/99-fbdev.conf
    fi
    con2fbmap 1 0
else
    echo "switch on PiTFT" >> /home/pi/tft.log
    cp /home/pi/scripts/99-fbdev.conf /usr/share/X11/xorg.conf.d/
    con2fbmap 1 1
fi
Contents of '99-fbdev.conf:

Code: Select all

section "Device"  
  Identifier "pitft"
  Driver "fbdev"
  Option "fbdev" "/dev/fb1"
EndSection
You may have to change the paths, depending on where you save the files.
The script needs root permissions, which it will have if you call it from rc.local.

Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 5:48 pm

Using expandable's advice the following bash script, does the trick;

Code: Select all

#!/bin/bash
   
    tvservice -m CEA > /home/pi/tvservice/tvservice2.sh

    tvservice=$(cat /home/pi/tvservice/tvservice2.sh | awk '/(prefer)/ {print $4}' | tail -c 10)
    state=$(cat /home/pi/tvservice/state.txt)

    if [[ $tvservice != "" ]] && [[ $state != "1" ]] 
     then
   sudo sed -i '/spi-bcm2708/d' /etc/modules
   sudo sed -i '/i2c-bcm2708/d' /etc/modules
   sudo sed -i '/fbtft_device/d' /etc/modules
   sudo sed -i 's/fbcon=map:10 fbcon=font:VGA8x8//' /boot/cmdline.txt
   sudo sed -i 's_export FRAMEBUFFER=/dev/fb1__' /home/pi/.profile
   sudo sed -i 's/0/1/' /home/pi/tvservice/state.txt
   sudo reboot
    fi

    if [[ $tvservice = "" ]] && [[ $state != "0" ]] 
     then
   sudo sed -i '/snd-bcm2835/ a\fbtft_device' /etc/modules
   sudo sed -i '/snd-bcm2835/ a\i2c-bcm2708' /etc/modules
   sudo sed -i '/snd-bcm2835/ a\spi-bcm2708' /etc/modules
   sudo sed -i '/console=tty1/ s/$/fbcon=map:10 fbcon=font:VGA8x8/' /boot/cmdline.txt
   sudo sed -i '/#umask 022/ a\ \nexport FRAMEBUFFER=/dev/fb1' /home/pi/.profile
   sudo sed -i 's/1/0/' /home/pi/tvservice/state.txt
   sudo reboot
    fi


exit 0

This is kept in the file /home/pi/tvservice/tvservice.sh, and run at boot time using /etc/rc.local.

Firstly the $tvservice variable contains whether there is a preferred HDMI output (if there isn't, it is empty). The $state variable accesses a file state.txt, which contains information (either a 1 or a 0) on whether the PiTFT kernel modules have already been unloaded or not. The if statements then make the desired changes if necessary and reboots.

As I mentioned, I'm new to bash. This could be ridiculed with errors. It also becomes obsolete should some sort of version update change the module names. As you mention Dirk, it might not work with device trees in 3.18. I'm not sure what those are but I'm guessing any sort of kernel update will ruin this script, which might be what you were explaining.


I don't even know whether unloading the kernel modules is the extent of what needs to be done, but so far it looks like everything works as I need it to.

Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 7:48 pm

DirkS wrote:I have been working with another user on something like this (http://www.raspberrypi.org/forums/viewt ... 44&t=91764)
In the end I came to the conclusion that it's much simpler to change display settings instead of trying to manipulate modules.
AFAICT this should also work on the new 3.18 kernel with device trees.
I'm not sure if that will be the case using your method.

The script checks the current output using 'tvservice -s'.
It changes the configuration file for X if necessary.
It also changes the the mapping between console and framebuffer (using con2fbmap)

The script (with some logging included)

Code: Select all

#!/bin/bash

echo "is it HDMI or DMT?" > /home/pi/tft.log
/usr/bin/tvservice -s >> /home/pi/tft.log
if (/usr/bin/tvservice -s | /bin/egrep 'HDMI|DVI') ; then
    echo "HDMI / DVI screen" >> /home/pi/tft.log
    if [ -f  /usr/share/X11/xorg.conf.d/99-fbdev.conf ]; then
      rm /usr/share/X11/xorg.conf.d/99-fbdev.conf
    fi
    con2fbmap 1 0
else
    echo "switch on PiTFT" >> /home/pi/tft.log
    cp /home/pi/scripts/99-fbdev.conf /usr/share/X11/xorg.conf.d/
    con2fbmap 1 1
fi
Contents of '99-fbdev.conf:

Code: Select all

section "Device"  
  Identifier "pitft"
  Driver "fbdev"
  Option "fbdev" "/dev/fb1"
EndSection
You may have to change the paths, depending on where you save the files.
The script needs root permissions, which it will have if you call it from rc.local.
I'm trying to modify my method to avoid removing kernel modules and instead change the display like you've done. The issue I'm having is when you remove /usr/share/X11/xorg.conf.d/99-fbdev.conf, when loading startx it seems to look for a file in /usr/share/X11/xorg.conf.d/ which no longer exists.

This image shows the issue when loading startx (where it gets stuck), and my script: http://i.imgur.com/IGlWUN5.png
Last edited by Silversonic on Sun Feb 22, 2015 9:09 pm, edited 1 time in total.

DirkS
Posts: 10759
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 8:35 pm

The issue I'm having is when you remove /usr/share/X11/xorg.conf.d/99-fbdev.conf, when loading startx it seems like look for a file in /usr/share/X11/xorg.conf.d/ which no longer exists.
That only means that it's looking for files there. If nothing is there it will use the defaults.
But you can always change it from removing the file to replacing it with an alternative version, something like

Code: Select all

section "Device"  
  Identifier "hdmi"
  Driver "fbdev"
  Option "fbdev" "/dev/fb0"
EndSection
BTW: personally I think your detection method is overly complicated. The method I use (checking for the text HDMI or DVI) works.
I also see that you're changing cmdline.txt. You *do* realise that you have to reboot to activate these settings?
And 'con2fbmap 1 0' actually does the same as 'the fbcon=map:10' in config.txt, albeit at a later stadium.

Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Re: Swapping between PiTFT and HDMI display.

Sun Feb 22, 2015 11:47 pm

DirkS wrote:
The issue I'm having is when you remove /usr/share/X11/xorg.conf.d/99-fbdev.conf, when loading startx it seems like look for a file in /usr/share/X11/xorg.conf.d/ which no longer exists.
That only means that it's looking for files there. If nothing is there it will use the defaults.
Aye but at that stage it was showing a black screen on my HDMI, so I figured something went wrong and it doesn't seem to be using any default settings. I'll do what you suggest.
DirkS wrote:
BTW: personally I think your detection method is overly complicated. The method I use (checking for the text HDMI or DVI) works.
I also see that you're changing cmdline.txt. You *do* realise that you have to reboot to activate these settings?
And 'con2fbmap 1 0' actually does the same as 'the fbcon=map:10' in config.txt, albeit at a later stadium.
I'm alright with rebooting, I'd written it my code (maybe not in my most recent example, but in the one two prior)

Edit: I am in the process of changing this to your method, but I'm trying to understand it all first. I am new to this.
Last edited by Silversonic on Mon Feb 23, 2015 12:33 am, edited 1 time in total.

Silversonic
Posts: 34
Joined: Wed Aug 27, 2014 8:42 pm

Re: Swapping between PiTFT and HDMI display.

Mon Feb 23, 2015 12:33 am

DirkS wrote:
And 'con2fbmap 1 0' actually does the same as 'the fbcon=map:10' in config.txt, albeit at a later stadium.
Last thing really. Everything would be perfect except con2fbmap 1 0 stops fbcon=map:10, but it does not stop the fbcon=font:VGA8x8 in /boot/cmdline.txt. Thus when the console swaps over the resolution is very small (resolution of the PiTFT screen). How exactly would I stop fbcon=font:VGA8x8 without need of a reboot?

Edit: sudo setfont Uni2-Fixed18.psf.gz works pretty okay, but I'd optimally want it to use whatever the original default font of raspbian command line is (which I can't seem to find).

Return to “Beginners”