| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local tonumber = tonumber |
| 22 |
local io = { open = io.open } |
| 23 |
local setmetatable = setmetatable |
| 24 |
local math = { |
| 25 |
min = math.min, |
| 26 |
floor = math.floor |
| 27 |
} |
| 28 |
local string = { |
| 29 |
find = string.find, |
| 30 |
match = string.match, |
| 31 |
format = string.format |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
local batpmu_linux = {} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
local function worker(format, batid) |
| 43 |
local battery_state = { |
| 44 |
["full"] = "↯", |
| 45 |
["unknown"] = "⌁", |
| 46 |
["00000013"] = "+", |
| 47 |
["00000011"] = "-" |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
local f = io.open("/proc/pmu/" .. batid) |
| 52 |
|
| 53 |
if not f then return {battery_state["unknown"], 0, "N/A"} end |
| 54 |
local statefile = f:read("*all") |
| 55 |
f:close() |
| 56 |
|
| 57 |
|
| 58 |
local f = io.open("/proc/pmu/info") |
| 59 |
local infofile = f:read("*all") |
| 60 |
f:close() |
| 61 |
|
| 62 |
|
| 63 |
if infofile == nil or string.find(infofile, "Battery count[%s]+:[%s]0") then |
| 64 |
return {battery_state["unknown"], 0, "N/A"} |
| 65 |
end |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
local capacity = string.match(statefile, "max_charge[%s]+:[%s]([%d]+).*") |
| 70 |
local remaining = string.match(statefile, "charge[%s]+:[%s]([%d]+).*") |
| 71 |
|
| 72 |
|
| 73 |
local percent = math.min(math.floor(remaining / capacity * 100), 100) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
local timer = string.match(statefile, "time rem%.[%s]+:[%s]([%d]+).*") |
| 78 |
if timer == "0" then return {battery_state["full"], percent, "N/A"} end |
| 79 |
|
| 80 |
|
| 81 |
local state = string.match(statefile, "flags[%s]+:[%s]([%d]+).*") |
| 82 |
local state = battery_state[state] or battery_state["unknown"] |
| 83 |
|
| 84 |
|
| 85 |
local hoursleft = math.floor(tonumber(timer) / 3600) |
| 86 |
local minutesleft = math.floor((tonumber(timer) / 60) % 60) |
| 87 |
local time = string.format("%02d:%02d", hoursleft, minutesleft) |
| 88 |
|
| 89 |
return {state, percent, time} |
| 90 |
end |
| 91 |
|
| 92 |
|
| 93 |
return setmetatable(batpmu_linux, { __call = function(_, ...) return worker(...) end }) |