Pages

Friday, September 7, 2012

Notion Status-bar-display

In the last few days I fiddled with notion and its status-bar. I was looking in to displaying various things in the taskbar at the bottom of the screen.

As I run notion on my laptop, I wanted an easy way to know my IP-address among other things.

The first thing to cross my mind was adopting the battery-display and including a method to display the capacity of all batteries in the system, as well as other information.
Like power consumption and remaining running time.

I developed 3 small Perl-scripts doing all the readout and calculation for this task.

battery-percent display (bat-percent.pl)
1:  #!/usr/bin/perl  
2:  # Number of batteries in the system (theoretically)  
3:  my $BatteryCount = 2;  
4:  # Basepath  
5:  my $BatteryPath = "/sys/devices/platform/smapi/BAT";  
6:  my $i=0;  
7:  my $Available=0;  
8:  my $TotalPercent=0;  
9:  while($i < $BatteryCount){  
10:       my $BatPresentPath = $BatteryPath.$i."/installed";  
11:       open PRESENT,"<", $BatPresentPath or die $!;  
12:       my $BatPresent = <PRESENT>;  
13:       close PRESENT;  
14:       chomp $BatPresent;  
15:       if($BatPresent == 1){  
16:            $Available ++;  
17:            my $BatPercentPath = $BatteryPath.$i."/remaining_percent";  
18:            open PERCENT,"<",$BatPercentPath or die $!;  
19:            my $BatPercent = <PERCENT>;  
20:            close PERCENT;  
21:            chomp $BatPercent;  
22:            $TotalPercent = $TotalPercent + $BatPercent;  
23:       }  
24:       $i++;  
25:  }  
26:  my $ResultPercent=0; if($Available > 0){  
27:       $ResultPercent = ($TotalPercent / $Available);  
28:  }  
29:  print $ResultPercent;  

battery-running-time display (bat-time.pl)
1:  #!/usr/bin/perl  
2:  # Number of batteries in the system (theoretically)  
3:  my $BatteryCount = 2;  
4:  # Basepath  
5:  my $BatteryPath = "/sys/devices/platform/smapi/BAT";  
6:  # AC-Adapter  
7:  my $AcAdapterPath = "/sys/devices/platform/smapi/ac_connected";  
8:  open AC,"<",$AcAdapterPath or die $!;  
9:  my $AcOn = <AC>;  
10:  close AC;  
11:  chomp $AcOn;  
12:  my $i=0;  
13:  my $Available=0;  
14:  my $TotalTime=0;  
15:  while($i < $BatteryCount){  
16:       my $BatPresentPath = $BatteryPath.$i."/installed";  
17:       open PRESENT,"<", $BatPresentPath or die $!;  
18:       my $BatPresent = <PRESENT>;  
19:       close PRESENT;  
20:       chomp $BatPresent;  
21:       if($BatPresent == 1){  
22:            my $BatRemainingTime = $BatteryPath.$i."/remaining_running_time_now";  
23:            open TIME,"<",$BatRemainingTime or die $!;  
24:            my $BatTime = <TIME>;  
25:            close TIME;  
26:            chomp $BatTime;  
27:            if(index($BatTime,"not") == -1){  
28:                 $TotalTime = $TotalTime + $BatTime;  
29:            }  
30:       }  
31:       $i++;  
32:  }  
33:  if($AcOn){  
34:       print "AC-Power";  
35:  }else{  
36:       print $TotalTime." min";  
37:  }  

battery-power-consumption display (bat-power.pl)
1:  #!/usr/bin/perl  
2:  # Number of batteries in the system (theoretically)  
3:  my $BatteryCount = 2;  
4:  # Basepath  
5:  my $BatteryPath = "/sys/devices/platform/smapi/BAT";  
6:  my $i=0;  
7:  my $Available=0;  
8:  my $TotalPower=0;  
9:  while($i < $BatteryCount){  
10:       my $BatPresentPath = $BatteryPath.$i."/installed";  
11:       open PRESENT,"<", $BatPresentPath or die $!;  
12:       my $BatPresent = <PRESENT>;  
13:       close PRESENT;  
14:       chomp $BatPresent;  
15:       if($BatPresent == 1){  
16:            my $BatPowerPath = $BatteryPath.$i."/power_now";  
17:            open POWER,"<",$BatPowerPath or die $!;  
18:            my $BatPower = <POWER>;  
19:            close POWER;  
20:            chomp $BatPower;  
21:            if($BatPower < 0){  
22:                 $BatPower = $BatPower * -1;  
23:            }  
24:            $TotalPower = $TotalPower + $BatPower;  
25:       }  
26:       $i++;  
27:  }  
28:  my $ResultPower = sprintf("%.1f",($TotalPower / 1000));  
29:  print $ResultPower;  

Also I constructed a method to display my IP-address or “offline” if the device did not receive an address.

network display (net.pl)
1:  #!/usr/bin/perl  
2:  # Devices to check  
3:  my $Device = $ARGV[0];  
4:  my $IP = `/sbin/ip addr show $Device | grep "inet " | awk '{print \$2}'`;  
5:  chomp $IP;  
6:  if(index($IP,"/")==-1){  
7:       print "offline";  
8:  }else{  
9:       print $IP;  
10:  }  

Finaly I whipped up a smal snippet of bash-code to get a temperature readout from my nVidia graphics-card on my laptop:
1:  nvidia-smi -q | grep -A 1 "Temperature" | grep -m 1 ":" | awk \'{print $3}\'