Skip to content

Commit

Permalink
Craft name for vtx tables
Browse files Browse the repository at this point in the history
Reads craft name from MSP and uses that name to store the vtx tables. If the craft name is empty it falls backs on using the model name as before.
  • Loading branch information
kristjanbjarni committed Sep 15, 2020
1 parent f58c429 commit 4b87e63
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/SCRIPTS/BF/PAGES/vtx.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local md = model.getInfo();
local vtx_tables = loadScript("/BF/VTX/"..md.name..".lua")
local vtx_tables = loadScript("/BF/VTX/"..craftName..".lua")
if vtx_tables then
vtx_tables = vtx_tables()
else
Expand Down
37 changes: 37 additions & 0 deletions src/SCRIPTS/BF/craft_name.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local MSP_UID = 160

local craftNameReceived = false

local lastRunTS = 0
local INTERVAL = 100

local function processMspReply(cmd, payload)
if cmd == MSP_UID then
local i = 1
local name = ""
while payload[i] do
name = name..string.format("%02X",payload[i])
i = i+1
end
if name == "" then
craftName = model.getInfo().name
else
craftName = name
end
craftNameReceived = true
end
end

local function getCraftName()
if lastRunTS + INTERVAL < getTime() then
lastRunTS = getTime()
if not craftNameReceived then
protocol.mspRead(MSP_UID)
end
end
mspProcessTxQ()
processMspReply(mspPollReply())
return craftNameReceived
end

return getCraftName
2 changes: 1 addition & 1 deletion src/SCRIPTS/BF/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ local function getVtxTables()
uiState = uiStatus.init
PageFiles = nil
invalidatePages()
io.close(io.open("/BF/VTX/"..model.getInfo().name..".lua", 'w'))
io.close(io.open("/BF/VTX/"..craftName..".lua", 'w'))
return 0
end

Expand Down
28 changes: 19 additions & 9 deletions src/SCRIPTS/BF/ui_init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
local apiVersionReceived = false
local vtxTablesReceived = false
local data_init, getVtxTables
local vtxTables = loadScript("/BF/VTX/"..model.getInfo().name..".lua")

if vtxTables and vtxTables() then
vtxTablesReceived = true
vtxTables = nil
collectgarbage()
end
local data_init, getVtxTables, getCraftName

local function init()
if apiVersion == 0 then
Expand All @@ -18,6 +11,23 @@ local function init()
data_init = nil
apiVersionReceived = true
collectgarbage()
elseif not craftName then
if apiVersion >= 1.042 then
lcd.drawText(6, radio.yMinLimit, "Waiting for craft name")
getCraftName = getCraftName or assert(loadScript("craft_name.lua"))()
local craftNameReceived = getCraftName()
if craftNameReceived then
getCraftName = nil
local vtxTables = loadScript("/BF/VTX/"..craftName..".lua")
if vtxTables and vtxTables() then
vtxTablesReceived = true
vtxTables = nil
end
collectgarbage()
end
else
craftName = model.getInfo().name
end
elseif apiVersion >= 1.042 and not vtxTablesReceived then
lcd.drawText(6, radio.yMinLimit, "Downloading VTX Tables")
getVtxTables = getVtxTables or assert(loadScript("vtx_tables.lua"))()
Expand All @@ -29,7 +39,7 @@ local function init()
else
return true
end
return apiVersionReceived and vtxTablesReceived
return apiVersionReceived and vtxTablesReceived and craftName
end

return init
4 changes: 2 additions & 2 deletions src/SCRIPTS/BF/vtx_tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ local function getVtxTables()
end
end
if vtxTablesReceived then
local f = io.open("/BF/VTX/"..model.getInfo().name..".lua", 'w')
local f = io.open("/BF/VTX/"..craftName..".lua", 'w')
io.write(f, "return {", "\n")
io.write(f, " frequencyTable = {", "\n")
for i = 1, #frequencyTable do
Expand All @@ -109,7 +109,7 @@ local function getVtxTables()
io.write(f, powerString, "\n")
io.write(f, "}", "\n")
io.close(f)
assert(loadScript("/BF/VTX/"..model.getInfo().name..".lua", 'c'))
assert(loadScript("/BF/VTX/"..craftName..".lua", 'c'))
end
mspProcessTxQ()
processMspReply(mspPollReply())
Expand Down
1 change: 1 addition & 0 deletions src/SCRIPTS/TOOLS/bf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local toolName = "TNS|Betaflight setup|TNE"
chdir("/SCRIPTS/BF")

apiVersion = 0
craftName = nil

local run = nil
local scriptsCompiled = assert(loadScript("COMPILE/scripts_compiled.lua"))()
Expand Down

0 comments on commit 4b87e63

Please sign in to comment.