Skip to content

Commit

Permalink
Add capacity field to battery widget (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
mphe authored May 7, 2021
1 parent 5882000 commit 16e2e73
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions widget/bat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,32 @@ local function factory(args)
ac_status = "N/A",
perc = "N/A",
time = "N/A",
watt = "N/A"
watt = "N/A",
capacity = "N/A"
}

bat_now.n_status = {}
bat_now.n_perc = {}
bat_now.n_status = {}
bat_now.n_perc = {}
bat_now.n_capacity = {}
for i = 1, #batteries do
bat_now.n_status[i] = "N/A"
bat_now.n_perc[i] = 0
bat_now.n_capacity[i] = 0
end

-- used to notify full charge only once before discharging
local fullnotification = false

function bat.update()
local sum_rate_current = 0
local sum_rate_voltage = 0
local sum_rate_power = 0
local sum_rate_energy = 0
local sum_energy_now = 0
local sum_energy_full = 0
-- luacheck: globals bat_now
local sum_rate_current = 0
local sum_rate_voltage = 0
local sum_rate_power = 0
local sum_rate_energy = 0
local sum_energy_now = 0
local sum_energy_full = 0
local sum_charge_full = 0
local sum_charge_design = 0

for i, battery in ipairs(batteries) do
local bstr = pspath .. battery
Expand All @@ -109,30 +115,42 @@ local function factory(args)
local rate_current = tonumber(helpers.first_line(bstr .. "/current_now"))
local rate_voltage = tonumber(helpers.first_line(bstr .. "/voltage_now"))
local rate_power = tonumber(helpers.first_line(bstr .. "/power_now"))
local charge_full = tonumber(helpers.first_line(bstr .. "/charge_full"))
local charge_design = tonumber(helpers.first_line(bstr .. "/charge_full_design"))

-- energy_now(P)[uWh], charge_now(I)[uAh]
local energy_now = tonumber(helpers.first_line(bstr .. "/energy_now") or
helpers.first_line(bstr .. "/charge_now"))

-- energy_full(P)[uWh], charge_full(I)[uAh]
local energy_full = tonumber(helpers.first_line(bstr .. "/energy_full") or
helpers.first_line(bstr .. "/charge_full"))
charge_full)

local energy_percentage = tonumber(helpers.first_line(bstr .. "/capacity")) or
math.floor((energy_now / energy_full) * 100)

bat_now.n_status[i] = helpers.first_line(bstr .. "/status") or "N/A"
bat_now.n_perc[i] = energy_percentage or bat_now.n_perc[i]

sum_rate_current = sum_rate_current + (rate_current or 0)
sum_rate_voltage = sum_rate_voltage + (rate_voltage or 0)
sum_rate_power = sum_rate_power + (rate_power or 0)
sum_rate_energy = sum_rate_energy + (rate_power or (((rate_voltage or 0) * (rate_current or 0)) / 1e6))
sum_energy_now = sum_energy_now + (energy_now or 0)
sum_energy_full = sum_energy_full + (energy_full or 0)
if not charge_design or charge_design == 0 then
bat_now.n_capacity[i] = 0
else
bat_now.n_capacity[i] = math.floor((charge_full / charge_design) * 100)
end

sum_rate_current = sum_rate_current + (rate_current or 0)
sum_rate_voltage = sum_rate_voltage + (rate_voltage or 0)
sum_rate_power = sum_rate_power + (rate_power or 0)
sum_rate_energy = sum_rate_energy + (rate_power or (((rate_voltage or 0) * (rate_current or 0)) / 1e6))
sum_energy_now = sum_energy_now + (energy_now or 0)
sum_energy_full = sum_energy_full + (energy_full or 0)
sum_charge_full = sum_charge_full + (charge_full or 0)
sum_charge_design = sum_charge_design + (charge_design or 0)
end
end

bat_now.capacity = math.floor(math.min(100, (sum_charge_full / sum_charge_design) * 100))

-- When one of the battery is charging, others' status are either
-- "Full", "Unknown" or "Charging". When the laptop is not plugged in,
-- one or more of the batteries may be full, but only one battery
Expand Down

0 comments on commit 16e2e73

Please sign in to comment.