Page 1 of 1

Official 7" touchscreen backlight control

Posted: Sat Jul 06, 2019 8:17 pm
by bpm009
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.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
        secs -= 1

Re: Official 7" touchscreen backlight control

Posted: Sat Jul 06, 2019 8:20 pm
by ghp
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.

Re: Official 7" touchscreen backlight control

Posted: Tue Jul 09, 2019 3:37 pm
by bpm009
Thanks GHP, that was exactly the solution. I must have had it inside the os.system command last time.