Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable HDMI on Raspberry Pi?

So I can use "tvservice -o" to turn off the HDMI on the raspberry Pi, and "tvservice -p" to turn it back on. After turning it back on I apparently need to do "fbset -depth 8 && fbset -depth 16" to re-enable the frame buffer and then force an X11 redraw.

My question is, how do I do this in C? I have an X11 application and I can manage the X11 redraw no problem, but how do I disable/re-enable HDMI in C, and how do I re-enable the frame buffer after re-enabling HDMI?

To give background, I have a headless application running as a sort of media server, controlled by an Android app. Currently I am permanently disabling the turning off of HDMI after a timeout. However to save energy I would like to disable the HDMI when the app is not in use, and then turn it back on and display the RPi application on screen using libcec to determine when the TV is using the RPi's HDMI connection to turn HDMI on and off.

like image 417
AlastairG Avatar asked Mar 30 '15 07:03

AlastairG


People also ask

How do I disable my HDMI port?

If you just want to disable it permanent your answer is in the computer BIOS. You have to access your computers BIOS and set the HDMI Device state to Disabled of Off. Cheers!

How do I enable a disabled HDMI port?

4. Right-click the "Volume" icon on the Windows taskbar, select "Sounds" and choose the "Playback" tab. Click the "Digital Output Device (HDMI)" option and click "Apply" to turn on the audio and video functions for the HDMI port.

How do I force my Raspberry Pi to HDMI?

To force HDMI enable hdmi_force_hotplug=1 in /boot/config. txt or use sudo raspi-config to set a fixed HDMI resolution.


3 Answers

If you want to go for pure C, look at the source code for tvservice, which is a C program. It can be found at github. It appears to be using the vc_tv_hdmi_power_on_preferred(); function defined in #include "interface/vmcs_host/vc_tvservice.h"

If you decide to call the tvservice program like in @moffeltje's answer, you could use the execl() it's a little safer - you have to give full path to the binary. (With execlp you can also have control over environment variables):

pid_t pid;


pid = fork();
if (0 == pid) {
    execl("/opt/vc/bin/tvservice", "-p", NULL);
}
if (-1 == pid) {
    // Handle error here, close program?
}
like image 117
Prof. Falken Avatar answered Oct 16 '22 17:10

Prof. Falken


You can use the system() command to use those commands you described.

int main(){

   //some code before disable hdmi
   system("tvservice -o");

   //do somethings when HDMI is disabled

   //turn HDMI back on
   system("tvservice -p");
   system("fbset -depth 8 && fbset -depth 16");

   return 0;

}
like image 42
moffeltje Avatar answered Oct 16 '22 16:10

moffeltje


From https://gist.github.com/AGWA/9874925 I found the usefulness of chvt. So I suggest calling the shell commands

hdmioff() { tvservice -o; }
hdmion() { tvservice -p; sudo chvt 1; sudo chvt 7; }

with system().

like image 32
Frank Breitling Avatar answered Oct 16 '22 15:10

Frank Breitling