-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKISSQ7.lua
241 lines (174 loc) · 5.61 KB
/
KISSQ7.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
local versionInfo = "KISS Telemetry - v1.4"
local blnMenuMode = 0
local editMahMode = 0
-- mahTarget is used to set our target mah consumption and mahAlertPerc is used for division of alerts
local mahTarget = 1200
local mahAlertPerc = 90
-- OpenTX 2.0 - Percent Unit = 8 // OpenTx 2.1 - Percent Unit = 13
-- see: https://opentx.gitbooks.io/opentx-lua-reference-guide/content/general/playNumber.html
local percentUnit = 13
local lastMahAlert = 0
-- Fixes mahAlert not appearing after battery disconnect
local lastKnownMah = 0
local minV = 100.0
----------------------------------------------------------------
-- Custom Functions
----------------------------------------------------------------
local function getTelemetryId(name)
field = getFieldInfo(name)
if getFieldInfo(name) then return field.id end
return -1
end
local data = {}
data.fuelUsed = getTelemetryId("Fuel")
-------------------------------------------------------------------------
-- Utilities
-------------------------------------------------------------------------
-- Rounding Function
local function round(val, decimal)
local exp = decimal and 10^decimal or 1
return math.ceil(val * exp - 0.5) / exp
end
--MahAlert and Logging of last Value Played
local function playMahPerc(percVal)
playNumber(percVal,percentUnit)
lastMahAlert = percVal -- Set our lastMahAlert
end
local function playCritical(percVal)
playFile("batcrit.wav")
lastMahAlert = percVal -- Set our lastMahAlert
end
local function playAlerts()
percVal = 0
curMah = getValue(data.fuelUsed)
if curMah ~= 0 then
percVal = round(((curMah/mahTarget) * 100),0)
if percVal ~= lastMahAlert then
-- Alert the user we are in critical alert
if percVal > 100 then
playCritical(percVal)
elseif percVal > 90 and percVal < 100 then
playMahPerc(percVal)
elseif percVal % mahAlertPerc == 0 then
playMahPerc(percVal)
end
end
end
end
local function drawAlerts()
percVal = 0
-- Added to fix mah reseting to Zero on battery disconnects
tmpMah = getValue(data.fuelUsed)
if tmpMah ~= 0 then
lastKnownMah = tmpMah
end
-- The display of MAH data is now pulled from the lastKnownMah var which will only
-- be reset on Telemetry reset now.
percVal = round(((lastKnownMah/mahTarget) * 100),0)
lcd.drawText(5, 9, "USED: "..lastKnownMah.."mah" , MIDSIZE)
lcd.drawText(90, 23, percVal.." %" , MIDSIZE)
end
local function doMahAlert()
playAlerts()
drawAlerts()
end
local function draw()
drawAlerts()
end
----------------------------------------------------------------
--
----------------------------------------------------------------
local function init_func()
doMahAlert()
end
--------------------------------
----------------------------------------------------------------
-- Should handle any flow needed when the screen is NOT visible
----------------------------------------------------------------
local function bg_func()
playAlerts()
end
--------------------------------
function round(num, decimals)
local mult = 10^(decimals or 0)
return math.floor(num * mult + 0.5) / mult
end
----------------------------------------------------------------
-- Should handle any flow needed when the screen is visible
-- All screen updating should be done by as little (one) function
-- outside of this run_func
----------------------------------------------------------------
local function run_func(event)
if blnMenuMode == 1 then
--We are in our menu mode
if event == 32 then
--Take us out of menu mode
blnMenuMode = 0
end
-- Respond to user KeyPresses for mahSetup
if event == EVT_PLUS_FIRST or event == EVT_ROT_RIGHT then
mahAlertPerc = mahAlertPerc + 5
end
-- Long Presses
if event == 68 then
mahAlertPerc = mahAlertPerc + 1
end
if event == EVT_MINUS_FIRST or event == EVT_ROT_LEFT then
mahAlertPerc = mahAlertPerc - 5
end
-- Long Presses
if event == 69 then
mahAlertPerc = mahAlertPerc - 1
end
lcd.clear()
lcd.drawScreenTitle(versionInfo,2,2)
lcd.drawText(20,15, "Set Notification")
lcd.drawText(25,28,"Every "..mahAlertPerc.." %",MIDSIZE)
lcd.drawText(20,45, "Use wheel to change",SMLSIZE)
lcd.drawText(15, 58, "Press [MENU] to return",SMLSIZE)
else
if event == 32 then
--Put us in menu mode
blnMenuMode = 1
editMahMode = 0
end
if event == EVT_ENTER_BREAK then
if editMahMode == 1 then
editMahMode = 0
else
editMahMode = 1
end
end
-- Respond to user KeyPresses for mahSetup
if editMahMode == 1 and event == EVT_ROT_RIGHT then
mahTarget = mahTarget + 25
end
if editMahMode == 1 and event == EVT_ROT_LEFT then
mahTarget = mahTarget - 25
end
--Update our screen
lcd.clear()
lcd.drawScreenTitle(versionInfo,1,2)
lcd.drawGauge(6, 23, 70, 15, percVal, 100)
lcd.drawText(6, 41, "Target mAh : ",SMLSIZE)
if editMahMode == 0 then
lcd.drawText(65, 41, mahTarget,SMLSIZE)
else
lcd.drawText(65, 41, mahTarget,INVERS)
end
local vfas = getValue("VFAS")
if vfas < 0 then vfas = 0 end
lcd.drawText(95, 41, round(vfas, 2).."V", SMLSIZE)
if vfas > 0 and vfas < minV then minV = vfas end
if(minV < 100) then
lcd.drawText(76, 49, "min:"..round(minV, 2).."V", SMLSIZE)
else
lcd.drawText(76, 49, "min:0V", SMLSIZE)
end
lcd.drawText(7, 58, "Press [MENU] for more",SMLSIZE)
draw()
doMahAlert()
end
end
--------------------------------
return {run=run_func, background=bg_func, init=init_func }