1 -- statusd_linuxbatt.lua
5 -- Uses the /proc/acpi interface to get battery percentage.
7 -- Use the key "linuxbatt" to get the battery percentage; use
8 -- "linuxbatt_state" to get a symbol indicating charging "+",
9 -- discharging "-", or charged " ".
11 -- Now uses lua functions instead of bash, awk, dc. MUCH faster!
13 -- The "bat" option to the statusd settings for linuxbatt modifies which
14 -- battery we look at.
17 update_interval
=15*1000,
19 important_threshold
=30,
20 critical_threshold
=10,
22 local settings
=table.join(statusd
.get_config("linuxbatt"), defaults
)
24 function linuxbatt_do_find_capacity()
25 local f
=io
.open('/proc/acpi/battery/BAT'.. settings
.bat
..'/info')
26 local infofile
=f
:read('*a')
28 local i
, j
, capacity
= string.find(infofile
, 'last full capacity:%s*(%d+) .*')
32 local capacity
= linuxbatt_do_find_capacity()
34 function get_linuxbatt()
36 local f
=io
.open('/proc/acpi/battery/BAT'.. settings
.bat
..'/state')
37 local statefile
=f
:read('*a')
39 local i
, j
, remaining
= string.find(statefile
, 'remaining capacity:%s*(%d+) .*')
40 local percent
= math
.floor( remaining
* 100 / capacity
)
42 local i
, j
, statename
= string.find(statefile
, 'charging state:%s*(%a+).*')
43 if statename
== "charging" then
45 elseif statename
== "discharging" then
52 function update_linuxbatt()
53 local perc
, state
= get_linuxbatt()
54 statusd
.inform("linuxbatt", tostring(perc
))
55 statusd
.inform("linuxbatt_state", state
)
56 if perc
< settings
.critical_threshold
57 then statusd
.inform("linuxbatt_hint", "critical")
58 elseif perc
< settings
.important_threshold
59 then statusd
.inform("linuxbatt_hint", "important")
60 else statusd
.inform("linuxbatt_hint", "normal")
62 linuxbatt_timer
:set(settings
.update_interval
, update_linuxbatt
)
65 linuxbatt_timer
= statusd
.create_timer()