<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/perl -w

use strict;

my @time_scales = (1,10,100);

	foreach my $ts (@time_scales) {
		print $ts . "\t";		
	}
	print "\n";

	opendir (BATTERY_DIR,'/proc/acpi/battery');
	my $next_dir;
	my @bats;
	while (defined ($next_dir = readdir (BATTERY_DIR))) {
		chomp ($next_dir);
		if (!($next_dir =~ /\.|\.\./)) {
			push (@bats,$next_dir);
		}
	}
	closedir (BATTERY_DIR);

	my $averages = {};
	while (1) {
		
		my $total_usage = 0;
		foreach my $bat (@bats) {
			chomp ($bat);
			my $battery_info = read_battery_info ($bat);
			if (defined $battery_info) {
				$total_usage += $battery_info-&gt;{'present rate'};			
			}
		}
		
		foreach my $time_scale (@time_scales) {
			if (defined $averages-&gt;{$time_scale}) {
				$averages-&gt;{$time_scale} = (1.0/$time_scale)*$total_usage + (($time_scale-1.0)/$time_scale)*$averages-&gt;{$time_scale};
			} else {
				$averages-&gt;{$time_scale} = $total_usage;
			}

			printf '%.0d',$averages-&gt;{$time_scale}; 
			print "\t";
			
		}
		print "\n";		
		sleep (1);
	}	
	
	

#####################################################	
sub read_battery_info {
	my $battery = shift;# | die 'Didn\'t pass which battery.';
	
	open (BAT,'&lt;/proc/acpi/battery/' . $battery . '/state');
	my @state_info = &lt;BAT&gt;;
	close (BAT);
	
	my $whole_file = join ('',@state_info);
	
	
	my $values = {};
	if ($whole_file =~ /^present:\s+yes$/m) {

		$whole_file =~ /^present rate:\s+(\d+) mW$/m;
		$values-&gt;{'present rate'} = $1;

		$whole_file =~ /^remaining capacity:\s+(\d+) mWh$/m;
		$values-&gt;{'remaining capacity'} = $1;

		$whole_file =~ /^present capacity:\s+(\d+) mV$/m;
		$values-&gt;{'present capacity'} = $1;
		
		return $values;
	} else {
		return undef;
	}

	
}
#####################################################	
</pre></body></html>