Skip to content

Background function improvements #308

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
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
69 changes: 32 additions & 37 deletions src/SCRIPTS/BF/background.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
local INTERVAL = 50 -- in 1/100th seconds

local MSP_TX_INFO = 186

local lastRunTS = 0
local sensorId = -1
local dataInitialised = false
local data_init = nil
local rssiEnabled = true
local rssiTask = nil

local function getSensorValue()
if sensorId == -1 then
Expand All @@ -22,46 +19,44 @@ local function modelActive(sensorValue)
end

local function run_bg()
-- run in intervals
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
local sensorValue = getSensorValue()
if modelActive(sensorValue) 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 data_init == nil then
data_init = assert(loadScript(SCRIPT_HOME .. "/data_init.lua"))()
end
local sensorValue = getSensorValue()
if modelActive(sensorValue) 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(SCRIPT_HOME .. "/data_init.lua"))()
end

dataInitialised = data_init();
dataInitialised = data_init()

if dataInitialised then
data_init = nil
if dataInitialised then
data_init = nil

collectgarbage()
end
else
local rssi, alarm_low, alarm_crit = getRSSI()
-- Scale the [0, 85] (empirical) RSSI values to [0, 255]
rssi = rssi * 3
if rssi > 255 then
rssi = 255
end
collectgarbage()
end
elseif rssiEnabled and apiVersion >= 1.037 then
if not rssiTask then
rssiTask = assert(loadScript(SCRIPT_HOME.."/rssi.lua"))()
end

local values = {}
values[1] = rssi
rssiEnabled = rssiTask()

protocol.mspWrite(MSP_TX_INFO, values)
if not rssiEnabled then
rssiTask = nil

collectgarbage()
end
else
dataInitialised = false
end

lastRunTS = getTime()
else
dataInitialised = false
rssiEnabled = true
if data_init or rssiTask then
data_init = nil
rssiTask = nil
collectgarbage()
end
end

-- process queue
mspProcessTxQ()
end

return run_bg
79 changes: 42 additions & 37 deletions src/SCRIPTS/BF/data_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,62 @@ local MSP_SET_RTC = 246

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

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

apiVersionReceived = true
end
if cmd == MSP_SET_RTC then
timeIsSet = true
end
end

local function init()
if not apiVersionReceived then
protocol.mspRead(MSP_API_VERSION)

processMspReply(mspPollReply())
elseif apiVersionReceived and not timeIsSet 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)
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
if not apiVersionReceived then
protocol.mspRead(MSP_API_VERSION)
elseif apiVersionReceived and not timeIsSet 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

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
protocol.mspWrite(MSP_SET_RTC, values)
end
lastRunTS = getTime()
end

protocol.mspWrite(MSP_SET_RTC, values)
mspProcessTxQ()

timeIsSet = true
end
processMspReply(mspPollReply())

return apiVersionReceived and timeIsSet
end
Expand Down
46 changes: 46 additions & 0 deletions src/SCRIPTS/BF/rssi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local MSP_SET_TX_INFO = 186
local MSP_TX_INFO = 187

local RSSI_SOURCE_NONE = 0
local RSSI_SOURCE_MSP = 4

local rssiSourceReceived = false
local rssiSource = RSSI_SOURCE_NONE
local lastRunTS = 0
local INTERVAL = 50

local function processMspReply(cmd,rx_buf)
if cmd == MSP_TX_INFO and #rx_buf >= 1 then
rssiSource = rx_buf[1]
rssiSourceReceived = true
end
end

local function rssiTask()
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
if not rssiSourceReceived then
protocol.mspRead(MSP_TX_INFO)
elseif rssiSource == RSSI_SOURCE_NONE or rssiSource == RSSI_SOURCE_MSP then
local rssi, alarm_low, alarm_crit = getRSSI()
-- Scale the [0, 99] RSSI values to [0, 255]
rssi = rssi * 255 / 99
if rssi > 255 then
rssi = 255
end

local values = {}
values[1] = rssi

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

mspProcessTxQ()

processMspReply(mspPollReply())

return rssiSource == RSSI_SOURCE_NONE or rssiSource == RSSI_SOURCE_MSP
end

return rssiTask
12 changes: 6 additions & 6 deletions src/SCRIPTS/BF/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local pageScrollY = 0
local mainMenuScrollY = 0
local PageFiles = nil
local Page = nil
local background = nil
local init = nil

local backgroundFill = TEXT_BGCOLOR or ERASE
local foregroundColor = LINE_COLOR or SOLID
Expand Down Expand Up @@ -277,14 +277,14 @@ local function run_ui(event)
drawScreenTitle("Betaflight Config", 0, 0)
lcd.drawText(6, yMinLim, "Initialising")
if apiVersion == 0 then
if not background then
background = assert(loadScript("/SCRIPTS/BF/background.lua"))()
if not init then
init = assert(loadScript(SCRIPT_HOME.."/data_init.lua"))()
end
background()
init()
return 0
else
background = nil
PageFiles = assert(loadScript("/SCRIPTS/BF/pages.lua"))()
init = nil
PageFiles = assert(loadScript(SCRIPT_HOME.."/pages.lua"))()
invalidatePages()
uiState = uiStatus.mainMenu
end
Expand Down
File renamed without changes.