From b8fd9cd0dc73f28c3b4f7e0faf6c5568550e404d Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Thu, 19 Feb 2026 22:35:53 -0500 Subject: feat: new thinkpad battery script --- .local/bin/bat-thresh-toggle | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 .local/bin/bat-thresh-toggle 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 @@ +#!/bin/sh + +THRESH_START_PATH="/sys/class/power_supply/BAT0/charge_control_start_threshold" +THRESH_STOP_PATH="/sys/class/power_supply/BAT0/charge_control_end_threshold" +TLP_DROP_IN="/etc/tlp.d/05-bat-thresh.conf" + +TRAVEL_START=85 +TRAVEL_STOP=99 +DOCKED_START=75 +DOCKED_STOP=89 +DEFAULT_START=96 +DEFAULT_STOP=100 + +usage() { + cat < "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; } + printf "%s" "$DEFAULT_START" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; } + echo "restored system defaults — start: ${DEFAULT_START}% stop: ${DEFAULT_STOP}%" + if [ -f "$TLP_DROP_IN" ]; then + rm "$TLP_DROP_IN" \ + && echo "${TLP_DROP_IN} removed — TLP will use its built-in defaults after reboot." \ + || echo "error: failed to remove ${TLP_DROP_IN}." + else + echo "note: ${TLP_DROP_IN} was not present — nothing to remove." + fi + exit 0 +fi + +current_stop=$(cat "$THRESH_STOP_PATH") + +# determine which mode to switch INTO based on the current stop threshold +case "$current_stop" in + "$TRAVEL_STOP") + target_mode="docked" + new_start="$DOCKED_START" + new_stop="$DOCKED_STOP" + ;; + *) + target_mode="travel" + new_start="$TRAVEL_START" + new_stop="$TRAVEL_STOP" + ;; +esac + +# the kernel enforces start < stop at all times, so write order matters +case "$target_mode" in + travel) + printf "%s" "$new_stop" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; } + printf "%s" "$new_start" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; } + ;; + docked) + printf "%s" "$new_start" > "$THRESH_START_PATH" || { echo "error: failed to write start threshold."; exit 1; } + printf "%s" "$new_stop" > "$THRESH_STOP_PATH" || { echo "error: failed to write stop threshold."; exit 1; } + ;; +esac + +echo "switched to $target_mode mode — start: ${new_start}% stop: ${new_stop}%" + +# write a dedicated drop-in so thresholds survive a reboot; no sed fragility +if [ -d "/etc/tlp.d" ]; then + printf '# managed by bat-thresh-toggle — do not edit by hand\nSTART_CHARGE_THRESH_BAT0=%s\nSTOP_CHARGE_THRESH_BAT0=%s\n' \ + "$new_start" "$new_stop" > "$TLP_DROP_IN" \ + && echo "${TLP_DROP_IN} written" \ + || echo "error: failed to write ${TLP_DROP_IN}." +else + echo "note: /etc/tlp.d not found - sysfs write is not persistent across reboots." +fi \ No newline at end of file -- cgit v1.2.3