Add capacity field to battery widget (#504)

This commit is contained in:
Marvin Ewald 2021-05-07 18:41:39 +02:00 committed by GitHub
parent 5882000904
commit 16e2e73336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 16 deletions

View File

@ -79,26 +79,32 @@ local function factory(args)
ac_status = "N/A", ac_status = "N/A",
perc = "N/A", perc = "N/A",
time = "N/A", time = "N/A",
watt = "N/A" watt = "N/A",
capacity = "N/A"
} }
bat_now.n_status = {} bat_now.n_status = {}
bat_now.n_perc = {} bat_now.n_perc = {}
bat_now.n_capacity = {}
for i = 1, #batteries do for i = 1, #batteries do
bat_now.n_status[i] = "N/A" bat_now.n_status[i] = "N/A"
bat_now.n_perc[i] = 0 bat_now.n_perc[i] = 0
bat_now.n_capacity[i] = 0
end end
-- used to notify full charge only once before discharging -- used to notify full charge only once before discharging
local fullnotification = false local fullnotification = false
function bat.update() function bat.update()
local sum_rate_current = 0 -- luacheck: globals bat_now
local sum_rate_voltage = 0 local sum_rate_current = 0
local sum_rate_power = 0 local sum_rate_voltage = 0
local sum_rate_energy = 0 local sum_rate_power = 0
local sum_energy_now = 0 local sum_rate_energy = 0
local sum_energy_full = 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 for i, battery in ipairs(batteries) do
local bstr = pspath .. battery local bstr = pspath .. battery
@ -109,6 +115,8 @@ local function factory(args)
local rate_current = tonumber(helpers.first_line(bstr .. "/current_now")) local rate_current = tonumber(helpers.first_line(bstr .. "/current_now"))
local rate_voltage = tonumber(helpers.first_line(bstr .. "/voltage_now")) local rate_voltage = tonumber(helpers.first_line(bstr .. "/voltage_now"))
local rate_power = tonumber(helpers.first_line(bstr .. "/power_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] -- energy_now(P)[uWh], charge_now(I)[uAh]
local energy_now = tonumber(helpers.first_line(bstr .. "/energy_now") or local energy_now = tonumber(helpers.first_line(bstr .. "/energy_now") or
@ -116,7 +124,7 @@ local function factory(args)
-- energy_full(P)[uWh], charge_full(I)[uAh] -- energy_full(P)[uWh], charge_full(I)[uAh]
local energy_full = tonumber(helpers.first_line(bstr .. "/energy_full") or 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 local energy_percentage = tonumber(helpers.first_line(bstr .. "/capacity")) or
math.floor((energy_now / energy_full) * 100) math.floor((energy_now / energy_full) * 100)
@ -124,15 +132,25 @@ local function factory(args)
bat_now.n_status[i] = helpers.first_line(bstr .. "/status") or "N/A" 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] bat_now.n_perc[i] = energy_percentage or bat_now.n_perc[i]
sum_rate_current = sum_rate_current + (rate_current or 0) if not charge_design or charge_design == 0 then
sum_rate_voltage = sum_rate_voltage + (rate_voltage or 0) bat_now.n_capacity[i] = 0
sum_rate_power = sum_rate_power + (rate_power or 0) else
sum_rate_energy = sum_rate_energy + (rate_power or (((rate_voltage or 0) * (rate_current or 0)) / 1e6)) bat_now.n_capacity[i] = math.floor((charge_full / charge_design) * 100)
sum_energy_now = sum_energy_now + (energy_now or 0) end
sum_energy_full = sum_energy_full + (energy_full or 0)
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
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 -- When one of the battery is charging, others' status are either
-- "Full", "Unknown" or "Charging". When the laptop is not plugged in, -- "Full", "Unknown" or "Charging". When the laptop is not plugged in,
-- one or more of the batteries may be full, but only one battery -- one or more of the batteries may be full, but only one battery