Skip to content

Commit 8b3c16e

Browse files
committed
Background function improvements
Improved RTC synchronisation by periodically sending the time to the FC until we get the response confirming that the data was received and OK. Improved rssi functionality by detecting the rssi source set in the FC. Only sends rssi if source is NONE or MSP. Rssi also broken out into it's own file. Unloaded when not needed. Changed rssi scaling. It is now 0 - 99 to 0 - 255. Chose to do it like this because it doesn't need changes in the firmware and it's backwards compatible with earlier versions. No need for the user to adjust scaling/offset etc. The INTERVAL is now separate for data_init and rssi. This gives better control over what parts of the code to run at that interval. Previously the buffer was polled at the same interval as the commands were sent. Now it's polled every lua cycle. This speeds up apiVersion detection by up to 0.5 seconds. Same for RTC sync. Renamed rssi.lua function script to bfbkgd.lua(sorry. 6 char limit). Used "bf" to show where the script comes from to avoid confusion as people might have scripts from different sources in the FUNCTION folder.
1 parent 3b4f6f4 commit 8b3c16e

File tree

5 files changed

+126
-80
lines changed

5 files changed

+126
-80
lines changed

src/SCRIPTS/BF/background.lua

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
local INTERVAL = 50 -- in 1/100th seconds
2-
3-
local MSP_TX_INFO = 186
4-
5-
local lastRunTS = 0
61
local sensorId = -1
72
local dataInitialised = false
83
local data_init = nil
4+
local rssiEnabled = true
5+
local rssiTask = nil
96

107
local function getSensorValue()
118
if sensorId == -1 then
@@ -22,46 +19,44 @@ local function modelActive(sensorValue)
2219
end
2320

2421
local function run_bg()
25-
-- run in intervals
26-
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
27-
local sensorValue = getSensorValue()
28-
if modelActive(sensorValue) then
29-
-- Send data when the telemetry connection is available
30-
-- assuming when sensor value higher than 0 there is an telemetry connection
31-
if not dataInitialised then
32-
if data_init == nil then
33-
data_init = assert(loadScript(SCRIPT_HOME .. "/data_init.lua"))()
34-
end
22+
local sensorValue = getSensorValue()
23+
if modelActive(sensorValue) then
24+
-- Send data when the telemetry connection is available
25+
-- assuming when sensor value higher than 0 there is an telemetry connection
26+
if not dataInitialised then
27+
if not data_init then
28+
data_init = assert(loadScript(SCRIPT_HOME .. "/data_init.lua"))()
29+
end
3530

36-
dataInitialised = data_init();
31+
dataInitialised = data_init()
3732

38-
if dataInitialised then
39-
data_init = nil
33+
if dataInitialised then
34+
data_init = nil
4035

41-
collectgarbage()
42-
end
43-
else
44-
local rssi, alarm_low, alarm_crit = getRSSI()
45-
-- Scale the [0, 85] (empirical) RSSI values to [0, 255]
46-
rssi = rssi * 3
47-
if rssi > 255 then
48-
rssi = 255
49-
end
36+
collectgarbage()
37+
end
38+
elseif rssiEnabled and apiVersion >= 1.037 then
39+
if not rssiTask then
40+
rssiTask = assert(loadScript(SCRIPT_HOME.."/rssi.lua"))()
41+
end
5042

51-
local values = {}
52-
values[1] = rssi
43+
rssiEnabled = rssiTask()
5344

54-
protocol.mspWrite(MSP_TX_INFO, values)
45+
if not rssiEnabled then
46+
rssiTask = nil
47+
48+
collectgarbage()
5549
end
56-
else
57-
dataInitialised = false
5850
end
59-
60-
lastRunTS = getTime()
51+
else
52+
dataInitialised = false
53+
rssiEnabled = true
54+
if data_init or rssiTask then
55+
data_init = nil
56+
rssiTask = nil
57+
collectgarbage()
58+
end
6159
end
62-
63-
-- process queue
64-
mspProcessTxQ()
6560
end
6661

6762
return run_bg

src/SCRIPTS/BF/data_init.lua

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,62 @@ local MSP_SET_RTC = 246
33

44
local apiVersionReceived = false
55
local timeIsSet = false
6+
local lastRunTS = 0
7+
local INTERVAL = 50
68

79
local function processMspReply(cmd,rx_buf)
8-
if cmd == nil or rx_buf == nil then
9-
return
10-
end
11-
if cmd == MSP_API_VERSION and #(rx_buf) >= 3 then
10+
if cmd == MSP_API_VERSION and #rx_buf >= 3 then
1211
apiVersion = rx_buf[2] + rx_buf[3] / 1000
1312

1413
apiVersionReceived = true
1514
end
15+
if cmd == MSP_SET_RTC then
16+
timeIsSet = true
17+
end
1618
end
1719

