Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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]: Heh - I just realized, $work management just declared "no raises for anybody this year", and I'm not even angry about that. Even though that implicitly means "raises for the people with an automatic 5% raise in their contract", which the newly-merged ...
[Corion]: ... coworkers have. But I guess I've gone more mellow since I get to relax more, and such stuff doesn't make me as angry as it used to.
[Corion]: $boss will still get to listen to my interpretation :-D
[Eily]: hey, I'm just behind Larry in SioB \o/
[Corion]: Eily: Wheee ;)
[Eily]: I'll add that to my résumé

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-01-22 11:08 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.