Skip to content

Instantly share code, notes, and snippets.

@jeremija
Last active December 25, 2015 03:59
Show Gist options
  • Save jeremija/6913898 to your computer and use it in GitHub Desktop.
Save jeremija/6913898 to your computer and use it in GitHub Desktop.
A script which automatically dims the brightness when the laptop is disconnected from charger or sets it to a maximum value if the charger is connected.
#!/bin/bash
# This script automatically dims the brightness when the laptop is disconnected
# from charger or sets it to maximum value if the charger is connected
#
# Usage: place a symbolic link to this file in /etc/pm/power.d/ directory.
# Tested with Xubuntu 13.04 (x64) running on Thinkpad X230.
#
# Author: www.github.com/jeremija
# function for logging. first argument should be the text to log
LOG() {
`logger -t charger_brightness "$1"`
}
ac_online=`cat /sys/class/power_supply/AC/online`
#LOG "ac online status: $ac_online"
backlight="/sys/class/backlight/acpi_video0"
max_brightness=`cat $backlight/max_brightness`
battery_brightness=1
ac_brightness=15
if [ "$ac_online" == "0" ]; then
# transition to battery power
#brightness=$(( battery_brightness * max_brightness / 100 ))
LOG "on battery power"
brightness=$battery_brightness
fi
if [ "$ac_online" == "1" ]; then
# charger was connected
# brightness=$(( ac_brightness * max_brightness / 100 ))
LOG "on ac power"
brightness=$ac_brightness
fi
LOG "setting brightness level to $brightness"
ret=`echo $brightness > $backlight/brightness`
if [ $? != 0 ]; then
LOG "there was an error while trying to set the brightness"
exit 1
fi
LOG "done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment