diff --git a/Scripts/DCS-BIOS/doc/json/F-14.json b/Scripts/DCS-BIOS/doc/json/F-14.json index 05fbfacf2..0410b4a96 100644 --- a/Scripts/DCS-BIOS/doc/json/F-14.json +++ b/Scripts/DCS-BIOS/doc/json/F-14.json @@ -21251,6 +21251,24 @@ "physical_variant": "toggle_switch" } }, + "Set Frequency": { + "SET_UHF_FREQ": { + "category": "Set Frequency", + "control_type": "metadata", + "description": "Set the frequency of the radio (provide exact frequency)", + "identifier": "SET_UHF_FREQ", + "inputs": [ ], + "outputs": [ ] + }, + "SET_VUHF_FREQ": { + "category": "Set Frequency", + "control_type": "metadata", + "description": "Set the frequency of the radio (provide exact frequency)", + "identifier": "SET_VUHF_FREQ", + "inputs": [ ], + "outputs": [ ] + } + }, "Spoiler": { "PLT_SPOIL_OVER_COVER_INBOARD": { "category": "Spoiler", diff --git a/Scripts/DCS-BIOS/doc/json/F-14.jsonp b/Scripts/DCS-BIOS/doc/json/F-14.jsonp index e27ce6021..285f5a64b 100644 --- a/Scripts/DCS-BIOS/doc/json/F-14.jsonp +++ b/Scripts/DCS-BIOS/doc/json/F-14.jsonp @@ -21252,6 +21252,24 @@ docdata["F-14"] = "physical_variant": "toggle_switch" } }, + "Set Frequency": { + "SET_UHF_FREQ": { + "category": "Set Frequency", + "control_type": "metadata", + "description": "Set the frequency of the radio (provide exact frequency)", + "identifier": "SET_UHF_FREQ", + "inputs": [ ], + "outputs": [ ] + }, + "SET_VUHF_FREQ": { + "category": "Set Frequency", + "control_type": "metadata", + "description": "Set the frequency of the radio (provide exact frequency)", + "identifier": "SET_VUHF_FREQ", + "inputs": [ ], + "outputs": [ ] + } + }, "Spoiler": { "PLT_SPOIL_OVER_COVER_INBOARD": { "category": "Spoiler", diff --git a/Scripts/DCS-BIOS/lib/modules/Module.lua b/Scripts/DCS-BIOS/lib/modules/Module.lua index 8eac0ef00..b4f37ea1d 100644 --- a/Scripts/DCS-BIOS/lib/modules/Module.lua +++ b/Scripts/DCS-BIOS/lib/modules/Module.lua @@ -8,6 +8,7 @@ local ControlType = require("ControlType") local Documentation = require("Documentation") local FixedStepInput = require("FixedStepInput") local IntegerOutput = require("IntegerOutput") +local Log = require("Log") local MemoryMap = require("MemoryMap") local MomentaryPositions = require("MomentaryPositions") local PhysicalVariant = require("PhysicalVariant") @@ -976,6 +977,30 @@ function Module:defineEjectionHandleSwitch(identifier, device_id, command, arg_n return control end +--- Defines a blank control with an input for setting the frequency of a radio device +--- @param identifier string the unique identifier for the control +--- @param device_id integer the dcs device id +--- @return Control +function Module:defineSetFrequency(identifier, device_id) + -- todo: consider adding a new input type? + local control = Control:new("Set Frequency", ControlType.metadata, identifier, "Set the frequency of the radio (provide exact frequency)", {}, {}) + self:addControl(control) + + self:addInputProcessor(identifier, function(value) + local no_decimal_value = value:gsub("%.", "") -- remove decimals + local freq = tonumber(no_decimal_value) -- convert to number + + if not freq then + Log:log_error(string.format("Module.lua: Attempted to set nil frequency for control %s (source value %s)", identifier, value)) + return + end + + GetDevice(device_id):set_frequency(freq * 1000) + end) + + return control +end + --- Allocates space for a string to the memory map of the module --- @param max_length integer the maximum length of the string --- @param identifier string? the identifier of the control being allocated diff --git a/Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua b/Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua index ae0c15110..5fa442306 100644 --- a/Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua +++ b/Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua @@ -391,15 +391,7 @@ F_14:defineIntegerFromGetter("PLT_UHF_DIAL4_FREQ", getARC159_Decimal_DIAL4_Frequ F_14:defineIntegerFromGetter("PLT_UHF_DIAL3_FREQ", getARC159_Decimal_DIAL3_Frequency, 10, "UHF 1", "PILOT Dial 3 ARC-159 Frequency") F_14:defineIntegerFromGetter("PLT_UHF_HIGH_FREQ", getARC159_High_Frequency, 400, "UHF 1", "PILOT High ARC-159 Frequency") -F_14:addInputProcessor("SET_UHF_FREQ", function(freq) - freq = freq:gsub("%.", "") - freq = tonumber(freq) - if type(freq) == "nil" then - return - end - - GetDevice(3):set_frequency(freq * 1000) -end) +F_14:defineSetFrequency("SET_UHF_FREQ", 3) -- VHF/UHF ARC-182 ("V/UHF 2") F_14:defineMultipositionSwitch("RIO_VUHF_FREQ_MODE", 4, 3417, 353, 6, 0.2, "VUHF", "RIO VHF/UHF ARC-182 Frequency Mode 243 MAN G PRESET READ LOAD") @@ -452,15 +444,7 @@ F_14:defineIntegerFromGetter("RIO_VUHF_DIAL4_FREQ", getARC182_Decimal_DIAL4_Freq F_14:defineIntegerFromGetter("RIO_VUHF_DIAL3_FREQ", getARC182_Decimal_DIAL3_Frequency, 10, "VUHF", "RIO Dial 3 ARC-182 Frequency") F_14:defineIntegerFromGetter("RIO_VUHF_HIGH_FREQ", getARC182_High_Frequency, 400, "VUHF", "RIO High ARC-182 Frequency") -F_14:addInputProcessor("SET_VUHF_FREQ", function(freq) - freq = freq:gsub("%.", "") - freq = tonumber(freq) - if type(freq) == "nil" then - return - end - - GetDevice(4):set_frequency(freq * 1000) -end) +F_14:defineSetFrequency("SET_VUHF_FREQ", 4) -- KY-28 F_14:defineTumb("RIO_KY28_POWER", 2, 3423, 116, 0.5, { 0, 1 }, nil, false, "KY-28", "RIO KY-28 Power Mode") diff --git a/Scripts/DCS-BIOS/test/ModuleTest.lua b/Scripts/DCS-BIOS/test/ModuleTest.lua index 17b97941a..26677e448 100644 --- a/Scripts/DCS-BIOS/test/ModuleTest.lua +++ b/Scripts/DCS-BIOS/test/ModuleTest.lua @@ -55,3 +55,4 @@ require("VariableStepInput") require("Float8BitTest") require("EjectionHandleSwitchTest") require("SetCommandTumbTest") +require("SetFrequencyTest") diff --git a/Scripts/DCS-BIOS/test/controls/MockDevice.lua b/Scripts/DCS-BIOS/test/controls/MockDevice.lua index ba009503b..0ebb72d8b 100644 --- a/Scripts/DCS-BIOS/test/controls/MockDevice.lua +++ b/Scripts/DCS-BIOS/test/controls/MockDevice.lua @@ -5,11 +5,13 @@ module("MockDevice", package.seeall) --- @field clickable_actions {[integer]: number}[] a cache of clickable action calls that have been made --- @field set_commands {[integer]: number}[] a cache of SetCommand calls that have been made --- @field set_arguments {[integer]: number}[] a cache of set argument calls that have been made +--- @field set_frequencies number[] a cache of set frequency calls that have been made MockDevice = { value = 0, clickable_actions = {}, set_commands = {}, set_arguments = {}, + set_frequencies = {}, } --- Constructs a new mock cockpit device --- @param value number the value that should be returned on the mocked call to get_argument_value @@ -20,6 +22,7 @@ function MockDevice:new(value) clickable_actions = {}, set_commands = {}, set_arguments = {}, + set_frequencies = {}, } setmetatable(o, self) @@ -59,4 +62,10 @@ function MockDevice:set_argument_value(argument_id, value) table.insert(self.set_arguments, { [argument_id] = value }) end +--- Sets a frequency +--- @param value number +function MockDevice:set_frequency(value) + table.insert(self.set_frequencies, value) +end + return MockDevice diff --git a/Scripts/DCS-BIOS/test/controls/SetFrequencyTest.lua b/Scripts/DCS-BIOS/test/controls/SetFrequencyTest.lua new file mode 100644 index 000000000..920b0bc93 --- /dev/null +++ b/Scripts/DCS-BIOS/test/controls/SetFrequencyTest.lua @@ -0,0 +1,69 @@ +local ControlType = require("ControlType") +local MockDevice = require("MockDevice") +local Module = require("Module") + +local lu = require("luaunit") + +--- @class TestSetFrequency +--- @field module Module +TestSetFrequency = {} +local moduleName = "MyModule" +local moduleAddress = 0x4200 + +function TestSetFrequency:setUp() + self.module = Module:new(moduleName, moduleAddress, {}) + Input_Processor_Device = MockDevice:new(0) +end + +local id = "MY_SET_FREQUENCY_INPUT" +local device_id = 1 +local category = "Set Frequency" + +function TestSetFrequency:testAddSetFrequency() + local control = self.module:defineSetFrequency(id, device_id) + + lu.assertEquals(control, self.module.documentation[category][id]) + lu.assertEquals(control.control_type, ControlType.metadata) + lu.assertEquals(control.category, category) + lu.assertEquals(control.identifier, id) + lu.assertIsNil(control.momentary_positions) + lu.assertIsNil(control.physical_variant) + lu.assertIsNil(control.api_variant) + + lu.assertEquals(#control.inputs, 0) -- these may be added later + + lu.assertEquals(#control.outputs, 0) +end + +function TestSetFrequency:testInputSetNoDecimal() + self.module:defineSetFrequency(id, device_id) + local input_processor = self.module.inputProcessors[id] + + input_processor("123456") + + lu.assertEquals(#Input_Processor_Device.set_frequencies, 1) + local set_frequency = Input_Processor_Device.set_frequencies[1] + lu.assertAlmostEquals(set_frequency, 123456000) +end + +function TestSetFrequency:testInputSetWithDecimal() + self.module:defineSetFrequency(id, device_id) + local input_processor = self.module.inputProcessors[id] + + input_processor("123.456") + + lu.assertEquals(#Input_Processor_Device.set_frequencies, 1) + local set_frequency = Input_Processor_Device.set_frequencies[1] + lu.assertAlmostEquals(set_frequency, 123456000) +end + +function TestSetFrequency:testInputSet4Digit() + self.module:defineSetFrequency(id, device_id) + local input_processor = self.module.inputProcessors[id] + + input_processor("30.00") + + lu.assertEquals(#Input_Processor_Device.set_frequencies, 1) + local set_frequency = Input_Processor_Device.set_frequencies[1] + lu.assertAlmostEquals(set_frequency, 3000000) +end