0

I have an Adafruit piTFT3.5 running on a pi zero running raspbian-light. For this specific project the screen brightness should be 0 until a python script tells it to turn on.

Right now I achieved this by running a the following in the rc.local script:

sudo sh -c 'echo "0" > /sys/class/backlight/soc\:backlight/brightness'

This turns the brightness of the screen to 0 as soon as rc.local is run. The problem is that rc.local is run far after the screen is configured so the screen turns on and then takes a second before turning off again which is not optimal since sometimes it displays things it had on it before the last reboot.

So it seems that the brightness of the piTFT is set in the sudo nano /sys/class/backlight/soc:backlight/brightness location. The problem is that when the pi boots this always seems to be 1 even if it was set to 0 before the last reboot.

My question is if there is a way to have the bigness set to 0 at boot and then only turn on one my scripts sets it to 1 ?

1 Answer 1

1

You have found why you should not use /etc/rc.local anymore. It has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. The right way nowadays is to use a systemd unit file to start a service.

With your requirement we have to execute the command as early as possible on bootup, just after initializing the pseudo filesystem /sys/. With a look at the System bootup process this seems to be the sysinit.target with various low-level services and local-fs initialized. Try this unit file with:

rpi ~$ sudo systemctl --force --full edit switch-backlight-off.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Switch off backlight of the display
After=sysinit.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo "0" > /sys/class/backlight/soc\:backlight/brightness'
# RemainAfterExit=yes
# ExecStop=/bin/sh -c 'echo "1" > /sys/class/backlight/soc\:backlight/brightness'

[Install]
WantedBy=sysinit.target

Every time you start the service it should switch off the backlight:

rpi ~$ sudo systemctl start switch-backlight-off.service

Enable the new service, so it will run on bootup:

rpi ~$ sudo systemctl enable switch-backlight-off.service

If you like, you can also stop the service and the backlight switches on then. Just uncomment the two commented lines in the [Service] section.

1
  • 1
    @Mercury I love systemd :-) If you like, you can also stop the service and the backlight switches on then. You have just to add two lines to the Unit. I have commented them in the [Service] section.
    – Ingo
    Commented May 14, 2020 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.