eeepc debian battery alarm perl script

Problems with hardware & laptops not being detected or supported during or after install.
Locked
czz
Posts: 6
Joined: Wed May 13, 2009 10:35 am

eeepc debian battery alarm perl script

Post by czz »

Intill debian doesn't fix this problem i made a script for a battery alarm.

YOU MUST HAVE ZENYTY INSTALLED TO WORK

------------------------------------------------------------------------------------------------

Code: Select all

#!/usr/bin/perl
############################################################
### Script for allerting when battery is low on LXDE eeepc
### Author: Luca Cuzzolin 
### Opensourse
############################################################

## Subroutines

# To find if is charging or not
sub plug_status {
    $plug = `cat /proc/acpi/battery/BAT*/state | grep 'charging state'`;
    @string_plug = split(/\s+/, $plug);
    $charging_state = $string_plug[2];
}

# To find remaining capacity
sub battery_status {
    $remaining_capacity = `cat /proc/acpi/battery/BAT*/state | grep 'remaining capacity'`;
    @string_rcapacity = split(/\s+/, $remaining_capacity);
    $mAh = $string_rcapacity[2];
}

# Battery warning popup with zenity (will be inplemented with gtk in future)
sub battery_warning {
    $warning = `zenity --warning --text="Batteria Scarica" & aplay /usr/share/sounds/system_sounds/Drip.wav`;
    $numb = 10;
    while($numb--){
        sleep(1);
    }
    my $command = `killall eeepc_battery_alarm.pl & eeepc_battery_alarm.pl`;
}

# Loop if acplug is unplugged and remaining capacity is = or less than $mAh do warning else redo the loop (sub else_mAh)
sub if_mAh {
    if ($mAh <= 10 and $charging_state =~ m/discharging/ ) {
        &battery_warning;
    }
    else {
        &else_mAh;
    }
}

# else redo the loop
sub else_mAh {
    $num = 10;
    while($num--){
        sleep(1);
    }
    &plug_status;
    &battery_status;
    &if_mAh;
}

### Main program

&plug_status;
&battery_status;
&if_mAh;

### End

----------------------------------------------------------------------------------------------------------------
make a file as root with vi or nano or others in /usr/bin/ and name it eeepc_battery_alarm.pl

sudo vi /usr/bin/eeepc_battery_alarm.pl

copy the script inside the file

exit vi or other editors.

change permission to the file eeepc_battery_alarm.pl
chmod 755 /usr/bin/eeepc_battery_alarm.pl

if you want to adgiust your alert left time, open the file eepc_battery_alarm.pl and change $mAh <= 10 to $mAh <= any_multiple_of_10_taht_you_want


then in if you want to start in automatic on login user just make a .desktop file in /home/USER/.config/autostart/

vi /home/YOUR_USER/.config/autostart/eeepc_battery_alarm.desktop

and write this in to it:

[Desktop Entry]

Type=Application

Exec=eeepc_battery_alarm.pl

exit vi or other editors

first time you can run it just tiping in the terminal

eeepc_battery_alarm.pl


The script is very spartan but those it do what i want. I did not implement plug/unplug alarm but it is very easy to do if you want.

i used zenity but you can implement the script using gtk.

enjoy
czz
Posts: 6
Joined: Wed May 13, 2009 10:35 am

Re: eeepc debian battery alarm perl script

Post by czz »

sorry i forgot and & in the script so this is it :

Code: Select all

#!/usr/bin/perl
############################################################
### Script for allerting when battery is low on LXDE eeepc
### Author: Luca Cuzzolin
### Opensourse
############################################################

## Subroutines

# To find if is charging or not
sub plug_status {
    $plug = `cat /proc/acpi/battery/BAT*/state | grep 'charging state'`;
    @string_plug = split(/\s+/, $plug);
    $charging_state = $string_plug[2];
}

# To find remaining capacity
sub battery_status {
    $remaining_capacity = `cat /proc/acpi/battery/BAT*/state | grep 'remaining capacity'`;
    @string_rcapacity = split(/\s+/, $remaining_capacity);
    $mAh = $string_rcapacity[2];
}

# Battery warning popup with zenity (will be inplemented with gtk in future)
sub battery_warning {
    $warning = `zenity --warning --text="Batteria Scarica" & aplay /usr/share/sounds/system_sounds/Drip.wav`;
    $numb = 10;
    while($numb--){
        sleep(1);
    }
    my $command = `killall eeepc_battery_alarm.pl & eeepc_battery_alarm.pl &`;
}

