Skip to content

Commit

Permalink
Merge pull request betaflight#495 from atomgomba/hide-gps-if-unavailable
Browse files Browse the repository at this point in the history
Hide GPS settings if unavailable
  • Loading branch information
haslinghuis authored Jan 25, 2024
2 parents 9e7c375 + 571dfd5 commit 569e409
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 58 deletions.
3 changes: 0 additions & 3 deletions src/SCRIPTS/BF/CONFIRM/vtx_tables.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local template = assert(loadScript(radio.template))()
local margin = template.margin
local indent = template.indent
local lineSpacing = template.lineSpacing
local tableSpacing = template.tableSpacing
local sp = template.listSpacing.field
local yMinLim = radio.yMinLimit
local x = margin
local y = yMinLim - lineSpacing
Expand Down
11 changes: 11 additions & 0 deletions src/SCRIPTS/BF/PAGES/INIT/vtx.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local function precondition()
local hasVtxTable = loadScript("VTX_TABLES/"..mcuId..".lua")
collectgarbage()
if hasVtxTable then
return nil
else
return "CONFIRM/vtx_tables.lua"
end
end

return precondition()
4 changes: 1 addition & 3 deletions src/SCRIPTS/BF/PAGES/vtx.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local template = assert(loadScript(radio.template))()
local margin = template.margin
local indent = template.indent
local lineSpacing = template.lineSpacing
local tableSpacing = template.tableSpacing
local sp = template.listSpacing.field
local yMinLim = radio.yMinLimit
local x = margin
Expand All @@ -13,7 +11,7 @@ local fields = {}

local vtx_tables
if apiVersion >= 1.42 then
vtx_tables = assert(loadScript("VTX_TABLES/"..mcuId..".lua"))()
vtx_tables = assert(loadScript("VTX_TABLES/"..mcuId..".lua"), "No VTX table!")()
else
vtx_tables = assert(loadScript("VTX_TABLES/vtx_defaults.lua"))()
end
Expand Down
1 change: 1 addition & 0 deletions src/SCRIPTS/BF/features.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local features = {
vtx = true,
gps = true,
}

return features
52 changes: 52 additions & 0 deletions src/SCRIPTS/BF/features_info.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local MSP_GPS_CONFIG = 135
local MSP_VTX_CONFIG = 88

local isGpsRead = false
local isVtxRead = false

local lastRunTS = 0
local INTERVAL = 100

local returnTable = {
f = nil,
t = "",
}

local function processMspReply(cmd, payload, err)
local isOkay = not err
if cmd == MSP_GPS_CONFIG then
isGpsRead = true
local providerSet = payload[1] ~= 0
features.gps = isOkay and providerSet
elseif cmd == MSP_VTX_CONFIG then
isVtxRead = true
local vtxTableAvailable = payload[12] ~= 0
features.vtx = isOkay and vtxTableAvailable
end
end

local function updateFeatures()
if lastRunTS + INTERVAL < getTime() then
lastRunTS = getTime()
local cmd
if not isGpsRead then
cmd = MSP_GPS_CONFIG
returnTable.t = "Checking GPS..."
elseif not isVtxRead then
cmd = MSP_VTX_CONFIG
returnTable.t = "Checking VTX..."
end
if cmd then
protocol.mspRead(cmd)
else
return true
end
end
mspProcessTxQ()
processMspReply(mspPollReply())
return false
end

returnTable.f = updateFeatures

return returnTable
8 changes: 4 additions & 4 deletions src/SCRIPTS/BF/pages.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local PageFiles = {}

if apiVersion >= 1.36 then
PageFiles[#PageFiles + 1] = { title = "VTX Settings", script = "vtx.lua" }
if apiVersion >= 1.36 and features.vtx then
PageFiles[#PageFiles + 1] = { title = "VTX Settings", script = "vtx.lua", init = "PAGES/INIT/vtx.lua" }
end

if apiVersion >= 1.16 then
Expand Down Expand Up @@ -48,11 +48,11 @@ if apiVersion >= 1.16 then
PageFiles[#PageFiles + 1] = { title = "Failsafe", script = "failsafe.lua" }
end

if apiVersion >= 1.41 then
if apiVersion >= 1.41 and features.gps then
PageFiles[#PageFiles + 1] = { title = "GPS Rescue", script = "rescue.lua" }
end

if apiVersion >= 1.41 then
if apiVersion >= 1.41 and features.gps then
PageFiles[#PageFiles + 1] = { title = "GPS PIDs", script = "gpspids.lua" }
end

Expand Down
39 changes: 18 additions & 21 deletions src/SCRIPTS/BF/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,6 @@ local function confirm(page)
collectgarbage()
end

local function filterAvailablePages(pageFiles)
local newPageFiles = pageFiles

local function skipPage(script)
local currentPageFiles = {}
for i = 1, #newPageFiles do
if newPageFiles[i].script ~= script then
currentPageFiles[#currentPageFiles + 1] = newPageFiles[i]
end
end
newPageFiles = currentPageFiles
end

if not features.vtx then skipPage("vtx.lua") end

return newPageFiles
end

local function createPopupMenu()
popupMenuActive = 1
popupMenu = {}
Expand Down Expand Up @@ -314,7 +296,7 @@ local function run_ui(event)
return 0
end
init = nil
PageFiles = filterAvailablePages(assert(loadScript("pages.lua"))())
PageFiles = assert(loadScript("pages.lua"))()
invalidatePages()
uiState = prevUiState or uiStatus.mainMenu
prevUiState = nil
Expand Down Expand Up @@ -403,8 +385,23 @@ local function run_ui(event)
end
end
if not Page then
Page = assert(loadScript("PAGES/"..PageFiles[currentPage].script))()
collectgarbage()
local function selectPage(page)
Page = assert(loadScript("PAGES/"..page))()
collectgarbage()
end

local selectedPage = PageFiles[currentPage]
if selectedPage.init then
local initScript = assert(loadScript(selectedPage.init), "Missing init script")()
collectgarbage()
if initScript then
confirm(initScript)
else
selectPage(selectedPage.script)
end
else
selectPage(selectedPage.script)
end
end
if not Page.values and pageState == pageStatus.display then
requestPage()
Expand Down
38 changes: 11 additions & 27 deletions src/SCRIPTS/BF/ui_init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local apiVersionReceived = false
local vtxTablesReceived = false
local mcuIdReceived = false
local boardInfoReceived = false
local getApiVersion, getVtxTables, getMCUId, getBoardInfo
local featuresReceived = false
local getApiVersion, getMCUId, getBoardInfo, getFeaturesInfo
local returnTable = { f = nil, t = "" }

local function init()
Expand All @@ -22,30 +22,6 @@ local function init()
mcuIdReceived = getMCUId.f()
if mcuIdReceived then
getMCUId = nil
local f = loadScript("VTX_TABLES/" .. mcuId .. ".lua")
if f then
local table = f()
if table then
vtxTablesReceived = true
features.vtx = 0 < table.frequenciesPerBand
f = nil
table = nil
end
end
collectgarbage()
f = loadScript("BOARD_INFO/"..mcuId..".lua")
if f and f() then
boardInfoReceived = true
f = nil
end
collectgarbage()
end
elseif not vtxTablesReceived and apiVersion >= 1.42 then
getVtxTables = getVtxTables or assert(loadScript("vtx_tables.lua"))()
returnTable.t = getVtxTables.t
vtxTablesReceived = getVtxTables.f()
if vtxTablesReceived then
getVtxTables = nil
collectgarbage()
end
elseif not boardInfoReceived and apiVersion >= 1.44 then
Expand All @@ -56,10 +32,18 @@ local function init()
getBoardInfo = nil
collectgarbage()
end
elseif not featuresReceived and apiVersion >= 1.41 then
getFeaturesInfo = getFeaturesInfo or assert(loadScript("features_info.lua"))()
returnTable.t = getFeaturesInfo.t
featuresReceived = getFeaturesInfo.f()
if featuresReceived then
getFeaturesInfo = nil
collectgarbage()
end
else
return true
end
return apiVersionReceived and vtxTablesReceived and mcuId and boardInfoReceived
return apiVersionReceived and vtxTablesReceived and mcuId and boardInfoReceived and featuresReceived
end

returnTable.f = init
Expand Down

0 comments on commit 569e409

Please sign in to comment.