Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General bugfixes #253

Merged
merged 9 commits into from
Jun 28, 2024
69 changes: 54 additions & 15 deletions battery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,23 @@ function log() {
echo -e "$(date +%D-%T) - $1"
}

function validate_percentage() {
log "Validating $1"
if ! [[ "$1" =~ ^[0-9]+$ ]] || [[ "$1" -lt 0 ]] || [[ "$1" -gt 100 ]]; then
echo false
else
echo true
fi
}

## #################
## SMC Manipulation
## #################

# Change magsafe color
# see community sleuthing: https://github.com/actuallymentor/battery/issues/71
function change_magsafe_led_color() {
log "MagSafe LED function invoked"
color=$1

# Check whether user can run color changes without password (required for backwards compatibility)
Expand All @@ -135,11 +145,14 @@ function change_magsafe_led_color() {
fi

if [[ "$color" == "green" ]]; then
log "setting LED to green"
sudo smc -k ACLC -w 03
elif [[ "$color" == "orange" ]]; then
log "setting LED to orange"
sudo smc -k ACLC -w 04
else
# Default action: reset. Value 00 is a guess and needs confirmation
log "resetting LED"
sudo smc -k ACLC -w 00
fi
}
Expand All @@ -158,16 +171,26 @@ function disable_discharging() {
# Keep track of status
is_charging=$(get_smc_charging_status)

if [[ "$battery_percentage" -ge "$setting" && "$is_charging" == "enabled" ]]; then
if ! validate_percentage "$setting"; then

log "Disabling discharging: No valid maintain percentage set, enabling charging"
# use direct commands since enable_charging also calls disable_discharging, and causes an eternal loop
sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00
change_magsafe_led_color "orange"

elif [[ "$battery_percentage" -ge "$setting" && "$is_charging" == "enabled" ]]; then

log "Charge above $setting"
log "Disabling discharging: Charge above $setting, disabling charging"
disable_charging
change_magsafe_led_color "green"

elif [[ "$battery_percentage" -lt "$setting" && "$is_charging" == "disabled" ]]; then

log "Charge below $setting"
enable_charging
log "Disabling discharging: Charge below $setting, enabling charging"
# use direct commands since enable_charging also calls disable_discharging, and causes an eternal loop
sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00
change_magsafe_led_color "orange"

fi
Expand Down Expand Up @@ -346,6 +369,9 @@ if [[ "$action" == "charging" ]]; then
enable_charging
elif [[ "$setting" == "off" ]]; then
disable_charging
else
log "Error: $setting is not \"on\" or \"off\"."
exit 1
fi

exit 0
Expand All @@ -365,6 +391,9 @@ if [[ "$action" == "adapter" ]]; then
disable_discharging
elif [[ "$setting" == "off" ]]; then
enable_discharging
else
log "Error: $setting is not \"on\" or \"off\"."
exit 1
fi

exit 0
Expand All @@ -374,10 +403,9 @@ fi
# Charging on/off controller
if [[ "$action" == "charge" ]]; then

# Check if percentage is an integer [1-100]
if ! [[ $setting =~ ^[1-9][0-9]?$|^100$ ]]; then
log "Specified percentage ($setting) is not valid. Please specify an integer [1-100]."
exit 1
if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery charge. Please use a number between 0 and 100"
exit 1
fi

# Disable running daemon
Expand All @@ -389,14 +417,16 @@ if [[ "$action" == "charge" ]]; then
# Start charging
battery_percentage=$(get_battery_percentage)
log "Charging to $setting% from $battery_percentage%"
enable_charging
enable_charging # also disables discharging

# Loop until battery percent is exceeded
while [[ "$battery_percentage" -lt "$setting" ]]; do

log "Battery at $battery_percentage%"
caffeinate -is sleep 60
battery_percentage=$(get_battery_percentage)
if [[ "$battery_percentage" -ge "$((setting-3))" ]]; then
sleep 20
else
caffeinate -is sleep 60
fi

done

Expand All @@ -410,6 +440,11 @@ fi
# Discharging on/off controller
if [[ "$action" == "discharge" ]]; then

if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery discharge. Please use a number between 0 and 100"
exit 1
fi

# Start charging
battery_percentage=$(get_battery_percentage)
log "Discharging to $setting% from $battery_percentage%"
Expand All @@ -432,6 +467,11 @@ fi
# Maintain at level
if [[ "$action" == "maintain_synchronous" ]]; then

if ! validate_percentage "$setting"; then
log "Error: $setting is not a valid setting for battery maintain. Please use a number between 0 and 100"
exit 1
fi

# Recover old maintain status if old setting is found
if [[ "$setting" == "recover" ]]; then

Expand Down Expand Up @@ -507,13 +547,12 @@ if [[ "$action" == "maintain" ]]; then
rm $pidfile 2>/dev/null
battery disable_daemon
enable_charging
change_magsafe_led_color
battery status
exit 0
fi

# Check if setting is value between 0 and 100
if ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 0 ]] || [[ "$setting" -gt 100 ]]; then
if ! validate_percentage "$setting"; then

log "Called with $setting $action"
# If non 0-100 setting is not a special keyword, exit with an error.
Expand Down Expand Up @@ -657,4 +696,4 @@ if [[ "$action" == "logs" ]]; then

exit 0

fi
fi