Skip to content

Commit b2c24ce

Browse files
committed
Split data_init.lua
1 parent 1daa86b commit b2c24ce

File tree

5 files changed

+114
-99
lines changed

5 files changed

+114
-99
lines changed

src/SCRIPTS/BF/api_version.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local MSP_API_VERSION = 1
2+
3+
local apiVersionReceived = false
4+
local lastRunTS = 0
5+
local INTERVAL = 50
6+
7+
local function processMspReply(cmd,rx_buf)
8+
if cmd == MSP_API_VERSION and #rx_buf >= 3 then
9+
apiVersion = rx_buf[2] + rx_buf[3] / 1000
10+
apiVersionReceived = true
11+
end
12+
end
13+
14+
local function getApiVersion()
15+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
16+
protocol.mspRead(MSP_API_VERSION)
17+
lastRunTS = getTime()
18+
end
19+
20+
mspProcessTxQ()
21+
processMspReply(mspPollReply())
22+
23+
return apiVersionReceived
24+
end
25+
26+
return { f = getApiVersion, t = "Waiting for API version" }

src/SCRIPTS/BF/background.lua

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local dataInitialised = false
2-
local data_init = nil
1+
local apiVersionReceived = false
2+
local timeIsSet = false
3+
local getApiVersion, setRtc, rssiTask
34
local rssiEnabled = true
4-
local rssiTask = nil
55

66
local function modelActive()
77
return getValue(protocol.stateSensor) > 0
@@ -11,36 +11,35 @@ local function run_bg()
1111
if modelActive() then
1212
-- Send data when the telemetry connection is available
1313
-- assuming when sensor value higher than 0 there is an telemetry connection
14-
if not dataInitialised then
15-
if not data_init then
16-
data_init = assert(loadScript("data_init.lua"))()
14+
if not apiVersionReceived then
15+
getApiVersion = getApiVersion or assert(loadScript("api_version.lua"))()
16+
apiVersionReceived = getApiVersion.f()
17+
if apiVersionReceived then
18+
getApiVersion = nil
19+
collectgarbage()
1720
end
18-
19-
dataInitialised = data_init.f()
20-
21-
if dataInitialised then
22-
data_init = nil
23-
21+
elseif not timeIsSet then
22+
setRtc = setRtc or assert(loadScript("rtc.lua"))()
23+
timeIsSet = setRtc.f()
24+
if timeIsSet then
25+
setRtc = nil
2426
collectgarbage()
2527
end
2628
elseif rssiEnabled and apiVersion >= 1.037 then
27-
if not rssiTask then
28-
rssiTask = assert(loadScript("rssi.lua"))()
29-
end
30-
29+
rssiTask = rssiTask or assert(loadScript("rssi.lua"))()
3130
rssiEnabled = rssiTask()
32-
3331
if not rssiEnabled then
3432
rssiTask = nil
35-
3633
collectgarbage()
3734
end
3835
end
3936
else
40-
dataInitialised = false
37+
apiVersionReceived = false
38+
timeIsSet = false
4139
rssiEnabled = true
42-
if data_init or rssiTask then
43-
data_init = nil
40+
if getApiVersion or setRtc or rssiTask then
41+
getApiVersion = nil
42+
setRtc = nil
4443
rssiTask = nil
4544
collectgarbage()
4645
end

src/SCRIPTS/BF/data_init.lua

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/SCRIPTS/BF/rtc.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local MSP_SET_RTC = 246
2+
3+
local timeIsSet = false
4+
local lastRunTS = 0
5+
local INTERVAL = 50
6+
7+
local function processMspReply(cmd,rx_buf)
8+
if cmd == MSP_SET_RTC then
9+
timeIsSet = true
10+
end
11+
end
12+
13+
local function setRtc()
14+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
15+
-- only send datetime one time after telemetry connection became available
16+
-- or when connection is restored after e.g. lipo refresh
17+
local values = {}
18+
if apiVersion >= 1.041 then
19+
-- format: seconds after the epoch (32) / milliseconds (16)
20+
local now = getRtcTime()
21+
22+
for i = 1, 4 do
23+
values[i] = bit32.band(now, 0xFF)
24+
now = bit32.rshift(now, 8)
25+
end
26+
27+
values[5] = 0 -- we don't have milliseconds
28+
values[6] = 0
29+
else
30+
-- format: year (16) / month (8) / day (8) / hour (8) / min (8) / sec (8)
31+
local now = getDateTime()
32+
local year = now.year
33+
34+
values[1] = bit32.band(year, 0xFF)
35+
year = bit32.rshift(year, 8)
36+
values[2] = bit32.band(year, 0xFF)
37+
values[3] = now.mon
38+
values[4] = now.day
39+
values[5] = now.hour
40+
values[6] = now.min
41+
values[7] = now.sec
42+
end
43+
44+
protocol.mspWrite(MSP_SET_RTC, values)
45+
lastRunTS = getTime()
46+
end
47+
48+
mspProcessTxQ()
49+
processMspReply(mspPollReply())
50+
51+
return timeIsSet
52+
end
53+
54+
return { f = setRtc, t = "" }

src/SCRIPTS/BF/ui_init.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local apiVersionReceived = false
22
local vtxTablesReceived = false
3-
local data_init, getVtxTables, getMCUId
3+
local mcuIdReceived = false
4+
local getApiVersion, getVtxTables, getMCUId
45
local returnTable = { f = nil, t = "" }
56

67
local function modelActive()
@@ -10,18 +11,19 @@ end
1011
local function init()
1112
if not modelActive() then
1213
returnTable.t = "Waiting for connection"
13-
elseif apiVersion == 0 then
14-
data_init = data_init or assert(loadScript("data_init.lua"))()
15-
returnTable.t = data_init.t
16-
data_init.f()
17-
elseif apiVersion > 0 and not apiVersionReceived then
18-
data_init = nil
19-
apiVersionReceived = true
20-
collectgarbage()
21-
elseif apiVersion >= 1.042 and not mcuId then
14+
elseif not apiVersionReceived then
15+
getApiVersion = getApiVersion or assert(loadScript("api_version.lua"))()
16+
returnTable.t = getApiVersion.t
17+
apiVersionReceived = getApiVersion.f()
18+
if apiVersionReceived then
19+
getApiVersion = nil
20+
collectgarbage()
21+
end
22+
elseif not mcuIdReceived and apiVersion >= 1.042 then
2223
getMCUId = getMCUId or assert(loadScript("mcu_id.lua"))()
2324
returnTable.t = getMCUId.t
24-
if getMCUId.f() then
25+
mcuIdReceived = getMCUId.f()
26+
if mcuIdReceived then
2527
getMCUId = nil
2628
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
2729
if vtxTables and vtxTables() then
@@ -30,7 +32,7 @@ local function init()
3032
end
3133
collectgarbage()
3234
end
33-
elseif apiVersion >= 1.042 and not vtxTablesReceived then
35+
elseif not vtxTablesReceived and apiVersion >= 1.042 then
3436
getVtxTables = getVtxTables or assert(loadScript("vtx_tables.lua"))()
3537
returnTable.t = getVtxTables.t
3638
vtxTablesReceived = getVtxTables.f()

0 commit comments

Comments
 (0)