Control Display Brightness with SKipper

If you run SKipper app on a Raspberry Pi with a display capable of changing brightness, like the official Raspberry PI display, you can change the brightness automatically when SKipper changes its theme. So you can make the display dark when the theme is Dark, and make it bright when the theme is Light.

This post will explain how to configure SKipper to do that for an official Pi display.

Create a New Script for Updating Brightness

Let’s write a simple script tailored for official Raspberry Pi that will update the system backlight. This script will be called automatically by SKipper when the theme is changed.

Backlight control can be done by writing a number from 0255 to the file /sys/class/backlight/10-0045/brightness, which is a virtual file created by the backlight hardware controller. A value of 0 turns the backlight off, 255 makes the backlight fully bright, and any value between those two values makes the backlight something between off and full.

The script needs to be called with an argument (Light or Dark) that will be added by SKipper when it calls the script.

Here is the bash script that can handle everything:

#!/usr/bin/env bash
BRIGHTNESS_FILE="/sys/class/backlight/10-0045/brightness"

if [ $# -lt 1 ]; then
  echo "Usage: $0 <Mode>  # pass 'Dark' to set 50, anything else sets 200"
  exit 2
fi

if [ "$1" = "Dark" ]; then
  VALUE=50
else
  VALUE=200
fi

if command -v sudo >/dev/null 2>&1; then
  printf "%s" "$VALUE" | sudo tee "$BRIGHTNESS_FILE" >/dev/null
  exit $?
fi
echo "Error: cannot write to $BRIGHTNESS_FILE (permission denied) and sudo is not available." >&2
exit 1

Let’s talk about the major parts of the script. First, we need to determine which one of the required arguments is supplied. It will always be either „Dark“ or „Light“, so we have to check for only one of them – if it’s not that one, then it has to be the other one. This is done by these lines:

if [ $# -lt 1 ]; then
  echo "Usage: $0 <Mode>  # pass 'Dark' to set 50, anything else sets 200"
  exit 2
fi

When an argument is supplied, the script will decide what brightness value will be saved to brightness file. You can change the values for dark or bright if you want, but these are good starting values. Try them, and if you need to, change the „50“ and/or the „200“, save the file, then try it.

if [ "$1" = "Dark" ]; then
  VALUE=50
else
  VALUE=200
fi

This block will write the new value into the backlight file.

if command -v sudo >/dev/null 2>&1; then
  printf "%s" "$VALUE" | sudo tee "$BRIGHTNESS_FILE" >/dev/null
  exit $?
fi

If something goes wrong, the script will exit with a non-zero value.

Let’s create the script file. Copy all of the code in the longest code block above and paste it into a text editor. Save it in the Pi profile, something like /home/pi/update_brightness.sh.

Now we need to make the script executable. Run the following line in a Pi console. This will set the required execution permission for the script file.

chmod +x update_brightness.sh

To make sure it’s working, run the following lines one at a time from the console. The first one should set the brightness to 200, the second should set it to 50.

./update_brightness.sh Light

./update_brightness.sh Dark

Running the Script when the SKipper Theme Changes

Stop SKipper (via GUI, or with sudo systemctl stop skipper when running in DRM mode). This is a very important step!

Open the home/<username>/.Skipper/appsettings.json file in a text editor and find the Application section – it will look something like this:

"Application": {
   "EventHandlers": {}
}

The sub-section EventHandlers allows users to run scripts based on some SKipper event. Currently, you can run scripts when ThemeChanged change and when Notification state has been changed.

Add the new script into the Application section to look like this – note that you are adding THREE lines:

  "Application": {
    "EventHandlers": {
      "ThemeChanged": {
        "Command": "/home/pi/update_brightness.sh",
        "Arguments": "@(Theme)"
      }
    }
  }

The saved file must be valid JSON, so be very careful to use all the correct punctuation, as above. If your script is located at different path than shown above, use your path. Note that in „Arguments“, there is passed the argument Theme. That will be provided by SKipper, and will be either Light or Dark.

Save the file and restart SKipper. That’s it! Now when you change the SKipper theme, the display brightness will change. Cool, isn’t it?

1 názor na “Control Display Brightness with SKipper”

Diskuze

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *

Přejít nahoru