I wanted the ability to turn the display off and on manually with a pushbutton, so I created a little Python script to change the value in the "bl_power" file, to do the switching. A "0" in that file turns it on, a "1" turns it off. The script uses an interrupt and a long sleep cycle to keep it active, so CPU resources consumed are negligible.
Code: Select all
# backlight-toggle.py 10-25-2015
# RPF 7" Touchscreen Display
# Toggles backlight on/off with button press
# Uses GPIO5 (BOARD Pin 29)
import RPi.GPIO as gpio
from subprocess import call
gpio.setup(5, gpio.IN, pull_up_down = gpio.PUD_UP)
file = open('/sys/devices/platform/rpi_backlight/backlight/rpi_backlight/bl_power','r+')
current_status = int(file.read(1))
if current_status == 0:
bl_set = 1
bl_set = 0
bl_update = str(bl_set)
gpio.add_event_detect(5, gpio.FALLING, callback=set_backlight, bouncetime=300)
I've also turned off automatic screen blanking, so it stays on until I manually turn it off. This will be a battery powered project eventually and leaving it to blank automatically after 10 minutes consumes a lot of extra battery power. Just 6 automatic "blanks" a day would be an extra hour of unused screen "on" time and consume an additional half amp hour of power. I could shorten the inactivity time, but I find short duration screen blanking to be very annoying sometimes. So I press the button when I'm not using it, or leaving for a bit, and press it again when I want the screen back.
I'm starting the backlight-toggle script using ~/.config/lxsession/LXDE-pi/autostart so it runs when the Desktop comes up.
Just thought I'd post this as I'm sure someone else will want to toggle the screen on/off with a button.