Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 27 additions & 11 deletions src/SCRIPTS/BF/background.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,34 @@ local function run_bg()
-- assuming when sensor value higher than 0 there is an telemetry connection
-- 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;

values = {}
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
if API_VERSION < 1.041 then
local now = getDateTime()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that comes to mind is instead of having the two branches in the if/else expr, we do something like

assert(loadScript(SCRIPT_HOME.."/rtc_"..API_VERSION..".lua"))()` 

which contains the api-version specific section.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit divided on that. Until now the approach is for the lua scripts to only support the latest released version of the firmware, and I am not sure that going down the rabbit hole of introducing backwards compatibility is something we want to do, not least because we are already constrained by the available RAM as it is.

local year = now.year;

values = {}
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
else
local now = getRtcTime()

values = {}
values[1] = bit32.band(now, 0xFF)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps a loop 1..4?

local now = getRtcTime() values = {} for i = 1,2,3,4 do values[i] = bit32.band(now, 0xFF) now = bit32.rshift(now, 8) end values[5] = 0 values[6] = 0
now = bit32.rshift(now, 8)
values[2] = bit32.band(now, 0xFF)
now = bit32.rshift(now, 8)
values[3] = bit32.band(now, 0xFF)
now = bit32.rshift(now, 8)
values[4] = bit32.band(now, 0xFF)
values[5] = 0 -- we don't have milliseconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth documenting in here the expected payload, or is it documented on the FW side re: MSP_SET_RTC?

values[6] = 0
end

-- send msp message
protocol.mspWrite(MSP_SET_RTC, values)
Expand Down
9 changes: 9 additions & 0 deletions src/SCRIPTS/TELEMETRY/bf.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
SCRIPT_HOME = "/SCRIPTS/BF"

-- Change this to match the API version of the firmware you are using
-- (you can have multiple copies of this script with different values
-- for API_VERSION, and select different versions for different models
-- on your TX)
-- Version mapping:
-- 1.041: Betaflight 4.0 and newer
-- <1.041: Betaflight 3.5 and older
API_VERSION = 1.041

protocol = assert(loadScript(SCRIPT_HOME.."/protocols.lua"))()
radio = assert(loadScript(SCRIPT_HOME.."/radios.lua"))()

Expand Down