[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: acpi battery event



On Thu, 7 Oct 2004, eddyp wrote:

> 
>   You forgot to put a licence at the begining... Preferably GNU GPL.
>   As far as US laws (i am not there) no modification is allowed to your 
> script as it is now.

Ops, U're right. Try again

Take care
#Written  by Alessandro  Speranza (speranza@math.unifi.it)  to control
#battery events for  compaq presario 700 Presario 700  gives a battery
#event when remaining  capacity is below a certain  threshold and when
#the battery goes back under charge Thus this scrip analyses the event
#and if the battery is low, calls  a small script that sends the PC to
#sleep  (with  swsusp2) after  60  secs. It  als  sends  a message  to
#everyone.  After  that, if the AC  is plugged back  in, acpid detects
#the other  event. In this case, if  the process of sending  the PC to
#sleep is still running, this  script automatically kills it and sends
#another  message to  everyone, to  say that  the PC  is  under charge
#again.

#This program is free software;  you can redistribute it and/or modify
#it under the terms of the  GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or (at
#your option) any later version.

########################battery.sh script#################################

#/bin/sh
if ( echo $@ | grep -e "--silent" ) 1> /dev/null; then
    VERBOSE='/dev/null'
else
    if [ -w /dev/tty ] ;then
	VERBOSE='/dev/tty'
    else
	VERBOSE='/dev/console'
    fi
fi

if [ -w /etc/battery.conf ]; then
    CONF_FILE=/etc/battery.conf
else
    echo > /etc/battery.conf
    CONF_FILE=/etc/battery.conf
fi

if [ `cat /proc/acpi/battery/BAT0/state | grep present: | cut -d\  -f18` = "yes" ]; then
    
    function acpi_charge()
    {
	ACPI_CHARGE=`cat /proc/acpi/ac_adapter/AC/state | cut -d\  -f20`
	case $ACPI_CHARGE in
	    *on-line*)
    ACPI_CHARGE="+" ;;
    *off-line*)
    ACPI_CHARGE="-" ;;
    esac
#    echo -e $ACPI_CHARGE > $VERBOSE
    }
    function acpi_percent()
    {
	CAPACITY=`cat /proc/acpi/battery/BAT0/info |grep "last full capacity:"|cut -d\  -f9`
#	echo -e $CAPACITY >$VERBOSE
	LEVEL=`cat /proc/acpi/battery/BAT0/state | grep remaining|cut -d\  -f8`
#	echo -e $LEVEL >$VERBOSE
	ACPI_PERCENT=`echo -e $(( $LEVEL * 100 / $CAPACITY ))`
#	if [ "$LEVEL" = "$CAPACITY" ]; then 
#	    echo -e "full" >$VERBOSE
#	else
#	    echo -e $ACPI_PERCENT% >$VERBOSE
#	fi
    }
    acpi_charge
    acpi_percent
    if [ $ACPI_CHARGE = "-" ] ; then
	if [ $ACPI_PERCENT -le "1" ] ; then
	    echo -en "Battery: discharging, $ACPI_PERCENT% remaining.
	    Warning battery less than 1%
	    Suspending in 60 seconds..." > $CONF_FILE
	    /usr/bin/wall $CONF_FILE
	    /usr/local/sbin/sleep_hibernate
	    echo -en `ps -adef|grep "sleep"` > /usr/local/sbin/pid
	fi    
    else
	echo -en "Battery: charging, $ACPI_PERCENT% remaining." > $CONF_FILE
	/usr/bin/wall $CONF_FILE
	if [ -f /usr/local/sbin/pid ]; then
	    PID=`cat /usr/local/sbin/pid| grep "sleep"|cut -d\  -f3`
	    /bin/kill $PID
	    /bin/rm /usr/local/sbin/pid
	fi
    fi
fi

Reply to: