3

I've edited the config.txt file and added display_rotate = 1 to achieve portrait display on a small 7" screen. That works great for the smaller screen but when I plug my Raspberry Pi into the TV using HDMI I need the screen to rotate back 90 degrees to landscape display.

Is there a way to detect if HDMI is connected/not connected and only then rotate the display?

1

It is certainly possible to detect. An example is my script for /etc/rc.local.

The question is whether you can do anything useful with this, as the code to rotate the framebuffer runs early in the boot process, and you can't change the rotation. When I want to rotate my screen, I change config.txt then reboot. It isn't pretty but it only adds 30 seconds to the boot time.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# If running headless (i.e. default RCA video)
if /opt/vc/bin/tvservice -s | grep 'NTSC\|PAL'; then
  echo $(hostname) is running Headless
  /opt/vc/bin/tvservice -s
else
  echo $(hostname) is running hdmi
fi
  • Thanks, that's made understanding what I'm trying to achieve a lot clearer. I've rotated the display using config.txt but I need to have the display revert to landscape when HDMI is detected automatically. Is there no way to update this after boot via a script? – Martin Harvey Nov 28 '15 at 10:42
  • You could try the suggestion in raspberrypi.stackexchange.com/a/37977/8697 but I did not find the results met my needs. Otherwise you could detect the situation, change config (I have 2 config.txt files which I swap) and reboot. – Milliways Nov 28 '15 at 11:45
1

IIRC the "HDMI detect" signal is wired into one of the GPIO pins on the Broadcom SoC core at the heart of the Pi - so it ought to be possible to detect the state of it, though possibly not via the wiringPi library - the tvservice might give you some useful information in a shell script, with, I think, the -s status or -m monitor HDMI arguments.

From an electronics point of view, and looking at page 2 of the Rev2.1 schematic diagram I have traced the "HDMI_HPD_P" connection (pin 19 - Hot plug detect on the HDMI connector) to pin M6 of the BCM2835 which is labelled GPIO46 (and in terms of GPIO numbering its neighbours are GPIO45 being "PWM1_OUT" and GPIO47, "SD_CARD_DET") so I feel you may be able to detect the pin state with some low-level 'C' code running as the super user but it would be much safer to try and find some existing library that does this in a way that minimises the risks of potentially system damaging side effects. Eg. attempting to access that pin might actually work on a group of say 8 pins, and if some of those are involved in reading and writing from/to the SD card that could potentially mess up data on card.

  • I'm certainly looking for a software solution to this,so a script or library that can be run at boot would do me, if such a thing exists? Not really looking for anything too complicated or dangerous in terms of causing any damage. – Martin Harvey Nov 27 '15 at 18:12
  • The monitoring-option is tvservice -M and it indeed outputs changes of the hotplug status. Such as "HDMI cable is unplugged" and "HDMI is attached". – Ghanima Nov 27 '15 at 18:30
  • How and where would I implement or use tvservice -M? I'm not really up on C or Python and could really do with something a bit more 'cut and paste' that I could use. – Martin Harvey Nov 27 '15 at 18:42
  • If I understand the task correctly -M is not necessary. Just call once with -s for the status update whenever you like. Parse the result and check whether HDMI is connected or not. – Ghanima Nov 27 '15 at 19:10
1

Here it is stated that:

The Pi uses the HDMI hotplug signal to detect whether a HDMI is attached. If it is not detected then it falls back to composite.

which in turn suggests that there is some kind of software running to detect the change of the hotplug status. But what does the trick?

@SlySven's suggestion to use /opt/vc/bin/tvservice -s hits the nail head.

Example output of Pi with connected HDMI:

state 0x12001a [HDMI CEA (16) RGB lim 16:9], 1920x1080 @ 60.00Hz, progressive

and with cable unplugged:

state 0x120019 [HDMI CEA (16) RGB lim 16:9], 1920x1080 @ 60.00Hz, progressive

It's time to have a look at tvservice's source (or more precisely userland/interface/vmcs_host/vc_hdmi.h). They conveniently provide us with the enum VC_HDMI_NOTIFY_T which essentially tells us all we need to know:

typedef enum {

VC_HDMI_UNPLUGGED = (1 << 0), /*HDMI cable is detached */

VC_HDMI_ATTACHED = (1 << 1), /*HDMI cable is attached but not powered on */

[...]

} VC_HDMI_NOTIFY_T;

So all we need to do is to check for the least significant bit - if it is set then the cable is unplugged. So it would seem that running tvservice -s and parsing the status properly should enable to detect whether HDMI is connected or not.

  • So how and where would I use this information? – Martin Harvey Nov 27 '15 at 19:56
  • @Milliways discusses that, +1 there by the way. – Ghanima Nov 29 '15 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.