Get battery status in XFCE on OLPC
Genmon is a wonderful XFCE panel plugin for providing the status of just about anything. Using the trusty “grep” and “awk” tools we can format the output of a command to fit nicely inside of the genmon plugin. Here is a tutorial to make it work and give new users a chance to see the usefulness of “grep” and “awk”. This is assuming you have XFCE running on your machine (XO or otherwise) to use the genmon plugin, but the first part will be about polling HAL that you can do from the TERMINAL activity in Sugar too! HAL is short for hardware abstraction layer and is a way to interface with the hardware to get information or change how a device works. There is a battery monitor plugin for XFCE but it does not seem to like the XO way of doing stuff, so we will make our own.
- open the terminal in Sugar or XFCE and run hal-device to list all the devices interfaced by HAL
$ hal-device
- lots of text will zip by quickly, lets narrow it down with a pipe ( | is the shifted \ key! ) and grep
$ hal-device | grep battery
- the one we will focus on has to do with the battery level percentage and this looks fairly unique
$ hal-device | grep charge_level.percentage
- you will now have just a single line telling you the name of the property and the values. we are only concerned with the number itself so we can further pipe the output to awk
$ hal-device | grep charge_level.percentage | awk '{print $3}'
- a single value in decimal will appear because we use awk to give us the 3rd piece of output (seperated by space characters)
- let’s get fancy and plop a “%” behind it! ` are backticks found under the ~ key, things within them will execute and output before continuing on. We’ll take our command and encapsulate it within the echo command which just outputs whatever its given. Be careful not to confuse single quotes with quotes and backticks!
$ echo `hal-device | grep charge_level.percentage | awk '{print $3}'`%
- you should now have a nicely formated value with a % sign for garnish Lets make a script of it. (genmon does not like complicated commands) ~ is your home folder (probably /home/olpc )
$ nano ~/bat_p.sh
- add the command we created to this file and ctrl-x to save and exit. then make it exectutable
$ chmod +x ~/bat_p.sh
- test it
$ ~/bat_p.sh
Read on if you have XFCE and want to put the output in your panel.
Pages: 1 2
January 12th, 2008 at 1:25 pm
[...] Adding Battery Status (2) [...]