Ken (Chanoch) Bloom's Blog

31st January 2008

Battery monitor with sysfs

Debian's packages of Linux kernel 2.6.24 have disabled CONFIG_ACPI_PROCFS_POWER, so the /proc interface for reading your battery power is no longer present. This breaks most of the battery monitors in Debian. Not to worry. I wrote my own.

#!/usr/bin/env ruby
require 'dockapp'

PREFIX="/sys/class/power_supply"

def readint file
  open(file)do |f|
    f.read.to_i
  end
end
def readstr file
  open(file)do |f|
    f.read.chomp
  end
end

def update
  charge_full=readint "#{PREFIX}/BAT1/charge_full"
  charge_now=readint "#{PREFIX}/BAT1/charge_now"
  @bat_pct.set_text "#{(100*charge_now/charge_full)}%"
  @bat_status.set_text readstr("#{PREFIX}/BAT1/status")
  @time.set_text "" #TODO: change me

  acad_online=readint "#{PREFIX}/ACAD/online"
  case acad_online
  when 0: @cord.set_text "unplugged"
  when 1: @cord.set_text "plugged"
  end
end

dockapp=DockApp.new "Power Status"

@bat_pct=DockApp::Text.new("",9,1,0)
@bat_status=DockApp::Text.new("",9,1,0)
@cord=DockApp::Text.new("",9,1,0)
@time=DockApp::Text.new("",9,1,0)

dockapp.add(2,0,@bat_pct)
dockapp.add(2,15,@bat_status)
dockapp.add(2,30,@cord)
dockapp.add(2,45,@time)

timer=DockApp::Timer.new(6000) do
  update
end

update

dockapp.openwindow
timer.start
dockapp.start

Have a look at http://ruby-dockapp.sourceforge.net/ for the dockapp library.

Permalink | linux.
My Website Archives

Tags