This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Toggle RPF 7" touchscreen backlight with screensaver
#1
Following up on my own question in http://forum.openmarine.net/showthread.php?tid=2174 here's how I'm currently controlling the backlight on the official Raspberry Pi Foundation 7" touchscreen (v1.1) via xscreensaver.

It's not the perfect solution, but close enough for now. I'm still looking into whether I can make this into a proper xscreensaver module that can be distributed as a single file.

Using a script I found and edited, I launch a process to watch xscreensaver and toggle the backlight when xscreensaver blanks and unblanks the display. This means if you install the xscreensaver package, you can configure the screen blanking time from the GUI, turn it on or off entirely, and the RPi display backlight will respond to those changes. The process starts automatically on boot.

I'll write down the entire installation procedure in case anyone wants to try it out.

To install xscreensaver, do
Code:
sudo apt-get install xscreensaver

Edit a new file, I'll call it backlight_control.sh. I use nano:

Code:
nano ~/backlight_control.sh

Input the following

Code:
#!/bin/sh
process() {
   while read line; do
       case "$line" in
           UNBLANK*)
               sh -c 'echo "0" > /sys/class/backlight/rpi_backlight/bl_power'
           ;;
           BLANK*)
               sh -c 'echo "1" > /sys/class/backlight/rpi_backlight/bl_power'
           ;;
       esac
   done
}

xscreensaver-command -watch | process

Save the file. Now to make the script executable
Code:
chmod +x ~/backlight_control.sh

To launch the script as a process automatically on reboot, edit this file:

Code:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Add the following line. Make sure it's below the line that reads "@xscreensaver -no-splash"; xscreensaver-command needs to be started after xscreensaver.

Code:
@sudo /home/pi/backlight_control.sh &

The '&' makes sure the script can exit, leaving the new process running in the background. Save the file and reboot your Pi.

The backlight should turn off when the screensaver blanks the screen, and turn back on upon keyboard, mouse or touch input.

If anyone has any suggestions on how to improve this or a better solution, feel free to pitch in. Smile

Some notes:
  • I don't think you need to have the entire xscreensaver package installed for this, as long as you just want the default screen blanking functionality, but I haven't tested this without it.
  • There are more messages that xscreensaver sends out (specifically LOCK and RUN). Adding cases to the script can handle those too. I haven't looked more closely at what they do, but they might provide interesting functionality.

Fair winds,
Andy
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)