-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery.lua
More file actions
209 lines (154 loc) · 4.39 KB
/
battery.lua
File metadata and controls
209 lines (154 loc) · 4.39 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
--[[
Don't try to make this into an object, as Lookup functions
get put in a list and called as functions in that list
]]--
batteries={
low_battery_level=10,
read_battery =function(self, 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"))
bat.current=tonumber(SysFSReadFile(path.."/current_now"))
elseif filesys.exists(path.."/energy_full") ==true
then
bat.charge=tonumber(SysFSReadFile(path.."/energy_now"))
bat.max=tonumber(SysFSReadFile(path.."/energy_full"))
bat.power=tonumber(SysFSReadFile(path.."/power_now"))
end
return bat
end,
read_all=function(self)
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=self:read_battery(name, str)
table.insert(bats, bat)
end
str=Glob:next()
end
return bats
end,
fmt_life=function(self, hours)
local str=""
int,fract=math.modf(hours)
if int > 0 then str=int .. "h" end
str=str.. math.floor(fract * 60) .."m"
return str
end,
calc_life=function(self, bat)
local fill, draw
if bat.energy ~= nil
then
fill=bat.energy
draw=bat.power
else
fill=bat.charge
draw=bat.current
end
if fill ~= nil and draw ~= nil
then
display:add_value(name .. ":life", fill / draw, "%.2f", nill)
end
return fill, draw
end,
check_bats_low=function(self, total_amount, prev_amount)
if total_amount < self.low_battery_level then display:add_alert("low battery alert", "~R~w LOW BATTERY ~0") end
end,
process_total_life=function(self, total_fill, total_draw)
local nowcs, draw_cs, duration_cs, total_amount
now_cs=time.centisecs()
-- milliWattHours divided by milliWatts gives Hours
if total_draw > 0 then total_amount=total_fill / total_draw
-- charge / current can be more troublesome
elseif self.prev_fill ==nil
then
self.prev_fill=total_fill
self.prev_cs=now_cs
elseif self.prev_fill ~= total_fill
then
-- charge is in mAh, calculate draw per centisec
duration_cs=now_cs - self.prev_cs
draw_cs = (self.prev_fill - total_fill) / duration_cs
-- convert to draw per hour
total_draw=draw_cs * 100 * 3600
if total_draw > 0 then total_amount=total_fill / total_draw end
self.prev_fill=total_fill
self.prev_cs=now_cs
end
if total_amount ~= nil
then
display_values["bats_life"]=self:fmt_life(total_amount)
if total_amount > 1.0 then prefix="~g"
elseif total_amount > 0.5 then prefix="~y"
elseif total_amount > 0.1 then prefix="~r"
else prefix="~R~w"
end
display_values["bats_life:color"]=prefix .. self:fmt_life(total_amount) .."~0"
end
end,
process=function(self)
local bats, batnum, i, bat, perc, val, prefix, fill, draw
local total_charge=0
local total_max=0
local total_fill=0
local total_draw=0
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=self:read_all()
for i,bat in ipairs(bats)
do
batnum=tostring(i-1)
name="bat:"..batnum
-- 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
end
display:add_value(name, perc, "%d", color_map)
bats_str=bats_str .. name..":"..display_values[name].."%"
bats_str_color=bats_str_color .. name..":"..display_values[name..":color"].."%"
if bat.status == "Charging" then display_values["charging:" .. batnum]="y"
else display_values["charging:" .. batnum]="n" end
fill,draw=self:calc_life(bat)
if fill ~= nil then total_fill=total_fill + fill end
if draw ~= nil then total_draw=total_draw + draw end
if bat.charge ~= nil then total_charge=total_charge + bat.charge end
if bat.max ~= nil then total_max=total_max + bat.max end
end
display_values["bats"]=bats_str
display_values["bats:color"]=bats_str_color
perc=math.floor((total_charge * 100 / total_max) + 0.5)
self:check_bats_low(perc, self.prev_perc)
self.prev_perc=perc
self:process_total_life(total_fill, total_draw)
end
}
function LookupBatteries()
batteries:process()
end