User avatar
simonmcc
Posts: 192
Joined: Mon Aug 19, 2013 10:07 pm

using xscreensaver to power off HDMI

Mon Sep 30, 2013 7:49 am

for my application I've set up xscreensaver to blank my 7" eGalax touchscreen after 10 minutes, but when it's left unattended for 12 hours the backlight is still on. The screen sleeping options don't work in xscreensaver, so I rolled my own solution.

I'd appreciate suggestions if it isnt the best solution. It should work for others too.

Step 1 - Create scripts that manually turn off and on the HDMI

- create file hdmi_off.sh

Code: Select all

#!/bin/sh
#hdmi_off.sh - shell script to turn HDMI off - should work universially
tvservice -o
- create file hdmi_on.sh (make sure to set executable permissions on both - 'chmod 755 *sh')

Code: Select all

#!/bin/sh

#hdmi_on.sh - custom script to turn HDMI on 

#get the current tv state
tvstate=`tvservice -s | grep HDMI | wc -l`

#only turn on if its not already on
if [ "$tvstate" -eq 0 ] 
then
	tvservice --explicit="DMT 87"
	fbset -depth 8
	fbset -depth 16
	xrefresh
else
	echo "HDMI already on";
fi

exit;
You should make sure these scripts work from the command line for you - all my other script does is call them, so if these do not work it will not work. Stop here until you get them working. You'll have to search the forums to find how to make your screen come on, and start displaying stuff.

Step 2 - Create a perl script to detect the screensaver state

Basically we can use "xscreensaver-command -watch" to follow text output of what xscreensaver is doing. What I do is detect when the screensaver becomes active, then start a timer to determine when to turn the screen off. It is a lot simpler if you just want the screen to power off immediately. But I wanted this dual off approach as it takes the screen longer to come on when its fully off.

Code: Select all

#!/usr/bin/perl
 
#check_screen_state.pl - perl script to monitor screen state, and turn HDMI on or off
use strict;
use warnings;
use threads;
use Thread::Queue;
$|=1;

my $counter=-1;
#time in seconds
my $time_to_power_off_screen = 1200; #20 minutes
my $hdmi_off_command = "/home/pi/screensaver/hdmi_off.sh";
my $hdmi_on_command = "/home/pi/screensaver/hdmi_on.sh";

#code to run a shell command in a thread, and get output from it
sub pipeCommand {
    my $cmd = shift;
    my $Q = new Thread::Queue;
    async{
        my $pid = open my $pipe, $cmd or die $!;
        $Q->enqueue( $_ ) while <$pipe>;
        $Q->enqueue( undef );
    }->detach;
    return $Q;
}

my $pipe = pipeCommand(
    'xscreensaver-command -watch |'
) or die;

print_("Starting, waiting on input.\n");
while( 1 ) {
    if( $pipe->pending ) {
        my $line = $pipe->dequeue or last;
        print_("$line");
		if ($line =~ m/^(BLANK|LOCK)/) {
			$counter = $time_to_power_off_screen;
		} elsif ($line =~ m/^UNBLANK/) {
			if($counter <= 0){
				print_( "turn on\n");
				print_( `$hdmi_on_command `);
			}else{
				print_("screen already on\n");
			}
			$counter=-1;
 		}
    }
    else {
        sleep 1;
		if($counter >0){
			$counter--;
		}
		 
		if($counter == 0){
			$counter=-1;
			print_( "turn off acter $time_to_power_off_screen seconds\n");
			print_(`$hdmi_off_command`);			
		}
    }
}

sub print_{
	print "[".localtime(time) .  "] @_";
}
Step 3 - make the script run all the time

Make an inittab entry to make the perl script run all the time ('sudo vi /etc/inittab' and add the line to the bottom

Code: Select all

hdmi:2345:respawn:su - pi -c "perl /home/pi/screensaver/check_screen_state.pl >  /home/pi/screensaver/check_screen_state.pl.log"

Save the file, exit the editor, and then type 'sudo init q' to make the system re-read the file. 'ps -ef' should now list your script. (note that I do 'su - pi' to make sure the DISPLAY variable is set - it is set in my bash login file)
Last edited by simonmcc on Mon Oct 07, 2013 7:11 am, edited 1 time in total.
simonmcc.blogspot.com/search/label/pi

User avatar
DetlevSchm
Posts: 76
Joined: Tue Mar 12, 2013 8:43 am
Location: 3rd planet

Re: using xscreensaver to power off HDMI

Sun Oct 06, 2013 9:27 am

'tvservice -o' switches the HDMI output signal off. This has little impact on the monitor's power consumption. The monitor is still on and just receives a blank signal. My 10 year old monitor even annoyingly blinks the power LED.

There might be monitors, however, that switch to standby mode, if they receive no signal, and wake-up accordingly, if the signal returns. As you solved your backlight issue with this command, simonmcc, I assume you have such one.

ramses0
Posts: 3
Joined: Sun Dec 01, 2013 5:51 am

Re: using xscreensaver to power off HDMI

Sun Dec 01, 2013 5:56 am

Please take a look here-

I started with Simon's idea and tightened it up a bit, made a package for debian.

cd xscreensaver-pi-hdmi && dpkg-buildpackage && sudo dpkg -i ../xscreensaver-pi-hdmi_*.deb

You'll need some extra packages prior (maybe apt-get install build-essential) or you can just copy/run the command directly from the bin directory, as it's a single self-contained script.

I'll try to get in touch with Simon directly and see where to take it.

--Robert

ramses0
Posts: 3
Joined: Sun Dec 01, 2013 5:51 am

Re: using xscreensaver to power off HDMI

Sun Dec 01, 2013 3:32 pm

Not sure if the link got removed or I forgot it: https://github.com/ramses0/xscreensaver-pi-hdmi

User avatar
simonmcc
Posts: 192
Joined: Mon Aug 19, 2013 10:07 pm

Re: using xscreensaver to power off HDMI

Mon Dec 09, 2013 12:10 pm

ramses0 wrote:Please take a look here-

I'll try to get in touch with Simon directly and see where to take it.

--Robert
Robert, I solved my problem and posted it up with the hope that it may help someone else. You can do whatever you want with it. It seems like you've made a nice package which will make it more accessible for people to use.

Hopefully that will help others, and btw you don't have to credit me if you don't want to.
simonmcc.blogspot.com/search/label/pi

ramses0
Posts: 3
Joined: Sun Dec 01, 2013 5:51 am

Re: using xscreensaver to power off HDMI

Tue Dec 10, 2013 8:09 am

simonmcc wrote:Robert, I solved my problem and posted it up with the hope that it may help someone else. You can do whatever you want with it. It seems like you've made a nice package which will make it more accessible for people to use.

Hopefully that will help others, and btw you don't have to credit me if you don't want to.
...well it certainly did help me. I'll try to shop it around and see if there's a way to get more people able to use it. Thanks so much for the initial inspiration, as I wouldn't have gotten as far as I did without it.

--Robert

Return to “General discussion”