diff --git a/src/SCRIPTS/RF2/background.lua b/src/SCRIPTS/RF2/background.lua index 21eb426..f83ba85 100644 --- a/src/SCRIPTS/RF2/background.lua +++ b/src/SCRIPTS/RF2/background.lua @@ -1,6 +1,7 @@ local initTask = nil local adjTellerTask = nil local customTelemetryTask = nil +local statsTask = nil local isInitialized = false local modelIsConnected = false local lastTimeRssi = nil @@ -37,6 +38,7 @@ local function run() end adjTellerTask = nil customTelemetryTask = nil + statsTask = nil modelIsConnected = false isInitialized = false collectgarbage() @@ -46,6 +48,7 @@ local function run() if not isInitialized then adjTellerTask = nil customTelemetryTask = nil + statsTask = nil collectgarbage() initTask = initTask or rf2.executeScript("background_init") local initTaskResult = initTask.run(modelIsConnected) @@ -76,6 +79,14 @@ local function run() if customTelemetryTask then customTelemetryTask.run() end + if rf2.apiVersion >= 12.09 then + if not statsTask and customTelemetryTask and hasSensor("ARM") then + statsTask = rf2.executeScript("background_stats") + end + if statsTask then + statsTask.readStats() + end + end end local function runProtected() diff --git a/src/SCRIPTS/RF2/background_stats.lua b/src/SCRIPTS/RF2/background_stats.lua new file mode 100644 index 0000000..f085fc7 --- /dev/null +++ b/src/SCRIPTS/RF2/background_stats.lua @@ -0,0 +1,48 @@ +local mspFlightStats = rf2.useApi("mspFlightStats") +local flighStats = mspFlightStats.getDefaults() + +local doReadStats = true +local lastArmedState = false +local lastTelemetryUpdate = 0 +local lastReadAttempt = 0 + +local function checkArmedChanged() + local arm = getValue("ARM") + local currentArmedState = (arm == 1 or arm == 3) + if lastArmedState == true and currentArmedState == false then + doReadStats = true + end + lastArmedState = currentArmedState +end + +local function readStats() + checkArmedChanged() + + local now = rf2.clock() + + if (doReadStats or flighStats.stats_total_flights.value == nil) and (now - lastReadAttempt > 2) then + if rf2.mspQueue:isProcessed() then + mspFlightStats.read(nil, nil, flighStats) + lastReadAttempt = now + doReadStats = false + end + end + + if not rf2.mspQueue:isProcessed() then + rf2.mspQueue:processQueue() + end + + if flighStats.statsEnabled.value and flighStats.statsEnabled.value == 1 then + if now - lastTelemetryUpdate >= 1 then + lastTelemetryUpdate = now + if flighStats.stats_total_flights.value then + setTelemetryValue(0x2001, 0, 0, flighStats.stats_total_flights.value, UNIT_RAW, 0, "FlyC") + setTelemetryValue(0x2002, 0, 0, flighStats.stats_total_time_s.value, UNIT_RAW, 0, "FlyT") + setTelemetryValue(0x2003, 0, 0, flighStats.stats_total_dist_m.value, UNIT_RAW, 0, "FlyD") + end + end + end + +end + +return { readStats = readStats } \ No newline at end of file