Last active
March 6, 2020 09:42
-
-
Save NikkSaan/800c17d87dddfaa9edf5274130717d73 to your computer and use it in GitHub Desktop.
Battery protection for the nokia n900 running PostmarketOS
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/sh | |
# Battery protection for the nokia n900 | |
# When battery level falls below 8% gives led notification. | |
# When battery level falls below 6% powers off the device. | |
# | |
# To use it, place the script in `/sbin`, make it executable | |
# `sudo chmod +x /sbin/n900-battery-monitor`, then add | |
# `::respawn:/sbin/n900-battery-monitor` to /etc/inittab, | |
# or create a service for it. | |
# Disable battery charger's status pin | |
echo 0 > /sys/class/power_supply/bq24150a-0/stat_pin_enable | |
function alert_low_bat { | |
LED="/sys/class/leds/lp5523:r/brightness" | |
while true; do | |
echo 50 > $LED | |
sleep 0.1 | |
echo 0 > $LED | |
sleep 0.1 | |
echo 50 > $LED | |
sleep 0.5 | |
echo 0 > $LED | |
sleep 0.5 | |
done | |
} | |
ALERT_PID="" | |
while true; do | |
CAPACITY=$(cat /sys/class/power_supply/bq27200-0/capacity) | |
STATUS=$(cat /sys/class/power_supply/bq27200-0/status) | |
if [ $CAPACITY -le 8 ] && | |
[ "$STATUS" = "Not charging" ] && | |
[ "$ALERT_PID" = "" ]; | |
then | |
alert_low_bat & | |
ALERT_PID=$! | |
elif [ $CAPACITY -gt 8 ] || | |
[ "$STATUS" = "Charging" ] && | |
[ "$ALERT_PID" != "" ]; | |
then | |
kill $ALERT_PID | |
ALERT_PID="" | |
LED="/sys/class/leds/lp5523:r/brightness" | |
echo 0 > $LED | |
elif [ $CAPACITY -le 6 ] && | |
[ "$STATUS" = "Not charging" ]; | |
then | |
/sbin/poweroff | |
fi | |
sleep 60 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment