Programmatically turn screen off


18 posts
by rob_pi » Tue Jun 05, 2012 5:50 pm
Does anyone know how I might be able to programmatically switch the monitor off? It's connected by HDMI. I'd be happy if I could just disable output from the HDMI port, and then the monitor would switch itself off, but I can't manage even this!

I've tried
Code: Select all
$ xset dpms force off
but this just blanks the screen - the LCD monitor remains on. I think this is because the monitor is connected over HDMI, and so the EnergyStar stuff doesn't work.

I've also tried code that's supposed to disable the driver in the kernel:
Code: Select all
# echo "bcm2708_fb" > /sys/bus/platform/drivers/bcm2708_fb/unbind
This seemed to work (/dev/fb0 disappeared), but the display (with X running) carried on regardless.

Anybody got any other ideas? I think HDMI-CEC would be the gold-plated solution, and I know people are working on libcec, but I've no idea when this will be released.

The project is an always-on device, but to save power I want to power down the monitor when it's not needed.
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by dom » Tue Jun 05, 2012 6:10 pm
try
/opt/vc/bin/tvservice -o
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3999
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by rob_pi » Tue Jun 05, 2012 6:23 pm
That looks like just the ticket! but..
Code: Select all
root@raspberrypi:~# /opt/vc/bin/tvservice -o
Powering off HDMI
root@raspberrypi:~# /opt/vc/bin/tvservice -s
state 0x12000a, 1920x1080 @ 60Hz, progressive
root@raspberrypi:~# /opt/vc/bin/tvservice -M
Starting to monitor for HDMI events
[I] HDMI cable is unplugged
[I] HDMI in standby mode
^CShutting down...
Which looks like it's all working great, except the monitor rather stubbornly remains on, and is still showing updates from X..?
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by dom » Tue Jun 05, 2012 6:28 pm
Can you try it again. I've a slight recollection it needs to be told twice...

Just tested, and it's not working for me. I'll look into it.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3999
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by rob_pi » Tue Jun 05, 2012 6:52 pm
It worked for me once - now it seems to be a bit intermittent.
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by rob_pi » Wed Jun 06, 2012 12:44 am
I can get it to reliably turn hdmi off if I run /opt/vc/bin/tvservice -p before running /opt/vc/bin/tvservice -o.

The hdmi port can then be turned on again by running /opt/vc/bin/tvservice -p.

My challenge now is to get X to "reconnect" to the re-available monitor; I can do it by killing X, unbind'ing and bind'ing the driver and then restarting X but that seems a bit overkill!
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by unsightlygod » Fri Jun 15, 2012 10:39 pm
Hi Guys,

Found this thread while searching for a way to put my HDMI->DVI connected monitor to sleep.

I discovered that switching virtual consoles once the screen has been turned back on gets the system going again.

I've been tinkering with a few custom graphics apps, launched via a script passed as an argument to startx.
Code: Select all
startx /usr/bin/graphical_launcher `fgconsole`
In doing so I pass the foreground console session number, then as soon as the app starts I retrieve the new foreground session number that X is using.

The first thing the script does when executed by startx is retrive the consoles.
Code: Select all
VT_START=$1
VT_XSERVER=`fgconsole`
Then upon turning the screen back on I simply run:
Code: Select all
chvt $VT_START
chvt $VT_XSERVER
It's a bit of a hack but seems to be working nicely,

Hope it helps,

Phil
Posts: 4
Joined: Fri Jun 15, 2012 10:30 pm
by rob_pi » Tue Jun 19, 2012 10:56 pm
Hi Phil, Thanks for your reply!

I've been struggling with how to get the screen going for an age now and your method above works really well, except when the monitor comes back the image is moved over by about 200 pixels; and wraps around from the other side. It's a bit odd really and I can't find a way of fixing it!
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by unsightlygod » Tue Jun 19, 2012 11:54 pm
@rob_pi What's the resolution of your screen? Would be interested to see if I can reproduce the wraparound you've been experiencing. You using a PC monitor or TV?
Posts: 4
Joined: Fri Jun 15, 2012 10:30 pm
by svetlev » Mon Sep 17, 2012 11:36 pm
unsightlygod wrote:Hi Guys,

