| 1 |
|
| 2 |
|
| 3 |
m> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local pairs = pairs |
| 22 |
local tonumber = tonumber |
| 23 |
local table = { |
| 24 |
insert = table.insert |
| 25 |
} |
| 26 |
|
| 27 |
local math = { |
| 28 |
floor = math.floor, |
| 29 |
modf = math.modf |
| 30 |
} |
| 31 |
|
| 32 |
local helpers = require("vicious.helpers") |
| 33 |
|
| 34 |
|
| 35 |
local bat_openbsd = {} |
| 36 |
function bat_openbsd.async(format, warg, callback) |
| 37 |
local battery_id = warg or "bat0" |
| 38 |
|
| 39 |
local fields = { |
| 40 |
charging_rate = ("hw.sensors.acpi%s.power0"):format(battery_id), |
| 41 |
last_full_capacity = ("hw.sensors.acpi%s.watthour0"):format(battery_id), |
| 42 |
remaining_capacity = ("hw.sensors.acpi%s.watthour3"):format(battery_id), |
| 43 |
design_capacity = ("hw.sensors.acpi%s.watthour4"):format(battery_id), |
| 44 |
state = ("hw.sensors.acpi%s.raw0"):format(battery_id) |
| 45 |
} |
| 46 |
|
| 47 |
local sysctl_args = {} |
| 48 |
for _, v in pairs(fields) do table.insert(sysctl_args, v) end |
| 49 |
|
| 50 |
local battery = {} |
| 51 |
helpers.sysctl_async(sysctl_args, function (ret) |
| 52 |
for k, v in pairs(fields) do |
| 53 |
|
| 54 |
battery[k] = tonumber(ret[v]:match("(.-) ")) |
| 55 |
end |
| 56 |
|
| 57 |
local states = { |
| 58 |
[0] = "↯", ng |
| 59 |
[1] = "-", |
| 60 |
[2] = "!", |
| 61 |
[3] = "+", |
| 62 |
[4] = "N/A", |
| 63 |
[255] = "N/A" |
| 64 |
} |
| 65 |
local state = states[battery.state] |
| 66 |
|
| 67 |
local charge = tonumber(battery.remaining_capacity |
| 68 |
/ battery.last_full_capacity * 100) |
| 69 |
|
| 70 |
local remaining_time |
| 71 |
if battery.charging_rate < 1 then |
| 72 |
remaining_time = "∞" |
| 73 |
else |
| 74 |
local raw_time = battery.remaining_capacity / battery.rate |
| 75 |
local hours, hour_fraction = math.modf(raw_time) |
| 76 |
local minutes = math.floor(60 * hour_fraction) |
| 77 |
remaining_time = ("%d:%0.2d"):format(hours, minutes) |
| 78 |
end |
| 79 |
|
| 80 |
local wear = math.floor(battery.last_full_capacity, |
| 81 |
battery.design_capacity) |
| 82 |
|
| 83 |
|
| 84 |
A) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
return callback({ state, charge, remaining_time, |
| 90 |
wear, battery.charging_rate }) |
| 91 |
end) |
| 92 |
end |
| 93 |
|
| 94 |
return helpers.setasyncall(bat_openbsd) |