[bat] division by zero, if battery is full charged

If the battery state change from charging to full,
power_now is reseted to zero for a little time.
This cause division by zero, which was visible as a very big negative
number because of the behaviour of string.format.
This commit is contained in:
Jörg Thalheim 2012-01-29 10:20:08 +01:00
parent 7fd6234fa0
commit 29f809e819
1 changed files with 6 additions and 7 deletions

View File

@ -10,7 +10,6 @@ local string = { format = string.format }
local helpers = require("vicious.helpers")
local math = {
min = math.min,
max = math.max,
floor = math.floor
}
-- }}}
@ -57,9 +56,9 @@ local function worker(format, warg)
-- Get charge information
if battery.current_now then
rate = battery.current_now
rate = tonumber(battery.current_now)
elseif battery.power_now then
rate = battery.power_now
rate = tonumber(battery.power_now)
else
return {state, percent, "N/A"}
end
@ -67,7 +66,7 @@ local function worker(format, warg)
-- Calculate remaining (charging or discharging) time
local time = "N/A"
if tonumber(rate) then
if rate ~= nil and rate ~= 0 then
if state == "+" then
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
elseif state == "-" then
@ -76,9 +75,9 @@ local function worker(format, warg)
return {state, percent, time}
end
-- Calculate time (but work around broken BAT/ACPI implementations)
local hoursleft = math.max(math.floor(timeleft), 0)
local minutesleft = math.max(math.floor((timeleft - hoursleft) * 60 ), 0)
-- Calculate time
local hoursleft = math.floor(timeleft)
local minutesleft = math.floor((timeleft - hoursleft) * 60 )
time = string.format("%02d:%02d", hoursleft, minutesleft)
end