11 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
12 |
|
|
13 |
my $dt = 3; |
my $dt = 3; |
14 |
my $acpi_every = 3; |
my $acpi_every = 10; |
15 |
|
|
16 |
my $debug = 0; |
my $debug = shift @ARGV; |
17 |
|
|
18 |
$|=1; |
$|=1; |
19 |
|
|
33 |
|
|
34 |
sub unit { |
sub unit { |
35 |
my $v = shift; |
my $v = shift; |
36 |
|
|
37 |
|
warn "unit( $v )\n" if ($debug); |
38 |
|
|
39 |
my @units = qw/b k m g/; |
my @units = qw/b k m g/; |
40 |
my $o = 0; |
my $o = 0; |
41 |
|
|
44 |
$v /= 1024; |
$v /= 1024; |
45 |
} |
} |
46 |
|
|
47 |
return sprintf("%d%s/s", $v, $units[$o]); |
if ( $v >= 1 ) { |
48 |
|
return sprintf("%d%s", $v, $units[$o]); |
49 |
|
} elsif ( $v == 0 ) { |
50 |
|
return ''; |
51 |
|
} else { |
52 |
|
return sprintf("%.1f%s", $v, $units[$o]); |
53 |
|
} |
54 |
} |
} |
55 |
|
|
56 |
my ( $lrx, $ltx ) = ( 0, 0 ); |
my ( $lrx, $ltx ) = ( 0, 0 ); |
63 |
|
|
64 |
if ( $i % $acpi_every == 0 ) { |
if ( $i % $acpi_every == 0 ) { |
65 |
|
|
66 |
$bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
67 |
$bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
|
68 |
|
if ( $state->{'present rate'} != 0 ) { |
69 |
|
my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
70 |
|
|
71 |
|
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
72 |
|
|
73 |
$bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'}; |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
74 |
|
|
75 |
my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'}; |
warn "time = $time\n" if ($debug); |
76 |
|
|
77 |
$bat->{hh} = int( $time ); |
my $hh = int( $time ); |
78 |
$bat->{mm} = int( ( $time - $bat->{hh} ) * 60 ); |
my $mm = int( ( $time - $hh ) * 60 ); |
79 |
$bat->{ss} = ( $time * 3600 ) % 60; |
my $ss = ( $time * 3600 ) % 60; |
80 |
|
|
81 |
$bat->{new} = '!'; |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
82 |
|
substr($state->{'charging state'},0,1), |
83 |
|
$pcnt * 100, $hh, $mm, $ss, |
84 |
|
$state->{'present rate'} / 1000 |
85 |
|
); |
86 |
|
|
87 |
|
} else { |
88 |
|
$bat = ''; |
89 |
|
} |
90 |
} else { |
} else { |
91 |
$bat->{new} = ' '; |
$bat =~ s/!(\|\s)$/ $1/; |
92 |
} |
} |
93 |
$i++; |
$i++; |
94 |
|
|
118 |
my $t = ( $tx - $ltx ) / $dt; |
my $t = ( $tx - $ltx ) / $dt; |
119 |
( $lrx, $ltx ) = ( $rx, $tx ); |
( $lrx, $ltx ) = ( $rx, $tx ); |
120 |
|
|
121 |
printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d%s| %s\n", |
printf "%s | %s |%5s > %-5s| %s%s\n", $s, $load, unit( $r ), unit( $t ), $bat, $temp; |
|
$s, |
|
|
$load, |
|
|
unit( $r ), unit( $t ), |
|
|
substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss}, $bat->{new}, |
|
|
$temp; |
|
122 |
|
|
123 |
sleep $dt; |
sleep $dt; |
124 |
} |
} |