Sunday, 24 July 2011

Adding a battery monitor to the taskbar

Sadly, xfce's battery monitor won't work, for the simple reason
it depends on either APM, or APCI. Both require that there be
kernel support for them, and this kernel support is missing in
this particular debian image. However, there is a way to fix this
problem. This method will probably not provide as accurate results
as does APCI, but it is good enough to inform the user of general

The key is that the battery state is still reported in various
files in the directory /sys/class/power_supply/battery. The files
we are interested in are called capacity, present, and status.
'capacity' shows the charge in the battery. I believe, the numbers are
supposed to be between 0 and 100. On my tablet, the maximum is 91,
but I would not get a full battery on android either. This is why
I think that the maximum is supposed to be 100, but I might very well
be wrong. The file 'present' reports the battery voltage in mV, while
the file 'status' tells us whether the tablet is connected to the
power supply ("Charging"), or it is running on battery ("Not charging").

Having figured out which files are relevant, we need a means of
displaying our findings. For this, we will use the generic monitor
applet. It is not installed by default, so you will have to issue
the command

sudo apt-get install xfce4-genmon-plugin

This is a quite handy application, which takes a string, and
displays its content. What is wonderful is that, if the string
points to an image, then this image will be shown, and if the
string contains a number between 0, and 100, a bar will be
displayed. The bar's length is, of course, proportional to the
magnitude of this number.

Once apt-get has installed the applet, you should right-click on
the taskbar, and say "Add new", and choose "General monitor applet".
In the "Properties" section of the applet, set "/usr/bin/"
and set the period to whatever suits your need or your taste.

At this point, we are done with the applet, and we only need to set
up the script that produces the argument to the applet. Copy the
following to /usr/bin/, and do not forget to make it
executable (chmod a+x!


if [ ! -e "$tmp_battery" ]
echo "ac" > "$tmp_battery"

charge=$( cat /sys/class/power_supply/battery/capacity )
voltage=$( cat /sys/class/power_supply/battery/present )
# The potential is given in mV, so we convert it to V
voltage_int=`echo "$voltage" | cut -b 1`
voltage_frac=`echo "$voltage" | cut -b 2-4`
status=$( cat /sys/class/power_supply/battery/status )
old_status=$( cat "$tmp_battery" )

echo "<bar>"$charge"</bar>"
echo "<txt>"$charge"%</txt>"
if [ "$status" = "Not charging" ]
if [ "$old_status" = "ac" ]
echo "battery" > "$tmp_battery"
./ half

if [ "$charge" -gt 90 ]
echo "<img>"$img_batpath"battery-full.png</img>"
elif [ "$charge" -gt 30 ]
echo "<img>"$img_batpath"battery-good.png</img>"
elif [ "$charge" -gt 10 ]
echo "<img>"$img_batpath"battery-low.png</img>"
echo "<img>"$img_batpath"battery-caution.png</img>"
echo "<img>"$img_batpath"battery-charging.png</img>"
if [ "$old_status" = "battery" ]
echo "ac" > "$tmp_battery"
./ max
echo "<tool>Voltage: "$voltage_int"."$voltage_frac" V</tool>"

You should now have a bar, an image, and a number at the place
of your genmon applet. (You might have to set the font size to
7 pt, otherwise, the image and the text will overlap.) In addition,
if you hover with your mouse over the applet, you should also see the
present potential of the battery.

You can also combine the backlight dimmer with the battery monitor, and
make the script automatically dim the backlight, if the adapter is
unplugged. To this end, we have to check whether someone has tampered
with the plug. We do this by comparing the present connectedness to
that at the time of the last reading. Obviously, we have to keep the
old connectedness somewhere. We, therefore, always write the value to
a file in /dev/shm, and compare the value stored there to the value
that we read out now. By writing the file to /dev/shm, we actually
save it in memory, so the flash remains untouched. I don't know whether
this is really necessary, but it certainly does not hurt. However, when
we log out, and re-start the computer, this file will be gone (this was
the purpose, after all), so we have to make sure that it does exist when
we want to read from it. This is why we check its existence, and in case
of failure, we simply create the file. If you unplug your device now, it
should become dimmer when is run next, and the opposite should
happen, when you plug in the power cord.

A screenshot is below. When it was taken, the tablet was running on AC. 

The next screenshot shows how you can read off the battery voltage.

Adding a monitor dimmer to the taskbar

The brightness of the display is stored in a file called brightness
in the folder /sys/class/backlight/lcd-backlight. This file contains
a single number between 0, and 255. There is actually another file
that might be of interest, namely, max_brightness. Here you can check
whether the maximum brightness is indeed 255. In any case, the file
'brightness' can be read, and can be written to. By writing to it,
one can change the brightness of the screen. Amazing!

Now, we only need a means of easily writing to this file. We can achieve
this by creating a script that does this, and then connecting the
script to a button on the taskbar. So, copy the following script to
/usr/bin/ and make the script executable (chmod a+x


brightness=$( cat /sys/class/backlight/lcd-backlight/brightness )
max_brightness=$( cat /sys/class/backlight/lcd-backlight/max_brightness )
max_brightness=$((max_brightness - increment))

if [ "$1" = "down" ] && [ "$brightness" -gt "$increment" ]
new_brightness=$((brightness - increment))

if [ "$1" = "up" ] && [ "$brightness" -lt "$max_brightness" ]
new_brightness=$((brightness + increment))

if [ "$1" = "max" ]

if [ "$1" = "half" ]
new_brightness=$((max_brightness / 2))

echo "$new_brightness" > "/sys/class/backlight/lcd-backlight/brightness"

Once done with this, right-click on the taskbar, and add an application
launcher. In the "Properties" dialogue, add an extra item, and call it
"Brightness up", while name the first item "Brighness down". In the
command field, write "sudo /usr/bin/ up", and
"sudo /usr/bin/ down", respectively. If you are not
happy with the default icon, change it to something that you like.
(I chose a light bulb from the stock icon list.) Everything is set now,
and you can reduce the brightness of your screen by pressing the button,
and increase it by pressing on the small arrow on the right hand side.

Here is a screenshot of my desktop. You can see the dimmer next the the browser icon. You can also see the battery monitor applet on the right hand side. More about that in my next post.

Installing debian on a zenithink zt-180 tablet

This is just a placeholder for now. I will fill this up sometime later... In the meantime, you could check out .