Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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.
#!/usr/bin/perl
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;
Comment on Checking Laptop battery info
Download Code
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?
    meh.

        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

        emc

        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...

        meh.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://559977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-07-06 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls