Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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;
Comment on Checking Laptop battery info
Download Code
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...


Back to Snippets Section

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-11-27 11:20 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (724 votes), past polls