Skip to content

Instantly share code, notes, and snippets.

@discarn8
Last active April 17, 2022 05:56
Show Gist options
  • Save discarn8/4c9dfef09d6298ac745da031969c13d4 to your computer and use it in GitHub Desktop.
Save discarn8/4c9dfef09d6298ac745da031969c13d4 to your computer and use it in GitHub Desktop.
Screendimmer for Raspberry Pi 4 Using ddcutil
#!/bin/bash
# NOTE: Requires ddcutil
# sudo apt install ddcutil -y
# Edit /boot/config.txt and replace fkms with kms, then reboot
# Call using screendim.sh -b 70 -c 70
# Such as in sudo's crontab to bring the screens "alive" at 6am:
# 0 6 * * * /home/pi/scripts/screendim.sh -b 70 -c 70
#
# 70 being a number that you are visually comfortable with, for each setting
# ddcutil detect <-- To get the displays
# ddcutil capabilities <-- To get the group numbers
# ddcutil --display 1 getvcp 10 <-- To get the current brightness setting
# 70
# ddcutil --display 1 getvcp 12 <-- To get the current contrast setting
# 70
#
logger "screendim.sh started"
#ddcutil --display 1 getvcp 10
#96
#ddcutil --display 1 getvcp 12
#73
DISPLAY=:0.0
export DISPLAY=:0.0
export XAUTHORITY=/home/pi/.Xauthority
while getopts "c:b:" opt; do
case $opt in
b ) BRIGHTNESS=$OPTARG; ;;
c ) CONTRAST=$OPTARG; ;;
esac
done
shift $(($OPTIND - 1))
curb1=0
curb2=0
curc1=0
curc2=0
# Check number formats
if ! echo $BRIGHTNESS | grep -qE '^[0-9]+(\.[0-9]+)?$' ; then
echo "BRIGHTNESS UNKOWN - Wrong number: $BRIGHTNESS"
exit 3
fi
if ! echo $CONTRAST | grep -qE '^[0-9]+(\.[0-9]+)?$' ; then
echo "CONTRAST UNKOWN - Wrong number: $CONTRAST"
exit 3
fi
# Get existing Brightness and Contrast for both displays
curb1=$(ddcutil --display 1 getvcp 10 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
curb2=$(ddcutil --display 1 getvcp 10 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
curc1=$(ddcutil --display 1 getvcp 12 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
curc2=$(ddcutil --display 2 getvcp 12 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
# Compare current setting to existing and then set Brightness for Display 1 if different
while [ $curb1 != $BRIGHTNESS ]; do
ddcutil --display 1 setvcp 10 $BRIGHTNESS;
curb1=$(ddcutil --display 1 getvcp 10 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
done
# Compare current setting to existing and then set Brightness for Display 2 if different
while [ $curb2 != $BRIGHTNESS ]; do
ddcutil --display 2 setvcp 10 $BRIGHTNESS;
curb2=$(ddcutil --display 1 getvcp 10 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
done
# Compare current setting to existing and then set Contrast for Display 1 if different
while [ $curc1 != $CONTRAST ]; do
ddcutil --display 1 setvcp 12 $CONTRAST;
curc1=$(ddcutil --display 1 getvcp 12 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
done
# Compare current setting to existing and then set Contrast for Display 2 if different
while [ $curc2 != $CONTRAST ]; do
ddcutil --display 2 setvcp 12 $CONTRAST;
curc2=$(ddcutil --display 2 getvcp 12 | grep -o -P '(?<=current value =).*(?=,)' | xargs);
done
logger "screendim.sh completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment