diff options
| -rwxr-xr-x | .local/bin/bat-thresh-toggle | 61 |
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" | |||
| 4 | THRESH_STOP_PATH="/sys/class/power_supply/BAT0/charge_control_end_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" | 5 | TLP_DROP_IN="/etc/tlp.d/05-bat-thresh.conf" |
| 6 | 6 | ||
| 7 | TRAVEL_START=85 | ||
| 8 | TRAVEL_STOP=97 | ||
| 9 | DOCKED_START=75 | 7 | DOCKED_START=75 |
| 10 | DOCKED_STOP=87 | 8 | DOCKED_STOP=87 |
| 11 | DEFAULT_START=96 | 9 | DEFAULT_START=96 |
| @@ -15,18 +13,15 @@ usage() { | |||
| 15 | cat <<EOF | 13 | cat <<EOF |
| 16 | usage: $(basename "$0") [OPTION] | 14 | usage: $(basename "$0") [OPTION] |
| 17 | 15 | ||
| 18 | toggles BAT0 charge thresholds between travel and docked modes, | 16 | toggles BAT0 charge thresholds between docked and default modes, |
| 19 | writing a persistent TLP drop-in to ${TLP_DROP_IN}. | 17 | writing or removing a persistent TLP drop-in at ${TLP_DROP_IN}. |
| 20 | 18 | ||
| 21 | options: | 19 | options: |
| 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 | ||
| 26 | modes: | 22 | 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} | 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 | ||
| 31 | EOF | 26 | EOF |
| 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) | ||
| 51 | if [ "${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 | ||
| 63 | fi | ||
| 64 | |||
| 65 | current_stop=$(cat "$THRESH_STOP_PATH") | 45 | current_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 |
| 68 | case "$current_stop" in | 48 | case "$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 | ;; | ||
| 79 | esac | 59 | esac |
| 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 |
| 82 | case "$target_mode" in | 62 | case "$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 | ||
| 93 | echo "switched to $target_mode mode — start: ${new_start}% stop: ${new_stop}%" | 73 | echo "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 |
| 96 | if [ -d "/etc/tlp.d" ]; then | 76 | 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' \ | 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 | ||
| 101 | else | 92 | else |
| 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." |
| 103 | fi | 94 | fi |
