Sunday 24 July 2011

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/brightness.sh and make the script executable (chmod a+x brightness.sh).

#!/bin/sh

increment=50
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" ]
then
new_brightness=$((brightness - increment))
fi

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

if [ "$1" = "max" ]
then
new_brightness="$max_brightness"
fi

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

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/brightness.sh up", and
"sudo /usr/bin/brightness.sh 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.

2 comments:

  1. Thank you. May I use this in my Linux project that I'm doing for the ZT-180?

    ReplyDelete
  2. Hi,

    I am glad that you have found this useful. And, of course, everything is free, you can take the code, and do whatever you want with it.
    Cheers,
    Zoltán

    ReplyDelete