JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Added display backlight on/off toggle button

Sat Nov 07, 2015 7:43 pm

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

#!/usr/bin/python
#
#   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
import time

gpio.setmode(gpio.BCM)
gpio.setup(5, gpio.IN, pull_up_down = gpio.PUD_UP)

def set_backlight(channel):
    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
    else:
        bl_set = 0

    bl_update = str(bl_set)
    file.seek(0)
    file.write(bl_update)
    file.close
    
gpio.add_event_detect(5, gpio.FALLING, callback=set_backlight, bouncetime=300)

while 1:
    time.sleep(360)
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.

Aramas
Posts: 16
Joined: Tue Sep 29, 2015 10:24 am
Location: France - Lille

Re: Added display backlight on/off toggle button

Sat Nov 07, 2015 10:37 pm

Hello,

I was interesting for a button too, but after some test I saw that the touchscreen is still working when the backlight is off.
So in my kiosk app, I just added a little invisible button in the top left position to toggle the backlight and it works very well and I don't need a physical button.

Snyder
Posts: 31
Joined: Fri Sep 04, 2015 9:25 am

Re: Added display backlight on/off toggle button

Tue Nov 24, 2015 8:11 pm

The script works fine for me! Great!

makerbotspace
Posts: 2
Joined: Sat Dec 05, 2015 10:14 pm

Re: Added display backlight on/off toggle button

Sat Dec 05, 2015 10:31 pm

@ Aramas

I like your solution. Notice this says it will turn off the back light, not the display. I have not tried it but I would assume, based on it just flipping the backlight off, the touch screen will remain active.

Snyder
Posts: 31
Joined: Fri Sep 04, 2015 9:25 am

Re: Added display backlight on/off toggle button

Sun Dec 06, 2015 9:11 pm

yes, the screen remains active. Does anybody know how to disable the touch function?

pumpkinpi
Posts: 141
Joined: Fri Apr 18, 2014 7:11 pm

Re: Added display backlight on/off toggle button

Mon Jan 25, 2016 12:36 am

How did you do this part:
JimmyN wrote:I've also turned off automatic screen blanking, so it stays on until I manually turn it off.
Thanks,
PPi

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Added display backlight on/off toggle button

Mon Jan 25, 2016 4:12 am

pumpkinpi wrote:How did you do this part:
JimmyN wrote:I've also turned off automatic screen blanking, so it stays on until I manually turn it off.
Thanks,
PPi
I edited the "/etc/lightdm/lightdm.conf" file and added this line under the "[SeatDefaults]" section

Code: Select all

[SeatDefaults]
xserver-command=X -s 0 -dpms
That's correct, it only turns the backlight off, the screen is still "active". I was doing it to save power when on battery, as the backlight draws most of it. The desktop manager only reads the "lightdm.conf" file when it starts up, so I couldn't toggle the blanking along with the backlight on/off.

Jusufs
Posts: 3
Joined: Sun Jan 14, 2018 7:23 pm

Re: Added display backlight on/off toggle button

Mon Dec 31, 2018 4:20 pm

For trigger backlight on/off with screen not blanked forewer look at:
viewtopic.php?f=28&t=230047&p=1409643#p1409643

Works via Amazon echo command :D from openhab2

Return to “Official Foundation Display”