You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8 KiB

7 years ago
#!/bin/bash
# Copyright 2018 Denes Matetelki
# This file is part of battery_status_led.
7 years ago
# battery_status_led is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License v3 as published by the Free
# Software Foundation.
7 years ago
# battery_status_led is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License v3 for
# more details.
7 years ago
# You should have received a copy of the GNU General Public License v3 along
# with battery_status_led. If not, see
# https://www.gnu.org/licenses/gpl-3.0.html.
7 years ago
# limits in percentages
LOW=10 # flip LED every 2s
CRIT=5 # flip led every 0.5s
7 years ago
BAT_FULL_DEV=/sys/class/power_supply/BAT0/energy_full_design
BAT_STATUS_DEV=/sys/class/power_supply/BAT0/status
BAT_NOW_DEV=/sys/class/power_supply/BAT0/energy_now
CAPSLOCK_LED_DEV=/sys/class/leds/input4\:\:capslock/brightness
7 years ago
# If not set by the systemd service file
7 years ago
LED_DEV=${BATTERY_STATUS_LED_DEV:-$CAPSLOCK_LED_DEV}
7 years ago
7 years ago
LED=0
7 years ago
while :
do
# If charging, sleep 1m, then restart the cycle
7 years ago
STATUS=cat $BAT_STATUS_DEV
7 years ago
if [ $STATUS == "Charging" ] ; then
echo 0 > $LED_DEV
sleep 1m
continue
fi
7 years ago
BAT_NOW=cat $BAT_NOW_DEV
7 years ago
BAT_PCT=$(($BAT_NOW * 100 / $BAT_FULL))
# If battery has more than >10% left, sleep 1m then restart the cycle
7 years ago
if (($BAT_PCT >= $LOW )) ; then
7 years ago
sleep 1m
continue
fi
7 years ago
# If battery is on low, but not crytical, flip LED every 2s
if (( $BAT_PCT < $LOW )) && (( $BAT_PCT >= $CRIT)) ; then
LED=$((1 ^ $LED))
echo $LED > $LED_DEV
sleep 2s
continue
7 years ago
fi
7 years ago
# If battery is on crytical flip capslock LED every 0.5s
if (( $BAT_PCT < $CRIT )) ; then
LED=$((1 ^ $LED))
echo $LED > $LED_DEV
sleep 0.5s
continue
7 years ago
fi
done