Skip to content
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

MetadataEnd migrated to Module #274

Merged
merged 2 commits into from
Sep 29, 2023
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
5 changes: 4 additions & 1 deletion Scripts/DCS-BIOS/BIOS.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 11 additions & 3 deletions Scripts/DCS-BIOS/lib/Protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -187,6 +188,11 @@ local lastFrameTime = LoGetModelTime()
local updateCounter = 0
local updateSkipCounter = 0
function BIOS.protocol.step()

if( metadataStartModule == nil or metadataEndModule == nil) then
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
local curTime = LoGetModelTime()
bytesInTransit = bytesInTransit - ((curTime - lastFrameTime) * BIOS.protocol.maxBytesPerSecond)
Expand All @@ -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
Expand All @@ -210,19 +217,19 @@ 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
-- TODO: increase a frame skip counter for logging purposes
updateSkipCounter = (updateSkipCounter + 1) % 256
return
end
metadataEndModule.data.updateSkipCounter = updateSkipCounter
metadataEndModule:setUpdateSkipCounter(updateSkipCounter)
nextLowFreqStepTime = curTime + .033

-- send frame sync sequence
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Scripts/DCS-BIOS/lib/meta_files/DCS_API_defs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions Scripts/DCS-BIOS/lib/modules/common_modules/MetadataEnd.lua
Original file line number Diff line number Diff line change
@@ -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