From f414e61ff365d7306f6d02356f2ed5aa7ebbafeb Mon Sep 17 00:00:00 2001 From: Jerker Dahlblom Date: Fri, 29 Sep 2023 13:11:16 +0300 Subject: [PATCH 1/2] MetadataEnd migrated to Module --- Scripts/DCS-BIOS/BIOS.lua | 5 +- Scripts/DCS-BIOS/lib/Protocol.lua | 14 ++++-- .../DCS-BIOS/lib/meta_files/DCS_API_defs.lua | 2 + .../modules/common_modules/MetadataEnd.lua | 48 +++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 Scripts/DCS-BIOS/lib/modules/common_modules/MetadataEnd.lua diff --git a/Scripts/DCS-BIOS/BIOS.lua b/Scripts/DCS-BIOS/BIOS.lua index b0469a354..2b7d97114 100644 --- a/Scripts/DCS-BIOS/BIOS.lua +++ b/Scripts/DCS-BIOS/BIOS.lua @@ -35,7 +35,10 @@ BIOS.json = json and json() or require "JSON" -- if that fails, fall back to mod dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/Util.lua]]) dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/ProtocolIO.lua]]) dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/Protocol.lua]]) -dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/MetadataEnd.lua]]) +--dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/MetadataEnd.lua]]) +local MetadataEnd = require "MetadataEnd" +BIOS.protocol.writeNewModule(MetadataEnd) + dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/MetadataStart.lua]]) -- dofile(lfs.writedir()..[[Scripts/DCS-BIOS/lib/CommonData.lua]]) local CommonData = require "CommonData" diff --git a/Scripts/DCS-BIOS/lib/Protocol.lua b/Scripts/DCS-BIOS/lib/Protocol.lua index 0883cba15..43c095f38 100644 --- a/Scripts/DCS-BIOS/lib/Protocol.lua +++ b/Scripts/DCS-BIOS/lib/Protocol.lua @@ -2,6 +2,7 @@ BIOS.protocol = {} BIOS.protocol.maxBytesPerSecond = BIOS.protocol.maxBytesPerSecond or 11000 BIOS.protocol.maxBytesInTransit = BIOS.protocol.maxBytesPerSecond or 4000 +--- @type Module[] local exportModules = {} local aircraftNameToModuleNames = {} local aircraftNameToModules = {} @@ -187,6 +188,11 @@ local lastFrameTime = LoGetModelTime() local updateCounter = 0 local updateSkipCounter = 0 function BIOS.protocol.step() + + if( metadataStartModule == nil or metadataEndModule == nil) then + return -- this should never happen since init() is being called but it removes intellisense warnings + end + -- rate limiting local curTime = LoGetModelTime() bytesInTransit = bytesInTransit - ((curTime - lastFrameTime) * BIOS.protocol.maxBytesPerSecond) @@ -199,6 +205,7 @@ function BIOS.protocol.step() if selfData then acftName = selfData["Name"] end + metadataStartModule.data.acftName = acftName acftModules = aircraftNameToModules[acftName] if lastAcftName ~= acftName then @@ -210,11 +217,11 @@ function BIOS.protocol.step() lastAcftName = acftName end - -- export data + -- export data if curTime >= nextLowFreqStepTime then -- runs 30 times per second updateCounter = (updateCounter + 1) % 256 - metadataEndModule.data.updateCounter = updateCounter + metadataEndModule:setUpdateCounter(updateCounter) -- if the last frame update has not been completely transmitted, skip a frame if bytesInTransit > 0 then @@ -222,7 +229,7 @@ function BIOS.protocol.step() updateSkipCounter = (updateSkipCounter + 1) % 256 return end - metadataEndModule.data.updateSkipCounter = updateSkipCounter + metadataEndModule:setUpdateSkipCounter(updateSkipCounter) nextLowFreqStepTime = curTime + .033 -- send frame sync sequence @@ -236,6 +243,7 @@ function BIOS.protocol.step() bytesInTransit = bytesInTransit + data:len() BIOS.protocol_io.queue(data) + -- Export aircraft data if acftModules then for _, acftModule in pairs(acftModules) do local dev0 = GetDevice(0) diff --git a/Scripts/DCS-BIOS/lib/meta_files/DCS_API_defs.lua b/Scripts/DCS-BIOS/lib/meta_files/DCS_API_defs.lua index 8cc1b3b92..3cdaba9c5 100644 --- a/Scripts/DCS-BIOS/lib/meta_files/DCS_API_defs.lua +++ b/Scripts/DCS-BIOS/lib/meta_files/DCS_API_defs.lua @@ -13,6 +13,8 @@ end --- DCS Cockpit Device CockpitDevice = {} +--- @func Updates device's arguments +function CockpitDevice:update_arguments() end --- @func Sets command for a device --- @param command_id integer diff --git a/Scripts/DCS-BIOS/lib/modules/common_modules/MetadataEnd.lua b/Scripts/DCS-BIOS/lib/modules/common_modules/MetadataEnd.lua new file mode 100644 index 000000000..8d6d3c41f --- /dev/null +++ b/Scripts/DCS-BIOS/lib/modules/common_modules/MetadataEnd.lua @@ -0,0 +1,48 @@ +-- This is a special module: its data will always be exported +-- and its export hooks will be called regardless of aircraft. +-- It cannot access any cockpit arguments, because it will also +-- be called when there is no active aircraft. +module("MetadataEnd", package.seeall) + +local Module = require("Module") + +--- @class MetadataEnd : Module +--- @func setUpdateCounter +--- @func setUpdateSkipCounter +local MetadataEnd = Module:new("MetadataEnd", 0xfffe, BIOS.ALL_PLAYABLE_AIRCRAFT) + +local updateCounter = 0 +local updateSkipCounter = 0 + +--- @func Called from protocol +--- @param new_counter number +function MetadataEnd:setUpdateCounter(new_counter) + updateCounter = new_counter +end + +--- @func Called from protocol +--- @param new_counter number +function MetadataEnd:setUpdateSkipCounter(new_counter) + updateSkipCounter = new_counter +end + +-- "data" will be set by the Protocol module + +-- place update counters at address 0xfffe +-- DCS-BIOS guarantees that the value for address 0xfffe +-- will be the last one that is written in each update. +-- Clients can use that as an "update complete" signal. +-- At the point when the write access to 0xfffe has been received, +-- all string values have been completely updated (so the client +-- can assume they are in a consistent state) and some time will +-- pass until the next update has to be processed, so it is a good +-- trigger to update graphical displays and do other time-consuming +-- operations. +MetadataEnd:defineIntegerFromGetter("_UPDATE_COUNTER", function() + return updateCounter +end, 255, "Metadata", "Update Counter") +MetadataEnd:defineIntegerFromGetter("_UPDATE_SKIP_COUNTER", function() + return updateSkipCounter +end, 255, "Metadata", "Update Skip Counter") + +return MetadataEnd From 203700ae277b109c5162a3e202117cad8226493c Mon Sep 17 00:00:00 2001 From: Jerker Dahlblom Date: Fri, 29 Sep 2023 19:57:36 +0300 Subject: [PATCH 2/2] Error thrown if MetadataStart or MetadataEnd is nil in protocol.lua --- Scripts/DCS-BIOS/lib/Protocol.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scripts/DCS-BIOS/lib/Protocol.lua b/Scripts/DCS-BIOS/lib/Protocol.lua index 43c095f38..95e6e5ab5 100644 --- a/Scripts/DCS-BIOS/lib/Protocol.lua +++ b/Scripts/DCS-BIOS/lib/Protocol.lua @@ -190,7 +190,7 @@ local updateSkipCounter = 0 function BIOS.protocol.step() if( metadataStartModule == nil or metadataEndModule == nil) then - return -- this should never happen since init() is being called but it removes intellisense warnings + error("Either MetadataStart or MetadataEnd was nil.", 1) -- this should never happen since init() is being called but it removes intellisense warnings end -- rate limiting