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

Migrate String control #255

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
33 changes: 30 additions & 3 deletions Scripts/DCS-BIOS/lib/modules/Module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,33 @@ function Module:addTwoCommandFixedStepInputProcessor(identifier, device_id, decr
end)
end

--- Adds a new string output based on a custom getter function
--- @param identifier string the unique identifier for the control
--- @param getter fun(dev0: CockpitDevice): string the getter function which will return a string
--- @param max_length integer the maximum length of the string
--- @param category string the category in which the control should appear
--- @param description string additional information about the control
--- @return Control control the control which was added to the module
function Module:defineString(identifier, getter, max_length, category, description)
local alloc = self:allocateString(max_length)
self:addExportHook(function(dev0)
local value = getter(dev0) --ammo
if value == nil then
error("function " .. identifier .. " is sending a nil value from its getter")
end

alloc:setValue(value)
end)

local control = Control:new(category, ControlType.display, identifier, description, {}, {
StringOutput:new(alloc, Suffix.none, description),
})

self:addControl(control)

return control
end

--- Defines a two-command fixed-step rotary input
--- @param identifier string the unique identifier for the control
--- @param device_id integer the dcs device id
Expand Down Expand Up @@ -359,15 +386,15 @@ end

--- Adds a new integer output based on a custom getter function
--- @param identifier string the unique identifier for the control
--- @param getter fun(): integer the getter function which will return an integer
--- @param getter fun(dev0: CockpitDevice): integer the getter function which will return an integer
--- @param maxValue integer the maximum value the getter will return
--- @param category string the category in which the control should appear
--- @param description string additional information about the control
--- @return Control control the control which was added to the module
function Module:defineIntegerFromGetter(identifier, getter, maxValue, category, description)
local alloc = self:allocateInt(maxValue)
self:addExportHook(function(_)
alloc:setValue(getter())
self:addExportHook(function(dev0)
alloc:setValue(getter(dev0))
end)

local control = Control:new(category, ControlType.metadata, identifier, description, {}, {
Expand Down
1 change: 1 addition & 0 deletions Scripts/DCS-BIOS/test/ModuleTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require("RotaryTest")
require("ThreePosTumbTest")
require("FixedStepTumbTest")
require("FixedStepInputTest")
require("StringTest")
require("RadioWheelTest")
require("BitFromDrawArgumentTest")
require("FloatFromDrawArgumentTest")
Expand Down
132 changes: 132 additions & 0 deletions Scripts/DCS-BIOS/test/controls/StringTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
local ControlType = require("ControlType")
local MockDevice = require("MockDevice")
local Module = require("Module")
local OutputType = require("OutputType")
local Suffix = require("Suffix")

local lu = require("luaunit")

--- @class TestString
--- @field module Module
TestString = {}
local moduleName = "MyModule"
local moduleAddress = 0x4200

function TestString:setUp()
self.module = Module:new(moduleName, moduleAddress, {})
end

local id = "MY_STRING"
local category = "Strings"
local description = "This is a string"

function TestString:testAddString()
local max_length = 2
local getter = function() end
local control = self.module:defineString(id, getter, max_length, category, description)

lu.assertEquals(control, self.module.documentation[category][id])
lu.assertEquals(control.control_type, ControlType.display)
lu.assertEquals(control.category, category)
lu.assertEquals(control.description, description)
lu.assertEquals(control.identifier, id)
lu.assertIsNil(control.physical_variant)
lu.assertIsNil(control.api_variant)

lu.assertEquals(#control.inputs, 0)

lu.assertEquals(#control.outputs, 1)
local string_output = control.outputs[1] --[[@as StringOutput]]
lu.assertEquals(string_output.type, OutputType.string)
lu.assertEquals(string_output.maxLength, max_length)
lu.assertEquals(string_output.suffix, Suffix.none)
lu.assertEquals(string_output.address, moduleAddress) -- first control, should be plenty of room, no need to move the address
end

function TestString:testStringLength1Value()
local max_length = 1
local getter = function()
return "h"
end

self.module:defineString(id, getter, max_length, category, description)

local export_hook = self.module.exportHooks[1]

local alloc1 = self.module.memoryMap.entries[moduleAddress].allocations[1]

export_hook()
lu.assertEquals(alloc1.value, string.byte("h"))
end

function TestString:testStringLength2Value()
local max_length = 2
local getter = function()
return "hi"
end

self.module:defineString(id, getter, max_length, category, description)

local export_hook = self.module.exportHooks[1]

local alloc1 = self.module.memoryMap.entries[moduleAddress].allocations[1]
local alloc2 = self.module.memoryMap.entries[moduleAddress].allocations[2]

export_hook()
lu.assertEquals(alloc1.value, string.byte("h"))
lu.assertEquals(alloc2.value, string.byte("i"))
end

function TestString:testStringLength4Value()
local max_length = 4
local getter = function()
return "yarp"
end

self.module:defineString(id, getter, max_length, category, description)

local export_hook = self.module.exportHooks[1]

local alloc1 = self.module.memoryMap.entries[moduleAddress].allocations[1]
local alloc2 = self.module.memoryMap.entries[moduleAddress].allocations[2]
local alloc3 = self.module.memoryMap.entries[moduleAddress + 2].allocations[1]
local alloc4 = self.module.memoryMap.entries[moduleAddress + 2].allocations[2]

export_hook()
lu.assertEquals(alloc1.value, string.byte("y"))
lu.assertEquals(alloc2.value, string.byte("a"))
lu.assertEquals(alloc3.value, string.byte("r"))
lu.assertEquals(alloc4.value, string.byte("p"))
end

function TestString:testStringShortLengthValue()
local max_length = 2
local getter = function()
return "h" -- intentionally return a string shorter than the max length
end

self.module:defineString(id, getter, max_length, category, description)

local export_hook = self.module.exportHooks[1]

local alloc1 = self.module.memoryMap.entries[moduleAddress].allocations[1]
local alloc2 = self.module.memoryMap.entries[moduleAddress].allocations[2]

export_hook()
lu.assertEquals(alloc1.value, string.byte("h"))
lu.assertEquals(alloc2.value, string.byte(" ")) -- missing length padded with spaces
end

-- behavior is expected and correct, test disabled because the lua debugger still pauses on errors when running tests
-- function TestString:testStringNilErrorValue()
-- local max_length = 2
-- local getter = function()
-- return nil -- intentionally return nil
-- end

-- self.module:defineString(id, getter, max_length, category, description)

-- local export_hook = self.module.exportHooks[1]

-- lu.assertError(export_hook)
-- end