batsys: widget type rewritten

Widget was rewritten and cleaned up before it's moved into master.
This commit is contained in:
Adrian C. (anrxc) 2009-11-10 15:40:11 +01:00
parent 480aea8b37
commit 636abecd60
1 changed files with 26 additions and 40 deletions

View File

@ -5,19 +5,14 @@
--------------------------------------------------- ---------------------------------------------------
-- {{{ Grab environment -- {{{ Grab environment
local unpack = unpack local tonumber = tonumber
local setmetatable = setmetatable
local io = { open = io.open } local io = { open = io.open }
local setmetatable = setmetatable
local string = { format = string.format } local string = { format = string.format }
local math = { local math = {
min = math.min, min = math.min,
ceil = math.ceil,
floor = math.floor floor = math.floor
} }
local os = {
time = os.time,
difftime = os.difftime
}
-- }}} -- }}}
@ -25,17 +20,14 @@ local os = {
module("vicious.batsys") module("vicious.batsys")
-- Initialise tables
local time_energy = {}
-- {{{ Battery widget type -- {{{ Battery widget type
local function worker(format, batid) local function worker(format, batid)
local battery = setmetatable({}, {__index = function(table, name) local battery = setmetatable({}, {__index = function(table, name)
local file = io.open("/sys/class/power_supply/"..batid.."/"..name) local f = io.open("/sys/class/power_supply/"..batid.."/"..name)
if file then if f then
local f = file:read("*all") local s = f:read("*all")
file:close() f:close()
return f return s
end end
end}) end})
@ -56,42 +48,36 @@ local function worker(format, batid)
-- Get state information -- Get state information
local state = battery_state[battery.status] or battery_state["Unknown\n"] local state = battery_state[battery.status] or battery_state["Unknown\n"]
-- Get charge information -- Get capacity information
if battery.energy_now then if battery.charge_now then
energy_now, energy_full = battery.energy_now, battery.energy_full remaining, capacity = battery.charge_now, battery.charge_full
elseif battery.charge_now then elseif battery.energy_now then
energy_now, energy_full = battery.charge_now, battery.charge_full remaining, capacity = battery.energy_now, battery.energy_full
else else
return {battery_state["Unknown\n"], 0, "N/A"} return {battery_state["Unknown\n"], 0, "N/A"}
end end
-- Get charge information
if battery.current_now then rate = battery.current_now
else return {battery_state["Unknown\n"], 0, "N/A"} end
-- Calculate percentage (but work around broken BAT/ACPI implementations) -- Calculate percentage (but work around broken BAT/ACPI implementations)
local charge = energy_now / energy_full local percent = math.min(math.floor(remaining / capacity * 100), 100)
local percent = math.min(math.ceil(charge * 100), 100)
-- Calculate remaining (charging or discharging) time -- Calculate remaining (charging or discharging) time
if not time_energy[batid] then if state == "+"then
-- Default values on our first run timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
time_energy[batid] = { os.time(), energy_now } elseif state == "-" then
timeleft = tonumber(remaining) / tonumber(rate)
else
return {state, percent, "N/A"} return {state, percent, "N/A"}
end end
local hoursleft = math.floor(timeleft)
local minutesleft = math.floor((timeleft - hoursleft) * 60 )
local time = string.format("%02d:%02d", hoursleft, minutesleft)
local time, energy = unpack(time_energy[batid]) return {state, percent, time}
local timediff = os.difftime(os.time(), time)
local enerdiff = energy_now - energy
local rate = enerdiff / timediff
if rate > 0 then
timeleft = (energy_full - energy_now) / rate
else
timeleft = -energy_now / rate
end
local hoursleft = math.floor(timeleft / 3600)
local minutesleft = math.floor((timeleft - hoursleft * 3600) / 60)
return {state, percent, string.format("%02d:%02d", hoursleft, minutesleft)}
end end
-- }}} -- }}}