Code Sample |
#!/usr/bin/perl # # This is somem messy Perl script, but hey, # it works. If you somehow stuble upon this # code, then please note that you may have to # change certain paths, such as BAT0, depending # on how many batteries you have. Also, note # that this script has only been tested with # ACPI and not APM. # # Enjoy! :-) # # written by ateam (derek@backdrifts.net) # # modified for 2 batteries support by hs7sv (paphons@gmail.com) ##### Remain Capacity 1 ##### chop (@info = `cat /proc/acpi/battery/BAT0/state`); chop (@capacity = `cat /proc/acpi/battery/BAT0/info`); $state = @info[2]; chop ($state1 = `echo \"$state\" | awk '{ print \$3 }'`); chop ($left = @info[4]); chop ($left1 = `echo \"$left\" | awk '{ print \$3 }'`); chop ($full = @capacity[1]); chop ($full1 = `echo \"$full\" | awk '{ print \$3 }'`); ##### Remain Capacity 2 ##### chop (@info = `cat /proc/acpi/battery/BAT1/state`); chop (@capacity = `cat /proc/acpi/battery/BAT1/info`); $state = @info[2]; chop ($state2 = `echo \"$state\" | awk '{ print \$3 }'`); chop ($left = @info[4]); chop ($left2 = `echo \"$left\" | awk '{ print \$3 }'`); chop ($full = @capacity[1]); chop ($full2 = `echo \"$full\" | awk '{ print \$3 }'`); ##### Remain Time 1 ##### chop (@time = `cat /proc/acpi/battery/BAT0/state`); $rcap = @time[4]; chop ($rcap1 = `echo \"$rcap\" | awk '{ print \$3 }'`); $prate = @time[3]; chop ($prate1 = `echo \"$prate\" | awk '{ print \$3 }'`); ##### Remain Time 2 ##### chop (@time = `cat /proc/acpi/battery/BAT1/state`); $rcap = @time[4]; chop ($rcap2 = `echo \"$rcap\" | awk '{ print \$3 }'`); $prate = @time[3]; chop ($prate2 = `echo \"$prate\" | awk '{ print \$3 }'`); ##### Combination ##### if ($state1 eq "charging" or $state2 eq "charging") { $state = "charging"; } else { $state = "discharging"; } $left = $left1 + $left2; $full = $full1 + $full2; $remain = $left / $full * 100; $remain = int ($remain); $rcap = $rcap1 + $rcap2; $prate = $prate1 + $prate2; ##### Remain Time Calculation ##### if ($prate > 0) { if ($state eq "discharging") { $seconds = $rcap / $prate * 60 * 60; if ($remain < 10) { $charge = "remaining LOW"; } else { $charge = "remaining"; } } else { $seconds = ($full - $rcap) / $prate * 60 * 60; $charge = "until charged"; } $minutes = sprintf("%.2f", $minutes); $hours = $seconds / 60 / 60; $hours = sprintf("%.2f", $hours); chop ($singhour = $hours); chop ($singhour); chop ($singhour); $hourmins = $hours; $hourmins =~ s/^.//; $hourmins =~ s/^.//; $hourmins = $hourmins * .6; $hourmins = sprintf("%.0f", $hourmins); } else { $seconds = 0; } if ($singhour >= 1) { print " $singhour"; } if ($hourmins >= 1) { if ($singhour < 1) { print " 0"; } if ($hourmins < 10) { print ":0$hourmins"; } else { print ":$hourmins"; } print " $charge"; } elsif ($state eq "discharging" and $prate == 0) { print " A:C"; } else { print ":00 $charge"; } print "\n"; if ($state eq "discharging" and $prate == 0) { $printstate = "AC on-line"; } elsif ($state eq "charging") { $printstate = "AC on-line and charging"; } elsif ($state eq "discharging") { $printstate = "AC unplugged"; } print " $remain%, $printstate"; |