1820
local function init()
19-
if not apiVersionReceived then
20-
protocol.mspRead(MSP_API_VERSION)
21-
22-
processMspReply(mspPollReply())
23-
elseif apiVersionReceived and not timeIsSet then
24-
-- only send datetime one time after telemetry connection became available
25-
-- or when connection is restored after e.g. lipo refresh
26-
local values = {}
27-
if apiVersion >= 1.041 then
28-
-- format: seconds after the epoch (32) / milliseconds (16)
29-
local now = getRtcTime()
30-
31-
for i = 1, 4 do
32-
values[i] = bit32.band(now, 0xFF)
33-
now = bit32.rshift(now, 8)
21+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
22+
if not apiVersionReceived then
23+
protocol.mspRead(MSP_API_VERSION)
24+
elseif apiVersionReceived and not timeIsSet then
25+
-- only send datetime one time after telemetry connection became available
26+
-- or when connection is restored after e.g. lipo refresh
27+
local values = {}
28+
if apiVersion >= 1.041 then
29+
-- format: seconds after the epoch (32) / milliseconds (16)
30+
local now = getRtcTime()
31+
32+
for i = 1, 4 do
33+
values[i] = bit32.band(now, 0xFF)
34+
now = bit32.rshift(now, 8)
35+
end
36+
37+
values[5] = 0 -- we don't have milliseconds
38+
values[6] = 0
39+
else
40+
-- format: year (16) / month (8) / day (8) / hour (8) / min (8) / sec (8)
41+
local now = getDateTime()
42+
local year = now.year
43+
44+
values[1] = bit32.band(year, 0xFF)
45+
year = bit32.rshift(year, 8)
46+
values[2] = bit32.band(year, 0xFF)
47+
values[3] = now.mon
48+
values[4] = now.day
49+
values[5] = now.hour
50+
values[6] = now.min
51+
values[7] = now.sec
3452
end
3553

36-
values[5] = 0 -- we don't have milliseconds
37-
values[6] = 0
38-
else
39-
-- format: year (16) / month (8) / day (8) / hour (8) / min (8) / sec (8)
40-
local now = getDateTime()
41-
local year = now.year;
42-
43-
values[1] = bit32.band(year, 0xFF)
44-
year = bit32.rshift(year, 8)
45-
values[2] = bit32.band(year, 0xFF)
46-
values[3] = now.mon
47-
values[4] = now.day
48-
values[5] = now.hour
49-
values[6] = now.min
50-
values[7] = now.sec
54+
protocol.mspWrite(MSP_SET_RTC, values)
5155
end
56+
lastRunTS = getTime()
57+
end
5258

53-
protocol.mspWrite(MSP_SET_RTC, values)
59+
mspProcessTxQ()
5460

55-
timeIsSet = true
56-
end
61+
processMspReply(mspPollReply())
5762

5863
return apiVersionReceived and timeIsSet
5964
end

src/SCRIPTS/BF/rssi.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
local MSP_SET_TX_INFO = 186
2+
local MSP_TX_INFO = 187
3+
4+
local RSSI_SOURCE_NONE = 0
5+
local RSSI_SOURCE_MSP = 4
6+
7+
local rssiSourceReceived = false
8+
local rssiSource = RSSI_SOURCE_NONE
9+
local lastRunTS = 0
10+
local INTERVAL = 50
11+
12+
local function processMspReply(cmd,rx_buf)
13+
if cmd == MSP_TX_INFO and #rx_buf >= 1 then
14+
rssiSource = rx_buf[1]
15+
rssiSourceReceived = true
16+
end
17+
end
18+
19+
local function rssiTask()
20+
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
21+
if not rssiSourceReceived then
22+
protocol.mspRead(MSP_TX_INFO)
23+
elseif rssiSource == RSSI_SOURCE_NONE or rssiSource == RSSI_SOURCE_MSP then
24+
local rssi, alarm_low, alarm_crit = getRSSI()
25+
-- Scale the [0, 99] RSSI values to [0, 255]
26+
rssi = rssi * 255 / 99
27+
if rssi > 255 then
28+
rssi = 255
29+
end
30+
31+
local values = {}
32+
values[1] = rssi
33+
34+
protocol.mspWrite(MSP_SET_TX_INFO, values)
35+
end
36+
lastRunTS = getTime()
37+
end
38+
39+
mspProcessTxQ()
40+
41+
processMspReply(mspPollReply())
42+
43+
return rssiSource == RSSI_SOURCE_NONE or rssiSource == RSSI_SOURCE_MSP
44+
end
45+
46+
return rssiTask

src/SCRIPTS/BF/ui.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ local pageScrollY = 0
3434
local mainMenuScrollY = 0
3535
local PageFiles = nil
3636
local Page = nil
37-
local background = nil
37+
local init = nil
3838

3939
local backgroundFill = TEXT_BGCOLOR or ERASE
4040
local foregroundColor = LINE_COLOR or SOLID
@@ -277,14 +277,14 @@ local function run_ui(event)
277277
drawScreenTitle("Betaflight Config", 0, 0)
278278
lcd.drawText(6, yMinLim, "Initialising")
279279
if apiVersion == 0 then
280-
if not background then
281-
background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
280+
if not init then
281+
init = assert(loadScript(SCRIPT_HOME.."/data_init.lua"))()
282282
end
283-
background()
283+
init()
284284
return 0
285285
else
286-
background = nil
287-
PageFiles = assert(loadScript("/SCRIPTS/BF/pages.lua"))()
286+
init = nil
287+
PageFiles = assert(loadScript(SCRIPT_HOME.."/pages.lua"))()
288288
invalidatePages()
289289
uiState = uiStatus.mainMenu
290290
end
File renamed without changes.

0 commit comments

Comments
 (0)