zkv1000 / Nasal / routes.nas /
Newer Older
154 lines | 6.38kb
commit initial
Sébastien MARQUE authored on 2017-03-07
1
var selectNextWayPoint = func (_me_, command) {
2
    var w = geo.Coord.new();
3
    var r = geo.Coord.new();
4
    var c = [];
5
    var scratch = '/instrumentation/gps/scratch/';
6
    w.set_latlon(getprop(_me_.map_path ~ '/latitude-deg'),
7
                 getprop(_me_.map_path ~ '/longitude-deg'));
8
    lockSearches();
9
    foreach (var t; ['fix', 'airport', 'vor', 'ndb']) {
10
        searchNearestNavaid(t, 3, _me_.map_path);
11
        while (getprop(scratch ~ '/has-next')) {
12
            r.set_latlon(getprop(scratch ~ 'latitude-deg'),
13
                         getprop(scratch ~ 'longitude-deg'));
14
            append(c, [ w.distance_to(r),
15
                        getprop(scratch ~ 'name'),
16
                        getprop(scratch ~ 'ident'),
17
                        getprop(scratch ~ 'mag-bearing-deg'),
18
                        t
19
                       ]
20
             );
21
             setprop('/instrumentation/gps/command', 'next');
22
        }
23
    }
24
    unlockSearches();
25
    c = sort(c, func (a,b) { return a[0] - b[0] });
26
    _me_.mud.wipe();
27
    var action = '';
28
    if (command == 'dto') {
29
        _me_.mud.setTitle('DIRECT TO...');
30
        action = 'direct to '
31
    }
32
    else {
33
        _me_.mud.setTitle('SELECT A WAYPOINT');
34
        action = 'appended to route ';
35
    }
36
    _me_.mud.add(['CANCEL'], func { _me_.mud.close() });
37
    _me_.mud.add(['HERE'], func {
38
                               var n = 'instrumentation/gps/scratch/';
39
                               lockSearches();
40
                               setprop(n ~ 'longitude-deg', _me_.map_path ~ '/longitude-deg');
41
                               setprop(n ~ 'latitude-deg', _me_.map_path ~ '/latitude-deg');
42
                               setprop(n ~ 'type', 'USER');
43
                               setprop(n ~ 'name', 'USER WAYPOINT');
44
                               setprop(n ~ 'ident', '---');
45
                               setprop(n ~ 'index', -1);
46
                               setprop(n ~ '../command', command);
47
                               unlockSearches();
48
                               msg(action ~ 'unnamed waypoint');
49
                           });
50
    for (var i = 0; i < size(c) and c[i][0] < 18520 and i < 10; i += 1) { # only the 10 nearest and distance < 10NM
51
        _me_.mud.add(
52
            [c[i][1], sprintf('%s -- DST %.1fNM BRG %03i', c[i][2], c[i][0] / 1852, c[i][3])], 
53
            func { 
54
                var n = '/instrumentation/gps/scratch/';
55
                lockSearches();
56
                setprop(n ~ 'longitude-deg', getprop(_me_.map_path ~ '/longitude-deg'));
57
                setprop(n ~ 'latitude-deg', getprop(_me_.map_path ~ '/latitude-deg'));
58
                setprop(n ~ 'type', arg[0][0]);
59
                setprop(n ~ 'query', arg[0][1]);
60
                setprop(n ~ 'order-by-range', 1);
61
                setprop(n ~ 'exact', 1);
62
                setprop(n ~ '../command', 'search');
63
                setprop(n ~ 'index', -1);
64
                setprop(n ~ '../command', command);
65
                unlockSearches();
66
                msg(action ~ arg[0][0] ~ ' ' ~ arg[0][1]);
67
            },
68
            [ c[i][4], c[i][2] ]
69
        );
70
    }
71
    c = nil;
72
    _me_.mud.open(1); # allow wrapping
73
    _me_.mud.select(); # select the first entry
74
    _me_.mud.select(1); # select HERE;
75
    if (size(_me_.mud.content) > 2) _me_.mud.select(1); # select nearest navaid if available
76
    _me_.largeFMSknob = func (dir) { _me_.mud.select(dir) };
77
}
78

            
79
showSearchResults = func (mud, title, freq, callback) {
80
    var scratch = '/instrumentation/gps/scratch/';
81
    mud.wipe();
82
    mud.setTitle(title);
83
    while (getprop(scratch ~ 'has-next')) {
84
        mud.add([getprop(scratch ~ 'name'),
85
                 sprintf('%s -- DST %.1fNM BRG %03i', 
86
                    getprop(scratch ~ 'ident'),
87
                    getprop(scratch ~ 'distance-nm'), 
88
                    getprop(scratch ~ 'mag-bearing-deg')
89
                 )],
90
                 callback,
91
                 [ getprop(scratch ~ freq), getprop(scratch ~ 'ident'), getprop(scratch ~ 'type') ]);
92
        setprop('/instrumentation/gps/command', 'next');
93
    }
94
    mud.open();
95
    mud.select();
96
}
97

            
98
var selectSearchType = func (mud, dir) {
99
    lockSearches();
100
    var title = mud.node.getNode('title').getValue();
101
    if (title == 'NEAREST AIRPORTS') {
102
        searchNearestNavaid('vor',     5);
103
        showSearchResults(mud, 'NEAREST VOR', 'frequency-mhz',
104
                func { msg('set NAV1 standby freq to: ' ~ arg[0][0] ~ 'MHz');
105
                    setprop('/instrumentation/nav/frequencies/standby-mhz', arg[0][0]) 
106
                }
107
        );
108
    }
109
    elsif (title == 'NEAREST VOR') {
110
        searchNearestNavaid('ndb',     5);
111
        showSearchResults(mud, 'NEAREST NDB', 'frequency-khz', 
112
                func { msg('set ADF standby freq to ' ~ arg[0][0] ~ 'kHz');
113
                    setprop('/instrumentation/adf/frequencies/standby-khz', arg[0][0]) 
114
                }
115
        );
116
    }
117
    elsif (title == 'NEAREST NDB') {
118
        searchNearestNavaid('airport', 10);
119
        showSearchResults(mud, 'NEAREST AIRPORTS', 'mag-bearing-deg', 
120
                func { msg('set HDG to ' ~ arg[0][0]); 
121
                    setprop('/instrumentation/zkv1000/afcs/heading-bug-deg', arg[0][0]) 
122
                }
123
        );
124
    }
125
    unlockSearches();
126
}
127

            
128
var showAvailableFPL = func (_me_) {
129
    var files = [];
130
    var mud = _me_.mud;
131
    foreach (var subdir; ['/zkv1000/routes/', '/Routes/']) { # compatibility with zkv500 route store path
132
        var path = getprop('/sim/fg-home') ~ subdir;
133
        var s = io.stat(path);
134
        (s != nil and s[11] == 'dir') or continue;
135
        foreach (var f; directory(path)) {
136
            if (f[0] != `.` and substr(f, -4) == '.xml') append(files, path ~ f);
137
        }
138
    }
139
    if (size(files) == 0) {
140
        msg('unable to find a directory containing routes');
141
        return;
142
    }
143
    files = sort(files, func (a,b) { a[0] - b[0] });
144
    mud.wipe();
145
    mud.setTitle('STORED FLIGHTPLANS');
146
    mud.add(['CANCEL'], func { mud.close(mud) });
147
    for (var i = 0; i < size(files); i += 1) {
148
        mud.add([string.uc(split('.', split('/', files[i])[-1])[0])], loadFPLfromFile, files[i]);
149
    }
150
    mud.open(1); # allow wrapping
151
    mud.select(); # select CANCEL
152
    if (size(mud.content) > 1) mud.select(1); # select first entry if exists
153
    _me_.largeFMSknob = func (dir) { mud.select(dir) }
154
}