Compare commits

...

3 Commits
v1.9 ... master

Author SHA1 Message Date
Santiago Lo Coco 0c58b2c27d Read before writing smc 2024-04-10 14:09:05 +02:00
Santiago Lo Coco b54f0fbbfd Add log function 2024-04-10 13:58:21 +02:00
Santiago Lo Coco 9204bc1c07 Revert "Remove useless echo"
This reverts commit 4ced07c359.
2024-04-10 13:35:46 +02:00
1 changed files with 15 additions and 7 deletions

View File

@ -9,6 +9,7 @@ PATH="/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/op
usage() { usage() {
cat << EOF cat << EOF
usage: ${0##*/} [command] usage: ${0##*/} [command]
charging [on|off] - Toggle charging charging [on|off] - Toggle charging
adapter [on|off] - Toggle adapter connection adapter [on|off] - Toggle adapter connection
status - Get status information status - Get status information
@ -26,24 +27,31 @@ read_cache() {
cat "$TMP_FILE" 2> /dev/null cat "$TMP_FILE" 2> /dev/null
} }
log_message() {
timestamp=$(date "+%b %d %H:%M:%S")
echo "$timestamp - $1"
}
enable_discharging() { enable_discharging() {
echo "Enabling battery discharging" log_message "Enabling battery discharging"
sudo smc -k CH0I -w 01 sudo smc -k CH0I -w 01
} }
disable_discharging() { disable_discharging() {
echo "Disabling battery discharging" log_message "Disabling battery discharging"
sudo smc -k CH0I -w 00 sudo smc -k CH0I -w 00
} }
enable_charging() { enable_charging() {
echo "Enabling battery charging" [[ "$(get_smc_charging_hex)" == "00" ]] && return
log_message "Enabling battery charging"
sudo smc -k CH0B -w 00 sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00 sudo smc -k CH0C -w 00
} }
disable_charging() { disable_charging() {
echo "Disabling battery charging" [[ "$(get_smc_charging_hex)" != "00" ]] && return
log_message "Disabling battery charging"
sudo smc -k CH0B -w 02 sudo smc -k CH0B -w 02
sudo smc -k CH0C -w 02 sudo smc -k CH0C -w 02
} }
@ -117,7 +125,7 @@ runScript() {
[[ -f "$launch_daemon" ]] && sudo launchctl unload -w "$launch_daemon" [[ -f "$launch_daemon" ]] && sudo launchctl unload -w "$launch_daemon"
;; ;;
"charging") "charging")
echo "Setting $action to $setting" log_message "Setting $action to $setting"
case "$setting" in case "$setting" in
"on") enable_charging && write_cache "enabled" ;; "on") enable_charging && write_cache "enabled" ;;
"off") disable_charging && write_cache "disabled" ;; "off") disable_charging && write_cache "disabled" ;;
@ -125,7 +133,7 @@ runScript() {
esac esac
;; ;;
"adapter") "adapter")
echo "Setting $action to $setting" log_message "Setting $action to $setting"
case "$setting" in case "$setting" in
"on") enable_discharging ;; "on") enable_discharging ;;
"off") disable_discharging ;; "off") disable_discharging ;;
@ -135,7 +143,7 @@ runScript() {
"status") "status")
case "$setting" in case "$setting" in
"charging") exit "$(get_smc_charging_hex)" ;; "charging") exit "$(get_smc_charging_hex)" ;;
"cache") "$(get_cached_status)" ;; "cache") get_cached_status ;;
*) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;; *) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;;
esac esac
;; ;;