Tuesday, March 31, 2009

Reading battery status on an N800/N810

For one of my projects I need to be able to read out the energy levels of the battery on my Nokia Internet Tablet (N810) - and I need to be able to do so from within a Python program, so that I may continuously monitor the energy usage of my program. After reading this blog post and talking to my friend Jussi, who had spent some days implementing such functionality in a C++ program of his, I spent about five minutes porting it to Python (thanks Jussi!). The result is shown below:

import dbus

bus = dbus.SystemBus()
hal_obj = bus.get_object ('org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
hal = dbus.Interface (hal_obj, 'org.freedesktop.Hal.Manager')
uids = hal.FindDeviceByCapability('battery')
dev_obj = bus.get_object ('org.freedesktop.Hal', uids[0])

print 'charge level percentage',\
dev_obj.GetProperty('battery.charge_level.percentage')
print 'charge current', dev_obj.GetProperty('battery.reporting.current')
print 'charge design', dev_obj.GetProperty('battery.reporting.design')
print 'charge last full',\
dev_obj.GetProperty('battery.reporting.last_full')
print 'charge unit', dev_obj.GetProperty('battery.reporting.unit')
print 'voltage current', dev_obj.GetProperty('battery.voltage.current')
print 'voltage design', dev_obj.GetProperty('battery.voltage.design')
print 'voltage unit', dev_obj.GetProperty('battery.voltage.unit')

This little snippet of Python code talks to the HAL layer through D-Bus, and in that way is capable of coming up with quite a lot of information about the battery. Check the complete HAL battery API for more information about what can be read through these calls.

No comments:

Post a Comment