zkv1000 / Nasal / afcs.nas /
476952d 4 years ago
1 contributor
179 lines | 7.586kb
var APClass = {
    new : func {
        var m = { parents: [ APClass ] };

        m.system = 'none';
        var ap_systems = { # described AP systems to search, if it returns true system is set
            STEC55X: func contains(stec55x, 'ITAF'),
        };
        foreach (var s; sort(keys(ap_systems), func(a,b) cmp(a,b))) {
            call(ap_systems[s], [], nil, nil, var errors = []);
            if (!size(errors)) {
                msg('found autopilot system: ' ~ s);
                m.system = s;
                break;
            }
        }

        m.engaged = 0;

        if (! contains(data.timers, 'updateAP')) {
            data.timers.updateAP = maketimer(1, m, m.systems[m.system].updateDisplay);
            data.timers.updateAP.start();
        }

        var ap_annun = [
            'LATMOD-Armed-text',  'LATMOD-Active-text',
            'AP-Status-text',     'YD-Status-text',
            'VERMOD-Active-text', 'VERMOD-Reference-text', 'VERMOD-Armed-text'
        ];
        foreach (var elem; ap_annun) {
            var color = (elem == 'LATMOD-Armed-text' or elem == 'VERMOD-Armed-text') ? 'white' : 'green';
            flightdeck.PFD.display.screenElements[elem]
                .setColor(flightdeck.PFD.display.colors[color])
                .setVisible(0);
        }
        foreach (var ap; [ 'AP', 'YD' ])
            flightdeck.PFD.display.screenElements[ap ~ '-Status-text']
                .setText(ap);

        ap_annun = nil;
        ap_systems = nil;

        if (contains(m.systems[m.system], 'hook') and typeof(m.systems[m.system].hook) == 'func')
                m.systems[m.system].hook();

        return m;
    },
    softkey: func (side, row, a) {
        if (a)
            return;
        var _system = me.systems[me.system];
        var _side = _system[side];
        _side[row]();
    },
    systems : {
        # L: AP FD  NAV ALT VS FLC
        # R: YD HDG APR VNV UP DN
        none: {
            updateDisplay: func,
            hook: func,
            L: [ func, func, func, func, func, func ],
            R: [ func, func, func, func, func, func ],
        },
        STEC55X: {
            hook : func {
                me.trimTarget = 0;
                afcs.getNode('heading-bug-deg').unalias();
                stec55x.hdg.alias(afcs.getNode('heading-bug-deg'));
                afcs.getNode('selected-alt-ft').unalias();
                stec55x.alt.alias(afcs.getNode('selected-alt-ft'));
                setprop('/it-stec55x/input/ap-master-sw', 1);
            },
            updateDisplay: func {
                var se = flightdeck.PFD.display.screenElements;
                se['AP-Status-text'].setVisible(stec55x.rollMode  != -1 or stec55x.pitchMode != -1);
                se['YD-Status-text'].setVisible(stec55x.yaw.getValue() != -1);
                if (stec55x.rollMode  != -1) {
                    se['LATMOD-Active-text'].setVisible(1).setText('ROL');
                    var armed = '';
                    foreach (var m; [ 'NAV', 'CNAV', 'REV', 'CREV' ])
                        if (stec55x[m]) armed = m;
                    if (stec55x.roll.getValue() == 0) armed = 'HDG';
                    elsif (stec55x.roll.getValue() == 2) armed = 'GPS';
                    elsif (stec55x.APR_annun.getValue()) armed = 'APR';
                    se['LATMOD-Armed-text']
                        .setVisible(size(armed))
                        .setText(armed);
                }
                else {
                    se['LATMOD-Active-text'].setVisible(0);
                    se['LATMOD-Armed-text'].setVisible(0);
                }

                if (stec55x.pitchMode != 1) {
                    var armed     = '';
                    var active    = '';
                    var reference = '';
                    if (stec55x.ALT_annun.getBoolValue()) {
                        if (abs(data.alt - afcs.getValue('selected-alt-ft')) < 150) {
                            active    = 'ALT';
                            reference = sprintf('%5d ft', afcs.getValue('selected-alt-ft'));
                            armed     = 'ALTS'
                        }
                        else {
                            active    = 'ALT';
                            reference = sprintf('%5d ft', math.round(data.alt, 10));
                            armed     = 'ALT';
                        }
                    }
                    elsif (stec55x.VS_annun.getBoolValue()) {
                        active    = 'VS';
                        reference = sprintf('%s%4d fpm',
                                        utf8.chstr(stec55x.vs.getValue() > 0 ? 9650 : 9660),
                                        math.abs(math.round(stec55x.vs.getValue(), 10)));
                    }
                    elsif (stec55x.GSArmed.getBoolValue()) {
                        armed  = 'VPATH';
                        active = 'GS';
                    }
# TODO: ask Octal450 which prop or variable can be used here
#                    elsif (stec55x.???) {
#                        armed  = 'PIT';
#                        active = 'ALT';
#                        reference = sprintf("%d°", autopilot.systems.STEC55X.trim);
#                    }
                    se['VERMOD-Active-text'].setVisible(size(active)).setText(active);
                    se['VERMOD-Armed-text'].setVisible(size(armed)).setText(armed);
                    se['VERMOD-Reference-text'].setVisible(size(reference)).setText(reference);
                }
            },
            L: [
                func,
                func {
#                    var apfd_master_sw = '/it-stec55x/input/apfd-master-sw';
#                    setprop(apfd_master_sw, !getprop(apfd_master_sw));
                },
                func {
#                    call(stec55x.button.NAV, [], nil, stec55x);
                },
                func {
#                    call(stec55x.button.ALT, [], nil, stec55x);
                },
                func {
#                    stec55x.vs.setValue(math.round(data.vsi, 100));
#                    call(stec55x.button.VS, [], nil, stec55x);
                },
                func,
            ],
            R: [
                func {
#                    var yaw_dumper_sw = '/it-stec55x/input/yaw-damper-sw';
#                    setprop(yaw_dumper_sw, !getprop(yaw_dumper_sw));
                },
                func {
                    if (stec55x.roll.getValue() == 0)
                        stec55x.roll.setValue(-1);
                    else
                        call(stec55x.button.HDG, [], nil, stec55x);
                },
                func {
#                    call(stec55x.button.APR, [], nil, stec55x);
                },
                func,
                func { # UP (trim)
#                    if (autopilot.systems.STEC55X.trimTarget > -15)
#                        autopilot.systems.STEC55X.trimTarget -= 1;
#                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value: -1});
#                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value:  0});
                },
                func { # DN (trim)
#                    if (autopilot.systems.STEC55X.trimTarget < 20)
#                        autopilot.systems.STEC55X.trimTarget += 1;
#                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value: 1});
#                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value: 0});
                },
            ],
        }
    }
};