bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Official 7" touchscreen backlight control

Sat Jul 06, 2019 8:17 pm

I am at a loss, I had a python script using this function running before but corrupt SD card and not backing up my files leads me to relearning :( .
I am trying to turn on and off the backlight for the 7" official touchscreen using the "echo 1 / 0" commands. When I run the script I get a syntax on the line with the backlight command but I can't see what the issue is. I run the on and off line command in the command prompt and the screen reacts as it should. Below is the code I'm using.

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

shutdownStarted = False
while True:
    if (GPIO.input(25) == False and not shutdownStarted):
        os.system("sudo shutdown -h -57")
        echo 1 > /sys/class/backlight/rpi_backlight/bl_power
        # This stops the code inside this if from being executed again even if the pin stays False
        shutdownStarted = True

    secs = 57 * 60
    # Run this loop once per second for 57 mins as long as shutdown is still supposed to happen
    while (secs > 0 and shutdownStarted):
        if (GPIO.input(25) == True):
            # This now allows the shutdown -h code to be run again
            # assuming you want this to execute forever based on the while True you got goin on there
            shutdownStarted = False
            os.system("sudo shutdown -c")
            echo 0 > /sys/class/backlight/rpi_backlight/bl_power
        time.sleep(1)
        secs -= 1
    time.sleep(1)

ghp
Posts: 1391
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Official 7" touchscreen backlight control

Sat Jul 06, 2019 8:20 pm

there are multiple places like
os.system("sudo shutdown -c")
echo 0 > /sys/class/backlight/rpi_backlight/bl_power
think you should wrap the echo command into a os.system("echo ...")-command.

bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Re: Official 7" touchscreen backlight control

Tue Jul 09, 2019 3:37 pm

Thanks GHP, that was exactly the solution. I must have had it inside the os.system command last time.

Return to “Python”