Skip to content

Split data_init.lua #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/SCRIPTS/BF/api_version.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local MSP_API_VERSION = 1

local apiVersionReceived = false
local lastRunTS = 0
local INTERVAL = 50

local function processMspReply(cmd,rx_buf)
if cmd == MSP_API_VERSION and #rx_buf >= 3 then
apiVersion = rx_buf[2] + rx_buf[3] / 1000
apiVersionReceived = true
end
end

local function getApiVersion()
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
protocol.mspRead(MSP_API_VERSION)
lastRunTS = getTime()
end

mspProcessTxQ()
processMspReply(mspPollReply())

return apiVersionReceived
end

return { f = getApiVersion, t = "Waiting for API version" }
41 changes: 20 additions & 21 deletions src/SCRIPTS/BF/background.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local dataInitialised = false
local data_init = nil
local apiVersionReceived = false
local timeIsSet = false
local getApiVersion, setRtc, rssiTask
local rssiEnabled = true
local rssiTask = nil

local function modelActive()
return getValue(protocol.stateSensor) > 0
Expand All @@ -11,36 +11,35 @@ local function run_bg()
if modelActive() then
-- Send data when the telemetry connection is available
-- assuming when sensor value higher than 0 there is an telemetry connection
if not dataInitialised then
if not data_init then
data_init = assert(loadScript("data_init.lua"))()
if not apiVersionReceived then
getApiVersion = getApiVersion or assert(loadScript("api_version.lua"))()
apiVersionReceived = getApiVersion.f()
if apiVersionReceived then
getApiVersion = nil
collectgarbage()
end

dataInitialised = data_init.f()

if dataInitialised then
data_init = nil

elseif not timeIsSet then
setRtc = setRtc or assert(loadScript("rtc.lua"))()
timeIsSet = setRtc.f()
if timeIsSet then
setRtc = nil
collectgarbage()
end
elseif rssiEnabled and apiVersion >= 1.037 then
if not rssiTask then
rssiTask = assert(loadScript("rssi.lua"))()
end

rssiTask = rssiTask or assert(loadScript("rssi.lua"))()
rssiEnabled = rssiTask()

if not rssiEnabled then
rssiTask = nil

collectgarbage()
end
end
else
dataInitialised = false
apiVersionReceived = false
timeIsSet = false
rssiEnabled = true
if data_init or rssiTask then
data_init = nil
if getApiVersion or setRtc or rssiTask then
getApiVersion = nil
setRtc = nil
rssiTask = nil
collectgarbage()
end
Expand Down
66 changes: 0 additions & 66 deletions src/SCRIPTS/BF/data_init.lua

This file was deleted.

54 changes: 54 additions & 0 deletions src/SCRIPTS/BF/rtc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local MSP_SET_RTC = 246

local timeIsSet = false
local lastRunTS = 0
local INTERVAL = 50

local function processMspReply(cmd,rx_buf)
if cmd == MSP_SET_RTC then
timeIsSet = true
end
end

local function setRtc()
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
-- only send datetime one time after telemetry connection became available
-- or when connection is restored after e.g. lipo refresh
local values = {}
if apiVersion >= 1.041 then
-- format: seconds after the epoch (32) / milliseconds (16)
local now = getRtcTime()

for i = 1, 4 do
values[i] = bit32.band(now, 0xFF)
now = bit32.rshift(now, 8)
end

values[5] = 0 -- we don't have milliseconds
values[6] = 0
else
-- format: year (16) / month (8) / day (8) / hour (8) / min (8) / sec (8)
local now = getDateTime()
local year = now.year

values[1] = bit32.band(year, 0xFF)
year = bit32.rshift(year, 8)
values[2] = bit32.band(year, 0xFF)
values[3] = now.mon
values[4] = now.day
values[5] = now.hour
values[6] = now.min
values[7] = now.sec
end

protocol.mspWrite(MSP_SET_RTC, values)
lastRunTS = getTime()
end

mspProcessTxQ()
processMspReply(mspPollReply())

return timeIsSet
end

return { f = setRtc, t = "" }
26 changes: 14 additions & 12 deletions src/SCRIPTS/BF/ui_init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local apiVersionReceived = false
local vtxTablesReceived = false
local data_init, getVtxTables, getMCUId
local mcuIdReceived = false
local getApiVersion, getVtxTables, getMCUId
local returnTable = { f = nil, t = "" }

local function modelActive()
Expand All @@ -10,18 +11,19 @@ end
local function init()
if not modelActive() then
returnTable.t = "Waiting for connection"
elseif apiVersion == 0 then
data_init = data_init or assert(loadScript("data_init.lua"))()
returnTable.t = data_init.t
data_init.f()
elseif apiVersion > 0 and not apiVersionReceived then
data_init = nil
apiVersionReceived = true
collectgarbage()
elseif apiVersion >= 1.042 and not mcuId then
elseif not apiVersionReceived then
getApiVersion = getApiVersion or assert(loadScript("api_version.lua"))()
returnTable.t = getApiVersion.t
apiVersionReceived = getApiVersion.f()
if apiVersionReceived then
getApiVersion = nil
collectgarbage()
end
elseif not mcuIdReceived and apiVersion >= 1.042 then
getMCUId = getMCUId or assert(loadScript("mcu_id.lua"))()
returnTable.t = getMCUId.t
if getMCUId.f() then
mcuIdReceived = getMCUId.f()
if mcuIdReceived then
getMCUId = nil
local vtxTables = loadScript("/BF/VTX/"..mcuId..".lua")
if vtxTables and vtxTables() then
Expand All @@ -30,7 +32,7 @@ local function init()
end
collectgarbage()
end
elseif apiVersion >= 1.042 and not vtxTablesReceived then
elseif not vtxTablesReceived and apiVersion >= 1.042 then
getVtxTables = getVtxTables or assert(loadScript("vtx_tables.lua"))()
returnTable.t = getVtxTables.t
vtxTablesReceived = getVtxTables.f()
Expand Down