-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
191 lines (172 loc) · 8.98 KB
/
Copy pathmain.lua
File metadata and controls
191 lines (172 loc) · 8.98 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
---- ###############################################################################################################################################################################################
---- KBI Battery Monitor Lua Script Widget Version 3/11/24
---- This Telemetry Widget Will Monitor Flight Pack Voltage and Flight Time As Follows:
---- 1. Annunciates Remaining Battery Pecentage when Battery is First Connected to the Model and Resets Timer to Zero
---- 2. If Initial Battery Is Less than the Initial Low Battery Percentage warning, Annunciate Beep, Low Battery, and Remaining Battery Percentage (Default = 50%)
---- 3. Displays the Highest Voltage Within the Number of Defined 1-Second Samples (Default 8-Seconds)
---- 4. Annunciates Remaining Battery Percentage and Flight Time When Battery Is Not Low (Defalt 30-Seconds)
---- 5. Annunciates Beep, Remaining Battery Percentage, and Flight Time When Battery is Low (Default 15-Seconds)
---- 6. Flight Time Increments Every Second When Armed
---- 7. Reset Flight Time Based On the Defined Switch and When Battery is Connected
----***** Adjust These Variables For Your Model ******
local ARMSWITCH = "sh" --Enter Switch that Enables Arming. When Disarmed Flight Time Will Not Increment and Audio Messages Are Disabled
local FLIGHTTIMERESET = "sc" --Enter Switch that Resets Flight Time
local TELEMSENSOR = "VFAS" --Enter Name of Flight Pack Voltage Telemetry Sensor (RxBt/A1/A2/A3/VFAS/RxBt/etc..)
local SAMPLESIZE = 8 --Number of 1-Second Voltage Samples (Default Value = 8)
local ANNUNCIATESEC = 30 --Number of Seconds to Annunciate Battery Percentage and Flight Time When Battery Is Not Low (Default Value = 30)
local ANNUNCIATELOWBATTSEC = 7 --Number of Seconds to Annunciate Beep, Low Battery Percentage, and Flight Time When Battery At or Below Low Battery Percentage (Default Value = 15)
local LOWBATTPERCENT = 7 --Remaining Battery Percent to Start Low Battery Annunciation (Default = 8%)
local INITIALLOWBATTPERCENT = 50 --Initial Low Battery Warning When Battery is First Connected (Default = 50%)
---- ###############################################################################################################################################################################################
local options = {
{ "Color", COLOR, COLOR_THEME_SECONDARY1 },
{ "Shadow", BOOL, 0 }
}
local bootupcomplete = 0
local currentvbat = 0
local vbatread = 0
local lastupdate = 0
local timeresetlastupdate = 0
local cells = 0
local vbat = 0
local battArray = {}
local battIndex = 0
local result = 0
local flightTime = 0
local annunciateTime = 0
local previousvbat = 0
local vbatcount = 0
local myArrayPercentList = { { 3, 0 }, { 3.093, 1 }, { 3.196, 2 }, { 3.301, 3 }, { 3.401, 4 }, { 3.477, 5 }, { 3.544, 6 }, { 3.601, 7 }, { 3.637, 8 }, { 3.664, 9 }, { 3.679, 10 }, { 3.683, 11 }, { 3.689, 12 }, { 3.692, 13 }, { 3.705, 14 }, { 3.71, 15 }, { 3.713, 16 }, { 3.715, 17 }, { 3.72, 18 }, { 3.731, 19 }, { 3.735, 20 }, { 3.744, 21 }, { 3.753, 22 }, { 3.756, 23 }, { 3.758, 24 }, { 3.762, 25 }, { 3.767, 26 }, { 3.774, 27 }, { 3.78, 28 }, { 3.783, 29 }, { 3.786, 30 }, { 3.789, 31 }, { 3.794, 32 }, { 3.797, 33 }, { 3.8, 34 }, { 3.802, 35 }, { 3.805, 36 }, { 3.808, 37 }, { 3.811, 38 }, { 3.815, 39 }, { 3.818, 40 }, { 3.822, 41 }, { 3.825, 42 }, { 3.829, 43 }, { 3.833, 44 }, { 3.836, 45 }, { 3.84, 46 }, { 3.843, 47 }, { 3.847, 48 }, { 3.85, 49 }, { 3.854, 50 }, { 3.857, 51 }, { 3.86, 52 }, { 3.863, 53 }, { 3.866, 54 }, { 3.87, 55 }, { 3.874, 56 }, { 3.879, 57 }, { 3.888, 58 }, { 3.893, 59 }, { 3.897, 60 }, { 3.902, 61 }, { 3.906, 62 }, { 3.911, 63 }, { 3.918, 64 }, { 3.923, 65 }, { 3.928, 66 }, { 3.939, 67 }, { 3.943, 68 }, { 3.949, 69 }, { 3.955, 70 }, { 3.961, 71 }, { 3.968, 72 }, { 3.974, 73 }, { 3.981, 74 }, { 3.987, 75 }, { 3.994, 76 }, { 4.001, 77 }, { 4.007, 78 }, { 4.014, 79 }, { 4.021, 80 }, { 4.029, 81 }, { 4.036, 82 }, { 4.044, 83 }, { 4.052, 84 }, { 4.062, 85 }, { 4.074, 86 }, { 4.085, 87 }, { 4.095, 88 }, { 4.105, 89 }, { 4.111, 90 }, { 4.116, 91 }, { 4.12, 92 }, { 4.125, 93 }, { 4.129, 94 }, { 4.135, 95 }, { 4.145, 96 }, { 4.176, 97 }, { 4.179, 98 }, { 4.193, 99 }, { 4.2, 100 }, { 4.215, 101 }, { 4.23, 102 }, { 4.245, 103 }, { 4.26, 104}, { 4.275, 105 }, { 4.29, 106 }, { 4.305, 107 }, { 4.32, 108 }, { 4.335, 109 }, { 4.36, 110 }, {4.36, 110 } }
local function create(zone, options)
local battmonitor = { zone=zone, options=options, BattMon="Battery: 0%\nTime: 00:00" }
for i = 0, SAMPLESIZE - 1, 1 do
battArray[i] = 0
end
return battmonitor
end
local function update(battmonitor, options)
battmonitor.options = options
end
local function process()
if getValue(FLIGHTTIMERESET) >= 0 and (timeresetlastupdate > 2) then
timeresetlastupdate = 0
timeresetlastupdate = timeresetlastupdate + 1
flightTime = 0
annunciateTime = 0
playFile("/sounds/en/system/timovr1.wav")
end
-- Read Sensor
local sensorInfo = getSourceInfo(TELEMSENSOR)
if sensorInfo then
local value, isCurrent, isFresh = getSourceValue(sensorInfo.id)
if isCurrent and value then
vbatread = value
else
vbatread = 0
end
end
if vbatread < 3.5 then
cells = 0
result = 0
flightTime = 0
for i = 0, SAMPLESIZE - 1, 1 do
battArray[i] = 0
end
end
--Keep Highest Read Voltage For Each Second
if vbatread > currentvbat then
currentvbat = vbatread
end
if not(lastupdate == getRtcTime()) then
lastupdate = getRtcTime()
timeresetlastupdate = timeresetlastupdate +1
battArray[battIndex] = currentvbat
currentvbat = 0
battIndex = battIndex + 1
if battIndex > SAMPLESIZE -1 then
battIndex = 0
end
--Find Max Value Within Last SAMPLESIZE Default = 8-Seconds
vbat = 0
for i = 0, SAMPLESIZE -1, 1 do
if battArray[i] > vbat then
vbat = battArray[i]
end
end
for i, v in ipairs(myArrayPercentList) do
if v[1] >= vbat/cells then
result = v[2]
break
end
end
if vbatread < 3.5 then
previousvbat = 0
bootupcomplete = 0
vbatcount = 0
end
if bootupcomplete == 0 and vbatread > 3.5 then
if vbatread == previousvbat then
vbatcount = vbatcount + 1
end
previousvbat = vbatread
--When Battery Is First Connected Wait a Few Cycles With Same Result to Get Full Voltage Before Annunication
if vbatcount >= 2 then
if cells == 0 then
cells = math.ceil(vbatread/4.36)
else
bootupcomplete = 1
flightTime = 0
annunciateTime = 0
if result <= 50 then
playTone(0,250,PLAY_NOW)
playFile("/SOUNDS/en/lowbat.wav")
end
playNumber(cells,0)
playFile("/SOUNDS/en/cell.wav")
playNumber(result,0)
playFile("/SOUNDS/en/system/percent0.wav")
end
end
--elseif getLogicalSwitchValue(0) and not(cells==0) then
elseif getValue(ARMSWITCH) < 0 then
flightTime = flightTime + 1
annunciateTime = annunciateTime + 1
if annunciateTime >= ANNUNCIATESEC or (annunciateTime >= ANNUNCIATELOWBATTSEC and result <= LOWBATTPERCENT) then
annunciateTime = 0
if result <= LOWBATTPERCENT then
playTone(2500,500,PLAY_NOW)
playTone(0,500,PLAY_NOW)
end
playNumber(result,0)
playFile("/SOUNDS/en/system/percent0.wav")
if math.floor(flightTime/60) > 0 then
playNumber(math.floor(flightTime/60),0)
playFile("/SOUNDS/en/system/minute1.wav")
end
playNumber(math.floor(flightTime%60),0)
playFile("/SOUNDS/en/system/second1.wav")
end
end
end
return
end
local function background(battmonitor)
process()
battmonitor.BattMon = "Cells: " .. cells .. "\nBattery: " .. result .. "%" .. "\nTime: " .. string.format("%02d",math.floor(flightTime/60)) .. ":" .. string.format("%02d",math.floor(flightTime%60)) .. "\nPack: " .. string.format("%.2f", vbat)
if battmonitor.options.Shadow == 0 then
lcd.drawText(battmonitor.zone.x, battmonitor.zone.y, battmonitor.BattMon, LEFT + DBLSIZE + CUSTOM_COLOR);
else
lcd.drawText(battmonitor.zone.x, battmonitor.zone.y, battmonitor.BattMon, LEFT + DBLSIZE + CUSTOM_COLOR + SHADOWED);
end
end
function refresh(battmonitor)
process()
battmonitor.BattMon = "Cells: " .. cells .. "\nBattery: " .. result .. "%" .. "\nTime: " .. string.format("%02d",math.floor(flightTime/60)) .. ":" .. string.format("%02d",math.floor(flightTime%60) ) .. "\nPack: " .. string.format("%.2f", vbat)
lcd.setColor(CUSTOM_COLOR, battmonitor.options.Color)
if battmonitor.options.Shadow == 0 then
lcd.drawText(battmonitor.zone.x, battmonitor.zone.y, battmonitor.BattMon, LEFT + DBLSIZE + CUSTOM_COLOR);
else
lcd.drawText(battmonitor.zone.x, battmonitor.zone.y, battmonitor.BattMon, LEFT + DBLSIZE + CUSTOM_COLOR + SHADOWED);
end
end
return { name="BattMon", options=options, create=create, update=update, refresh=refresh, background=background }