vicious/widgets/bat_linux.lua

101 lines
3.1 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
-- * (c) 2013, NormalRa <normalrawr@gmail.com>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local tonumber = tonumber
local setmetatable = setmetatable
local string = { format = string.format }
local helpers = require("vicious.helpers")
local math = {
min = math.min,
floor = math.floor
}
-- }}}
-- Bat: provides state, charge, remaining time, and wear for a requested battery
-- vicious.widgets.bat
2017-01-25 17:59:31 +01:00
local bat_linux = {}
-- {{{ Battery widget type
local function worker(format, warg)
if not warg then return end
local battery = helpers.pathtotable("/sys/class/power_supply/"..warg)
local battery_state = {
["Full\n"] = "",
["Unknown\n"] = "",
["Charged\n"] = "",
["Charging\n"] = "+",
["Discharging\n"] = ""
}
2017-02-17 19:23:37 +01:00
-- Get current power usage in watt
local curpower = "N/A"
if battery.power_now then
curpower = string.format("%.2f", tonumber(battery.power_now) /1000000)
end
-- Check if the battery is present
2010-03-15 02:42:36 +01:00
if battery.present ~= "1\n" then
2017-02-17 19:23:37 +01:00
return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
2009-10-04 00:54:27 +02:00
end
2009-10-04 00:54:27 +02:00
-- Get state information
local state = battery_state[battery.status] or battery_state["Unknown\n"]
-- Get capacity information
if battery.charge_now then
remaining, capacity = battery.charge_now, battery.charge_full
capacity_design = battery.charge_full_design or capacity
elseif battery.energy_now then
remaining, capacity = battery.energy_now, battery.energy_full
capacity_design = battery.energy_full_design or capacity
else
2017-02-17 19:23:37 +01:00
return {battery_state["Unknown\n"], 0, "N/A", 0, curpower}
end
-- Calculate capacity and wear percentage (but work around broken BAT/ACPI implementations)
local percent = math.min(math.floor(remaining / capacity * 100), 100)
local wear = math.floor(100 - capacity / capacity_design * 100)
-- Get charge information
if battery.current_now then
rate = tonumber(battery.current_now)
elseif battery.power_now then
rate = tonumber(battery.power_now)
else
2017-02-17 19:23:37 +01:00
return {state, percent, "N/A", wear, curpower}
end
2009-10-04 00:54:27 +02:00
-- Calculate remaining (charging or discharging) time
local time = "N/A"
if rate ~= nil and rate ~= 0 then
if state == "+" then
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
elseif state == "" then
timeleft = tonumber(remaining) / tonumber(rate)
else
2017-02-17 19:23:37 +01:00
return {state, percent, time, wear, curpower}
end
-- Calculate time
local hoursleft = math.floor(timeleft)
local minutesleft = math.floor((timeleft - hoursleft) * 60 )
time = string.format("%02d:%02d", hoursleft, minutesleft)
end
2009-10-04 00:54:27 +02:00
2017-02-17 19:23:37 +01:00
return {state, percent, time, wear, curpower}
end
-- }}}
2017-01-25 17:59:31 +01:00
return setmetatable(bat_linux, { __call = function(_, ...) return worker(...) end })