-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_supply.lua
More file actions
128 lines (107 loc) · 4.42 KB
/
power_supply.lua
File metadata and controls
128 lines (107 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
local string = string
local io = io
local tonumber = tonumber
local math = math
module ("power_supply")
function powerSupplyOnline (power_supply)
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/online", "r")
if not FileHnd then
return nil
end
return FileHnd:read ()
end
function powerSupplyStatus (power_supply)
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/status", "r")
if not FileHnd then
return nil
end
return FileHnd:read ()
end
function prepareTime (power_supply)
status = powerSupplyStatus (power_supply)
if status == "Unknown" then
return ""
end
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/energy_now", "r")
if not FileHnd then
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/charge_now", "r")
if not FileHnd then
-- tried both, 'charge_now' and 'energy_now', to no avail
return ""
end
isCharge = true
end
local charge_now = tonumber (FileHnd:read ())
FileHnd:close ()
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/power_now", "r")
power_now = 0
if not FileHnd then
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/voltage_now", "r")
voltage_now = tonumber (FileHnd:read ())
FileHnd:close ()
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/current_now", "r")
current_now = tonumber (FileHnd:read ())
FileHnd:close ()
power_now = (voltage_now * current_now)/(1e7)
else
power_now = tonumber (FileHnd:read ())
FileHnd:close ()
end
if power_now == 0 then
-- failsafe until this is properly handled
return "xx:--"
end
hours = (charge_now/power_now)
seconds = hours * 3600
H = math.floor (hours)
M = (seconds - H * 3600)/60
return string.format ("%02d:%02d", H, M)
end
function percentLeft (power_supply)
status = powerSupplyStatus (power_supply)
status = FileHnd:read ()
if status == "Unknown" then
return nil
end
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/energy_full", "r")
if not FileHnd then
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/charge_full", "r")
if not FileHnd then
-- tried both, 'charge_now' and 'energy_now', to no avail
return "--:--"
end
isCharge = true
end
local charge_full = tonumber (FileHnd:read ())
FileHnd:close ()
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/charge_now", "r")
if not FileHnd then
FileHnd, ErrStr = io.open ("/sys/class/power_supply/" .. power_supply .. "/energy_now", "r")
end
local charge_now = tonumber (FileHnd:read ())
FileHnd:close ()
percentage = charge_now * 100.0/charge_full
return percentage
end
function prepareACImage (power_supply)
-- only call with AC!
-- if powerSupplyOnline () returns nil, this is an error, we want to be noticed abou!
online = tonumber (powerSupplyOnline (power_supply))
if online == 1 then
return "/home/obruns/.config/awesome/images/power_supply_AC-online.png"
end
return "/home/obruns/.config/awesome/images/power_supply_AC-offline.png"
end
function prepareImage (power_supply)
if powerSupplyStatus (power_supply) == nil then
-- battery is physically unavailable
return "/home/obruns/.config/awesome/images/battery-unavailable.png"
end
if percentLeft (power_supply) == nil then
return "/home/obruns/.config/awesome/images/battery-gray.png"
end
if percentLeft (power_supply) > 20 then
return "/home/obruns/.config/awesome/images/battery-green.png"
end
return "/home/obruns/.config/awesome/images/battery-orange.png"
end