diff options
| -rwxr-xr-x | .local/bin/bat-thresh-toggle | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/.local/bin/bat-thresh-toggle b/.local/bin/bat-thresh-toggle new file mode 100755 index 0000000..2819132 --- /dev/null +++ b/.local/bin/bat-thresh-toggle | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | THRESH_START_PATH="/sys/class/power_supply/BAT0/charge_control_start_threshold" | ||
| 4 | THRESH_STOP_PATH="/sys/class/power_supply/BAT0/charge_control_end_threshold" | ||
| 5 | TLP_DROP_IN="/etc/tlp.d/05-bat-thresh.conf" | ||
| 6 | |||
| 7 | TRAVEL_START=85 | ||
| 8 | TRAVEL_STOP=99 | ||
| 9 | DOCKED_START=75 | ||
| 10 | DOCKED_STOP=89 | ||
| 11 | DEFAULT_START=96 | ||
| 12 | DEFAULT_STOP=100 | ||
| 13 | |||
| 14 | usage() { | ||
| 15 | cat <<EOF | ||
| 16 | usage: $(basename "$0") [OPTION] | ||
| 17 | |||
| 18 | toggles BAT0 charge thresholds between travel and docked modes, | ||
| 19 | writing a persistent TLP drop-in to ${TLP_DROP_IN}. | ||
| 20 | |||
| 21 | options: | ||
| 22 | (none) toggle between travel (${TRAVEL_START}/${TRAVEL_STOP}%) and docked (${DOCKED_START}/${DOCKED_STOP}%) | ||
| 23 | --system-default restore TLP built-in defaults (${DEFAULT_START}/${DEFAULT_STOP}%) and remove drop-in | ||
| 24 | -h, --help show this help message and exit | ||
| 25 | |||
| 26 | modes: | ||
| 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} | ||
| 29 | system-default START_CHARGE_THRESH_BAT0=${DEFAULT_START} STOP_CHARGE_THRESH_BAT0=${DEFAULT_STOP} | ||
| 30 | |||
| 31 | EOF | ||
| 32 | } | ||
| 33 | |||
| 34 | case "${1}" in | ||
| 35 | -h|--help) usage; exit 0 ;; | ||
| 36 | esac | ||
| 37 | |||
| 38 | # gotta be root to write to sysfs | ||
| 39 | if [ "$(id -u)" -ne 0 ]; then | ||
| 40 | usage | ||
| 41 | sleep 1 | ||
| 42 | echo "error: script must be run as root."; echo "" | ||
| 43 | exit 1 | ||
| 44 | fi | ||
| 45 | |||
| 46 | # make sure the sysfs nodes are actually there | ||
| 47 | [ -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; } | ||
| 49 | |||
| 50 | # --system-default: remove drop-in and restore TLP built-in defaults (96/100) | ||
| 51 | if [ "${1}" = "--system-default" ]; 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 | ||
| 63 | fi | ||
| 64 | |||
| 65 | current_stop=$(cat "$THRESH_STOP_PATH") | ||
| 66 | |||
| 67 | # determine which mode to switch INTO based on the current stop threshold | ||
| 68 | case "$current_stop" in | ||
| 69 | "$TRAVEL_STOP") | ||
| 70 | target_mode="docked" | ||
| 71 | new_start="$DOCKED_START" | ||
| 72 | new_stop="$DOCKED_STOP" | ||
| 73 | ;; | ||
| 74 | *) | ||
| 75 | target_mode="travel" | ||
| 76 | new_start="$TRAVEL_START" | ||
| 77 | new_stop="$TRAVEL_STOP" | ||
| 78 | ;; | ||
| 79 | esac | ||
| 80 | |||
| 81 | # the kernel enforces start < stop at all times, so write order matters | ||
| 82 | case "$target_mode" in | ||
| 83 | travel) | ||
| 84 | 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; } | ||
| 86 | ;; | ||
| 87 | docked) | ||
| 88 | printf "%s" "$new_start" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; } | ||
| 89 | printf "%s" "$new_stop" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; } | ||
| 90 | ;; | ||
| 91 | esac | ||
| 92 | |||
| 93 | echo "switched to $target_mode mode — start: ${new_start}% stop: ${new_stop}%" | ||
| 94 | |||
| 95 | # write a dedicated drop-in so thresholds survive a reboot; no sed fragility | ||
| 96 | if [ -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' \ | ||
| 98 | "$new_start" "$new_stop" > "$TLP_DROP_IN" \ | ||
| 99 | && echo "${TLP_DROP_IN} written" \ | ||
| 100 | || echo "error: failed to write ${TLP_DROP_IN}." | ||
| 101 | else | ||
| 102 | echo "note: /etc/tlp.d not found - sysfs write is not persistent across reboots." | ||
| 103 | fi \ No newline at end of file | ||
