Skip to content

Instantly share code, notes, and snippets.

@fcwu
Created June 27, 2013 08:47
Show Gist options
  • Save fcwu/5874993 to your computer and use it in GitHub Desktop.
Save fcwu/5874993 to your computer and use it in GitHub Desktop.
Linux power statistics
#!/bin/sh
echo "current average capacity full_capacity time_left"
factor=0.7
last_pwr=0
charge_full=`cat /sys/class/power_supply/BAT0/charge_full`
voltage_min_design=`cat /sys/class/power_supply/BAT0/voltage_min_design`
full_pwr=`echo "scale=5; $charge_full * $voltage_min_design / 1000000 / 1000000" | bc`
while [ 1 ]; do
voltage_now=`cat /sys/class/power_supply/BAT0/voltage_now`
current_now=`cat /sys/class/power_supply/BAT0/current_now`
charge_now=`cat /sys/class/power_supply/BAT0/charge_now`
voltage_now=`cat /sys/class/power_supply/BAT0/voltage_now`
cur_pwr=`echo "scale=5; $voltage_now * $current_now / 1000000 / 1000000" | bc`
if [ "$last_pwr" != "0" ]; then
last_pwr=`echo "scale=5; $last_pwr * $factor + $cur_pwr * (1.0 - $factor)" | bc`
else
last_pwr=$cur_pwr
fi
rest_pwr=`echo "scale=5; $voltage_min_design * $charge_now / 1000000 / 1000000" | bc`
time_left_h=`echo "scale=2; $rest_pwr / $last_pwr" | bc`
echo $cur_pwr " " $last_pwr " " $rest_pwr " " $full_pwr " " $time_left_h
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment