random/brightness-by-daylight.sh

50 lines
1.6 KiB
Bash
Raw Normal View History

2022-06-26 19:22:50 -05:00
#!/bin/sh
# This is a script to set my external monitor's brightness
# to high (70%) or low (40%) automatically according to day/night.
# It uses ddcutil to access the monitor's settings and sunwait to know
# if it's day or night. It expects a file in the following format in
# the .config directory (the values should be according to location)
# ~/.config/latlong.toml
# latitude="0.000000N"
# longitude="0.000000E"
# Add the following to crontab to make it run automatically and properly
2022-06-26 21:53:58 -05:00
# @reboot <location-of-this-script> && <location-of-this-script> crontab
2022-06-26 19:22:50 -05:00
# 0 0,12 * * * <location-of-this-script> crontab
confdir="/home/sintan/.config"
if test -f $confdir/latlong.toml ; then
source $confdir/latlong.toml
else
echo "No location config found!"
2022-06-26 21:53:58 -05:00
exit
2022-06-26 19:22:50 -05:00
fi
2022-06-26 21:53:58 -05:00
# Check if called by crontab as routine. If yes, start waiting,
# otherwise adjust brightness immediately
2022-06-26 19:22:50 -05:00
if [ "$1" == "crontab" ]; then
2022-06-26 21:53:58 -05:00
if test -f /tmp/brightness-crontab; then
exit
2022-06-26 19:22:50 -05:00
else
2022-06-26 21:53:58 -05:00
touch /tmp/brightness-crontab
if [ $(sunwait poll $latitude $longitude) == "DAY" ]; then
sunwait wait set offset 10 $latitude $longitude && ddcutil setvcp 10 40
else
sunwait wait rise offset 10 $latitude $longitude && ddcutil setvcp 10 70
fi
rm /tmp/brightness-crontab
2022-06-26 19:22:50 -05:00
fi
else
if [ $(sunwait poll $latitude $longitude) == "DAY" ]; then
ddcutil setvcp 10 70
echo "Monitor brightness set to 70%, since it's day time."
else
ddcutil setvcp 10 40
2022-06-26 21:53:58 -05:00
echo "Monitor brightness set to 40%, since it's night time."
2022-06-26 19:22:50 -05:00
fi
fi