From e66e5075a0b42c902c9bbc628ab3c8006a261144 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Tue, 10 Nov 2009 15:46:54 +0100 Subject: batsys: import battery widget that uses sysfs Initial widget code was sent by Benedikt Sauer. After some cleanup it is ready to go into master. It uses data exposed trough /sys and it is used in the exact same way as the bat widget (and /proc). This widget will replace batat and acpitool, it will be moved to contrib and retired. --- README | 9 +++++++ bat.lua | 2 +- batsys.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 1 + 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 batsys.lua diff --git a/README b/README index f545247..91b6dfe 100644 --- a/README +++ b/README @@ -165,6 +165,7 @@ vicious.widgets.uptime vicious.widgets.bat - provides state, charge, and remaining time for a requested battery + using procfs - takes battery ID as an argument, i.e. "BAT0" - returns 1st value as state of requested battery, 2nd as charge level in percent and 3rd as remaining (charging or discharging) @@ -177,6 +178,14 @@ vicious.widgets.batat level in percent, 3rd as remaining (charging or discharging) time, 4th as state of the second battery etc. +vicious.widgets.batsys + - provides state, charge, and remaining time for a requested battery + using sysfs + - takes battery ID as an argument, i.e. "BAT0" + - returns 1st value as state of requested battery, 2nd as charge + level in percent and 3rd as remaining (charging or discharging) + time + vicious.widgets.mem - provides RAM and Swap usage statistics - returns 1st value as memory usage in percent, 2nd as memory usage, diff --git a/bat.lua b/bat.lua index 3bcdc75..24936fd 100644 --- a/bat.lua +++ b/bat.lua @@ -19,7 +19,7 @@ local string = { -- }}} --- Bat: provides state, charge, and remaining time for a requested battery +-- Bat: provides state, charge, and remaining time for a requested battery using procfs module("vicious.bat") diff --git a/batsys.lua b/batsys.lua new file mode 100644 index 0000000..654c899 --- /dev/null +++ b/batsys.lua @@ -0,0 +1,84 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2009, Adrian C. +-- * (c) 2009, Benedikt Sauer +--------------------------------------------------- + +-- {{{ Grab environment +local tonumber = tonumber +local io = { open = io.open } +local setmetatable = setmetatable +local string = { format = string.format } +local math = { + min = math.min, + floor = math.floor +} +-- }}} + + +-- Batsys: provides state, charge, and remaining time for a requested battery using sysfs +module("vicious.batsys") + + +-- {{{ Battery widget type +local function worker(format, batid) + local battery = setmetatable({}, {__index = function(table, name) + local f = io.open("/sys/class/power_supply/"..batid.."/"..name) + if f then + local s = f:read("*all") + f:close() + return s + end + end}) + + local battery_state = { + ["Full\n"] = "↯", + ["Unknown\n"] = "⌁", + ["Charged\n"] = "↯", + ["Charging\n"] = "+", + ["Discharging\n"] = "-" + } + + -- Check if the battery is present + if not battery.present == "1\n" then + return {battery_state["Unknown\n"], 0, "N/A"} + end + + + -- 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 + elseif battery.energy_now then + remaining, capacity = battery.energy_now, battery.energy_full + else + return {battery_state["Unknown\n"], 0, "N/A"} + 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) + local percent = math.min(math.floor(remaining / capacity * 100), 100) + + -- Calculate remaining (charging or discharging) time + if state == "+"then + timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate) + elseif state == "-" then + timeleft = tonumber(remaining) / tonumber(rate) + else + return {state, percent, "N/A"} + end + local hoursleft = math.floor(timeleft) + local minutesleft = math.floor((timeleft - hoursleft) * 60 ) + local time = string.format("%02d:%02d", hoursleft, minutesleft) + + return {state, percent, time} +end +-- }}} + +setmetatable(_M, { __call = function(_, ...) return worker(...) end }) diff --git a/init.lua b/init.lua index 638d901..80c62d0 100644 --- a/init.lua +++ b/init.lua @@ -29,6 +29,7 @@ require("vicious.load") require("vicious.uptime") require("vicious.bat") require("vicious.batat") +require("vicious.batsys") require("vicious.mem") require("vicious.fs") require("vicious.dio") -- cgit v1.2.3