Groesbeek, view of the 'National Liberation Museum 1944-1945' in Groesbeek. © Ton Kersten
Posts tagged as code

Eeepc battery script for 2.6.24 AND 2.6.25

2008-09-13 (1) by Ton Kersten, tagged as code

The new kernel for the EeePC (2.6.25) has deprecated the /proc/acpi/battery interface, so I had to write a new script for use in my own zsh prompt.

The script will work in both 2.6.24 and 2.6.25, so without further ado, here it is. It is written as a function for easy inclusion in any prompts.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/zsh
bat() {
PROC=/proc/acpi/battery/BAT0
SYS=/sys/class/power_supply/BAT0/uevent
STATE=
# dc: design capacity, rc: remaining capacity
if [ -f $PROC/info ]; then
    STATE=$PROC/state   # 2.6.24
    dc=$(grep 'last full' < $PROC/info | awk '{ print $4 }')
    rc=$(grep 'remaining' < $PROC/state | awk '{ print $3 }')
elif [ -f $SYS ]; then
    STATE=$SYS          # 2.6.25
    dc=$(grep '\<power_SUPPLY_CHARGE_FULL\>' < $SYS | awk -F= '{ print $2 }')
    rc=$(grep '\<power_SUPPLY_CHARGE_NOW\>' < $SYS | awk -F= '{ print $2 } ')
else
    exit
fi
p=$(echo 3k $rc $dc / 100 \* p | dc )

if grep -iq discharging $STATE; then
    printf " %02d" "$p"
else
    if [ ${p%.*} -lt 100 ]; then
    printf " %02d+" "$p"
    fi
fi
}
bat

Update

After some tweaking I even made a better one which saves a few greps.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/zsh
bat() {
PROC=/proc/acpi/battery/BAT0
SYS=/sys/class/power_supply/BAT0/uevent
STATE=
# dc: design capacity, rc: remaining capacity
if [ -f $PROC/info ]
then
    STATE=$PROC/state   # 2.6.24
    dc=$(awk '/last full/ { print $4 }' $PROC/info)
    rc=$(awk '/remaining/ { print $3 }' $PROC/state)
elif [ -f $SYS ]
then
    STATE=$SYS          # 2.6.25
    dc=$(awk -F= '$1 ~ /^POWER_SUPPLY_CHARGE_FULL$/ { print $2 }' $SYS)
    rc=$(awk -F= '$1 ~ /^POWER_SUPPLY_CHARGE_NOW$/  { print $2 }' $SYS)
else
    return 0
fi

p=$(echo 3k $rc $dc / 100 \* p | dc )

if grep -iq discharging $STATE; then
    printf " %02d" "$p"
else
    if [ ${p%.*} -lt 100 ]; then
    printf " %02d+" "$p"
    fi
fi
}
bat

Update 2

Just found that there is also a nice tool, called acpi...which makes it even more easy:

% acpi -V
Battery 0: Full, 100%
AC Adapter 0: on-line
Thermal 0: ok, 53.0 degrees C
Cooling 0: Processor 0 of 7