# Loop if acplug is unplugged and remaining capacity is = or less than $mAh do warning else redo the loop (sub else_mAh)
sub if_mAh {
    if ($mAh <= 10 and $charging_state =~ m/discharging/ ) {
        &battery_warning;
    }
    else {
        &else_mAh;
    }
}

# else redo the loop
sub else_mAh {
    $num = 10;
    while($num--){
        sleep(1);
    }
    &plug_status;
    &battery_status;
    &if_mAh;
}

### Main program

&plug_status;
&battery_status;
&if_mAh;

### End

fix now the killall process works fine
czz
Posts: 6
Joined: Wed May 13, 2009 10:35 am

Re: eeepc debian battery alarm perl script

Post by czz »

last thing to set up is the file sound inside the script.

line:

$warning = `zenity --warning --text="Batteria Scarica" & aplay /usr/share/sounds/system_sounds/Drip.wav`;

giust change the path of the .wav

MUST HAVE APLAY INSTALLED
ribbit
Posts: 2
Joined: Wed Dec 02, 2009 5:42 pm

Re: eeepc debian battery alarm perl script

Post by ribbit »

This script is great! I've been looking for a script for my laptop, after my battery's capacity was reduced because of too many total discharges without a low battery warning. I did make some changes to the script so that it would work with my laptop, and I think even other laptops. I added the subroutine design_capacity, I think I commented all my additions with my name.

http://paste.ubuntu.com/333279/
ribbit
Posts: 2
Joined: Wed Dec 02, 2009 5:42 pm

Re: eeepc debian battery alarm perl script

Post by ribbit »

It looks like I jumped the gun. The subroutine I added doesn't seem to be getting the mAh like your battery_warning subroutine is. I'll have to study your script a lot more I think before that subroutine works, but I'd like the subroutine to work, because then it should work for any laptop (right?). I'm in #ubuntu-beginners on freenode. nickname is seidos.
czz
Posts: 6
Joined: Wed May 13, 2009 10:35 am

Re: eeepc debian battery alarm perl script

Post by czz »

Sorry if i am late for the answer

Nice kevin i really appreciate that you implemented the script :)

We could try to implement the lxde battery bar with c++ for people ho has eeepc.

The script is a workaround but it works.

Let me know
czz
Posts: 6
Joined: Wed May 13, 2009 10:35 am

Re: eeepc debian battery alarm perl script

Post by czz »

Ii have just change to squeeze kernel 2.6.30 because of the Super Hybrid Engine support.

This workaround doesn't work with this kernel, so i make some changes.

Code: Select all

#!/usr/bin/perl
############################################################
### Script for allerting when battery is low on LXDE eeepc
### Kernel 2.6.30 (Debian squeeze)
### Author: Luca Cuzzolin 
### Opensource
############################################################

## Subroutines

# To find if is charging or not
sub plug_status {
    $plug = `cat /sys/class/power_supply/BAT*/status`;
}

# To find remaining capacity
sub battery_status {
    $remaining_capacity = `cat /sys/class/power_supply/BAT*/charge_now`;
}

# Battery warning popup with zenity (will be inplemented with gtk in future)
sub battery_warning {
    $warning = `zenity --warning --text="Batteria Scarica" & aplay /usr/share/sounds/system_sounds/Drip.wav`;
    $numb = 10;
    while($numb--){
        sleep(1);
    }
    my $command = `killall eeepc_battery_alarm.pl & eeepc_battery_alarm.pl &`;
}

# Loop if acplug is unplugged and remaining capacity is = or less than $remaining_capacity do warning else redo the loop (sub else_mAh)
# Change the default 20000 that it corresponds at 20% of battery
sub if_remaining_capacity {
    if ($remaining_capacity <= 20000 and $plug =~ m/Discharging/ ) {
        &battery_warning;
    }
else {
        &else_remaining_capacity;
    }
}

# else redo the loop
sub else_remaining_capacity {
    $num = 10;
    while($num--){
        sleep(1);
    }
    &plug_status;
    &battery_status;
    &if_remaining_capacity;
}

### Main program

&plug_status;
&battery_status;
&if_remaining_capacity;

### End


bye
Locked