Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Checking Laptop battery info

by /dev/trash (Curate)
on Jul 09, 2006 at 03:07 UTC ( #559977=snippet: print w/replies, xml ) Need Help??
Description: This is a simple piece of Perl I wrote to see what percentage my laptop battery was. The die needs to be more descriptive and I also need to take out the hardcoded values for a full battery, but it gets the job done faster than cat and scanning.
use warnings;
use strict;
use diagnostics;
my $status;
open (BATT, "/proc/acpi/battery/BAT0/state") || die("Can't Open: $!");
while (<BATT>) {
  if ($_ =~ /(charging state:)(\s+)(\w+)/ ) {
    $status = $3;
  if ($_ =~ /(:)(\s+)(\d*)(\s+)(mAh)/ ) {
     print "\n";
     my $perc = $3/2791;
     printf "The battery is %s", $status;
     print "\n";
     printf "Battery percentage is: %.0f", $perc*100;
     print "\n-----------------------------\n\n";
     } else {
     print "Incorrect Format";
close BATT;
Replies are listed 'Best First'.
Re: Checking Laptop battery info
by stonecolddevin (Vicar) on Jul 09, 2006 at 09:17 UTC
    what would it take for something like this to be done for windows/mac?

        Micro$oft does have VBS code, which can serve as a starting point. After all, translating from VBS to Perl should be easy &foolish_grin;

        Microsoft scripting center


        e(π√−1) = −1

      For win you could do something like this:

      #!/usr/bin/perl -l use Win32::OLE('in'); my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\.\\root\\cimv2 +") or die "WMI connection failed.\n"; $colItems = $objWMIService->ExecQuery("Select * from Win32_Battery","W +QL",0x30); print "$_->{DeviceID} ($_->{Description}): $_->{EstimatedChargeRemaini +ng}%" for in $colItems;

      Of course, I get percentages greater than 100% when I'm plugged in so it isn't completely ideal. =/

        well it's most certainly a start. if nothing else i can be like, "hey dad, check this out" :-)

        might even be able to play around with it and see what the problem is with the percentages...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://559977]
[Corion]: Meh, first round of escalations for me not wanting to fix in production what a project has mismanaged. Now another project, which eats up all the resources until end of this year wants to take that task and put it on my list of things as well.
[Corion]: So now there will be the fun of me explaining to the project that $other_project had low priority because $project has high priority. If $project picks up the task from $other_project, they also will need to reprioritize my tasks to get that done...
[Corion]: ... of course at the cost of something else on my list of tasks for $project, as there only is a finite amount of days until $deadline.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-08-17 12:22 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (287 votes). Check out past polls.