]> git.donarmstrong.com Git - bin.git/blob - pb
add reset usb bus command
[bin.git] / pb
1 #! /usr/bin/perl -w
2
3 use strict;
4
5 use IO::File;
6
7 use POSIX qw(floor);
8
9 use Term::ANSIColor qw(:constants);
10
11 sub catfile{
12     my $temp = IO::File->new($_[0],'r') or
13         die "Unable to open $_[0] for reading: $!";
14     my $foo = <$temp>;
15     chomp $foo;
16     return $foo;
17 }
18
19 my %bat_inf;
20
21 if (-r '/proc/pmu/battery_0') {
22     my $bat_inf_fh = new IO::File '/proc/pmu/battery_0';
23     my $max_out = -1300;
24     my $max_in = 1700;
25
26     my $battery_info = {};
27
28     while (<$bat_inf_fh>) {
29         if (/(.+?)\s*:\s*(-?\d+)/) {
30             $battery_info->{$1} = int $2;
31         }
32     }
33     undef $bat_inf_fh;
34     $bat_inf{charging} = hex $battery_info->{flags} & 2;
35     $bat_inf{current} = 0;
36     if ($battery_info->{current} > 0) {
37         $bat_inf{current} = floor($battery_info->{current}/$max_in*1000)/10;
38     }
39     else {
40         $bat_inf{current} = floor($battery_info->{current}/$max_out*1000)/10; 
41     }
42     $bat_inf{percent} = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10;
43     $bat_inf{time_rem} = $battery_info->{'time rem.'};
44 }
45 elsif (-r '/sys/devices/platform/smapi/BAT0/remaining_capacity') {
46     $bat_inf{percent} = catfile('/sys/devices/platform/smapi/BAT0/remaining_percent');
47     $bat_inf{charging} = catfile('/sys/devices/platform/smapi/BAT0/state') ne 'discharging';
48     $bat_inf{time_rem} = $bat_inf{charging} ? catfile('/sys/devices/platform/smapi/BAT0/remaining_charging_time') : 
49         catfile('/sys/devices/platform/smapi/BAT0/remaining_running_time');
50     $bat_inf{time_rem} !~ /\d/ and $bat_inf{time_rem} = 0;
51     $bat_inf{time_rem} *= 60;
52     $bat_inf{current} = -catfile('/sys/devices/platform/smapi/BAT0/current_now') / 10;}
53 else {
54     print STDERR "No battery information\n";
55     exit 1;
56 }
57
58
59 #use Data::Dumper;
60 #print Dumper($battery_info);
61
62
63 my $hours = int $bat_inf{time_rem}/3600;
64 my $minutes = (int $bat_inf{time_rem}/60)%60;
65 my $seconds = $bat_inf{time_rem}%60;
66 my $current = $bat_inf{current};
67 my $charge_state = $bat_inf{charging}?'Charging':'Discharging';
68 my $percentage = $bat_inf{percent};
69 if ($bat_inf{charging}) {
70     print BOLD GREEN;
71 }
72 else {
73     print BOLD RED;
74 }
75 print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n";
76 print RESET;