Skip to content

Instantly share code, notes, and snippets.

@victortrac
Last active February 8, 2021 22:47
Show Gist options
  • Save victortrac/5667f2a7133adba3055609b0b93d42f3 to your computer and use it in GitHub Desktop.
Save victortrac/5667f2a7133adba3055609b0b93d42f3 to your computer and use it in GitHub Desktop.
sync laptop and external monitor brightness
#!/bin/bash
# This script syncs the brightness on my laptop screen as well as my
# Dell 2720QM external usb-c monitor
# References:
# https://blog.tcharles.fr/ddc-ci-screen-control-on-linux
# Steps
# 1. Add udev rules to /etc/udev/rules.d/45-ddcutil-i2c.rules:
# KERNEL=="i2c-[0-9]*", GROUP="victor", MODE="0660"
# 2. Get I2C bus ID:
# ddcutil detect
#
# Usage
# I have my keyboard brightness keys mapped in ~/.config/i3/config like this:
# bindsym XF86MonBrightnessDown exec "/home/victor/.config/i3/brightness.sh down"
# bindsym XF86MonBrightnessUp exec "/home/victor/.config/i3/brightness.sh up"
#
# I also set crontab to call the brightness and audio volumes based on time of day.
BUS_ID=5
INCREMENT=25
function getDellBrightness() {
local busId=${BUS_ID}
echo $(ddcutil --bus=$busId getvcp 10 | sed -n 's/.*current value =[[:space:]]*\(.*\),.*$/\1/p')
}
function setBrightness() {
local level=$1
local busId=${BUS_ID}
# Set Dell brightness
ddcutil --bus=$busId setvcp 10 $level
# set laptop screen
brightnessctl set $level%
}
function decrementBrightness() {
current=$(getDellBrightness)
new=$(python -c "print(max(1, $current - $INCREMENT))")
setBrightness $new
}
function incrementBrightness() {
current=$(getDellBrightness)
new=$(python -c "print(min(100, $current + $INCREMENT))")
setBrightness $new
}
function main() {
action=$1
is_number='^[0-9]+$'
if [ "${action}" == "up" ]; then
incrementBrightness
elif [ "${action}" == "down" ]; then
decrementBrightness
elif [[ "${action}" =~ $is_number ]]; then
setBrightness "${action}"
fi
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment