Skip to content

Commit 1daa86b

Browse files
authored
Merge pull request #389 from klutvott123/popup-improvements
2 parents 2bb514d + d00b921 commit 1daa86b

File tree

9 files changed

+146
-94
lines changed

9 files changed

+146
-94
lines changed

src/SCRIPTS/BF/PAGES/accelerometer.lua renamed to src/SCRIPTS/BF/CONFIRM/acc_cal.lua

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local template = loadScript(radio.templateHome.."accelerometer.lua")
1+
local template = loadScript(radio.templateHome.."acc_cal.lua")
22
if template then
33
template = template()
44
else
@@ -20,17 +20,11 @@ labels[#labels + 1] = { t = "Make sure the craft is level", x = x, y = inc.y(lin
2020
labels[#labels + 1] = { t = "and stable, then press", x = x, y = inc.y(lineSpacing) }
2121
labels[#labels + 1] = { t = "[ENTER] to calibrate, or", x = x, y = inc.y(lineSpacing) }
2222
labels[#labels + 1] = { t = "[EXIT] to cancel.", x = x, y = inc.y(lineSpacing) }
23-
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true, onClick = function(self) self.accCal(self) end }
23+
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true }
2424

2525
return {
26-
write = 205, -- MSP_ACC_CALIBRATION
27-
title = "Accelerometer",
28-
reboot = false,
29-
eepromWrite = false,
30-
minBytes = 0,
31-
labels = labels,
32-
fields = fields,
33-
accCal = function(self)
34-
protocol.mspRead(self.write)
35-
end,
26+
title = "Accelerometer",
27+
labels = labels,
28+
fields = fields,
29+
init = assert(loadScript("acc_cal.lua"))(),
3630
}

src/SCRIPTS/BF/CONFIRM/vtx_tables.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local template = loadScript(radio.templateHome.."vtx_tables.lua")
2+
if template then
3+
template = template()
4+
else
5+
template = assert(loadScript(radio.templateHome.."default_template.lua"))()
6+
end
7+
local margin = template.margin
8+
local indent = template.indent
9+
local lineSpacing = template.lineSpacing
10+
local tableSpacing = template.tableSpacing
11+
local sp = template.listSpacing.field
12+
local yMinLim = radio.yMinLimit
13+
local x = margin
14+
local y = yMinLim - lineSpacing
15+
local inc = { x = function(val) x = x + val return x end, y = function(val) y = y + val return y end }
16+
local labels = {}
17+
local fields = {}
18+
19+
labels[#labels + 1] = { t = "Download VTX tables? Press", x = x, y = inc.y(lineSpacing) }
20+
labels[#labels + 1] = { t = "[ENTER] to download, or", x = x, y = inc.y(lineSpacing) }
21+
labels[#labels + 1] = { t = "[EXIT] to cancel.", x = x, y = inc.y(lineSpacing) }
22+
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true }
23+
24+
return {
25+
title = "VTX Tables",
26+
labels = labels,
27+
fields = fields,
28+
init = assert(loadScript("vtx_tables.lua"))(),
29+
}

src/SCRIPTS/BF/acc_cal.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local MSP_ACC_CALIBRATION = 205
2+
local accCalibrated = false
3+
local lastRunTS = 0
4+
local INTERVAL = 500
5+
6+
local function processMspReply(cmd,rx_buf)
7+
if cmd == MSP_ACC_CALIBRATION then
8+
accCalibrated = true
9+
end
10+
end
11+
12+
local function accCal()
13+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
14+
lastRunTS = getTime()
15+
if not accCalibrated then
16+
protocol.mspRead(MSP_ACC_CALIBRATION)
17+
end
18+
end
19+
mspProcessTxQ()
20+
processMspReply(mspPollReply())
21+
return accCalibrated
22+
end
23+
24+
return { f = accCal, t = "Calibrating Accelerometer" }

src/SCRIPTS/BF/background.lua

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
local sensorId = -1
21
local dataInitialised = false
32
local data_init = nil
43
local rssiEnabled = true
54
local rssiTask = nil
65

7-
local function getSensorValue()
8-
if sensorId == -1 then
9-
local sensor = getFieldInfo(protocol.stateSensor)
10-
if type(sensor) == "table" then
11-
sensorId = sensor['id'] or -1
12-
end
13-
end
14-
return getValue(sensorId)
15-
end
16-
17-
local function modelActive(sensorValue)
18-
return type(sensorValue) == "number" and sensorValue > 0
6+
local function modelActive()
7+
return getValue(protocol.stateSensor) > 0
198
end
209

2110
local function run_bg()
22-
local sensorValue = getSensorValue()
23-
if modelActive(sensorValue) then
11+
if modelActive() then
2412
-- Send data when the telemetry connection is available
2513
-- assuming when sensor value higher than 0 there is an telemetry connection
2614
if not dataInitialised then
2715
if not data_init then
2816
data_init = assert(loadScript("data_init.lua"))()
2917
end
3018

31-
dataInitialised = data_init()
19+
dataInitialised = data_init.f()
3220

3321
if dataInitialised then
3422
data_init = nil

src/SCRIPTS/BF/data_init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ local function init()
6363
return apiVersionReceived and timeIsSet
6464
end
6565

66-
return init
66+
return { f = init, t = "Waiting for API version" }

src/SCRIPTS/BF/mcu_id.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ local function getMCUId()
3434
return MCUIdReceived
3535
end
3636

37-
return getMCUId
37+
return { f = getMCUId, t = "Waiting for device ID" }

src/SCRIPTS/BF/ui.lua

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
local uiStatus =
22
{
33
init = 1,
4-
pages = 2,
5-
mainMenu = 3,
4+
mainMenu = 2,
5+
pages = 3,
6+
confirm = 4,
67
}
78

89
local pageStatus =
910
{
10-
display = 2,
11-
editing = 3,
12-
saving = 4,
13-
popupMenu = 5,
11+
display = 1,
12+
editing = 2,
13+
saving = 3,
1414
}
1515

1616
local uiMsp =
@@ -20,6 +20,7 @@ local uiMsp =
2020
}
2121

2222
local uiState = uiStatus.init
23+
local prevUiState
2324
local pageState = pageStatus.display
2425
local requestTimeout = 80
2526
local currentPage = 1
@@ -32,7 +33,7 @@ local popupMenuActive = 1
3233
local killEnterBreak = 0
3334
local pageScrollY = 0
3435
local mainMenuScrollY = 0
35-
local PageFiles, Page, init, popupMenuList
36+
local PageFiles, Page, init, popupMenu
3637

3738
local backgroundFill = TEXT_BGCOLOR or ERASE
3839
local foregroundColor = LINE_COLOR or SOLID
@@ -72,30 +73,26 @@ local function eepromWrite()
7273
protocol.mspRead(uiMsp.eepromWrite)
7374
end
7475

75-
local function accCal()
76+
local function confirm(page)
77+
prevUiState = uiState
78+
uiState = uiStatus.confirm
7679
invalidatePages()
7780
currentField = 1
78-
Page = assert(loadScript("Pages/accelerometer.lua"))()
81+
Page = assert(loadScript(page))()
7982
collectgarbage()
8083
end
8184

82-
local function getVtxTables()
83-
uiState = uiStatus.init
84-
PageFiles = nil
85-
invalidatePages()
86-
io.close(io.open("/BF/VTX/"..mcuId..".lua", 'w'))
87-
return 0
88-
end
89-
9085
local function createPopupMenu()
91-
popupMenuList = {
92-
{ t = "save page", f = saveSettings },
93-
{ t = "reload", f = invalidatePages },
94-
{ t = "reboot", f = rebootFc },
95-
{ t = "acc cal", f = accCal },
96-
}
86+
popupMenuActive = 1
87+
popupMenu = {}
88+
if uiState == uiStatus.pages then
89+
popupMenu[#popupMenu + 1] = { t = "save page", f = saveSettings }
90+
popupMenu[#popupMenu + 1] = { t = "reload", f = invalidatePages }
91+
end
92+
popupMenu[#popupMenu + 1] = { t = "reboot", f = rebootFc }
93+
popupMenu[#popupMenu + 1] = { t = "acc cal", f = function() confirm("CONFIRM/acc_cal.lua") end }
9794
if apiVersion >= 1.042 then
98-
popupMenuList[#popupMenuList + 1] = { t = "vtx tables", f = getVtxTables }
95+
popupMenu[#popupMenu + 1] = { t = "vtx tables", f = function() confirm("CONFIRM/vtx_tables.lua") end }
9996
end
10097
end
10198

@@ -162,7 +159,7 @@ local function incMainMenu(inc)
162159
end
163160

164161
local function incPopupMenu(inc)
165-
popupMenuActive = clipValue(popupMenuActive + inc, 1, #popupMenuList)
162+
popupMenuActive = clipValue(popupMenuActive + inc, 1, #popupMenu)
166163
end
167164

168165
local function requestPage()
@@ -251,13 +248,13 @@ local function drawPopupMenu()
251248
local w = radio.MenuBox.w
252249
local h_line = radio.MenuBox.h_line
253250
local h_offset = radio.MenuBox.h_offset
254-
local h = #popupMenuList * h_line + h_offset*2
251+
local h = #popupMenu * h_line + h_offset*2
255252

256253
lcd.drawFilledRectangle(x,y,w,h,backgroundFill)
257254
lcd.drawRectangle(x,y,w-1,h-1,foregroundColor)
258255
lcd.drawText(x+h_line/2,y+h_offset,"Menu:",globalTextOptions)
259256

260-
for i,e in ipairs(popupMenuList) do
257+
for i,e in ipairs(popupMenu) do
261258
local textOptions = globalTextOptions
262259
if popupMenuActive == i then
263260
textOptions = textOptions + INVERS
@@ -267,18 +264,35 @@ local function drawPopupMenu()
267264
end
268265

269266
local function run_ui(event)
270-
if uiState == uiStatus.init then
267+
if popupMenu then
268+
drawPopupMenu()
269+
if event == EVT_VIRTUAL_EXIT then
270+
popupMenu = nil
271+
elseif event == EVT_VIRTUAL_PREV then
272+
incPopupMenu(-1)
273+
elseif event == EVT_VIRTUAL_NEXT then
274+
incPopupMenu(1)
275+
elseif event == EVT_VIRTUAL_ENTER then
276+
if killEnterBreak == 1 then
277+
killEnterBreak = 0
278+
else
279+
popupMenu[popupMenuActive].f()
280+
popupMenu = nil
281+
end
282+
end
283+
elseif uiState == uiStatus.init then
271284
lcd.clear()
272285
drawScreenTitle("Betaflight Config")
273286
init = init or assert(loadScript("ui_init.lua"))()
274-
if not init() then
287+
lcd.drawText(6, radio.yMinLimit, init.t)
288+
if not init.f() then
275289
return 0
276290
end
277291
init = nil
278-
createPopupMenu()
279292
PageFiles = assert(loadScript("pages.lua"))()
280293
invalidatePages()
281-
uiState = uiStatus.mainMenu
294+
uiState = prevUiState or uiStatus.mainMenu
295+
prevUiState = nil
282296
elseif uiState == uiStatus.mainMenu then
283297
if event == EVT_VIRTUAL_EXIT then
284298
return 2
@@ -288,6 +302,9 @@ local function run_ui(event)
288302
incMainMenu(-1)
289303
elseif event == EVT_VIRTUAL_ENTER then
290304
uiState = uiStatus.pages
305+
elseif event == EVT_VIRTUAL_ENTER_LONG then
306+
killEnterBreak = 1
307+
createPopupMenu()
291308
end
292309
lcd.clear()
293310
local yMinLim = radio.yMinLimit
@@ -322,21 +339,6 @@ local function run_ui(event)
322339
invalidatePages()
323340
end
324341
end
325-
elseif pageState == pageStatus.popupMenu then
326-
if event == EVT_VIRTUAL_EXIT then
327-
pageState = pageStatus.display
328-
elseif event == EVT_VIRTUAL_PREV then
329-
incPopupMenu(-1)
330-
elseif event == EVT_VIRTUAL_NEXT then
331-
incPopupMenu(1)
332-
elseif event == EVT_VIRTUAL_ENTER then
333-
if killEnterBreak == 1 then
334-
killEnterBreak = 0
335-
else
336-
pageState = pageStatus.display
337-
return popupMenuList[popupMenuActive].f() or 0
338-
end
339-
end
340342
elseif pageState == pageStatus.display then
341343
if event == EVT_VIRTUAL_PREV_PAGE then
342344
incPage(-1)
@@ -350,17 +352,13 @@ local function run_ui(event)
350352
elseif event == EVT_VIRTUAL_ENTER then
351353
if Page then
352354
local f = Page.fields[currentField]
353-
if f.onClick then
354-
f.onClick(Page)
355-
end
356355
if Page.values and f.vals and Page.values[f.vals[#f.vals]] and not f.ro then
357356
pageState = pageStatus.editing
358357
end
359358
end
360359
elseif event == EVT_VIRTUAL_ENTER_LONG then
361-
popupMenuActive = 1
362360
killEnterBreak = 1
363-
pageState = pageStatus.popupMenu
361+
createPopupMenu()
364362
elseif event == EVT_VIRTUAL_EXIT then
365363
invalidatePages()
366364
currentField = 1
@@ -380,17 +378,15 @@ local function run_ui(event)
380378
end
381379
end
382380
if not Page then
383-
Page = assert(loadScript("Pages/"..PageFiles[currentPage].script))()
381+
Page = assert(loadScript("PAGES/"..PageFiles[currentPage].script))()
384382
collectgarbage()
385383
end
386384
if not Page.values and pageState == pageStatus.display then
387385
requestPage()
388386
end
389387
lcd.clear()
390388
drawScreen()
391-
if pageState == pageStatus.popupMenu then
392-
drawPopupMenu()
393-
elseif pageState == pageStatus.saving then
389+
if pageState == pageStatus.saving then
394390
local saveMsg = "Saving..."
395391
if saveRetries > 0 then
396392
saveMsg = "Retrying"
@@ -399,6 +395,18 @@ local function run_ui(event)
399395
lcd.drawRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,SOLID)
400396
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,saveMsg,DBLSIZE + globalTextOptions)
401397
end
398+
elseif uiState == uiStatus.confirm then
399+
lcd.clear()
400+
drawScreen()
401+
if event == EVT_VIRTUAL_ENTER then
402+
uiState = uiStatus.init
403+
init = Page.init
404+
invalidatePages()
405+
elseif event == EVT_VIRTUAL_EXIT then
406+
invalidatePages()
407+
uiState = prevUiState
408+
prevUiState = nil
409+
end
402410
end
403411
if protocol.rssi() == 0 then
404412
lcd.drawText(radio.NoTelem[1],radio.NoTelem[2],radio.NoTelem[3],radio.NoTelem[4])

0 commit comments

Comments
 (0)