Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Some time ago I wanted to get the battery information in my linux box. I knew of sys-power/acpi, and I checked if there was anything I could use in Perl. There is the Acpi::Battery module, but this module gets information from the deprecated directory /proc/acpi. Improving the module looked like an affordable task for a beginner and I ended up writing a general module, Acpi::Class, that could read information from the directory /sys/class.

Acpi::Class has been my first module, and I have recently been able to emulate the sys-power/acpi after learning to "read" some C. This is the code:

#!/usr/bin/env perl # {{{ Description #===================================================================== +========== # Print Battery Information using Acpi::Class # Emulate acpiclient: #===================================================================== +========== # }}} # {{{ Modules use strict; use warnings; use Acpi::Class; # }}} # {{{ Define Variables # We will get information of BAT1: /sys/class/power_supply/BAT1 my $my_class = "power_supply"; my $my_device = "BAT1"; my $class = Acpi::Class->new( class => $my_class, device => $my_device +); my $values = $class->g_values; my @attributes = qw(energy_now charge_now power_now current_now energy_full charge_full energy_full_design charge_full_design status capacity); # }}} # {{{ Obtain and calculate values my %att_value; foreach my $attribute (@attributes) { $att_value{$attribute} = $values->{$attribute}; } my $remaining_capacity = $att_value{'energy_now'} // $att_value{'charg +e_now'}; my $last_capacity = $att_value{'energy_full'} // $att_value{'char +ge_full'}; my $present_rate = $att_value{'power_now'} // $att_value{'curren +t_now'}; my $design_capacity = $att_value{'energy_full_design'} // $att_valu +e{'charge_full_design'}; my $percentage = $att_value{'capacity'} // $remaining_capacity + * 100 / $last_capacity; my $battery_loss = 100 - $last_capacity * 100 / $design_capacity +; my $status = $att_value{'status'}; my $seconds = 0; my $message = ""; if ( $status =~ /Discharging/ ) { $seconds = 3600 * $remaining_capacity / $present_rate; $message = "Remaining"; } elsif ( $status =~ /Charging/ ) { $seconds = 3600 * ($last_capacity - $remaining_capacity) / $presen +t_rate; $message = "Until Charged"; if ($remaining_capacity > $last_capacity) { $seconds = 3600 * $remaining_capacity / $present_rate; $message = "Remaining, Charging on top last recorded full capa +city"; } } elsif ( $status =~ /Unknown/ ) { $status = "Full"; } else { die "status = $att_value{'status'} and current_now = $present_rate +" } my $there_are_seconds = $seconds; my $there_is_battery_loss = $battery_loss; my $minutes = int($seconds / 60); $seconds = $seconds % 60; my $hours = int($minutes / 60); $minutes = $minutes % 60; # }}} # {{{ Print values my $time = ""; $time = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds) +if ($there_are_seconds); $percentage = sprintf("%.0f%%", $percentage); $battery_loss = sprintf("%.1f%%", $battery_loss); print "$my_device: $status $percentage, "; print "$time $message, " if ($there_are_seconds); print "$battery_loss Battery Loss \n" if ($battery_loss); # }}}

I would very much appreciate some indications on which should be the right direction to follow. Here some options I have thought about:

  1. Improve the Acpi::Class module. Much more needs to be done.
  2. Improve the submitted code. Much more needs to be done.
  3. Transform this code into a module, so other people can use the module to get battery information and see how they can use Acpi::Class to develop code to get information on other devices.
  4. Improve the documentation. It is very difficult to understand what this is all about
  5. More time should be spend in more important Perl modules.
  6. There are other areas, like learning English or gardening, that could make people happier than seeing this code.
  7. All of the above

Actually, I am considering the three first points ;-) . I would very much appreciate if you have some suggestions on how to improve the code of if you think this should be transformed into a module.

Thanks and happy Perling!

P.S.: I have been able to grasp "some basic" C after learning "some" Perl. I have just realised how Perl makes life easier in understanding other languages and producing some code ;-).

In reply to Get battery information in GNU/Linux emulating acpiclient by mimosinnet

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2018-06-23 20:26 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.