From: Luca CPZ Date: Wed, 25 Jul 2018 10:43:01 +0000 (+0200) Subject: widget.bat: autodetection function; closes #340 #389 X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/89ae5308061ac828b4f5002ba77c069585b1e088?ds=inline;hp=a4c86f9ace9dc5caad4f942af5d1be3eca47f666 widget.bat: autodetection function; closes #340 #389 --- diff --git a/widget/bat.lua b/widget/bat.lua index 34d63e5..8afd6a1 100644 --- a/widget/bat.lua +++ b/widget/bat.lua @@ -6,15 +6,12 @@ --]] -local first_line = require("lain.helpers").first_line -local newtimer = require("lain.helpers").newtimer +local helpers = require("lain.helpers") +local fs = require("gears.filesystem") local naughty = require("naughty") local wibox = require("wibox") -local math = { abs = math.abs, - floor = math.floor, - log10 = math.log10, - min = math.min } -local string = { format = string.format } +local math = math +local string = string local ipairs = ipairs local tonumber = tonumber @@ -25,12 +22,32 @@ local function factory(args) local bat = { widget = wibox.widget.textbox() } local args = args or {} local timeout = args.timeout or 30 - local batteries = args.batteries or (args.battery and {args.battery}) or {"BAT0"} - local ac = args.ac or "AC0" local notify = args.notify or "on" local n_perc = args.n_perc or { 5, 15 } + local pspath = args.pspath or "/sys/class/power_supply/" local settings = args.settings or function() end + if not fs.is_dir(pspath) then + naughty.notify { text = "lain.widget.bat: invalid power supply path", timeout = 0 } + return + end + + bat.batteries = args.batteries or (args.battery and {args.battery}) or {} + bat.ac = args.ac or "AC0" + + function bat.get_batteries() + helpers.line_callback("ls -1 " .. pspath, function(line) + local bstr = string.match(line, "BAT%w+") + if bstr then + bat.batteries[#bat.batteries + 1] = bstr + else + bat.ac = string.match(line, "AC%w+") -- fallback one might be wrong + end + end) + end + + if #bat.batteries == 0 then bat.get_batteries() end + bat_notification_critical_preset = { title = "Battery exhausted", text = "Shutdown imminent", @@ -65,7 +82,7 @@ local function factory(args) bat_now.n_status = {} bat_now.n_perc = {} - for i = 1, #batteries do + for i = 1, #bat.batteries do bat_now.n_status[i] = "N/A" bat_now.n_perc[i] = 0 end @@ -80,30 +97,29 @@ local function factory(args) local sum_rate_energy = 0 local sum_energy_now = 0 local sum_energy_full = 0 - local pspath = "/sys/class/power_supply/" - for i, battery in ipairs(batteries) do + for i, battery in ipairs(bat.batteries) do local bstr = pspath .. battery - local present = first_line(bstr .. "/present") + local present = helpers.first_line(bstr .. "/present") if tonumber(present) == 1 then -- current_now(I)[uA], voltage_now(U)[uV], power_now(P)[uW] - local rate_current = tonumber(first_line(bstr .. "/current_now")) - local rate_voltage = tonumber(first_line(bstr .. "/voltage_now")) - local rate_power = tonumber(first_line(bstr .. "/power_now")) + 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")) -- energy_now(P)[uWh], charge_now(I)[uAh] - local energy_now = tonumber(first_line(bstr .. "/energy_now") or - first_line(bstr .. "/charge_now")) + 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(first_line(bstr .. "/energy_full") or - first_line(bstr .. "/charge_full")) + local energy_full = tonumber(helpers.first_line(bstr .. "/energy_full") or + helpers.first_line(bstr .. "/charge_full")) - local energy_percentage = tonumber(first_line(bstr .. "/capacity")) or + local energy_percentage = tonumber(helpers.first_line(bstr .. "/capacity")) or math.floor((energy_now / energy_full) * 100) - bat_now.n_status[i] = 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] sum_rate_current = sum_rate_current + (rate_current or 0) @@ -125,7 +141,7 @@ local function factory(args) bat_now.status = status end end - bat_now.ac_status = tonumber(first_line(string.format("%s%s/online", pspath, ac))) or "N/A" + bat_now.ac_status = tonumber(helpers.first_line(string.format("%s%s/online", pspath, ac))) or "N/A" if bat_now.status ~= "N/A" then if bat_now.status ~= "Full" and sum_rate_power == 0 and bat_now.ac_status == 1 then @@ -192,7 +208,7 @@ local function factory(args) end end - newtimer("batteries", timeout, bat.update) + helpers.newtimer("batteries", timeout, bat.update) return bat end diff --git a/wiki b/wiki index abaf929..d9612b6 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit abaf929d0f77d7a268c6390a24ad9258049e5623 +Subproject commit d9612b62c8b45759e6701cf86ed78158a87a1866