You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.6 KiB

+++
title = 'Backlight control on a Lenovo X1 Carbon'
aliases = '/posts/2023-05-28-backlight-control-on-a-lenovo-x1-carbon'
summary = '''These two scripts let the X1 switch to a predefined setting for
battery or ac mode. It also remembers the last used setting for either mode.'''
date = '2023-05-28T17:35:45+02:00'
# lastmod = ''
categories = [ 'computerstuff' ]
tags = [ 'freebsd', 'scripting' ]
+++
I like to have my computer reduce the screen brightness when it's not
connected to an external power source.
## powersave.sh
This is (mainly) used to control Intel SpeedStep®. I tend to save my custom
applications and scripts in `$HOME/bin`. Saved as `$HOME/bin/powersave.sh`.
```sh
#! /bin/sh
case $( sysctl -n hw.acpi.acline ) in
(0) # BATTERY
doas sysctl dev.hwpstate_intel.0.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.1.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.2.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.3.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.4.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.5.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.6.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.7.epp=100 1> /dev/null 2> /dev/null
#backlight 20 1> /dev/null 2> /dev/null
backlight $(cat ${HOME}/.backlight-bat) 1> /dev/null 2> /dev/null
;;
(1) # AC
doas sysctl dev.hwpstate_intel.0.epp=0 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.1.epp=50 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.2.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.3.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.4.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.5.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.6.epp=100 1> /dev/null 2> /dev/null
doas sysctl dev.hwpstate_intel.7.epp=100 1> /dev/null 2> /dev/null
#backlight 100 1> /dev/null 2> /dev/null
backlight $(cat ${HOME}/.backlight-ac) 1> /dev/null 2> /dev/null
;;
esac
```
The script is run by a line in my personal cron table. A little delay (up to
a minute) is expected.
```crontab
* * * * * ~/bin/powersave.sh
```
## xbl
A script to modify different backlight settings depending on the connected
power source. Saved as `$HOME/bin/xbl`.
```sh
#! /bin/sh
print_usage () {
echo >&2 "usage: $(basename ${0}) [0..100]"
exit 1
}
# check if argument given or not (list or set value)
if [ $# -eq 1 ]
then
# set value (select between ac or bat mode)
# check if argument is integer between 0,100
if [ "$1" -eq "$1" ] 2>/dev/null
then
if [ "$1" -ge 0 ] && [ "$1" -le 100 ] 2>/dev/null
then
# argument given and between 0,100
case $( sysctl -n hw.acpi.acline ) in
(0) # BATTERY
echo "$1" > ${HOME}/.backlight-bat
;;
(1) # AC
echo "$1" > ${HOME}/.backlight-ac
;;
esac
backlight "$1"
else
# arg not between 0 and 100
print_usage
fi
else
# arg not an integer
print_usage
fi
else
# no args given, only list values
case $( sysctl -n hw.acpi.acline ) in
(0) # BATTERY
current=bat
;;
(1) # AC
current=ac
;;
esac
for status in ac bat
do
if [ "${status}" = "${current}" ]
then
echo -e "${status}: ★ \t$( cat ${HOME}/.backlight-${status} )" | tr "[:lower:]" "[:upper:]"
else
echo -e "${status}:\t$( cat ${HOME}/.backlight-${status} )" | tr "[:lower:]" "[:upper:]"
fi
done
fi
```