Found this thread while searching for a way to put my HDMI->DVI connected monitor to sleep.

I discovered that switching virtual consoles once the screen has been turned back on gets the system going again.

I've been tinkering with a few custom graphics apps, launched via a script passed as an argument to startx.
Code: Select all
startx /usr/bin/graphical_launcher `fgconsole`
In doing so I pass the foreground console session number, then as soon as the app starts I retrieve the new foreground session number that X is using.

The first thing the script does when executed by startx is retrive the consoles.
Code: Select all
VT_START=$1
VT_XSERVER=`fgconsole`
Then upon turning the screen back on I simply run:
Code: Select all
chvt $VT_START
chvt $VT_XSERVER
It's a bit of a hack but seems to be working nicely,

Hope it helps,

Phil


Hi, can you elaborate just a bit. Is this
Code: Select all
VT_START=$1
VT_XSERVER=`fgconsole`

what goes in your /usr/bin/graphical_launcher shell script?

Thanks so much and sorry for replying to an old post, but this is the closest I've found to a solution after much searching.
Posts: 6
Joined: Sun Jul 01, 2012 10:46 pm
by rob_pi » Wed Mar 27, 2013 10:05 pm
Apologies for bumping an old post; but have finally revisited this and figured out a way to achive this:

download/install cec-client from http://sourceforge.net/projects/selfprogramming/files/libCEC.deb/libcec_2.1.0-1_armhf.deb/download
Code: Select all
sudo dpkg -i libcec_2.1.0-1_armhf.deb

Verify it's working ok:
Code: Select all
pi@raspberrypi:~$ cec-client -l
Found devices: 1

device:              1
com port:            RPI
vendor id:           2708
product id:          1001
firmware version:    1
type:                Raspberry Pi


Turn TV off(standby):
Code: Select all
pi@raspberrypi:~$ echo "standby 0" | cec-client -s -d 1
opening a connection to the CEC adapter...


Turn TV on:
Code: Select all
pi@raspberrypi:~$ echo "on 0" | cec-client -s -d 1
opening a connection to the CEC adapter...
Posts: 8
Joined: Mon May 21, 2012 9:01 pm
by averell23 » Tue Apr 09, 2013 10:16 am
libcec didn't work for me. I didn't have time to debug further, but here's the output in case anyone wants to give it another spin:

Code: Select all
echo "standby 0" | cec-client -s -d 0
== skipped log-level parameter: invalid level '0' ==
opening a connection to the CEC adapter...
DEBUG:   [             142]   unregistering all CEC clients
DEBUG:   [             145]   Broadcast (F): osd name set to 'Broadcast'
DEBUG:   [             148]   InitHostCEC - vchiq_initialise succeeded
DEBUG:   [             151]   InitHostCEC - vchi_initialise succeeded
DEBUG:   [             153]   InitHostCEC - vchi_connect succeeded
DEBUG:   [             156]   logical address changed to Broadcast (f)
DEBUG:   [             158]   RegisterLogicalAddress - registering address e
DEBUG:   [             340]   logical address changed to Recorder 1 (1)
DEBUG:   [             340]   logical address changed to Free use (e)
DEBUG:   [             340]   Open - vc_cec initialised
NOTICE:  [             340]   connection opened
DEBUG:   [             342]   << Broadcast (F) -> TV (0): POLL
DEBUG:   [             345]   initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [             346]   << e0
DEBUG:   [             347]   processor thread started
DEBUG:   [             439]   command 'POLL' was not acked by the controller
DEBUG:   [             439]   initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [             439]   << e0
DEBUG:   [             530]   command 'POLL' was not acked by the controller
DEBUG:   [             530]   >> POLL not sent
DEBUG:   [             530]   TV (0): device status changed into 'not present'
NOTICE:  [             530]   registering new CEC client - v2.1.1
DEBUG:   [             530]   detecting logical address for type 'recording device'
DEBUG:   [             531]   trying logical address 'Recorder 1'
DEBUG:   [             531]   << Recorder 1 (1) -> Recorder 1 (1): POLL
TRAFFIC: [             531]   << 11
DEBUG:   [             531]   UnregisterLogicalAddress - releasing previous logical address
DEBUG:   [             532]   logical address changed to Broadcast (f)
DEBUG:   [             535]   RegisterLogicalAddress - registering address 1
DEBUG:   [             717]   logical address changed to Free use (e)
DEBUG:   [             718]   logical address changed to Recorder 1 (1)
TRAFFIC: [             718]   << 11
DEBUG:   [             718]   >> POLL not sent
DEBUG:   [             718]   using logical address 'Recorder 1'
DEBUG:   [             718]   Recorder 1 (1): device status changed into 'handled by libCEC'
DEBUG:   [             718]   Recorder 1 (1): power status changed from 'unknown' to 'on'
DEBUG:   [             719]   Recorder 1 (1): CEC version 1.4
DEBUG:   [             719]   AllocateLogicalAddresses - device '0', type 'recording device', LA '1'
DEBUG:   [             719]   Recorder 1 (1): osd name set to 'CECTester'
DEBUG:   [             719]   Recorder 1 (1): menu language set to 'eng'
DEBUG:   [             720]   GetPhysicalAddress - physical address = ffff
DEBUG:   [             723]   SetDevicePhysicalAddress - not setting invalid physical address ffff
NOTICE:  [             731]   setting HDMI port to 1 on device TV (0)
DEBUG:   [             733]   << Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [             734]   << 10
DEBUG:   [             825]   command 'POLL' was not acked by the controller
TRAFFIC: [             826]   << 10
DEBUG:   [             916]   command 'POLL' was not acked by the controller
DEBUG:   [             916]   >> POLL not sent
DEBUG:   [             917]   Recorder 1 (1): physical address changed from ffff to 1000
DEBUG:   [             917]   << Recorder 1 (1) -> broadcast (F): physical adddress 1000
TRAFFIC: [             917]   << 1f:84:10:00:01
NOTICE:  [            1068]   CEC client registered: libCEC version = 2.1.1, client version = 2.1.1, firmware version = 1, logical address(es) = Recorder 1 (1) , base device: TV (0), HDMI port number: 1, physical address: 1.0.0.0, host: armv6l-unknown-linux-gnueabihf, features: 'P8 USB' 'P8 USB detect' 'RPi', git revision: 3ff78be, compiled on: Mon Mar 25 06:49:17 UTC 2013 by pi@raspberrypi on Linux 3.6.11+ (armv6l)
DEBUG:   [            1068]   << Recorder 1 (1) -> TV (0): OSD name 'CECTester'
DEBUG:   [            1069]   << Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1069]   << 10
DEBUG:   [            1159]   command 'POLL' was not acked by the controller
TRAFFIC: [            1160]   << 10
DEBUG:   [            1250]   command 'POLL' was not acked by the controller
DEBUG:   [            1250]   >> POLL not sent
DEBUG:   [            1251]   not sending command 'set osd name': destination device 'TV' marked as not present
DEBUG:   [            1251]   << requesting power status of 'TV' (0)
DEBUG:   [            1251]   << Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1251]   << 10
DEBUG:   [            1342]   command 'POLL' was not acked by the controller
TRAFFIC: [            1342]   << 10
DEBUG:   [            1433]   command 'POLL' was not acked by the controller
DEBUG:   [            1433]   >> POLL not sent
DEBUG:   [            1433]   not sending command 'give device power status': destination device 'TV' marked as not present
DEBUG:   [            1434]   << Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1434]   << 10
DEBUG:   [            1525]   command 'POLL' was not acked by the controller
TRAFFIC: [            1525]   << 10
DEBUG:   [            1616]   command 'POLL' was not acked by the controller
DEBUG:   [            1616]   >> POLL not sent
NOTICE:  [            1616]   << putting 'TV' (0) in standby mode
DEBUG:   [            1617]   << Recorder 1 (1) -> TV (0): POLL
TRAFFIC: [            1617]   << 10
DEBUG:   [            1707]   command 'POLL' was not acked by the controller
TRAFFIC: [            1708]   << 10
DEBUG:   [            1798]   command 'POLL' was not acked by the controller
DEBUG:   [            1799]   >> POLL not sent
DEBUG:   [            1799]   not sending command 'standby': destination device 'TV' marked as not present
DEBUG:   [            1799]   unregistering all CEC clients
NOTICE:  [            1799]   unregistering client: libCEC version = 2.1.1, client version = 2.1.1, firmware version = 1, logical address(es) = Recorder 1 (1) , base device: TV (0), HDMI port number: 1, physical address: 1.0.0.0, host: armv6l-unknown-linux-gnueabihf, features: 'P8 USB' 'P8 USB detect' 'RPi', git revision: 3ff78be, compiled on: Mon Mar 25 06:49:17 UTC 2013 by pi@raspberrypi on Linux 3.6.11+ (armv6l)
DEBUG:   [            1799]   Recorder 1 (1): power status changed from 'on' to 'unknown'
DEBUG:   [            1799]   Recorder 1 (1): CEC version unknown
DEBUG:   [            1800]   Recorder 1 (1): osd name set to 'Recorder 1'
DEBUG:   [            1800]   Recorder 1 (1): device status changed into 'unknown'
DEBUG:   [            1800]   unregistering all CEC clients
DEBUG:   [            1801]   UnregisterLogicalAddress - releasing previous logical address
DEBUG:   [            1805]   logical address changed to Broadcast (f)
Posts: 1
Joined: Tue Apr 09, 2013 10:12 am
by cleverca22 » Sun Apr 14, 2013 10:58 am
are you using a desktop monitor, or a tv?, only tv's have cec
Posts: 168
Joined: Sat Aug 18, 2012 2:33 pm
by jojopi » Sun Apr 14, 2013 11:39 am
Is tvservice really not working? Power off monitor:
Code: Select all
tvservice -o
Power on:
Code: Select all
tvservice -p; fbset -depth 8; fbset -depth 16
User avatar
Posts: 1966
Joined: Tue Oct 11, 2011 8:38 pm
by hans.mussmann » Wed Jun 12, 2013 11:06 pm
Thanks to code found here I thought I should post what worked for me.

