| 1 |
# ============================================================================== |
| 2 |
# Boeing Navigation Display by Gijs de Rooy |
| 3 |
# See: http://wiki.flightgear.org/Canvas_ND_Framework |
| 4 |
# ============================================================================== |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
## |
| 9 |
# this file contains a hash that declares features in a generic fashion |
| 10 |
# we want to get rid of the bloated update() method sooner than later |
| 11 |
# PLEASE DO NOT ADD any code to update() !! |
| 12 |
# Instead, help clean up the file and move things over to the navdisplay.styles file |
| 13 |
# |
| 14 |
# This is the only sane way to keep on generalizing the framework, so that we can |
| 15 |
# also support different makes/models of NDs in the future |
| 16 |
# |
| 17 |
# a huge bloated update() method is going to make that basically IMPOSSIBLE |
| 18 |
# |
| 19 |
io.include("Nasal/canvas/map/navdisplay.styles"); |
| 20 |
|
| 21 |
## |
| 22 |
# encapsulate hdg/lat/lon source, so that the ND may also display AI/MP aircraft in a pilot-view at some point (aka stress-testing) |
| 23 |
# TODO: this predates aircraftpos.controller (MapStructure) should probably be unified to some degree ... |
| 24 |
|
| 25 |
var wxr_live_tree = "/instrumentation/wxr"; |
| 26 |
|
| 27 |
var NDSourceDriver = {}; |
| 28 |
NDSourceDriver.new = func { |
| 29 |
var m = {parents:[NDSourceDriver]}; |
| 30 |
m.get_hdg_mag= func getprop("/orientation/heading-magnetic-deg"); |
| 31 |
m.get_hdg_tru= func getprop("/orientation/heading-deg"); |
| 32 |
m.get_hgg = func getprop("instrumentation/afds/settings/heading"); |
| 33 |
m.get_trk_mag= func |
| 34 |
{ |
| 35 |
if(getprop("/velocities/groundspeed-kt") > 80) |
| 36 |
getprop("/orientation/track-magnetic-deg"); |
| 37 |
else |
| 38 |
getprop("/orientation/heading-magnetic-deg"); |
| 39 |
}; |
| 40 |
m.get_trk_tru = func |
| 41 |
{ |
| 42 |
if(getprop("/velocities/groundspeed-kt") > 80) |
| 43 |
getprop("/orientation/track-deg"); |
| 44 |
else |
| 45 |
getprop("/orientation/heading-deg"); |
| 46 |
}; |
| 47 |
m.get_lat= func getprop("/position/latitude-deg"); |
| 48 |
m.get_lon= func getprop("/position/longitude-deg"); |
| 49 |
m.get_alt= func getprop("/position/altitude-ft"); |
| 50 |
m.get_spd= func getprop("/instrumentation/airspeed-indicator/true-speed-kt"); |
| 51 |
m.get_gnd_spd= func getprop("/velocities/groundspeed-kt"); |
| 52 |
m.get_vspd= func getprop("/velocities/vertical-speed-fps"); |
| 53 |
return m; |
| 54 |
} |
| 55 |
|
| 56 |
## |
| 57 |
# configure aircraft specific cockpit switches here |
| 58 |
# these are some defaults, can be overridden when calling NavDisplay.new() - |
| 59 |
# see the 744 ND.nas file the backend code should never deal directly with |
| 60 |
# aircraft specific properties using getprop. |
| 61 |
# To get started implementing your own ND, just copy the switches hash to your |
| 62 |
# ND.nas file and map the keys to your cockpit properties - and things will just work. |
| 63 |
|
| 64 |
# TODO: switches are ND specific, so move to the NDStyle hash! |
| 65 |
|
| 66 |
var default_switches = { |
| 67 |
'toggle_range': {path: '/inputs/range-nm', value:40, type:'INT'}, |
| 68 |
'toggle_weather': {path: '/inputs/wxr', value:0, type:'BOOL'}, |
| 69 |
'toggle_airports': {path: '/inputs/arpt', value:0, type:'BOOL'}, |
| 70 |
'toggle_stations': {path: '/inputs/sta', value:0, type:'BOOL'}, |
| 71 |
'toggle_waypoints': {path: '/inputs/wpt', value:0, type:'BOOL'}, |
| 72 |
'toggle_position': {path: '/inputs/pos', value:0, type:'BOOL'}, |
| 73 |
'toggle_data': {path: '/inputs/data',value:0, type:'BOOL'}, |
| 74 |
'toggle_terrain': {path: '/inputs/terr',value:0, type:'BOOL'}, |
| 75 |
'toggle_traffic': {path: '/inputs/tfc',value:0, type:'BOOL'}, |
| 76 |
'toggle_centered': {path: '/inputs/nd-centered',value:0, type:'BOOL'}, |
| 77 |
'toggle_lh_vor_adf': {path: '/inputs/lh-vor-adf',value:0, type:'INT'}, |
| 78 |
'toggle_rh_vor_adf': {path: '/inputs/rh-vor-adf',value:0, type:'INT'}, |
| 79 |
'toggle_display_mode': {path: '/mfd/display-mode', value:'MAP', type:'STRING'}, # valid values are: APP, MAP, PLAN or VOR |
| 80 |
'toggle_display_type': {path: '/mfd/display-type', value:'CRT', type:'STRING'}, # valid values are: CRT or LCD |
| 81 |
'toggle_true_north': {path: '/mfd/true-north', value:0, type:'BOOL'}, |
| 82 |
'toggle_rangearc': {path: '/mfd/rangearc', value:0, type:'BOOL'}, |
| 83 |
'toggle_track_heading':{path: '/trk-selected', value:0, type:'BOOL'}, |
| 84 |
'toggle_weather_live': {path: '/mfd/wxr-live-enabled', value: 0, type: 'BOOL'}, |
| 85 |
'toggle_chrono': {path: '/inputs/CHRONO', value: 0, type: 'INT'}, |
| 86 |
'toggle_xtrk_error': {path: '/mfd/xtrk-error', value: 0, type: 'BOOL'}, |
| 87 |
'toggle_trk_line': {path: '/mfd/trk-line', value: 0, type: 'BOOL'}, |
| 88 |
'toggle_hdg_bug_only': {path: '/mfd/hdg-bug-only', value: 0, type: 'BOOL'}, |
| 89 |
}; |
| 90 |
|
| 91 |
## |
| 92 |
# TODO: |
| 93 |
# - introduce a MFD class (use it also for PFD/EICAS) |
| 94 |
# - introduce a SGSubsystem class and use it here |
| 95 |
# - introduce a Boeing NavDisplay class |
| 96 |
var NavDisplay = { |
| 97 |
# static |
| 98 |
id:0, |
| 99 |
|
| 100 |
del: func { |
| 101 |
print("Cleaning up NavDisplay"); |
| 102 |
# shut down all timers and other loops here |
| 103 |
me.update_timer.stop(); |
| 104 |
foreach(var t; me.timers) |
| 105 |
t.stop(); |
| 106 |
foreach(var l; me.listeners) |
| 107 |
removelistener(l); |
| 108 |
# clean up MapStructure |
| 109 |
me.map.del(); |
| 110 |
# call(canvas.Map.del, [], me.map); |
| 111 |
# destroy the canvas |
| 112 |
if (me.canvas_handle != nil) |
| 113 |
me.canvas_handle.del(); |
| 114 |
me.inited = 0; |
| 115 |
NavDisplay.id -= 1; |
| 116 |
}, |
| 117 |
|
| 118 |
addtimer: func(interval, cb) { |
| 119 |
append(me.timers, var job=maketimer(interval, cb)); |
| 120 |
return job; # so that we can directly work with the timer (start/stop) |
| 121 |
}, |
| 122 |
|
| 123 |
listen: func(p,c) { |
| 124 |
append(me.listeners, setlistener(p,c)); |
| 125 |
}, |
| 126 |
|
| 127 |
# listeners for cockpit switches |
| 128 |
listen_switch: func(s,c) { |
| 129 |
# print("event setup for: ", id(c)); |
| 130 |
if (!contains(me.efis_switches, s)) { |
| 131 |
print('EFIS Switch not defined: '~ s); |
| 132 |
return; |
| 133 |
} |
| 134 |
me.listen( me.get_full_switch_path(s), func { |
| 135 |
# print("listen_switch triggered:", s, " callback id:", id(c) ); |
| 136 |
c(); |
| 137 |
}); |
| 138 |
}, |
| 139 |
|
| 140 |
# get the full property path for a given switch |
| 141 |
get_full_switch_path: func (s) { |
| 142 |
# debug.dump( me.efis_switches[s] ); |
| 143 |
return me.efis_path ~ me.efis_switches[s].path; # FIXME: should be using props.nas instead of ~ |
| 144 |
}, |
| 145 |
|
| 146 |
# helper method for getting configurable cockpit switches (which are usually different in each aircraft) |
| 147 |
get_switch: func(s) { |
| 148 |
var switch = me.efis_switches[s]; |
| 149 |
if(switch == nil) return nil; |
| 150 |
var path = me.efis_path ~ switch.path ; |
| 151 |
#print(s,":Getting switch prop:", path); |
| 152 |
|
| 153 |
return getprop( path ); |
| 154 |
}, |
| 155 |
|
| 156 |
# helper method for setting configurable cockpit switches (which are usually different in each aircraft) |
| 157 |
set_switch: func(s, v) { |
| 158 |
var switch = me.efis_switches[s]; |
| 159 |
if(switch == nil) return nil; |
| 160 |
var path = me.efis_path ~ switch.path ; |
| 161 |
#print(s,":Getting switch prop:", path); |
| 162 |
|
| 163 |
setprop( path, v ); |
| 164 |
}, |
| 165 |
|
| 166 |
# for creating NDs that are driven by AI traffic instead of the main aircraft (generalization rocks!) |
| 167 |
connectAI: func(source=nil) { |
| 168 |
me.aircraft_source = { |
| 169 |
get_hdg_mag: func source.getNode('orientation/heading-magnetic-deg').getValue(), |
| 170 |
get_trk_mag: func source.getNode('orientation/track-magnetic-deg').getValue(), |
| 171 |
get_lat: func source.getNode('position/latitude-deg').getValue(), |
| 172 |
get_lon: func source.getNode('position/longitude-deg').getValue(), |
| 173 |
get_alt: func source.getNode('position/altitude-ft').getValue(), |
| 174 |
get_spd: func source.getNode('velocities/true-airspeed-kt').getValue(), |
| 175 |
get_gnd_spd: func source.getNode('velocities/groundspeed-kt').getValue(), |
| 176 |
}; |
| 177 |
}, # of connectAI |
| 178 |
|
| 179 |
setTimerInterval: func(update_time=0.05) me.update_timer.restart(update_time), |
| 180 |
onDisplay: func {me.setTimerInterval();}, |
| 181 |
offDisplay: func {me.update_timer.stop();}, |
| 182 |
# TODO: the ctor should allow customization, for different aircraft |
| 183 |
# especially properties and SVG files/handles (747, 757, 777 etc) |
| 184 |
new : func(prop1, switches=default_switches, style='Boeing') { |
| 185 |
NavDisplay.id +=1; |
| 186 |
var m = { parents : [NavDisplay]}; |
| 187 |
|
| 188 |
var df_toggles = keys(default_switches); |
| 189 |
foreach(var toggle_name; df_toggles){ |
| 190 |
if(!contains(switches, toggle_name)) |
| 191 |
switches[toggle_name] = default_switches[toggle_name]; |
| 192 |
} |
| 193 |
|
| 194 |
m.inited = 0; |
| 195 |
|
| 196 |
m.timers=[]; |
| 197 |
m.listeners=[]; # for cleanup handling |
| 198 |
m.aircraft_source = NDSourceDriver.new(); # uses the main aircraft as the driver/source (speeds, position, heading) |
| 199 |
|
| 200 |
m.nd_style = NDStyles[style]; # look up ND specific stuff (file names etc) |
| 201 |
m.style_name = style; |
| 202 |
|
| 203 |
m.radio_list=["instrumentation/comm/frequencies","instrumentation/comm[1]/frequencies", |
| 204 |
"instrumentation/nav/frequencies", "instrumentation/nav[1]/frequencies"]; |
| 205 |
m.mfd_mode_list=["APP","VOR","MAP","PLAN"]; |
| 206 |
|
| 207 |
m.efis_path = prop1; |
| 208 |
m.efis_switches = switches; |
| 209 |
|
| 210 |
# just an alias, to avoid having to rewrite the old code for now |
| 211 |
m.rangeNm = func m.get_switch('toggle_range'); |
| 212 |
|
| 213 |
m.efis = props.globals.initNode(prop1); |
| 214 |
m.mfd = m.efis.initNode("mfd"); |
| 215 |
|
| 216 |
# TODO: unify this with switch handling |
| 217 |
m.mfd_mode_num = m.mfd .initNode("mode-num",2,"INT"); |
| 218 |
m.std_mode = m.efis.initNode("inputs/setting-std",0,"BOOL"); |
| 219 |
m.previous_set = m.efis.initNode("inhg-previous",29.92); |
| 220 |
m.kpa_mode = m.efis.initNode("inputs/kpa-mode",0,"BOOL"); |
| 221 |
m.kpa_output = m.efis.initNode("inhg-kpa",29.92); |
| 222 |
m.kpa_prevoutput = m.efis.initNode("inhg-kpa-previous",29.92); |
| 223 |
m.temp = m.efis.initNode("fixed-temp",0); |
| 224 |
m.alt_meters = m.efis.initNode("inputs/alt-meters",0,"BOOL"); |
| 225 |
m.fpv = m.efis.initNode("inputs/fpv",0,"BOOL"); |
| 226 |
|
| 227 |
m.mins_mode = m.efis.initNode("inputs/minimums-mode",0,"BOOL"); |
| 228 |
m.mins_mode_txt = m.efis.initNode("minimums-mode-text","RADIO","STRING"); |
| 229 |
m.minimums = m.efis.initNode("minimums",250,"INT"); |
| 230 |
m.mk_minimums = props.globals.getNode("instrumentation/mk-viii/inputs/arinc429/decision-height"); |
| 231 |
|
| 232 |
# TODO: these are switches, can be unified with switch handling hash above (eventually): |
| 233 |
m.nd_plan_wpt = m.efis.initNode("inputs/plan-wpt-index", -1, "INT"); # not yet in switches hash |
| 234 |
|
| 235 |
### |
| 236 |
# initialize all switches based on the defaults specified in the switch hash |
| 237 |
# |
| 238 |
foreach(var switch; keys( m.efis_switches ) ) |
| 239 |
props.globals.initNode |
| 240 |
( m.get_full_switch_path (switch), |
| 241 |
m.efis_switches[switch].value, |
| 242 |
m.efis_switches[switch].type |
| 243 |
); |
| 244 |
|
| 245 |
|
| 246 |
return m; |
| 247 |
}, |
| 248 |
newMFD: func(canvas_group, parent=nil, nd_options=nil, update_time=0.05) |
| 249 |
{ |
| 250 |
if (me.inited) die("MFD already was added to scene"); |
| 251 |
me.inited = 1; |
| 252 |
me.range_dependant_layers = []; |
| 253 |
me.always_update_layers = {}; |
| 254 |
me.update_timer = maketimer(update_time, func me.update() ); |
| 255 |
me.nd = canvas_group; |
| 256 |
me.canvas_handle = parent; |
| 257 |
me.df_options = nil; |
| 258 |
if (contains(me.nd_style, 'options')) |
| 259 |
me.df_options = me.nd_style.options; |
| 260 |
nd_options = canvas.default_hash(nd_options, me.df_options); |
| 261 |
me.options = nd_options; |
| 262 |
me.route_driver = nil; |
| 263 |
if (me.options == nil) me.options = {}; |
| 264 |
if (contains(me.options, 'route_driver')) { |
| 265 |
me.route_driver = me.options.route_driver; |
| 266 |
} |
| 267 |
elsif (contains(me.options, 'defaults')) { |
| 268 |
if(contains(me.options.defaults, 'route_driver')) |
| 269 |
me.route_driver = me.options.defaults.route_driver; |
| 270 |
} |
| 271 |
|
| 272 |
# load the specified SVG file into the me.nd group and populate all sub groups |
| 273 |
|
| 274 |
canvas.parsesvg(me.nd, me.nd_style.svg_filename, {'font-mapper': me.nd_style.font_mapper}); |
| 275 |
me.symbols = {}; # storage for SVG elements, to avoid namespace pollution (all SVG elements end up here) |
| 276 |
|
| 277 |
foreach(var feature; me.nd_style.features ) { |
| 278 |
me.symbols[feature.id] = me.nd.getElementById(feature.id).updateCenter(); |
| 279 |
if(contains(feature.impl,'init')) feature.impl.init(me.nd, feature); # call The element's init code (i.e. updateCenter) |
| 280 |
} |
| 281 |
|
| 282 |
me.nd_style.initialize_elements(me); |
| 283 |
|
| 284 |
|
| 285 |
var map_rect = [124, 1024, 1024, 0]; |
| 286 |
var map_opts = me.options['map']; |
| 287 |
if (map_opts == nil) map_opts = {}; |
| 288 |
if (typeof(map_opts['rect']) == 'vector') |
| 289 |
map_rect = map_opts.rect; |
| 290 |
map_rect = string.join(', ', map_rect); |
| 291 |
|
| 292 |
me.map = me.nd.createChild("map","map") |
| 293 |
.set("clip", map_rect) |
| 294 |
.set("projection","azimuthalequidistant") |
| 295 |
.set("screen-range", 700); |
| 296 |
var z_idx = map_opts['z-index']; |
| 297 |
if (z_idx != nil) me.map.set('z-index', z_idx); |
| 298 |
|
| 299 |
me.update_sub(); # init some map properties based on switches |
| 300 |
|
| 301 |
# predicate for the draw controller |
| 302 |
var is_tuned = func(freq) { |
| 303 |
var nav1=getprop("instrumentation/nav[0]/frequencies/selected-mhz"); |
| 304 |
var nav2=getprop("instrumentation/nav[1]/frequencies/selected-mhz"); |
| 305 |
if (freq == nav1 or freq == nav2) return 1; |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
# another predicate for the draw controller |
| 310 |
var get_course_by_freq = func(freq) { |
| 311 |
if (freq == getprop("instrumentation/nav[0]/frequencies/selected-mhz")) |
| 312 |
return getprop("instrumentation/nav[0]/radials/selected-deg"); |
| 313 |
else |
| 314 |
return getprop("instrumentation/nav[1]/radials/selected-deg"); |
| 315 |
} |
| 316 |
|
| 317 |
var get_current_position = func { |
| 318 |
delete(caller(0)[0], "me"); # remove local me, inherit outer one |
| 319 |
return [ |
| 320 |
me.aircraft_source.get_lat(), me.aircraft_source.get_lon(), me.aircraft_source.get_alt() |
| 321 |
]; |
| 322 |
} |
| 323 |
|
| 324 |
# a hash with controller callbacks, will be passed onto draw routines to customize behavior/appearance |
| 325 |
# the point being that draw routines don't know anything about their frontends (instrument or GUI dialog) |
| 326 |
# so we need some simple way to communicate between frontend<->backend until we have real controllers |
| 327 |
# for now, a single controller hash is shared by most layers - unsupported callbacks are simply ignored by the draw files |
| 328 |
# |
| 329 |
|
| 330 |
var controller = { |
| 331 |
parents: [canvas.Map.Controller], |
| 332 |
_pos: nil, _time: nil, |
| 333 |
is_tuned:is_tuned, |
| 334 |
get_tuned_course:get_course_by_freq, |
| 335 |
get_position: get_current_position, |
| 336 |
new: func(map) return { parents:[controller], map:map }, |
| 337 |
del: func() {print("cleaning up nd controller");}, |
| 338 |
should_update_all: func { |
| 339 |
# TODO: this is just copied from aircraftpos.controller, |
| 340 |
# it really should be moved to somewhere common and reused |
| 341 |
# and extended to fully differentiate between "static" |
| 342 |
# and "volatile" layers. |
| 343 |
var pos = me.map.getPosCoord(); |
| 344 |
if (pos == nil) return 0; |
| 345 |
var time = systime(); |
| 346 |
if (me._pos == nil) |
| 347 |
me._pos = geo.Coord.new(pos); |
| 348 |
else { |
| 349 |
var dist_m = me._pos.direct_distance_to(pos); |
| 350 |
# 2 NM until we update again |
| 351 |
if (dist_m < 2 * NM2M) return 0; |
| 352 |
# Update at most every 4 seconds to avoid excessive stutter: |
| 353 |
elsif (time - me._time < 4) return 0; |
| 354 |
} |
| 355 |
#print("update aircraft position"); |
| 356 |
var (x,y,z) = pos.xyz(); |
| 357 |
me._pos.set_xyz(x,y,z); |
| 358 |
me._time = time; |
| 359 |
return 1; |
| 360 |
}, |
| 361 |
}; |
| 362 |
me.map.setController(controller); |
| 363 |
|
| 364 |
### |
| 365 |
# set up various layers, controlled via callbacks in the controller hash |
| 366 |
# revisit this code once Philosopher's "Smart MVC Symbols/Layers" work is committed and integrated |
| 367 |
|
| 368 |
# helper / closure generator |
| 369 |
var make_event_handler = func(predicate, layer) func predicate(me, layer); |
| 370 |
|
| 371 |
me.layers={}; # storage container for all ND specific layers |
| 372 |
# look up all required layers as specified per the NDStyle hash and do the initial setup for event handling |
| 373 |
var default_opts = me.options != nil and contains(me.options, 'defaults') ? me.options.defaults : nil; |
| 374 |
foreach(var layer; me.nd_style.layers) { |
| 375 |
if(layer['disabled']) continue; # skip this layer |
| 376 |
#print("newMFD(): Setting up ND layer:", layer.name); |
| 377 |
|
| 378 |
var the_layer = nil; |
| 379 |
if(!layer['isMapStructure']) # set up an old INEFFICIENT and SLOW layer |
| 380 |
the_layer = me.layers[layer.name] = canvas.MAP_LAYERS[layer.name].new( me.map, layer.name, controller ); |
| 381 |
else { |
| 382 |
logprint(canvas._MP_dbg_lvl, "Setting up MapStructure-based layer for ND, name:", layer.name); |
| 383 |
var opt = me.options != nil and me.options[layer.name] != nil ? me.options[layer.name] : nil; |
| 384 |
if (opt == nil and contains(layer, 'options')) |
| 385 |
opt = layer.options; |
| 386 |
if (opt != nil and default_opts != nil) |
| 387 |
opt = canvas.default_hash(opt, default_opts); |
| 388 |
#elsif(default_opts != nil) |
| 389 |
# opt = default_opts; |
| 390 |
var style = nil; |
| 391 |
if(contains(layer, 'style')) |
| 392 |
style = layer.style; |
| 393 |
#print("Options is: ", opt!=nil?"enabled":"disabled"); |
| 394 |
#debug.dump(opt); |
| 395 |
me.map.addLayer( |
| 396 |
factory: canvas.SymbolLayer, |
| 397 |
type_arg: layer.name, |
| 398 |
opts: opt, |
| 399 |
visible:0, |
| 400 |
style: style, |
| 401 |
priority: layer['z-index'] |
| 402 |
); |
| 403 |
the_layer = me.layers[layer.name] = me.map.getLayer(layer.name); |
| 404 |
if(opt != nil and contains(opt, 'range_dependant')){ |
| 405 |
if(opt.range_dependant) |
| 406 |
append(me.range_dependant_layers, the_layer); |
| 407 |
} |
| 408 |
if(contains(layer, 'always_update')) |
| 409 |
me.always_update_layers[layer.name] = layer.always_update; |
| 410 |
if (1) (func { |
| 411 |
var l = layer; |
| 412 |
var _predicate = l.predicate; |
| 413 |
l.predicate = func { |
| 414 |
var t = systime(); |
| 415 |
call(_predicate, arg, me); |
| 416 |
logprint(canvas._MP_dbg_lvl, "Took "~((systime()-t)*1000)~"ms to update layer "~l.name); |
| 417 |
} |
| 418 |
})(); |
| 419 |
} |
| 420 |
|
| 421 |
# now register all layer specific notification listeners and their corresponding update predicate/callback |
| 422 |
# pass the ND instance and the layer handle to the predicate when it is called |
| 423 |
# so that it can directly access the ND instance and its own layer (without having to know the layer's name) |
| 424 |
var event_handler = make_event_handler(layer.predicate, the_layer); |
| 425 |
foreach(var event; layer.update_on) { |
| 426 |
# this handles timers |
| 427 |
if (typeof(event)=='hash' and contains(event, 'rate_hz')) { |
| 428 |
#print("FIXME: navdisplay.mfd timer handling is broken ATM"); |
| 429 |
var job=me.addtimer(1/event.rate_hz, event_handler); |
| 430 |
job.start(); |
| 431 |
} |
| 432 |
# and this listeners |
| 433 |
else |
| 434 |
# print("Setting up subscription:", event, " for ", layer.name, " handler id:", id(event_handler) ); |
| 435 |
me.listen_switch(event, event_handler); |
| 436 |
} # foreach event subscription |
| 437 |
# and now update/init each layer once by calling its update predicate for initialization |
| 438 |
event_handler(); |
| 439 |
} # foreach layer |
| 440 |
|
| 441 |
#print("navdisplay.mfd:ND layer setup completed"); |
| 442 |
|
| 443 |
# start the update timer, which makes sure that the update() will be called |
| 444 |
me.update_timer.start(); |
| 445 |
|
| 446 |
# TODO: move this to RTE.lcontroller ? |
| 447 |
me.listen("/autopilot/route-manager/current-wp", func(activeWp) { |
| 448 |
canvas.updatewp( activeWp.getValue() ); |
| 449 |
}); |
| 450 |
|
| 451 |
}, |
| 452 |
|
| 453 |
in_mode:func(switch, modes) |
| 454 |
{ |
| 455 |
foreach(var m; modes) if(me.get_switch(switch)==m) return 1; |
| 456 |
return 0; |
| 457 |
}, |
| 458 |
# Helper function for below (update()) and above (newMFD()) |
| 459 |
# to ensure position etc. are correct. |
| 460 |
update_sub: func() |
| 461 |
{ |
| 462 |
# Variables: |
| 463 |
var userLat = me.aircraft_source.get_lat(); |
| 464 |
var userLon = me.aircraft_source.get_lon(); |
| 465 |
var userAlt = me.aircraft_source.get_alt(); |
| 466 |
var userGndSpd = me.aircraft_source.get_gnd_spd(); |
| 467 |
var userVSpd = me.aircraft_source.get_vspd(); |
| 468 |
var dispLCD = me.get_switch('toggle_display_type') == "LCD"; |
| 469 |
# Heading update |
| 470 |
var userHdgMag = me.aircraft_source.get_hdg_mag(); |
| 471 |
var userHdgTru = me.aircraft_source.get_hdg_tru(); |
| 472 |
var userTrkMag = me.aircraft_source.get_trk_mag(); |
| 473 |
var userTrkTru = me.aircraft_source.get_trk_tru(); |
| 474 |
|
| 475 |
if(me.get_switch('toggle_true_north')) { |
| 476 |
var userHdg=userHdgTru; |
| 477 |
me.userHdg=userHdgTru; |
| 478 |
var userTrk=userTrkTru; |
| 479 |
me.userTrk=userTrkTru; |
| 480 |
} else { |
| 481 |
var userHdg=userHdgMag; |
| 482 |
me.userHdg=userHdgMag; |
| 483 |
var userTrk=userTrkMag; |
| 484 |
me.userTrk=userTrkMag; |
| 485 |
} |
| 486 |
# this should only ever happen when testing the experimental AI/MP ND driver hash (not critical) |
| 487 |
# or when an error occurs (critical) |
| 488 |
if (!userHdg or !userTrk or !userLat or !userLon) { |
| 489 |
print("aircraft source invalid, returning !"); |
| 490 |
return; |
| 491 |
} |
| 492 |
if (me.aircraft_source.get_gnd_spd() < 80) { |
| 493 |
userTrk = userHdg; |
| 494 |
me.userTrk=userHdg; |
| 495 |
} |
| 496 |
|
| 497 |
if((me.in_mode('toggle_display_mode', ['MAP']) and me.get_switch('toggle_display_type') == "CRT") |
| 498 |
or (me.get_switch('toggle_track_heading') and me.get_switch('toggle_display_type') == "LCD")) |
| 499 |
{ |
| 500 |
userHdgTrk = userTrk; |
| 501 |
me.userHdgTrk = userTrk; |
| 502 |
userHdgTrkTru = userTrkTru; |
| 503 |
me.symbols.hdgTrk.setText("TRK"); |
| 504 |
} else { |
| 505 |
userHdgTrk = userHdg; |
| 506 |
me.userHdgTrk = userHdg; |
| 507 |
userHdgTrkTru = userHdgTru; |
| 508 |
me.symbols.hdgTrk.setText("HDG"); |
| 509 |
} |
| 510 |
|
| 511 |
# First, update the display position of the map |
| 512 |
var oldRange = me.map.getRange(); |
| 513 |
var pos = { |
| 514 |
lat: nil, lon: nil, |
| 515 |
alt: nil, hdg: nil, |
| 516 |
range: nil, |
| 517 |
}; |
| 518 |
# reposition the map, change heading & range: |
| 519 |
var pln_wpt_idx = getprop(me.efis_path ~ "/inputs/plan-wpt-index"); |
| 520 |
if(me.in_mode('toggle_display_mode', ['PLAN']) and pln_wpt_idx >= 0) { |
| 521 |
if(me.route_driver != nil){ |
| 522 |
var wp = me.route_driver.getPlanModeWP(pln_wpt_idx); |
| 523 |
if(wp != nil){ |
| 524 |
pos.lat = wp.wp_lat; |
| 525 |
pos.lon = wp.wp_lon; |
| 526 |
} else { |
| 527 |
pos.lat = getprop("/autopilot/route-manager/route/wp["~pln_wpt_idx~"]/latitude-deg"); |
| 528 |
pos.lon = getprop("/autopilot/route-manager/route/wp["~pln_wpt_idx~"]/longitude-deg"); |
| 529 |
} |
| 530 |
} else { |
| 531 |
pos.lat = getprop("/autopilot/route-manager/route/wp["~pln_wpt_idx~"]/latitude-deg"); |
| 532 |
pos.lon = getprop("/autopilot/route-manager/route/wp["~pln_wpt_idx~"]/longitude-deg"); |
| 533 |
} |
| 534 |
} else { |
| 535 |
pos.lat = userLat; |
| 536 |
pos.lon = userLon; |
| 537 |
pos.alt = userAlt; |
| 538 |
} |
| 539 |
if(me.in_mode('toggle_display_mode', ['PLAN'])) { |
| 540 |
pos.hdg = 0; |
| 541 |
pos.range = me.rangeNm()*2 |
| 542 |
} else { |
| 543 |
pos.range = me.rangeNm(); # avoid this here, use a listener instead |
| 544 |
pos.hdg = userHdgTrkTru; |
| 545 |
} |
| 546 |
if(me.options != nil and (var pos_callback = me.options['position_callback']) != nil) |
| 547 |
pos_callback(me, pos); |
| 548 |
call(me.map.setPos, [pos.lat, pos.lon, nil, nil, pos.alt], me.map, pos); |
| 549 |
if(pos.range != oldRange){ |
| 550 |
foreach(l; me.range_dependant_layers){ |
| 551 |
l.update(); |
| 552 |
} |
| 553 |
} |
| 554 |
}, |
| 555 |
# each model should keep track of when it last got updated, using current lat/lon |
| 556 |
# in update(), we can then check if the aircraft has traveled more than 0.5-1 nm (depending on selected range) |
| 557 |
# and update each model accordingly |
| 558 |
# TODO: Hooray is still waiting for a really rainy weekend to clean up all the mess here... so plz don't add to it! |
| 559 |
update: func() # FIXME: This stuff is still too aircraft specific, cannot easily be reused by other aircraft |
| 560 |
{ |
| 561 |
var _time = systime(); |
| 562 |
# Disables WXR Live if it's not enabled. The toggle_weather_live should be common to all |
| 563 |
# ND instances. |
| 564 |
var wxr_live_enabled = getprop(wxr_live_tree~'/enabled'); |
| 565 |
if(wxr_live_enabled == nil or wxr_live_enabled == '') |
| 566 |
wxr_live_enabled = 0; |
| 567 |
me.set_switch('toggle_weather_live', wxr_live_enabled); |
| 568 |
|
| 569 |
call(me.update_sub, nil, nil, caller(0)[0]); # call this in the same namespace to "steal" its variables |
| 570 |
|
| 571 |
# MapStructure update! |
| 572 |
if (me.map.controller.should_update_all()) { |
| 573 |
me.map.update(); |
| 574 |
} else { |
| 575 |
# TODO: ugly list here |
| 576 |
# FIXME: use a VOLATILE layer helper here that handles TFC, APS, WXR etc ? |
| 577 |
var update_layers = me.always_update_layers; |
| 578 |
me.map.update(func(layer) contains(update_layers, layer.type)); |
| 579 |
} |
| 580 |
|
| 581 |
# Other symbol update |
| 582 |
# TODO: should be refactored! |
| 583 |
var translation_callback = nil; |
| 584 |
if(me.options != nil) |
| 585 |
translation_callback = me.options['translation_callback']; |
| 586 |
if(typeof(translation_callback) == 'func'){ |
| 587 |
var trsl = translation_callback(me); |
| 588 |
me.map.setTranslation(trsl.x, trsl.y); |
| 589 |
} else { |
| 590 |
if(me.in_mode('toggle_display_mode', ['PLAN'])) |
| 591 |
me.map.setTranslation(512,512); |
| 592 |
elsif(me.get_switch('toggle_centered')) |
| 593 |
me.map.setTranslation(512,565); |
| 594 |
else |
| 595 |
me.map.setTranslation(512,824); |
| 596 |
} |
| 597 |
|
| 598 |
if(me.get_switch('toggle_rh_vor_adf') == 1) { |
| 599 |
me.symbols.vorR.setText("VOR R"); |
| 600 |
me.symbols.vorR.setColor(0.195,0.96,0.097); |
| 601 |
me.symbols.dmeR.setText("DME"); |
| 602 |
me.symbols.dmeR.setColor(0.195,0.96,0.097); |
| 603 |
if(getprop("instrumentation/nav[1]/in-range")) |
| 604 |
me.symbols.vorRId.setText(getprop("instrumentation/nav[1]/nav-id")); |
| 605 |
else |
| 606 |
me.symbols.vorRId.setText(getprop("instrumentation/nav[1]/frequencies/selected-mhz-fmt")); |
| 607 |
me.symbols.vorRId.setColor(0.195,0.96,0.097); |
| 608 |
if(getprop("instrumentation/dme[1]/in-range")) |
| 609 |
me.symbols.dmeRDist.setText(sprintf("%3.1f",getprop("instrumentation/dme[1]/indicated-distance-nm"))); |
| 610 |
else me.symbols.dmeRDist.setText(" ---"); |
| 611 |
me.symbols.dmeRDist.setColor(0.195,0.96,0.097); |
| 612 |
} elsif(me.get_switch('toggle_rh_vor_adf') == -1) { |
| 613 |
me.symbols.vorR.setText("ADF R"); |
| 614 |
me.symbols.vorR.setColor(0,0.6,0.85); |
| 615 |
me.symbols.dmeR.setText(""); |
| 616 |
me.symbols.dmeR.setColor(0,0.6,0.85); |
| 617 |
if((var navident=getprop("instrumentation/adf[1]/ident")) != "") |
| 618 |
me.symbols.vorRId.setText(navident); |
| 619 |
else me.symbols.vorRId.setText(sprintf("%3d",getprop("instrumentation/adf[1]/frequencies/selected-khz"))); |
| 620 |
me.symbols.vorRId.setColor(0,0.6,0.85); |
| 621 |
me.symbols.dmeRDist.setText(""); |
| 622 |
me.symbols.dmeRDist.setColor(0,0.6,0.85); |
| 623 |
} else { |
| 624 |
me.symbols.vorR.setText(""); |
| 625 |
me.symbols.dmeR.setText(""); |
| 626 |
me.symbols.vorRId.setText(""); |
| 627 |
me.symbols.dmeRDist.setText(""); |
| 628 |
} |
| 629 |
|
| 630 |
# Hide heading bug 10 secs after change |
| 631 |
var vhdg_bug = getprop("autopilot/settings/heading-bug-deg") or 0; |
| 632 |
var hdg_bug_active = getprop("autopilot/settings/heading-bug-active"); |
| 633 |
if (hdg_bug_active == nil) |
| 634 |
hdg_bug_active = 1; |
| 635 |
|
| 636 |
if((me.in_mode('toggle_display_mode', ['MAP']) and me.get_switch('toggle_display_type') == "CRT") |
| 637 |
or (me.get_switch('toggle_track_heading') and me.get_switch('toggle_display_type') == "LCD")) |
| 638 |
{ |
| 639 |
me.symbols.trkInd.setRotation(0); |
| 640 |
me.symbols.curHdgPtr.setRotation((userHdg-userTrk)*D2R); |
| 641 |
me.symbols.curHdgPtr2.setRotation((userHdg-userTrk)*D2R); |
| 642 |
} |
| 643 |
else |
| 644 |
{ |
| 645 |
me.symbols.trkInd.setRotation((userTrk-userHdg)*D2R); |
| 646 |
me.symbols.curHdgPtr.setRotation(0); |
| 647 |
me.symbols.curHdgPtr2.setRotation(0); |
| 648 |
} |
| 649 |
if(!me.in_mode('toggle_display_mode', ['PLAN'])) |
| 650 |
{ |
| 651 |
var hdgBugRot = (vhdg_bug-userHdgTrk)*D2R; |
| 652 |
me.symbols.selHdgLine.setRotation(hdgBugRot); |
| 653 |
me.symbols.hdgBug.setRotation(hdgBugRot); |
| 654 |
me.symbols.hdgBug2.setRotation(hdgBugRot); |
| 655 |
me.symbols.selHdgLine2.setRotation(hdgBugRot); |
| 656 |
} |
| 657 |
|
| 658 |
var staPtrVis = !me.in_mode('toggle_display_mode', ['PLAN']); |
| 659 |
if((me.in_mode('toggle_display_mode', ['MAP']) and me.get_switch('toggle_display_type') == "CRT") |
| 660 |
or (me.get_switch('toggle_track_heading') and me.get_switch('toggle_display_type') == "LCD")) |
| 661 |
{ |
| 662 |
var vorheading = userTrkTru; |
| 663 |
var adfheading = userTrkMag; |
| 664 |
} |
| 665 |
else |
| 666 |
{ |
| 667 |
var vorheading = userHdgTru; |
| 668 |
var adfheading = userHdgMag; |
| 669 |
} |
| 670 |
if(getprop("instrumentation/nav/heading-deg") != nil) |
| 671 |
var nav0hdg=getprop("instrumentation/nav/heading-deg") - vorheading; |
| 672 |
if(getprop("instrumentation/nav[1]/heading-deg") != nil) |
| 673 |
var nav1hdg=getprop("instrumentation/nav[1]/heading-deg") - vorheading; |
| 674 |
var adf0hdg=getprop("instrumentation/adf/indicated-bearing-deg"); |
| 675 |
var adf1hdg=getprop("instrumentation/adf[1]/indicated-bearing-deg"); |
| 676 |
if(!me.get_switch('toggle_centered')) |
| 677 |
{ |
| 678 |
if(me.in_mode('toggle_display_mode', ['PLAN'])) |
| 679 |
me.symbols.trkInd.hide(); |
| 680 |
else |
| 681 |
me.symbols.trkInd.show(); |
| 682 |
if((getprop("instrumentation/nav/in-range") and me.get_switch('toggle_lh_vor_adf') == 1)) { |
| 683 |
me.symbols.staArrowL.setVisible(staPtrVis); |
| 684 |
me.symbols.staToL.setColor(0.195,0.96,0.097); |
| 685 |
me.symbols.staFromL.setColor(0.195,0.96,0.097); |
| 686 |
me.symbols.staArrowL.setRotation(nav0hdg*D2R); |
| 687 |
} |
| 688 |
elsif(getprop("instrumentation/adf/in-range") and (me.get_switch('toggle_lh_vor_adf') == -1)) { |
| 689 |
me.symbols.staArrowL.setVisible(staPtrVis); |
| 690 |
me.symbols.staToL.setColor(0,0.6,0.85); |
| 691 |
me.symbols.staFromL.setColor(0,0.6,0.85); |
| 692 |
me.symbols.staArrowL.setRotation(adf0hdg*D2R); |
| 693 |
} else { |
| 694 |
me.symbols.staArrowL.hide(); |
| 695 |
} |
| 696 |
if((getprop("instrumentation/nav[1]/in-range") and me.get_switch('toggle_rh_vor_adf') == 1)) { |
| 697 |
me.symbols.staArrowR.setVisible(staPtrVis); |
| 698 |
me.symbols.staToR.setColor(0.195,0.96,0.097); |
| 699 |
me.symbols.staFromR.setColor(0.195,0.96,0.097); |
| 700 |
me.symbols.staArrowR.setRotation(nav1hdg*D2R); |
| 701 |
} elsif(getprop("instrumentation/adf[1]/in-range") and (me.get_switch('toggle_rh_vor_adf') == -1)) { |
| 702 |
me.symbols.staArrowR.setVisible(staPtrVis); |
| 703 |
me.symbols.staToR.setColor(0,0.6,0.85); |
| 704 |
me.symbols.staFromR.setColor(0,0.6,0.85); |
| 705 |
me.symbols.staArrowR.setRotation(adf1hdg*D2R); |
| 706 |
} else { |
| 707 |
me.symbols.staArrowR.hide(); |
| 708 |
} |
| 709 |
me.symbols.staArrowL2.hide(); |
| 710 |
me.symbols.staArrowR2.hide(); |
| 711 |
me.symbols.curHdgPtr2.hide(); |
| 712 |
me.symbols.HdgBugCRT2.hide(); |
| 713 |
me.symbols.TrkBugLCD2.hide(); |
| 714 |
me.symbols.HdgBugLCD2.hide(); |
| 715 |
me.symbols.selHdgLine2.hide(); |
| 716 |
me.symbols.curHdgPtr.setVisible(staPtrVis); |
| 717 |
me.symbols.HdgBugCRT.setVisible(staPtrVis and !dispLCD); |
| 718 |
if(me.get_switch('toggle_track_heading') and !me.get_switch('toggle_hdg_bug_only')) |
| 719 |
{ |
| 720 |
me.symbols.HdgBugLCD.hide(); |
| 721 |
me.symbols.TrkBugLCD.setVisible(staPtrVis and dispLCD); |
| 722 |
} |
| 723 |
else |
| 724 |
{ |
| 725 |
me.symbols.TrkBugLCD.hide(); |
| 726 |
me.symbols.HdgBugLCD.setVisible(staPtrVis and dispLCD); |
| 727 |
} |
| 728 |
me.symbols.selHdgLine.setVisible(staPtrVis and hdg_bug_active); |
| 729 |
} else { |
| 730 |
me.symbols.trkInd.hide(); |
| 731 |
if((getprop("instrumentation/nav/in-range") and me.get_switch('toggle_lh_vor_adf') == 1)) { |
| 732 |
me.symbols.staArrowL2.setVisible(staPtrVis); |
| 733 |
me.symbols.staFromL2.setColor(0.195,0.96,0.097); |
| 734 |
me.symbols.staToL2.setColor(0.195,0.96,0.097); |
| 735 |
me.symbols.staArrowL2.setRotation(nav0hdg*D2R); |
| 736 |
} elsif(getprop("instrumentation/adf/in-range") and (me.get_switch('toggle_lh_vor_adf') == -1)) { |
| 737 |
me.symbols.staArrowL2.setVisible(staPtrVis); |
| 738 |
me.symbols.staFromL2.setColor(0,0.6,0.85); |
| 739 |
me.symbols.staToL2.setColor(0,0.6,0.85); |
| 740 |
me.symbols.staArrowL2.setRotation(adf0hdg*D2R); |
| 741 |
} else { |
| 742 |
me.symbols.staArrowL2.hide(); |
| 743 |
} |
| 744 |
if((getprop("instrumentation/nav[1]/in-range") and me.get_switch('toggle_rh_vor_adf') == 1)) { |
| 745 |
me.symbols.staArrowR2.setVisible(staPtrVis); |
| 746 |
me.symbols.staFromR2.setColor(0.195,0.96,0.097); |
| 747 |
me.symbols.staToR2.setColor(0.195,0.96,0.097); |
| 748 |
me.symbols.staArrowR2.setRotation(nav1hdg*D2R); |
| 749 |
} elsif(getprop("instrumentation/adf[1]/in-range") and (me.get_switch('toggle_rh_vor_adf') == -1)) { |
| 750 |
me.symbols.staArrowR2.setVisible(staPtrVis); |
| 751 |
me.symbols.staFromR2.setColor(0,0.6,0.85); |
| 752 |
me.symbols.staToR2.setColor(0,0.6,0.85); |
| 753 |
me.symbols.staArrowR2.setRotation(adf1hdg*D2R); |
| 754 |
} else { |
| 755 |
me.symbols.staArrowR2.hide(); |
| 756 |
} |
| 757 |
me.symbols.staArrowL.hide(); |
| 758 |
me.symbols.staArrowR.hide(); |
| 759 |
me.symbols.curHdgPtr.hide(); |
| 760 |
me.symbols.HdgBugCRT.hide(); |
| 761 |
me.symbols.TrkBugLCD.hide(); |
| 762 |
me.symbols.HdgBugLCD.hide(); |
| 763 |
me.symbols.selHdgLine.hide(); |
| 764 |
me.symbols.curHdgPtr2.setVisible(staPtrVis); |
| 765 |
me.symbols.HdgBugCRT2.setVisible(staPtrVis and !dispLCD); |
| 766 |
if(me.get_switch('toggle_track_heading') and !me.get_switch('toggle_hdg_bug_only')) |
| 767 |
{ |
| 768 |
me.symbols.HdgBugLCD2.hide(); |
| 769 |
me.symbols.TrkBugLCD2.setVisible(staPtrVis and dispLCD); |
| 770 |
} |
| 771 |
else |
| 772 |
{ |
| 773 |
me.symbols.TrkBugLCD2.hide(); |
| 774 |
me.symbols.HdgBugLCD2.setVisible(staPtrVis and dispLCD); |
| 775 |
} |
| 776 |
me.symbols.selHdgLine2.setVisible(staPtrVis and hdg_bug_active); |
| 777 |
} |
| 778 |
|
| 779 |
## run all predicates in the NDStyle hash and evaluate their true/false behavior callbacks |
| 780 |
## this is in line with the original design, but normally we don't need to getprop/poll here, |
| 781 |
## using listeners or timers would be more canvas-friendly whenever possible |
| 782 |
## because running setprop() on any group/canvas element at framerate means that the canvas |
| 783 |
## will be updated at frame rate too - wasteful ... (check the performance monitor!) |
| 784 |
|
| 785 |
foreach(var feature; me.nd_style.features ) { |
| 786 |
# for stuff that always needs to be updated |
| 787 |
if (contains(feature.impl, 'common')) feature.impl.common(me); |
| 788 |
# conditional stuff |
| 789 |
if(!contains(feature.impl, 'predicate')) continue; # no conditional stuff |
| 790 |
if ( var result=feature.impl.predicate(me) ) |
| 791 |
feature.impl.is_true(me, result); # pass the result to the predicate |
| 792 |
else |
| 793 |
feature.impl.is_false( me, result ); # pass the result to the predicate |
| 794 |
} |
| 795 |
|
| 796 |
## update the status flags shown on the ND (wxr, wpt, arpt, sta) |
| 797 |
# this could/should be using listeners instead ... |
| 798 |
me.symbols['status.wxr'].setVisible( me.get_switch('toggle_weather') and me.in_mode('toggle_display_mode', ['MAP'])); |
| 799 |
me.symbols['status.wpt'].setVisible( me.get_switch('toggle_waypoints') and me.in_mode('toggle_display_mode', ['MAP'])); |
| 800 |
me.symbols['status.arpt'].setVisible( me.get_switch('toggle_airports') and me.in_mode('toggle_display_mode', ['MAP'])); |
| 801 |
me.symbols['status.sta'].setVisible( me.get_switch('toggle_stations') and me.in_mode('toggle_display_mode', ['MAP'])); |
| 802 |
# Okay, _how_ do we hook this up with FGPlot? |
| 803 |
logprint(canvas._MP_dbg_lvl, "Total ND update took "~((systime()-_time)*100)~"ms"); |
| 804 |
setprop("/instrumentation/navdisplay["~ NavDisplay.id ~"]/update-ms", systime() - _time); |
| 805 |
} # of update() method (50% of our file ...seriously?) |
| 806 |
}; |