Interact with this post using Mastodon or
Brightness auto adjustment
Published on
If your machine is equipped with a light sensor, you can set it up to auto adjust to the ambient light level. I found this feature to be good but not very well suited to my need. Sometimes an auto adjustment was made when I did not want it and sometimes it was not made at the exact time I needed it.
This is the reason why I came up with a script.
Prepare your system
The only thing you will have to do once before using the script is to install the iio-sensor-proxy package and then run monitor-sensor to check that your light sensor is supported and automatically create the appropriate directories and files to control it.
Once done, you can freely remove the iio-sensor-proxy
Variables you can adjust in the script
When called (with a keybinding/mouse binding), the script increases/decreases the screen backlight level by 5% step to match the light sensor level or your defined minimum in case the sensor level is below that.
I left comments in the script so it should be pretty much self explanatory.
The script uses several variables and the ones below can/should be adjusted:
- “minb” to define a minumum backlight level (currently set to 7%)
- “backlight”: check the path in your system and adapt if necessary
- “sensor”: check the path in your system and adapt if necessary
Dependencies you can change
Everyone has his/her own preference. I use dunstify as my notification daemon and xbacklight to control the screen backlight. You can replace both and adjust the commands in the script.
Stop talking, give me the script!
Here it is:
#!/usr/bin/env bash
# define variables
maxl=$(cat /sys/class/backlight/intel_backlight/max_brightness)
max=$(("$maxl"/100))
sensitivity=$((max/20))
minb=7 # set minimum backlight to 7%
minl=$((("$minb"/100)*96000))
backlight=$(cat /sys/class/backlight/intel_backlight/brightness)
sensor=$(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw)
if [ "$sensor" -gt "$max" ]; then
target=$max
elif [ "$sensor" -lt 1 ]; then
target="$min"
else
target="$sensor"
fi
# increase screen brightness
if [ "$backlight" -lt "$sensor" ] && [ $(("$backlight" - "$sensor")) -lt "$sensitivity" ] && [ "$backlight" -lt "$maxl" ]
then
target=$(("$target"/"$max"))
itarget=$(("$backlight"/"$max"))
until [ $itarget -ge "$target" ]; do
itarget=$(("$itarget"+5))
xbacklight -set "$itarget"
sleep 0.05
done
dunstify -u low -t 1500 \"backlight: "$itarget"\"
sleep 2 &&
dunstify -C 0
fi
# decrease screen brightness
if [ "$backlight" -gt "$sensor" ] && [ $(("$backlight" - "$sensor")) -gt "$sensitivity" ] && [ "$backlight" -gt "$minl" ]; then
target=$(("$target"/"$max"))
itarget=$(("$backlight"/"$max"))
until [ "$itarget" -le "$target" ] || [ "$itarget" -eq "$minb" ] ; do
itarget=$(("$itarget"-5))
if [ "$itarget" -le "$minb" ]; then
xbacklight -set "$minb" # set screen brightness to minimum
itarget="$minb"
else
xbacklight -set "$itarget"
fi
sleep 0.05
done
dunstify -u low -t 1500 \"backlight: "$itarget"\"
fiMore food for thoughts? Check other posts about: #System
Thanks for your read. Hope it's been useful to you.
✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