Last active
August 6, 2021 20:04
-
-
Save 3nt3/a7cc5940230cd1a6014ad9d0d3aa6a1d to your computer and use it in GitHub Desktop.
bash script to warn you when your battery is low and play sounds when the charging connector is pluggin in/out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
get_current() { | |
bat0=$(cat /sys/class/power_supply/BAT0/capacity) | |
bat1=$(cat /sys/class/power_supply/BAT1/capacity) | |
echo $bat0 + $bat1 - 5 | bc # -5 because that's what the driver will say when the battery is unusably low | |
} | |
warn() { | |
charging_status=$(cat /sys/class/power_supply/AC/online) | |
last_charging_status=$charging_status | |
current=$(get_current) | |
last=$(echo $current + 1 | bc) | |
while :; do | |
# if charging | |
if [ $charging_status == "0" ]; then | |
if [ $current -lt 15 ]; then | |
mplayer ~/.local/share/batwarn/ping.webm | |
notify-send batwarn "only $current% left" | |
google_speech "only $current% battery charge left" | |
sleep 60 | |
fi | |
fi | |
# do this. | |
last=$current | |
current=$(get_current) | |
last_charging_status=$charging_status | |
charging_status=$(cat /sys/class/power_supply/AC/online) | |
sleep .1 | |
done | |
} | |
charging() { | |
c_charging_status=$(cat /sys/class/power_supply/AC/online) | |
c_last_charging_status=$c_charging_status | |
while :; do | |
# if charging | |
if [ "$c_charging_status" != "$c_last_charging_status" ]; then | |
if [ "$c_charging_status" == "1" ]; then | |
echo connected | |
notify-send batwarn "connecting connector connected" | |
mpv ~/.local/share/batwarn/charging.ogg >/dev/null & | |
elif [ "$c_charging_status" == "0" ]; then | |
echo disconnected | |
notify-send batwarn "connecting connector disconnected" | |
mpv ~/.local/share/batwarn/plugged_out.ogg >/dev/null & | |
fi | |
fi | |
c_last_charging_status="$c_charging_status" | |
c_charging_status=$(cat /sys/class/power_supply/AC/online) | |
sleep .1 | |
done | |
} | |
charging & | |
warn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment