-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
167 lines (146 loc) · 5.37 KB
/
main.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
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
---- #########################################################################
---- # #
---- # Telemetry Widget for EdgeTX Radios #
---- # Copyright (C) EdgeTX #
-----# #
---- # License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html #
---- # #
---- # This program is free software; you can redistribute it and/or modify #
---- # it under the terms of the GNU General Public License version 2 as #
---- # published by the Free Software Foundation. #
---- # #
---- # This program is distributed in the hope that it will be useful #
---- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
---- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
---- # GNU General Public License for more details. #
---- # #
---- #########################################################################
-- Widget to display the levels of lipo/HVLipo battery with mAh Used based on battery voltage
-- JRWieland
-- Date: 2024
local app_name = "Batt/mAh"
local app_ver = "1.2"
local _options = {
{ "Sensor", SOURCE, 0}, -- use'Cels'
{"Capacity", STRING, "3200"}, -- Lipo Capacity
{"Cells", VALUE, 6,2,12}, --Default Cells of Lipo, Min Value, Max Value
{"Voltage", STRING, "4.20"}, --Default Voltage of Lipo Cells, 4.35 for HV Lipos
}
-----------------------------------------------------------------
local function create(zone, options)
local wgt = {
zone = zone,
options = options,
isDataAvailable = 0,
cellDataHistoryLowest = { 5, 5, 5, 5, 5, 5, 5, 5 },
cellDataHistoryCellLowest = 5,
cellMin = 0,
cellPercent = 0,
}
return wgt
end
local function update(wgt, options)
if (wgt == nil) then return end
wgt.options = options
end
local function getCellPercent(wgt, cellValue)
if cellValue == nil then
return 0
end
local result = 0;
if wgt.options.Voltage >= "4.21"then
loadScript("/WIDGETS/BattmAh/4_35lipo.lua")()
else
loadScript("/WIDGETS/BattmAh/4_2lipo.lua")()
end
local _percentSplit = lipoValue
for i1, v1 in ipairs(_percentSplit) do
if (cellValue <= v1[#v1][1]) then
for i2, v2 in ipairs(v1) do
if v2[1] >= cellValue then
result = v2[2]
return result
end
end
end
end
return 100
end
--- This function returns a table with cels values
local function calculateBatteryData(wgt)
local newCellData = getValue(wgt.options.Sensor)
if type(newCellData) ~= "table" then
wgt.isDataAvailable = false
return
end
local cellMax = 0
local cellMin = 5
local cellSum = 0
for k, v in pairs(newCellData) do
-- stores the lowest cell values in historical table
if v > 1 and v < wgt.cellDataHistoryLowest[k] then
-- min 1v to consider a valid reading
wgt.cellDataHistoryLowest[k] = v
--- calc history lowest of all cells
if v < wgt.cellDataHistoryCellLowest then
wgt.cellDataHistoryCellLowest = v
end
end
--- calc lowest of all cells
if v < cellMin and v > 1 then
cellMin = v
end
end
wgt.cellMin = cellMin
wgt.isDataAvailable = true
wgt.cellPercent = getCellPercent(wgt, wgt.cellMin)
end
--- Zone size: 70x39 top bar
local function refreshZoneTiny(wgt)
calculateBatteryData(wgt)
getCellPercent(wgt)
-- local battCell=wgt.options.Cells
local battDis = wgt.options.Capacity
local mAhUsed = (battDis*((wgt.cellPercent)/100))
function comma(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
lcd.drawText(0,0,comma(mAhUsed).." Mha",SMLSIZE+WHITE)-- Change or remove color if desired
lcd.drawText(0,15, string.format("%2.0f%%",wgt.cellPercent),SMLSIZE+WHITE)
end
--- Zone size: others
local function refreshZoneSmall(wgt)
calculateBatteryData(wgt)
getCellPercent(wgt)
local battCell=wgt.options.Cells
local battDis = wgt.options.Capacity
local mAhUsed = (battDis*((wgt.cellPercent)/100))
function comma(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
lcd.drawText(2,0,comma(battDis).." "..battCell.."S Lipo "..wgt.options.Voltage.."v/cell")
lcd.drawText(2,18,comma(mAhUsed).." Used / ".. string.format("%2.0f%%", wgt.cellPercent))
end
local function refresh(wgt)
if wgt.zone.w > 150 and wgt.zone.h > 28 then refreshZoneSmall(wgt)
elseif wgt.zone.w > 65 and wgt.zone.h > 35 then refreshZoneTiny(wgt)
end
end
local function background(wgt)
calculateBatteryData(wgt)
end
return { name = app_name, options = _options, create = create, update = update, background = background, refresh = refresh }