Skip to content

Commit 9c2ccfd

Browse files
Merge pull request #81 from codecae/crsf_smartport_rssi_datetime
Made rssi and datetime bg function portable to crsf and sport
2 parents 21ffcd2 + 347e31a commit 9c2ccfd

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

src/SCRIPTS/BF/background.lua

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,33 @@
1-
SCRIPT_HOME = "/SCRIPTS/BF"
2-
3-
assert(loadScript(SCRIPT_HOME.."/MSP/common.lua"))()
4-
51
local INTERVAL = 50 -- in 1/100th seconds
62
local MSP_SET_RTC = 246
73
local MSP_TX_INFO = 186
8-
local sensorName = "Tmp1" -- T1 is never 0 in Betaflight
94

105
local lastRunTS
116
local timeIsSet = false
12-
local sensorId
137
local mspMsgQueued = false
148

15-
local function getTelemetryId(name)
16-
local field = getFieldInfo(name)
17-
if field then
18-
return field['id']
19-
else
20-
return -1
21-
end
9+
local function modelActive()
10+
local telemId = (getFieldInfo(protocol.stateSensor)['id'] or -1)
11+
local sensorValue = getValue(telemId)
12+
return type(sensorValue) == "number" and sensorValue > 0
2213
end
2314

2415
local function init()
25-
sensorId = getTelemetryId(sensorName)
2616
lastRunTS = 0
2717
end
2818

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

22+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
3323
mspMsgQueued = false
34-
3524
-- ------------------------------------
3625
-- SYNC DATE AND TIME
3726
-- ------------------------------------
38-
39-
-- get sensor value
40-
local newSensorValue = getValue(sensorId)
41-
42-
if not timeIsSet and type(newSensorValue) == "number" and newSensorValue > 0 then
27+
if not timeIsSet and modelActive() then
4328
-- Send datetime when the telemetry connection is available
4429
-- assuming when sensor value higher than 0 there is an telemetry connection
45-
-- only send datetime one time after telemetry connection became available
30+
-- only send datetime one time after telemetry connection became available
4631
-- or when connection is restored after e.g. lipo refresh
4732
local now = getDateTime()
4833
local year = now.year;
@@ -58,11 +43,11 @@ local function run_bg()
5843
values[7] = now.sec
5944

6045
-- send msp message
61-
mspSendRequest(MSP_SET_RTC, values)
46+
protocol.mspWrite(MSP_SET_RTC, values)
6247
mspMsgQueued = true
6348

6449
timeIsSet = true
65-
elseif type(newSensorValue) ~= "number" or newSensorValue == 0 then
50+
elseif not modelActive() then
6651
timeIsSet = false
6752
end
6853

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

8570
-- send msp message
86-
mspSendRequest(MSP_TX_INFO, values)
71+
protocol.mspWrite(MSP_TX_INFO, values)
8772
mspMsgQueued = true
8873
end
8974

src/SCRIPTS/BF/protocols.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local supportedProtocols =
55
transport = SCRIPT_HOME.."/MSP/sp.lua",
66
rssi = function() return getValue("RSSI") end,
77
exitFunc = function() return 0 end,
8+
stateSensor = "Tmp1",
89
push = sportTelemetryPush,
910
maxTxBufferSize = 6,
1011
maxRxBufferSize = 6,
@@ -16,6 +17,7 @@ local supportedProtocols =
1617
transport = SCRIPT_HOME.."/MSP/crsf.lua",
1718
rssi = function() return getValue("TQly") end,
1819
exitFunc = function() return "/CROSSFIRE/crossfire.lua" end,
20+
stateSensor = "1RSS",
1921
push = crossfireTelemetryPush,
2022
maxTxBufferSize = 8,
2123
maxRxBufferSize = 58,

src/SCRIPTS/TELEMETRY/bf.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ function run_bg()
2626
end
2727
end
2828

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

0 commit comments

Comments
 (0)