2 -- License: Public domain
3 -- Last Changed: Unknown
5 -- statusd_linuxbatt.lua
9 -- Uses the /proc/acpi interface to get battery percentage.
11 -- Use the key "linuxbatt" to get the battery percentage; use
12 -- "linuxbatt_state" to get a symbol indicating charging "+",
13 -- discharging "-", or charged " ".
15 -- Now uses lua functions instead of bash, awk, dc. MUCH faster!
17 -- The "bat" option to the statusd settings for linuxbatt modifies which
18 -- battery we look at.
21 update_interval
=15*1000,
23 important_threshold
=30,
24 critical_threshold
=10,
26 local settings
=table.join(statusd
.get_config("linuxbatt"), defaults
)
28 function linuxbatt_do_find_capacity()
29 local f
=io
.open('/proc/acpi/battery/BAT'.. settings
.bat
..'/info')
30 local infofile
=f
:read('*a')
32 local i
, j
, capacity
= string.find(infofile
, 'last full capacity:%s*(%d+) .*')
36 local capacity
= linuxbatt_do_find_capacity()
38 function get_linuxbatt()
40 local f
=io
.open('/proc/acpi/battery/BAT'.. settings
.bat
..'/state')
41 local statefile
=f
:read('*a')
43 local i
, j
, remaining
= string.find(statefile
, 'remaining capacity:%s*(%d+) .*')
44 local percent
= math
.floor( remaining
* 100 / capacity
)
46 local i
, j
, statename
= string.find(statefile
, 'charging state:%s*(%a+).*')
47 if statename
== "charging" then
49 elseif statename
== "discharging" then
56 function update_linuxbatt()
57 local perc
, state
= get_linuxbatt()
58 statusd
.inform("linuxbatt", tostring(perc
))
59 statusd
.inform("linuxbatt_state", state
)
60 if perc
< settings
.critical_threshold
61 then statusd
.inform("linuxbatt_hint", "critical")
62 elseif perc
< settings
.important_threshold
63 then statusd
.inform("linuxbatt_hint", "important")
64 else statusd
.inform("linuxbatt_hint", "normal")
66 linuxbatt_timer
:set(settings
.update_interval
, update_linuxbatt
)
69 linuxbatt_timer
= statusd
.create_timer()