Skip to content

Commit

Permalink
Add control for setting radio frequencies (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliefoxtwo authored Oct 10, 2023
1 parent 01573b0 commit d4e2843
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 18 deletions.
18 changes: 18 additions & 0 deletions Scripts/DCS-BIOS/doc/json/F-14.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions Scripts/DCS-BIOS/doc/json/F-14.jsonp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions Scripts/DCS-BIOS/lib/modules/Module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
20 changes: 2 additions & 18 deletions Scripts/DCS-BIOS/lib/modules/aircraft_modules/F-14.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
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 @@ -55,3 +55,4 @@ require("VariableStepInput")
require("Float8BitTest")
require("EjectionHandleSwitchTest")
require("SetCommandTumbTest")
require("SetFrequencyTest")
9 changes: 9 additions & 0 deletions Scripts/DCS-BIOS/test/controls/MockDevice.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +22,7 @@ function MockDevice:new(value)
clickable_actions = {},
set_commands = {},
set_arguments = {},
set_frequencies = {},
}

setmetatable(o, self)
Expand Down Expand Up @@ -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
69 changes: 69 additions & 0 deletions Scripts/DCS-BIOS/test/controls/SetFrequencyTest.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d4e2843

Please sign in to comment.