Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/SCRIPTS/BF/background.lua
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
SCRIPT_HOME = "/SCRIPTS/BF"

assert(loadScript(SCRIPT_HOME.."/MSP/common.lua"))()

local INTERVAL = 50 -- in 1/100th seconds
local MSP_SET_RTC = 246
local MSP_TX_INFO = 186
local sensorName = "Tmp1" -- T1 is never 0 in Betaflight

local lastRunTS
local timeIsSet = false
local sensorId
local mspMsgQueued = false

local function getTelemetryId(name)
local field = getFieldInfo(name)
if field then
return field['id']
else
return -1
end
local function modelActive()
local telemId = (getFieldInfo(protocol.stateSensor)['id'] or -1)
local sensorValue = getValue(telemId)
return type(sensorValue) == "number" and sensorValue > 0
end

local function init()
sensorId = getTelemetryId(sensorName)
lastRunTS = 0
end

local function run_bg()
-- run in intervals
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then

if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
mspMsgQueued = false

-- ------------------------------------
-- SYNC DATE AND TIME
-- ------------------------------------

-- get sensor value
local newSensorValue = getValue(sensorId)

if not timeIsSet and type(newSensorValue) == "number" and newSensorValue > 0 then
if not timeIsSet and modelActive() then
-- Send datetime when the telemetry connection is available
-- assuming when sensor value higher than 0 there is an telemetry connection
-- only send datetime one time after telemetry connection became available
-- only send datetime one time after telemetry connection became available
-- or when connection is restored after e.g. lipo refresh
local now = getDateTime()
local year = now.year;
Expand All @@ -58,11 +43,11 @@ local function run_bg()
values[7] = now.sec

-- send msp message
mspSendRequest(MSP_SET_RTC, values)
protocol.mspWrite(MSP_SET_RTC, values)
mspMsgQueued = true

timeIsSet = true
elseif type(newSensorValue) ~= "number" or newSensorValue == 0 then
elseif not modelActive() then
timeIsSet = false
end

Expand All @@ -83,7 +68,7 @@ local function run_bg()
values[1] = rssi

-- send msp message
mspSendRequest(MSP_TX_INFO, values)
protocol.mspWrite(MSP_TX_INFO, values)
mspMsgQueued = true
end

Expand Down
2 changes: 2 additions & 0 deletions src/SCRIPTS/BF/protocols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local supportedProtocols =
transport = SCRIPT_HOME.."/MSP/sp.lua",
rssi = function() return getValue("RSSI") end,
exitFunc = function() return 0 end,
stateSensor = "Tmp1",
push = sportTelemetryPush,
maxTxBufferSize = 6,
maxRxBufferSize = 6,
Expand All @@ -16,6 +17,7 @@ local supportedProtocols =
transport = SCRIPT_HOME.."/MSP/crsf.lua",
rssi = function() return getValue("TQly") end,
exitFunc = function() return "/CROSSFIRE/crossfire.lua" end,
stateSensor = "1RSS",
push = crossfireTelemetryPush,
maxTxBufferSize = 8,
maxRxBufferSize = 58,
Expand Down
3 changes: 1 addition & 2 deletions src/SCRIPTS/TELEMETRY/bf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ function run_bg()
end
end

--return { init=background.init, run=run, background=run_bg }
return { run=run_ui }
return { init=background.init, run=run, background=run_bg }