Skip to content

Commit 248d0fa

Browse files
committed
Download board info
1 parent af7e1ab commit 248d0fa

File tree

3 files changed

+158
-5
lines changed

3 files changed

+158
-5
lines changed

src/SCRIPTS/BF/BOARD_INFO/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Board info downloaded from the flight controller will be stored in this folder.

src/SCRIPTS/BF/board_info.lua

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
local MSP_BOARD_INFO = 4
2+
3+
local boardInfoReceived = false
4+
5+
local boardIdentifier = ""
6+
local hardwareRevision = 0
7+
local boardType = 0
8+
local targetCapabilities = 0
9+
local targetName = ""
10+
local boardName = ""
11+
local manufacturerId = ""
12+
local signature = {}
13+
local mcuTypeId = 255
14+
local configurationState = 0
15+
local gyroSampleRateHz = 0
16+
local configurationProblems = 0
17+
local spiRegisteredDeviceCount = 0
18+
local i2cRegisteredDeviceCount = 0
19+
20+
local lastRunTS = 0
21+
local INTERVAL = 100
22+
23+
local function processMspReply(cmd, payload)
24+
if cmd == MSP_BOARD_INFO then
25+
local length
26+
local i = 1
27+
length = 4
28+
for c = 1, 4 do
29+
boardIdentifier = boardIdentifier..string.char(payload[i])
30+
i = i + 1
31+
end
32+
for idx = 1, 2 do
33+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
34+
hardwareRevision = bit32.bor(hardwareRevision, raw_val)
35+
i = i + 1
36+
end
37+
if apiVersion >= 1.035 then
38+
boardType = payload[i]
39+
end
40+
i = i + 1
41+
if apiVersion >= 1.037 then
42+
targetCapabilities = payload[i]
43+
i = i + 1
44+
length = payload[i]
45+
i = i + 1
46+
for c = 1, length do
47+
targetName = targetName..string.char(payload[i])
48+
i = i + 1
49+
end
50+
end
51+
if apiVersion >= 1.039 then
52+
length = payload[i]
53+
i = i + 1
54+
for c = 1, length do
55+
boardName = boardName..string.char(payload[i])
56+
i = i + 1
57+
end
58+
length = payload[i]
59+
i = i + 1
60+
for c = 1, length do
61+
manufacturerId = manufacturerId..string.char(payload[i])
62+
i = i + 1
63+
end
64+
length = 32
65+
for c = 1, 32 do
66+
signature[#signature + 1] = payload[i]
67+
i = i + 1
68+
end
69+
end
70+
i = i + 1
71+
if apiVersion >= 1.041 then
72+
mcuTypeId = payload[i]
73+
end
74+
i = i + 1
75+
if apiVersion >= 1.042 then
76+
configurationState = payload[i]
77+
end
78+
if apiVersion >= 1.043 then
79+
for idx = 1, 2 do
80+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
81+
gyroSampleRateHz = bit32.bor(gyroSampleRateHz, raw_val)
82+
i = i + 1
83+
end
84+
for idx = 1, 4 do
85+
local raw_val = bit32.lshift(payload[i], (idx-1)*8)
86+
configurationProblems = bit32.bor(configurationProblems, raw_val)
87+
i = i + 1
88+
end
89+
end
90+
if apiVersion >= 1.044 then
91+
spiRegisteredDeviceCount = payload[i]
92+
i = i + 1
93+
i2cRegisteredDeviceCount = payload[i]
94+
end
95+
boardInfoReceived = true
96+
end
97+
end
98+
99+
local function getBoardInfo()
100+
if lastRunTS + INTERVAL < getTime() then
101+
lastRunTS = getTime()
102+
if not boardInfoReceived then
103+
protocol.mspRead(MSP_BOARD_INFO)
104+
end
105+
end
106+
mspProcessTxQ()
107+
processMspReply(mspPollReply())
108+
if boardInfoReceived then
109+
local f = io.open("BOARD_INFO/"..mcuId..".lua", 'w')
110+
io.write(f, "return {", "\n")
111+
io.write(f, " boardIdentifier = "..boardIdentifier..",", "\n")
112+
io.write(f, " hardwareRevision = "..tostring(hardwareRevision)..",", "\n")
113+
io.write(f, " boardType = "..tostring(boardType)..",", "\n")
114+
io.write(f, " targetCapabilities = "..tostring(targetCapabilities)..",", "\n")
115+
io.write(f, " targetName = "..targetName..",", "\n")
116+
io.write(f, " boardName = "..boardName..",", "\n")
117+
io.write(f, " manufacturerId = "..manufacturerId..",", "\n")
118+
local signatureString = " signature = { "
119+
for i = 1, #signature do
120+
signatureString = signatureString..tostring(signature[i])..", "
121+
end
122+
signatureString = signatureString.."},"
123+
io.write(f, signatureString, "\n")
124+
io.write(f, " mcuTypeId = "..tostring(mcuTypeId)..",", "\n")
125+
io.write(f, " configurationState = "..tostring(configurationState)..",", "\n")
126+
io.write(f, " gyroSampleRateHz = "..tostring(gyroSampleRateHz)..",", "\n")
127+
io.write(f, " configurationProblems = "..tostring(configurationProblems)..",", "\n")
128+
io.write(f, " spiRegisteredDeviceCount = "..tostring(spiRegisteredDeviceCount)..",", "\n")
129+
io.write(f, " i2cRegisteredDeviceCount = "..tostring(i2cRegisteredDeviceCount)..",", "\n")
130+
io.write(f, "}", "\n")
131+
io.close(f)
132+
assert(loadScript("BOARD_INFO/"..mcuId..".lua", 'c'))
133+
end
134+
return boardInfoReceived
135+
end
136+
137+
return { f = getBoardInfo, t = "Downloading board info" }

src/SCRIPTS/BF/ui_init.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local apiVersionReceived = false
22
local vtxTablesReceived = false
33
local mcuIdReceived = false
4-
local getApiVersion, getVtxTables, getMCUId
4+
local boardInfoReceived = false
5+
local getApiVersion, getVtxTables, getMCUId, getBoardInfo
56
local returnTable = { f = nil, t = "" }
67

78
local function modelActive()
@@ -25,10 +26,16 @@ local function init()
2526
mcuIdReceived = getMCUId.f()
2627
if mcuIdReceived then
2728
getMCUId = nil
28-
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
29-
if vtxTables and vtxTables() then
29+
local f = loadScript("/BF/VTX/"..mcuId..".lua")
30+
if f and f() then
3031
vtxTablesReceived = true
31-
vtxTables = nil
32+
f = nil
33+
end
34+
collectgarbage()
35+
f = loadScript("BOARD_INFO/"..mcuId..".lua")
36+
if f and f() then
37+
boardInfoReceived = true
38+
f = nil
3239
end
3340
collectgarbage()
3441
end
@@ -40,10 +47,18 @@ local function init()
4047
getVtxTables = nil
4148
collectgarbage()
4249
end
50+
elseif not boardInfoReceived then
51+
getBoardInfo = getBoardInfo or assert(loadScript("board_info.lua"))()
52+
returnTable.t = getBoardInfo.t
53+
boardInfoReceived = getBoardInfo.f()
54+
if boardInfoReceived then
55+
getBoardInfo = nil
56+
collectgarbage()
57+
end
4358
else
4459
return true
4560
end
46-
return apiVersionReceived and vtxTablesReceived and mcuId
61+
return apiVersionReceived and vtxTablesReceived and mcuId and boardInfoReceived
4762
end
4863

4964
returnTable.f = init

0 commit comments

Comments
 (0)