This will go in a cron job to switch screen off during the night and on during the day.

Placed this in:
/usr/bin/screen.sh

and call it with:
/usr/bin/screen.sh off
or
/usr/bin/screen.sh on

Code: Select all
#!/bin/bash

if [ "$1" == 'on' ]; then
  tvservice -p;
  fbset -depth 8;
  fbset -depth 16;
  chvt 6;
  chvt 7;
  echo 'Switched Screen ON!'
fi

if [ "$1" == 'off' ]; then
  tvservice -o
  echo 'Switched Screen OFF!'
fi
Posts: 1
Joined: Wed Jun 12, 2013 10:58 pm
by peterjaap » Mon Nov 04, 2013 4:18 pm
The above code did not work for me, because of my Bash not agreeing with the if structure. Here's my code;

Code: Select all
#!/bin/bash

if [ $1 = 'on' ]; then
  tvservice -p;
  fbset -depth 8;
  fbset -depth 16;
  chvt 6;
  chvt 7;
  echo 'Switched Screen ON!'
fi

if [ $1 = 'off' ]; then
  tvservice -o
  echo 'Switched Screen OFF!'
fi
Posts: 1
Joined: Mon Nov 04, 2013 4:16 pm
by dmichel76 » Fri Jan 10, 2014 10:02 am
I'm working on an application where I also have to programmatically turn the hdmi output on and off.

Turning it off with
Code: Select all
tvservice -o
and back on with
Code: Select all
tvservice -p
followed by
Code: Select all
chvt 6
chvt 7

seems to work fine...

But calling 'chvt 7' without calling 'chvt 6' before doesn't... Is there no way to go back to the tty7 directly?

In my case, it is critical to be able to go back to my full-screen app, without any other text, consoles prompts appearing...


David
Posts: 4
Joined: Wed Jan 08, 2014 4:56 pm
by jojopi » Fri Jan 10, 2014 5:31 pm
dmichel76 wrote:But calling 'chvt 7' without calling 'chvt 6' before doesn't... Is there no way to go back to the tty7 directly?
You are already on tty7. I assume that switching away and back is being done for the side-effect of making Xorg redraw all windows. You can get that effect more cleanly with "xrefresh" (provided your script has inherited the DISPLAY and XAUTHORITY variables from inside the X session).

Instead of tty6, you can also switch to one that is blank, such as 9.
User avatar
Posts: 1966
Joined: Tue Oct 11, 2011 8:38 pm