aboutsummaryrefslogtreecommitdiffstats
path: root/bin/battery-notify.sh
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2024-12-25 14:26:51 +0000
committerLeonardo Bishop <me@leonardobishop.com>2024-12-25 14:26:51 +0000
commitdc41873a79b32ed3d34dd7dac09c081f7472d207 (patch)
treeba3c80c8d137263a8f346cf32853d15f75094fab /bin/battery-notify.sh
parentf4c682dc6bdeebe9f0c6a20deb7092cf893791a1 (diff)
Change most things to symlinks instead of hardlinks
Diffstat (limited to 'bin/battery-notify.sh')
-rwxr-xr-xbin/battery-notify.sh78
1 files changed, 69 insertions, 9 deletions
diff --git a/bin/battery-notify.sh b/bin/battery-notify.sh
index ed07119..a2a6e23 100755
--- a/bin/battery-notify.sh
+++ b/bin/battery-notify.sh
@@ -1,14 +1,55 @@
-#!/bin/sh
+#!/bin/bash
last="NONE"
+nid=0
-low=15
+low=10
critical=5
+polltime=2
+
bat="BAT1"
-while true; do
+str_time () {
+ h=$(bc <<< "${1}/3600")
+ m=$(bc <<< "(${1}%3600)/60")
+ s=$(bc <<< "${1}%60")
+
+ printf "%d h %02d min %02.0f sec\n" $h $m $s
+}
+
+
+calc_time_left () {
+ voltage_now=$(cat "$1/voltage_now")
+ charge_now=$(cat "$1/charge_now")
+ current_now=$(cat "$1/current_now")
+
+ energy=$(bc -l <<< "($voltage_now * $charge_now) / 1000000")
+ power=$(bc -l <<< "($voltage_now * $current_now) / 1000000")
+ hours=$(bc -l <<< "$energy / $power")
+
+ echo $(bc -l <<< "$hours * 3600")
+}
+
+
+dismiss_last_notification () {
+ if [[ "$nid" -ne "0" ]]; then
+ echo "poof"
+ notify-send -r $nid -t 1 " "
+ nid=0
+ fi
+}
+
+send_power_warning () {
+ time_left=$(str_time $(calc_time_left $2))
+ if [[ "$nid" -ne "0" ]]; then
+ nid=$(notify-send -p -r $nid -u critical -t 0 "Power" "Battery $1: $3%\nRemaining: $time_left")
+ else
+ nid=$(notify-send -p -u critical -t 0 "Power" "Battery $1: $3%\nRemaining: $time_left")
+ fi
+}
+while true; do
battery="/sys/class/power_supply/$bat"
if [ -d $battery ]; then
@@ -16,22 +57,41 @@ while true; do
status=$(cat $battery/status)
if [ "$last" != "FULL" ] && [ "$status" = "Full" ]; then
+ dismiss_last_notification
notify-send "Power" "Battery is full"
last="FULL"
fi
# If low and discharging
- if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && \
- [ $capacity -le $low ]; then
- notify-send "Power" "Battery warning: $capacity%"
- last=LOW
+ if [ "$last" != "CRITICAL" ] && [ "$status" = "Discharging" ] && [ $capacity -le $low ]; then
+ send_power_warning "warning" $battery $capacity
+ last=LOW polltime=2
fi
# If critical and discharging
if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then
- notify-send "Power" "Battery critical: $capacity%"
+ send_power_warning "critical" $battery $capacity
last=CRITICAL
+ polltime=2
+ fi
+
+ # If charging
+ if [ "$status" = "Charging" ] && [ "$last" != "CHARGING" ]; then
+ dismiss_last_notification
+ notify-send "Power" "Battery charging: $capacity%"
+ last=CHARGING
+ polltime=2
+ fi
+
+ # If disharging
+ if [ "$status" = "Discharging" ] && [ "$last" != "LOW" ] && [ "$last" != "CRITICAL" ] && [ "$last" != "DISCHARGING" ]; then
+ time_left=$(str_time $(calc_time_left $battery))
+ notify-send "Power" "Battery discharging: $capacity%"
+ last=DISCHARGING
+ polltime=2
fi
+ else
+ sleep 60
fi
- sleep 60
+ sleep $polltime
done