From c440417ea405edc43dd389e8e2a284b6eb544620 Mon Sep 17 00:00:00 2001 From: denes Date: Sun, 15 Jul 2018 21:56:47 +0200 Subject: [PATCH] Initial commit --- battery_status_led.service | 7 ++++++ battery_status_led.sh | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 battery_status_led.service create mode 100755 battery_status_led.sh diff --git a/battery_status_led.service b/battery_status_led.service new file mode 100644 index 0000000..bc476ce --- /dev/null +++ b/battery_status_led.service @@ -0,0 +1,7 @@ +[Unit] +Description=Battery monitoring on CapsLock LED + +[Service] +ExecStart="/usr/bin/battery_led.sh" +Type=idle + diff --git a/battery_status_led.sh b/battery_status_led.sh new file mode 100755 index 0000000..9164948 --- /dev/null +++ b/battery_status_led.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# limits in percentages +LOW=10 # flip LED every 2s +CRIT=5 # flip led every 0.5s + +BAT_FULL=`cat /sys/class/power_supply/BAT0/energy_full_design` +CAPSLOCK_LED=`cat /sys/class/leds/input4\:\:capslock/brightness` + +LED_DEV=/sys/class/leds/input4\:\:capslock/brightness + + +CNT=0 +while : +do + # If charging, sleep 1m, then restart the cycle + STATUS=`cat /sys/class/power_supply/BAT0/status` + if [ $STATUS == "Charging" ] ; then + echo 0 > $LED_DEV + sleep 1m + continue + fi + + BAT_NOW=`cat /sys/class/power_supply/BAT0/energy_now` + BAT_PCT=$(($BAT_NOW * 100 / $BAT_FULL)) + + # If battery has more than >10% left, sleep 1m then restart the cycle + if (($BAT_PCT > $LOW )) ; then + sleep 1m + continue + fi + + sleep 0.5s + CNT=$((CNT+1)) + + # If battery <5% (crytical) flip capslock LED every 0.5s + if (( $BAT_PCT <= $CRIT )) ; then + CAPSLOCK_LED=$((1 ^ $CAPSLOCK_LED)) + echo $CAPSLOCK_LED > $LED_DEV + # If battery is on low, but not crytical, flip LED every 4th 0.5s (2s) + elif (( $BAT_PCT <= $LOW )) && (( $CNT % 4 == 1 )) ; then + CAPSLOCK_LED=$((1 ^ $CAPSLOCK_LED)) + echo $CAPSLOCK_LED > $LED_DEV + fi + + # not sure if i need this: + if (( $CNT > 100000 )) ; then + CNT=1 + fi +done +