Skip to content

Commit b207b1f

Browse files
MCU id for vtx tables
Reads MCU id from MSP and uses that unique id to store the vtx tables. This should solve the problem of using an incorrect vtx table when there are different crafts with different vtx hardware under the same model name.
1 parent f58c429 commit b207b1f

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

src/SCRIPTS/BF/PAGES/vtx.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
local md = model.getInfo();
2-
local vtx_tables = loadScript("/BF/VTX/"..md.name..".lua")
1+
local vtx_tables = loadScript("/BF/VTX/"..mcuId..".lua")
32
if vtx_tables then
43
vtx_tables = vtx_tables()
54
else

src/SCRIPTS/BF/mcu_id.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local MSP_UID = 160
2+
3+
local MCUIdReceived = false
4+
5+
local lastRunTS = 0
6+
local INTERVAL = 100
7+
8+
local function processMspReply(cmd, payload)
9+
if cmd == MSP_UID then
10+
local i = 1
11+
local id = ""
12+
for j = 1, 3 do
13+
local s = ""
14+
for k = 1, 4 do
15+
s = string.format("%02x", payload[i])..s
16+
i = i + 1
17+
end
18+
id = id..s
19+
end
20+
mcuId = id
21+
MCUIdReceived = true
22+
end
23+
end
24+
25+
local function getMCUId()
26+
if lastRunTS + INTERVAL < getTime() then
27+
lastRunTS = getTime()
28+
if not MCUIdReceived then
29+
protocol.mspRead(MSP_UID)
30+
end
31+
end
32+
mspProcessTxQ()
33+
processMspReply(mspPollReply())
34+
return MCUIdReceived
35+
end
36+
37+
return getMCUId

src/SCRIPTS/BF/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ local function getVtxTables()
7676
uiState = uiStatus.init
7777
PageFiles = nil
7878
invalidatePages()
79-
io.close(io.open("/BF/VTX/"..model.getInfo().name..".lua", 'w'))
79+
io.close(io.open("/BF/VTX/"..mcuId..".lua", 'w'))
8080
return 0
8181
end
8282

src/SCRIPTS/BF/ui_init.lua

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
local apiVersionReceived = false
22
local vtxTablesReceived = false
3-
local data_init, getVtxTables
4-
local vtxTables = loadScript("/BF/VTX/"..model.getInfo().name..".lua")
5-
6-
if vtxTables and vtxTables() then
7-
vtxTablesReceived = true
8-
vtxTables = nil
9-
collectgarbage()
10-
end
3+
local data_init, getVtxTables, getMCUId
114

125
local function init()
136
if apiVersion == 0 then
@@ -18,6 +11,18 @@ local function init()
1811
data_init = nil
1912
apiVersionReceived = true
2013
collectgarbage()
14+
elseif not mcuId then
15+
lcd.drawText(6, radio.yMinLimit, "Waiting for unique device ID")
16+
getMCUId = getMCUId or assert(loadScript("mcu_id.lua"))()
17+
if getMCUId() then
18+
getMCUId = nil
19+
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
20+
if vtxTables and vtxTables() then
21+
vtxTablesReceived = true
22+
vtxTables = nil
23+
end
24+
collectgarbage()
25+
end
2126
elseif apiVersion >= 1.042 and not vtxTablesReceived then
2227
lcd.drawText(6, radio.yMinLimit, "Downloading VTX Tables")
2328
getVtxTables = getVtxTables or assert(loadScript("vtx_tables.lua"))()
@@ -29,7 +34,7 @@ local function init()
2934
else
3035
return true
3136
end
32-
return apiVersionReceived and vtxTablesReceived
37+
return apiVersionReceived and vtxTablesReceived and mcuId
3338
end
3439

3540
return init

src/SCRIPTS/BF/vtx_tables.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ local function getVtxTables()
8282
end
8383
end
8484
if vtxTablesReceived then
85-
local f = io.open("/BF/VTX/"..model.getInfo().name..".lua", 'w')
85+
local f = io.open("/BF/VTX/"..mcuId..".lua", 'w')
8686
io.write(f, "return {", "\n")
8787
io.write(f, " frequencyTable = {", "\n")
8888
for i = 1, #frequencyTable do
@@ -109,7 +109,7 @@ local function getVtxTables()
109109
io.write(f, powerString, "\n")
110110
io.write(f, "}", "\n")
111111
io.close(f)
112-
assert(loadScript("/BF/VTX/"..model.getInfo().name..".lua", 'c'))
112+
assert(loadScript("/BF/VTX/"..mcuId..".lua", 'c'))
113113
end
114114
mspProcessTxQ()
115115
processMspReply(mspPollReply())

src/SCRIPTS/TOOLS/bf.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local toolName = "TNS|Betaflight setup|TNE"
22
chdir("/SCRIPTS/BF")
33

44
apiVersion = 0
5+
mcuId = nil
56

67
local run = nil
78
local scriptsCompiled = assert(loadScript("COMPILE/scripts_compiled.lua"))()

0 commit comments

Comments
 (0)