zkv1000 / Nasal / afcs.nas /
3e5a1b0 4 years ago
1 contributor
141 lines | 5.236kb
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;
            }
        }
        ap_systems = nil;

        m.engaged = 0;

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

        if (! contains(data.timers, 'AP_blinking')) {
            data.timers.AP_blinking = maketimer(0, m,  m._blinking);
            data.timers.AP_blinking.singleShot = 1;
        }

        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 == 0 or me._locked)
            return;
        var _system = me.systems[me.system];
        var _side = _system[side];
        _side[row]();
    },
    _locked: 0, # no status modification while blinking, is it really useful ?
    _blinking: func {
        autopilot._locked += 1;
        var _display = flightdeck['PFD'].display;
        var i = math.mod(autopilot._locked, 2);
        var delays = [ 0.7, 0.3 ];

        _display.screenElements['AP-text'].setVisible(i);
        
        if (autopilot._locked < 11)
            data.timers.AP_blinking.restart(delays[i]);
        else {
            autopilot._locked = 0;
            data.timers.AP_blinking.stop();
            _display.screenElements['AP-text']
                .setVisible(1)
                .setText('');
        }
    },
    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,
            updateDisplay: func {
                if (autopilot._locked)
                    return;
                var ap_annun = '';
                if (flightdeck['PFD'].display.screenElements['AP-text'].getText() != ''
                and stec55x.pitch.getValue() == -1 and stec55x.roll.getValue()  == -1) {
                    autopilot._blinking();
                    return;
                }
                elsif (stec55x.roll.getValue() != -1 or stec55x.pitch.getValue() != -1)
                    ap_annun = 'AP';

                foreach (var mode; ['HDG', 'NAV', 'APR'])
                    if (stec55x[mode ~ '_annun'].getValue())
                        ap_annun ~= ' ' ~ mode;

                if (stec55x.ALT_annun.getValue())
                    ap_annun ~= sprintf('ALT %5d ft', math.round(me.data.alt), 10); # even if pressure is hold, we show alt in feet

                if (stec55x.VS_annun.getValue())
                    ap_annun ~= sprintf('VS %s %4d fpm',
                                        utf8.chstr(stec55x.vs.getValue() > 0 ? 9650 : 9660),
                                        math.abs(math.round(stec55x.vs.getValue(), 10)));

            },
            L: [
                func {
                    var ap_master_sw = '/it-stec55x/input/ap-master-sw';
                    setprop(ap_master_sw, !getprop(ap_master_sw));
                },
                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 {
                    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 {
                    call(stec55x.button.HDG, [], nil, stec55x);
                },
                func {
                    call(stec55x.button.APR, [], nil, stec55x);
                },
                func,
                func { # UP (trim)
                    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)
                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value: 1});
                    fgcommand('property-assign', {property: '/it-stec55x/input/man-trim', value: 0});
                },
            ],
        }
    }
};