-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.lua
97 lines (78 loc) · 1.99 KB
/
battery.lua
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
-- functions relating to looking up battery life/usage
function GetBattery(name, path)
local bat={}
bat.name=name
bat.charge=0
bat.max=0
bat.status=SysFSReadFile(path.."/status")
if filesys.exists(path.."/charge_full") ==true
then
bat.charge=tonumber(SysFSReadFile(path.."/charge_now"))
bat.max=tonumber(SysFSReadFile(path.."/charge_full"))
elseif filesys.exists(path.."/energy_full") ==true
then
bat.charge=tonumber(SysFSReadFile(path.."/energy_now"))
bat.max=tonumber(SysFSReadFile(path.."/energy_full"))
end
return bat
end
function GetBatteries()
local Glob, str, bat
local bats={}
Glob=filesys.GLOB("/sys/class/power_supply/*")
str=Glob:next()
while str ~= nil
do
name=filesys.basename(str)
if
filesys.exists(str.."/charge_full") ==true or
filesys.exists(str.."/energy_full") ==true
then
bat=GetBattery(name, str)
table.insert(bats, bat)
end
str=Glob:next()
end
return bats
end
function LookupBatteries()
local bats, i, bat, perc
local bats_str=""
local bats_str_color=""
local color_map={
{value=0, color="~R"},
{value=10, color="~r"},
{value=25, color="~y"},
{value=75, color="~g"}
}
display_values["bats"]=""
bats=GetBatteries()
for i,bat in ipairs(bats)
do
name="bat:"..tostring(i-1)
-- sometimes this is nil, maybe because we've failed to open the file
if bat.charge ~= nil
then
if bat.max ~= nil and bat.max > 0
then
perc=math.floor((bat.charge * 100 / bat.max) + 0.5)
else
perc=0
end
AddDisplayValue(name, perc, "%d", color_map)
if bat.status == "Charging" then display_values["charging:"..i]="~~" end
bats_str=bats_str .. name..":"..display_values[name].."%"
bats_str_color=bats_str_color .. name..":"..display_values[name..":color"].."%"
if bat.status == "Charging"
then
bats_str=bats_str.."~"
bats_str_color=bats_str_color.."~"
else
bats_str=bats_str.." "
bats_str_color=bats_str_color.." "
end
end
end
display_values["bats"]=bats_str
display_values["bats:color"]=bats_str_color
end