Skip to content

Instantly share code, notes, and snippets.

@pettazz
Last active January 19, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pettazz/3759bbc9135f42b103e9bc31a2f4addb to your computer and use it in GitHub Desktop.
Save pettazz/3759bbc9135f42b103e9bc31a2f4addb to your computer and use it in GitHub Desktop.
Set the brightness on certain Raspberry Pi compatible screens as a percentage
#!/usr/bin/env bash
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
echo "Percentage must be an integer" 1>&2
exit 1
fi
if (( $1 < 0 || $1 > 100 )); then
echo "Percentage value must be at least 0 or at most 100" 1>&2
exit 1
fi
curr=`cat /sys/class/backlight/rpi_backlight/brightness`
target=`printf %.0f $(echo "$1 * 0.01 * 255" | bc)`
(( $curr > $target )) && inc=-1 || inc=1
read -d "" looper << LOOPERDEF || true
for i in \$(seq $curr $inc $target);
do
echo \$i | tee /sys/class/backlight/rpi_backlight/brightness > /dev/null;
done
LOOPERDEF
sudo bash -c "$(echo $looper)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment