Created
January 4, 2011 05:00
-
-
Save dfreedm/764415 to your computer and use it in GitHub Desktop.
Perl script to check battery information on Thinkpads, requires tp-smapi kernel module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| # Perl script to check battery levels on Thinkpads | |
| # Requires the tp-smapi linux kernel module | |
| # Daniel Freedman 2010 | |
| use strict; | |
| use warnings; | |
| use autodie; | |
| use Getopt::Std; | |
| # Setup getopts configuration | |
| $Getopt::Std::STANDARD_HELP_VERSION = 1; | |
| $main::VERSION = "1.1"; | |
| # better than parsing myself | |
| my %opts = (); | |
| getopts( "vb", \%opts ); | |
| my ( $verbose, $bar ) = ( $opts{v}, $opts{b} ); | |
| # Path to battery files | |
| my $SMAPI_PATH = '/sys/devices/platform/smapi/BAT0'; | |
| # If this directoy isn't here, then tp-smapi kernel module isn't present | |
| unless ( -d $SMAPI_PATH ) { | |
| print "tp-smapi not installed!\n"; | |
| exit; | |
| } | |
| # Factored out file reads for great justice | |
| sub read_file { | |
| my $filename = shift; | |
| local $\; | |
| open( my $fh, '<', "$SMAPI_PATH/$filename" ); | |
| chomp( my $input = <$fh> ); | |
| close($fh); | |
| return $input; | |
| } | |
| # Check the state of the battery (charging, idle, discharging) | |
| sub get_state { | |
| return read_file("state"); | |
| } | |
| # Get percent of battery charged | |
| sub get_percent { | |
| return read_file("remaining_percent"); | |
| } | |
| # Get time left, icon for charge/discharge | |
| sub get_time_icon { | |
| my $state = shift; | |
| my $symbol = $bar ? '|' : ' '; | |
| my $time = 0; | |
| # Check if state is charging or discharging. Set icon accordingly | |
| if ( $state eq "charging" ) { | |
| $time = read_file("remaining_charging_time"); | |
| $symbol = $bar ? '>' : '+'; | |
| } | |
| if ( $state eq "discharging" ) { | |
| $time = read_file("remaining_running_time"); | |
| $symbol = $bar ? '<' : '-'; | |
| } | |
| # time can report as "Not Charging" | |
| $time = 0 if $time =~ m{\D}; | |
| return ( $time, $symbol ); | |
| } | |
| # Check the health of the battery | |
| sub get_battery_status { | |
| my $full = read_file("last_full_capacity"); | |
| my $design = read_file("design_capacity"); | |
| my $capacity = int( $full * 100 / $design ); | |
| # capacities are in microAh, convert to mAh | |
| return ($full*.1, $design*.1, $capacity); | |
| } | |
| # Check how many cycles the battery reports | |
| sub get_cycle_count { | |
| return read_file("cycle_count"); | |
| } | |
| #Check date of manufacture | |
| sub get_manufacture_date { | |
| return read_file("manufacture_date"); | |
| } | |
| #Check first date of use | |
| sub get_firstuse_date { | |
| return read_file("first_use_date"); | |
| } | |
| sub get_dates { | |
| return (get_manufacture_date(), get_firstuse_date()); | |
| } | |
| #Check the manufacturer | |
| sub get_maker { | |
| return read_file("manufacturer"); | |
| } | |
| my $state = get_state(); | |
| my $percent = get_percent(); | |
| my ( $time, $icon ) = get_time_icon($state); | |
| if ($bar) { | |
| # Print in bar: [####> ] | |
| my $n = int( $percent / 10 ); | |
| print '[' . ( '#' x ( $n - 1 ) ) . $icon . ( ' ' x ( 9 - $n ) ) . "]\n"; | |
| } | |
| else { | |
| my $hours = int( $time / 60 ); | |
| my $minutes = $time % 60; | |
| # Print the status: Example: [ ] 00:00 (93%) | |
| printf( "[%s] %02d:%02d (%02d%%)\n", $icon, $hours, $minutes, $percent ); | |
| } | |
| # Print health of the battery and cycle count if verbose is specified | |
| if ($verbose) { | |
| my $count = get_cycle_count(); | |
| my ($full, $design, $health) = get_battery_status(); | |
| my ($make_date, $use_date) = get_dates(); | |
| my $maker = get_maker(); | |
| print "Full Charge Capacity: $full mAh\n"; | |
| print "Design Charge Capacity: $design mAh\n"; | |
| print "Battery Health: $health% of original capacity\n"; | |
| print "Cycle Count: $count\n"; | |
| print "Manufacturer: $maker\n"; | |
| print "Battery Date: $make_date\n"; | |
| print "First Use: $use_date\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment