This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
forked from DynamikArray/KISS_Battery_Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KISS.lua
199 lines (156 loc) · 5.37 KB
/
KISS.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
local versionInfo = "KISS TLM v1.3.1"
local blnMenuMode = 0
-- mahTarget is used to set our target mah consumption and mahAlertPerc is used for division of alerts
local mahTarget = 1250
local mahAlertPerc = 10
-- OpenTX 2.0 - Percent Unit = 8 // OpenTx 2.1/2.2 - 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
----------------------------------------------------------------
-- 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")
data.volts = getTelemetryId("VFAS")
-------------------------------------------------------------------------
-- 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
tmpVolts = round(getValue(data.volts), 2)
-- 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, 10, "USED: " .. lastKnownMah .. "mah", MIDSIZE)
lcd.drawText(90, 30, percVal .. " %", MIDSIZE)
lcd.drawText(150, 25, tmpVolts .. " V", 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
----------------------------------------------------------------
-- 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 then
mahAlertPerc = mahAlertPerc + 1
end
-- Long Presses
if event == 68 then
mahAlertPerc = mahAlertPerc + 1
end
if event == EVT_MINUS_FIRST then
mahAlertPerc = mahAlertPerc - 1
end
-- Long Presses
if event == 69 then
mahAlertPerc = mahAlertPerc - 1
end
lcd.clear()
lcd.drawScreenTitle(versionInfo, 2, 2)
lcd.drawText(35, 10, "Set Percentage Notification")
lcd.drawText(70, 20, "Every " .. mahAlertPerc .. " %", MIDSIZE)
lcd.drawText(66, 35, "Use +/- to change", SMLSIZE)
lcd.drawText(60, 55, "Press [MENU] to return", SMLSIZE)
else
if event == 32 then
--Put us in menu mode
blnMenuMode = 1
end
-- Respond to user KeyPresses for mahSetup
if event == EVT_PLUS_FIRST then
mahTarget = mahTarget + 50
end
if event == 68 then
mahTarget = mahTarget + 50
end
if event == EVT_MINUS_FIRST then
mahTarget = mahTarget - 50
end
if event == 69 then
mahTarget = mahTarget - 50
end
--Update our screen
lcd.clear()
lcd.drawScreenTitle(versionInfo, 1, 2)
lcd.drawGauge(6, 25, 70, 20, percVal, 100)
lcd.drawText(130, 10, "Target mAh : ", MIDSIZE)
lcd.drawText(160, 25, mahTarget, MIDSIZE)
lcd.drawText(130, 40, "Use +/- to change", SMLSIZE)
lcd.drawText(30, 55, "Press [MENU] for more options", SMLSIZE)
draw()
doMahAlert()
end
end
return { run = run_func, background = bg_func, init = init_func }