Skip to content

Instantly share code, notes, and snippets.

@llucax
Last active June 29, 2022 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llucax/3f3a5a7d20d71a1b087b52075f83e218 to your computer and use it in GitHub Desktop.
Save llucax/3f3a5a7d20d71a1b087b52075f83e218 to your computer and use it in GitHub Desktop.
dell-battery
#!/bin/bash
set -eu
cctk=/opt/dell/dcc/cctk
usage_error() {
exec >&2
echo "Error: $@"
echo
echo "Usage: $0 [MODE]"
echo
echo "Where MODE is one of: Standard, PrimAcUse, Adaptive, FullOnce[:<PCT=95>] or "
echo "Custom[:<START=50>-<STOP=75>]."
echo
echo "If Custom is used, the battery will charge only when it's less than <START>%"
echo "full and will stop when it reaches <STOP>%. <START> must be >= 50."
echo
echo "If FullOnce is used, it will charge the battery until <PCT>% one time and then "
echo "set the previous mode again."
echo
echo "If no MODE is specified, the current mode is shown."
exit 2
}
set_mode() {
local mode=$1 pwd=$2
sudo "$cctk" --PrimaryBattChargeCfg="$mode" "${pw:+--ValSetupPwd=$pw}"
}
PREV_MODE=
if test $# -eq 0
then
echo "Options: Standard PrimAcUse Adaptive Custom[:<START=50>-<STOP=80>]"
sudo "$cctk" --PrimaryBattChargeCfg
exit 0
elif test $# -eq 1
then
case "$1" in
Standard|PrimAcUse|Adaptive)
MODE=$1
;;
Custom*)
if test "$1" = "Custom"
then
MODE="$1:50-75"
elif echo "$1" | grep -q '^Custom:[5-9]\?[0-9]-1\?[0-9]\?[0-9]$'
then
MODE="$1"
else
usage_error "Unrecognized format for MODE Custom: $1"
fi
;;
FullOnce*)
PCT=$(echo "$1" | cut -sd: -f2)
if test -z "$PCT"
then
PCT=95
fi
MODE="Standard"
PREV_MODE=$(sudo "$cctk" --PrimaryBattChargeCfg | cut -d= -f2)
;;
*)
usage_error "Unknown MODE: $1"
;;
esac
fi
if ! pw=$(/usr/bin/secret-tool lookup id leela-bios)
then
exec >&2
echo "Error: Failed to get password, please unlock the password" \
"database, or type the BIOS password manually"
exit 1
fi
set_mode "$MODE" "$pw"
if test -n "$PREV_MODE"
then
trap 'r=$?; set_mode "$PREV_MODE" "$pw"; exit $r' EXIT
echo "Waiting until charged to $PCT% ..."
while test "$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 \
| sed -En 's/\s*percentage:\s*([0-9]+)%/\1/p')" \
-lt "$PCT"
do
sleep 60
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment