| 1 |
################################################################## |
| 2 |
# |
| 3 |
# These are the helper functions for the kma20 audio panel |
| 4 |
# Maintainer: Thorsten Brehm (brehmt at gmail dot com) |
| 5 |
# Enhanced: 1/2025 B. Hallinger |
| 6 |
# |
| 7 |
# Usage: |
| 8 |
# just create one instance of kma20 class for each kma20 panel |
| 9 |
# you have in your aircraft: |
| 10 |
# kma20.new(0); |
| 11 |
# |
| 12 |
# KMA20 audio panel properties: |
| 13 |
# root: /instrumentation/kma20 |
| 14 |
# knob: microphone/radio selector (com1/2) |
| 15 |
# auto: selects COM1/2 based on microphone selector |
| 16 |
# com1: enable/disable COM1 audio (e.g. for ATIS) |
| 17 |
# com2: enable/disable COM2 audio (e.g. for ATIS) |
| 18 |
# nav1: enable/disable NAV1 station identifier |
| 19 |
# nav2: enable/disable NAV2 station identifier |
| 20 |
# adf: enable/disable ADF station identifier |
| 21 |
# dme: enable/disable DME station identifier |
| 22 |
# mkr: enable/disable marker beacon audio |
| 23 |
# sens: beacon receiver sensitivity |
| 24 |
# |
| 25 |
# KMA20 operational properties: |
| 26 |
# serviceable: (writable) false, when the audio panel failed |
| 27 |
# operable: (readonly) true, when power is supplied and serviceable is true |
| 28 |
|
| 29 |
var kma20 = { |
| 30 |
instances: [] |
| 31 |
}; |
| 32 |
|
| 33 |
kma20.new = func(rootPath) { |
| 34 |
var obj = {}; |
| 35 |
obj.parents = [kma20]; |
| 36 |
obj.rootPath = rootPath; |
| 37 |
|
| 38 |
# Hash containing all listeners / timers / aliases, for the destructor. |
| 39 |
obj.listeners = {}; |
| 40 |
obj.timers = {}; |
| 41 |
obj.aliases = {}; |
| 42 |
|
| 43 |
append(kma20.instances, obj); |
| 44 |
obj.fgcom.instance = obj; |
| 45 |
obj.init(); |
| 46 |
return obj; |
| 47 |
}; |
| 48 |
|
| 49 |
# Destructor |
| 50 |
kma20.del = func() { |
| 51 |
foreach (var l; keys(me.listeners)) removelistener(me.listeners[l]); |
| 52 |
foreach (var t; keys(me.timers)) me.timers[t].stop(); |
| 53 |
foreach (var a; keys(me.aliases)) me.aliases[a].unalias(); |
| 54 |
me.listeners = {}; |
| 55 |
me.timers = {}; |
| 56 |
me.aliases = {}; |
| 57 |
# todo: remove instance |
| 58 |
}; |
| 59 |
|
| 60 |
kma20.init = func() { |
| 61 |
me.unit = props.globals.getNode(me.rootPath); |
| 62 |
me.radios = [props.globals.getNode("/instrumentation/comm[0]"), props.globals.getNode("/instrumentation/comm[1]")]; |
| 63 |
|
| 64 |
# Monitor changes to COM related switches and knobs |
| 65 |
me.listeners.opr = setlistener(me.unit.getChild("operable"), func {me.updateCOMvolumes(); me.updateOtherVolumes();}, 0, 0); |
| 66 |
me.listeners.knob = setlistener(me.unit.getChild("knob"), func {me.updateCOMvolumes();}, 0, 0); |
| 67 |
me.listeners.auto = setlistener(me.unit.getChild("auto"), func {me.updateCOMvolumes();}, 0, 0); |
| 68 |
me.listeners.com1 = setlistener(me.unit.getChild("com1"), func {me.updateCOMvolumes();}, 0, 0); |
| 69 |
me.listeners.com2 = setlistener(me.unit.getChild("com2"), func {me.updateCOMvolumes();}, 0, 0); |
| 70 |
me.listeners.com1vol = setlistener(me.radios[0].getChild("volume-selected"), func {me.updateCOMvolumes();}, 0, 0); |
| 71 |
me.listeners.com2vol = setlistener(me.radios[1].getChild("volume-selected"), func {me.updateCOMvolumes();}, 0, 0); |
| 72 |
me.updateCOMvolumes(); |
| 73 |
|
| 74 |
# FGCom integration: Monitor COMs PTT in order to mute them when sending |
| 75 |
me.listeners.com1ptt = setlistener(me.radios[0].getChild("ptt"), func {me.updateCOMvolumes();}, 0, 0); # for fgcom integration |
| 76 |
me.listeners.com2ptt = setlistener(me.radios[1].getChild("ptt"), func {me.updateCOMvolumes();}, 0, 0); # for fgcom integration |
| 77 |
|
| 78 |
# Monitor changes to NAV, adf, dme and mkr switches |
| 79 |
me.listeners.nav1 = setlistener(me.unit.getChild("nav1"), func {me.updateOtherVolumes();}, 0, 0); |
| 80 |
me.listeners.nav2 = setlistener(me.unit.getChild("nav2"), func {me.updateOtherVolumes();}, 0, 0); |
| 81 |
me.listeners.adf = setlistener(me.unit.getChild("adf"), func {me.updateOtherVolumes();}, 0, 0); |
| 82 |
me.listeners.dme = setlistener(me.unit.getChild("dme"), func {me.updateOtherVolumes();}, 0, 0); |
| 83 |
me.listeners.mkr = setlistener(me.unit.getChild("mkr"), func {me.updateOtherVolumes();}, 0, 0); |
| 84 |
me.updateOtherVolumes(); |
| 85 |
|
| 86 |
|
| 87 |
# Initialize timer to recalculate operable state of the panel. |
| 88 |
# (We do this timed, because it seems to be enough; and the electrical output property changes way too often) |
| 89 |
me.updateOperable(); |
| 90 |
me.timers.operable_loop = maketimer(1.0, me, me.updateOperable); |
| 91 |
me.timers.operable_loop.start(); |
| 92 |
|
| 93 |
|
| 94 |
print( "KMA20 audio panel initialized" ); |
| 95 |
}; |
| 96 |
|
| 97 |
# Recalculate operable property |
| 98 |
kma20.updateOperable = func() { |
| 99 |
var svc = me.unit.getNode("serviceable",1).getBoolValue(); |
| 100 |
var power = getprop("/systems/electrical/outputs/audio-panel"); |
| 101 |
var op_p = me.unit.getNode("operable",1); |
| 102 |
if (svc and power != nil and power > 12.5) { |
| 103 |
op_p.setBoolValue(1); |
| 104 |
} else { |
| 105 |
op_p.setBoolValue(0); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
kma20.updateCOMvolumes = func() { |
| 110 |
# Idea for the COM is to have volume-selected set to always match the volume knob of the COM radio volume knob. |
| 111 |
# The volume (original property) is set to 0 when disabled by the KMA-20 or the volume-selected when enabled. |
| 112 |
|
| 113 |
# get KMA20 unit |
| 114 |
var knob_position = me.unit.getValue("knob"); # -1=COM1; 0=COM2; 1=EXT |
| 115 |
var auto_switch = me.unit.getValue("auto"); # -1=SPKR; 0=OFF; 1=HEADSET |
| 116 |
var com1_switch = me.unit.getValue("com1"); # -1=SPKR; 0=OFF; 1=HEADSET |
| 117 |
var com2_switch = me.unit.getValue("com2"); # -1=SPKR; 0=OFF; 1=HEADSET |
| 118 |
|
| 119 |
# get panel operational state (turned on? power? serviceable?) |
| 120 |
var operable = me.unit.getValue("operable"); |
| 121 |
|
| 122 |
# calculate com tgt volume |
| 123 |
# Auto mode: In case the COM switch is OFF but AUTO mode is on, make the COM audible if the knob has it selected |
| 124 |
# Failsafe mode: When the panel fails, COM1 is wired as fallback. |
| 125 |
tgt = { |
| 126 |
com1: 0, |
| 127 |
com2: 0 |
| 128 |
}; |
| 129 |
var com1_automode = (com1_switch == 0 and auto_switch != 0 and knob_position == -1); |
| 130 |
var com1_selected = (com1_switch != 0); |
| 131 |
if (operable) { |
| 132 |
if (com1_automode or com1_selected) { |
| 133 |
tgt.com1 = me.radios[0].getValue("volume-selected"); |
| 134 |
} else { |
| 135 |
tgt.com1 = 0.0; |
| 136 |
} |
| 137 |
} else { |
| 138 |
# Failsafe mode |
| 139 |
tgt.com1 = me.radios[0].getValue("volume-selected"); |
| 140 |
} |
| 141 |
|
| 142 |
var com2_automode = (com2_switch == 0 and auto_switch != 0 and knob_position == 0); |
| 143 |
var com2_selected = (com2_switch != 0); |
| 144 |
if ( operable and (com2_automode or com2_selected) ) { |
| 145 |
tgt.com2 = me.radios[1].getValue("volume-selected"); |
| 146 |
} else { |
| 147 |
tgt.com2 = 0.0; |
| 148 |
} |
| 149 |
|
| 150 |
# FGCom integration: When sending, mute the radio |
| 151 |
if (me.radios[0].getValue("ptt")) tgt.com1 = 0.0; |
| 152 |
if (me.radios[1].getValue("ptt")) tgt.com2 = 0.0; |
| 153 |
|
| 154 |
|
| 155 |
# finally persist |
| 156 |
me.radios[0].setValue("volume", tgt.com1); |
| 157 |
me.radios[1].setValue("volume", tgt.com2); |
| 158 |
}; |
| 159 |
|
| 160 |
kma20.updateOtherVolumes = func() { |
| 161 |
tgt = { |
| 162 |
nav1: 0, |
| 163 |
nav2: 0, |
| 164 |
adf: 0, |
| 165 |
dme: 0, |
| 166 |
mkr: 0 |
| 167 |
}; |
| 168 |
|
| 169 |
# get panel operational state (turned on? power? serviceable?) |
| 170 |
var operable = me.unit.getValue("operable"); |
| 171 |
|
| 172 |
if (operable) { |
| 173 |
# Idea for the NAV is to set audio-btn (original-property) to true when both KMA-20 NAV switch is active & the NAV ident is pulled (ident-audible). |
| 174 |
tgt.nav1 = (me.unit.getValue("nav1") != 0 and getprop("/instrumentation/nav[0]/ident-audible") and getprop("/instrumentation/nav[0]/operable")); |
| 175 |
tgt.nav2 = (me.unit.getValue("nav2") != 0 and getprop("/instrumentation/nav[1]/ident-audible") and getprop("/instrumentation/nav[1]/operable")); |
| 176 |
|
| 177 |
tgt.adf = (me.unit.getValue("adf") != 0 and getprop("/instrumentation/adf[0]/operable")); |
| 178 |
tgt.dme = (me.unit.getValue("dme") != 0 and getprop("/instrumentation/dme[0]/operable")); |
| 179 |
|
| 180 |
tgt.mkr = (me.unit.getValue("mkr") != 0 and getprop("/instrumentation/marker-beacon/operable")); |
| 181 |
} else { |
| 182 |
# Failsafe mode |
| 183 |
# ? No failsafe mode, as far as I have seen? |
| 184 |
} |
| 185 |
|
| 186 |
# set target values effective |
| 187 |
setprop("/instrumentation/nav[0]/audio-btn", tgt.nav1); |
| 188 |
setprop("/instrumentation/nav[1]/audio-btn", tgt.nav2); |
| 189 |
setprop("/instrumentation/adf/ident-audible", tgt.adf); |
| 190 |
setprop("/instrumentation/dme/ident", tgt.dme); |
| 191 |
setprop("/instrumentation/marker-beacon/audio-btn", tgt.mkr); |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
########### |
| 196 |
# FGCom integration |
| 197 |
# |
| 198 |
kma20.fgcom = { |
| 199 |
instance: {}, # registered by kma20.new() to make it visible in subnamespace here |
| 200 |
|
| 201 |
# PTT function, gets called when pressing/releasing space |
| 202 |
# active: 1 if ptt is pressed |
| 203 |
# mode: 0 normal, 1 shift-space |
| 204 |
ptt: func(active, mode) { |
| 205 |
var modifiers = props.globals.getNode("/devices/status/keyboard"); |
| 206 |
var fgcom_integration = getprop("/instrumentation/audio-panel/fgcom-integration"); |
| 207 |
if (fgcom_integration == nil) fgcom_integration = 0; |
| 208 |
#print("KMA20: [PTT] integrated=", fgcom_integration, "; active=", active, "; mode=", mode, "; modifiers=", modifiers.getValue()); |
| 209 |
|
| 210 |
if (fgcom_integration) { |
| 211 |
# FGCom integration activated: detect which radio is activated at the panel and if it is operable |
| 212 |
var selectedRadio = me.instance.fgcom.get_selected_radio(); |
| 213 |
if (selectedRadio >= 0) { |
| 214 |
if (active) { |
| 215 |
print("KMA20: [PTT] activated; panel selected radio COM", selectedRadio + 1); |
| 216 |
var selectedRadio_isOK = me.instance.fgcom.check_radio(selectedRadio); |
| 217 |
if (selectedRadio_isOK) { |
| 218 |
print("KMA20: [PTT] now transmitting on selected radio COM", selectedRadio + 1); |
| 219 |
me.instance.radios[selectedRadio].getNode("ptt").setValue(1); |
| 220 |
} else { |
| 221 |
# Radio was not operable |
| 222 |
print("KMA20: [PTT] selected radio COM", selectedRadio + 1, " is not operable. Not sending transmission!"); |
| 223 |
} |
| 224 |
|
| 225 |
} else { |
| 226 |
print("KMA20: [PTT] released, selected radio=COM", selectedRadio + 1); |
| 227 |
me.instance.radios[selectedRadio].getNode("ptt").setValue(0); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
} else { |
| 232 |
# FGCom not integrated: tunnel trough to standard behavior; |
| 233 |
# ie: space=COM1, shift+space=COM2 and no operable checks. |
| 234 |
# we use the listeners the default implementation listens to. |
| 235 |
|
| 236 |
# space pressed |
| 237 |
if (active and mode == 0) { |
| 238 |
me.instance.radios[0].getNode("ptt").setValue(1); |
| 239 |
# space released |
| 240 |
} else if (!active and mode == 0) { |
| 241 |
me.instance.radios[0].getNode("ptt").setValue(0); |
| 242 |
|
| 243 |
# shift-space pressed |
| 244 |
} else if (active and mode == 1) { |
| 245 |
me.instance.radios[1].getNode("ptt").setValue(1); |
| 246 |
|
| 247 |
# shift-space released |
| 248 |
} else if (!active and mode == 1) { |
| 249 |
me.instance.radios[1].getNode("ptt").setValue(0); |
| 250 |
|
| 251 |
# fallback |
| 252 |
} else { |
| 253 |
me.instance.radios[0].getNode("ptt").setValue(0); |
| 254 |
me.instance.radios[0].getNode("ptt").setValue(0); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
}, |
| 259 |
|
| 260 |
# Returns the audio panels selected radio (as index in me.instance.radios) |
| 261 |
# Returns -1 if invalid selection (in this case no PTT is activated when running in "integrated mode") |
| 262 |
get_selected_radio: func() { |
| 263 |
var unit_operable = me.instance.unit.getChild("operable").getBoolValue(); |
| 264 |
if (unit_operable) { |
| 265 |
var knob_position = me.instance.unit.getValue("knob"); # -1=COM1; 0=COM2; 1=EXT |
| 266 |
if (knob_position == -1) return 0; # COM1 |
| 267 |
if (knob_position == 0) return 1; # COM2 |
| 268 |
if (knob_position == 1) return -1; # EXT: with FGCom-mumble, this could potentially link the intercom |
| 269 |
} else { |
| 270 |
# Failsafe: COM1 wired |
| 271 |
return 0; |
| 272 |
} |
| 273 |
}, |
| 274 |
|
| 275 |
# Returns if the selected radio (as index in me.instance.radios) is able to transmit |
| 276 |
check_radio: func(selectedRadio) { |
| 277 |
var unit_operable = me.instance.unit.getChild("operable").getBoolValue(); |
| 278 |
if (unit_operable) { |
| 279 |
return me.instance.radios[selectedRadio].getChild("operable").getBoolValue(); |
| 280 |
} else { |
| 281 |
# Failsafe: get_selected_radio() returns COM1 in case of panel failure |
| 282 |
return me.instance.radios[0].getChild("operable").getBoolValue(); |
| 283 |
} |
| 284 |
return 0; |
| 285 |
} |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
# Initialize at startup (delayed, so propertys are properly initialized) |
| 290 |
setlistener("/sim/signals/fdm-initialized", func { |
| 291 |
var kma20_0 = kma20.new( "/instrumentation/kma20" ); |
| 292 |
}); |