summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x.local/bin/bat-thresh-toggle61
1 files changed, 26 insertions, 35 deletions
diff --git a/.local/bin/bat-thresh-toggle b/.local/bin/bat-thresh-toggle
index cc5ca33..7992cfe 100755
--- a/.local/bin/bat-thresh-toggle
+++ b/.local/bin/bat-thresh-toggle
@@ -4,8 +4,6 @@ THRESH_START_PATH="/sys/class/power_supply/BAT0/charge_control_start_threshold"
4THRESH_STOP_PATH="/sys/class/power_supply/BAT0/charge_control_end_threshold" 4THRESH_STOP_PATH="/sys/class/power_supply/BAT0/charge_control_end_threshold"
5TLP_DROP_IN="/etc/tlp.d/05-bat-thresh.conf" 5TLP_DROP_IN="/etc/tlp.d/05-bat-thresh.conf"
6 6
7TRAVEL_START=85
8TRAVEL_STOP=97
9DOCKED_START=75 7DOCKED_START=75
10DOCKED_STOP=87 8DOCKED_STOP=87
11DEFAULT_START=96 9DEFAULT_START=96
@@ -15,18 +13,15 @@ usage() {
15 cat <<EOF 13 cat <<EOF
16usage: $(basename "$0") [OPTION] 14usage: $(basename "$0") [OPTION]
17 15
18toggles BAT0 charge thresholds between travel and docked modes, 16toggles BAT0 charge thresholds between docked and default modes,
19writing a persistent TLP drop-in to ${TLP_DROP_IN}. 17writing or removing a persistent TLP drop-in at ${TLP_DROP_IN}.
20 18
21options: 19options:
22 (none) toggle between travel (${TRAVEL_START}/${TRAVEL_STOP}%) and docked (${DOCKED_START}/${DOCKED_STOP}%)
23 -d restore TLP built-in defaults (${DEFAULT_START}/${DEFAULT_STOP}%) and remove drop-in
24 -h, --help show this help message and exit 20 -h, --help show this help message and exit
25 21
26modes: 22modes:
27 travel START_CHARGE_THRESH_BAT0=${TRAVEL_START} STOP_CHARGE_THRESH_BAT0=${TRAVEL_STOP}
28 docked START_CHARGE_THRESH_BAT0=${DOCKED_START} STOP_CHARGE_THRESH_BAT0=${DOCKED_STOP} 23 docked START_CHARGE_THRESH_BAT0=${DOCKED_START} STOP_CHARGE_THRESH_BAT0=${DOCKED_STOP}
29 default (-d) START_CHARGE_THRESH_BAT0=${DEFAULT_START} STOP_CHARGE_THRESH_BAT0=${DEFAULT_STOP} 24 default START_CHARGE_THRESH_BAT0=${DEFAULT_START} STOP_CHARGE_THRESH_BAT0=${DEFAULT_STOP}
30 25
31EOF 26EOF
32} 27}
@@ -47,40 +42,25 @@ fi
47[ -f "$THRESH_START_PATH" ] || { echo "error: $THRESH_START_PATH not found — is thinkpad_acpi loaded?"; exit 1; } 42[ -f "$THRESH_START_PATH" ] || { echo "error: $THRESH_START_PATH not found — is thinkpad_acpi loaded?"; exit 1; }
48[ -f "$THRESH_STOP_PATH" ] || { echo "error: $THRESH_STOP_PATH not found — is thinkpad_acpi loaded?"; exit 1; } 43[ -f "$THRESH_STOP_PATH" ] || { echo "error: $THRESH_STOP_PATH not found — is thinkpad_acpi loaded?"; exit 1; }
49 44
50# --system-default: remove drop-in and restore TLP built-in defaults (96/100)
51if [ "${1}" = "-d" ]; then
52 printf "%s" "$DEFAULT_STOP" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; }
53 printf "%s" "$DEFAULT_START" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; }
54 echo "restored system defaults — start: ${DEFAULT_START}% stop: ${DEFAULT_STOP}%"
55 if [ -f "$TLP_DROP_IN" ]; then
56 rm "$TLP_DROP_IN" \
57 && echo "${TLP_DROP_IN} removed — TLP will use its built-in defaults after reboot." \
58 || echo "error: failed to remove ${TLP_DROP_IN}."
59 else
60 echo "note: ${TLP_DROP_IN} was not present — nothing to remove."
61 fi
62 exit 0
63fi
64
65current_stop=$(cat "$THRESH_STOP_PATH") 45current_stop=$(cat "$THRESH_STOP_PATH")
66 46
67# determine which mode to switch INTO based on the current stop threshold 47# determine which mode to switch INTO based on the current stop threshold
68case "$current_stop" in 48case "$current_stop" in
69 "$TRAVEL_STOP") 49 "$DOCKED_STOP")
50 target_mode="default"
51 new_start="$DEFAULT_START"
52 new_stop="$DEFAULT_STOP"
53 ;;
54 *)
70 target_mode="docked" 55 target_mode="docked"
71 new_start="$DOCKED_START" 56 new_start="$DOCKED_START"
72 new_stop="$DOCKED_STOP" 57 new_stop="$DOCKED_STOP"
73 ;; 58 ;;
74 *)
75 target_mode="travel"
76 new_start="$TRAVEL_START"
77 new_stop="$TRAVEL_STOP"
78 ;;
79esac 59esac
80 60
81# the kernel enforces start < stop at all times, so write order matters 61# the kernel enforces start < stop at all times, so write order matters
82case "$target_mode" in 62case "$target_mode" in
83 travel) 63 default)
84 printf "%s" "$new_stop" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; } 64 printf "%s" "$new_stop" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; }
85 printf "%s" "$new_start" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; } 65 printf "%s" "$new_start" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; }
86 ;; 66 ;;
@@ -92,12 +72,23 @@ esac
92 72
93echo "switched to $target_mode mode — start: ${new_start}% stop: ${new_stop}%" 73echo "switched to $target_mode mode — start: ${new_start}% stop: ${new_stop}%"
94 74
95# write a dedicated drop-in so thresholds survive a reboot; no sed fragility 75# write or remove drop-in so thresholds survive a reboot; no sed fragility
96if [ -d "/etc/tlp.d" ]; then 76if [ -d "/etc/tlp.d" ]; then
97 printf '# managed by bat-thresh-toggle — do not edit by hand\nSTART_CHARGE_THRESH_BAT0=%s\nSTOP_CHARGE_THRESH_BAT0=%s\n' \ 77 case "$target_mode" in
98 "$new_start" "$new_stop" > "$TLP_DROP_IN" \ 78 docked)
99 && echo "${TLP_DROP_IN} written" \ 79 printf '# managed by bat-thresh-toggle — do not edit by hand\nSTART_CHARGE_THRESH_BAT0=%s\nSTOP_CHARGE_THRESH_BAT0=%s\n' \
100 || echo "error: failed to write ${TLP_DROP_IN}." 80 "$new_start" "$new_stop" > "$TLP_DROP_IN" \
81 && echo "${TLP_DROP_IN} written" \
82 || echo "error: failed to write ${TLP_DROP_IN}."
83 ;;
84 default)
85 if [ -f "$TLP_DROP_IN" ]; then
86 rm "$TLP_DROP_IN" \
87 && echo "${TLP_DROP_IN} removed — TLP will use its built-in defaults after reboot." \
88 || echo "error: failed to remove ${TLP_DROP_IN}."
89 fi
90 ;;
91 esac
101else 92else
102 echo "note: /etc/tlp.d not found - sysfs write is not persistent across reboots." 93 echo "note: /etc/tlp.d not found - sysfs write is not persistent across reboots."
103fi 94fi