bat: expose information on battery wear and tear

Modern batteries should expose information about their design capacity
which we can compare to current capacity and deduce how much 'wear'
the battery got and expose that as a negative value percentage.

Feature sent in August took a while to convince the maintainer many
modern batteries provide this information.

Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org>
This commit is contained in:
Normal Ra 2013-12-22 13:09:16 +01:00 committed by Adrian C. (anrxc)
parent 76269896fc
commit 946271c41d
2 changed files with 15 additions and 10 deletions

7
README
View File

@ -159,11 +159,12 @@ vicious.widgets.uptime
for 5 minutes and 6th for 15 minutes
vicious.widgets.bat
- provides state, charge, and remaining time for a requested battery
- provides state, charge, remaining time and wear for a requested
battery
- 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
level in percent, 3rd as remaining (charging or discharging)
time and 4th as the wear level in percent
vicious.widgets.mem
- provides RAM and Swap usage statistics

View File

@ -1,6 +1,7 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
-- * (c) 2013, NormalRa <normalrawr@gmail.com>
---------------------------------------------------
-- {{{ Grab environment
@ -15,7 +16,7 @@ local math = {
-- }}}
-- Bat: provides state, charge, and remaining time for a requested battery
-- Bat: provides state, charge, remaining time, and wear for a requested battery
-- vicious.widgets.bat
local bat = {}
@ -35,7 +36,7 @@ local function worker(format, warg)
-- Check if the battery is present
if battery.present ~= "1\n" then
return {battery_state["Unknown\n"], 0, "N/A"}
return {battery_state["Unknown\n"], 0, "N/A", 0}
end
@ -45,14 +46,17 @@ local function worker(format, warg)
-- 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
return {battery_state["Unknown\n"], 0, "N/A"}
return {battery_state["Unknown\n"], 0, "N/A", 0}
end
-- Calculate percentage (but work around broken BAT/ACPI implementations)
-- 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
@ -61,7 +65,7 @@ local function worker(format, warg)
elseif battery.power_now then
rate = tonumber(battery.power_now)
else
return {state, percent, "N/A"}
return {state, percent, "N/A", wear}
end
-- Calculate remaining (charging or discharging) time
@ -73,7 +77,7 @@ local function worker(format, warg)
elseif state == "-" then
timeleft = tonumber(remaining) / tonumber(rate)
else
return {state, percent, time}
return {state, percent, time, wear}
end
-- Calculate time
@ -83,7 +87,7 @@ local function worker(format, warg)
time = string.format("%02d:%02d", hoursleft, minutesleft)
end
return {state, percent, time}
return {state, percent, time, wear}
end
-- }}}