zkv1000 / Nasal / display.nas /
696979c 7 years ago
1 contributor
989 lines | 36.281kb
# vim: set foldmethod=marker foldmarker={{{,}}} :
var displayClass = {
    new: func(device, role) {
# the contructor {{{
        var m = { parents: [ displayClass ] };

        m.display = canvas.new({
                "name"      : role,
                "size"      : [1024, 768],
                "view"      : [1024, 768],
                "mipmapping": 1
        });
        m.display.addPlacement({
                "node": "Screen",
                "parent": role
        });
        m.display.setColorBackground(0,0,0);
        m.role = role;
        m.device = device;
        m.screenElements = {};

        return m;
    },
#}}}

# timers stuff {{{
    timers : {},

    timerTrigger : func {
        var now = systime();
        foreach (var id; keys(me.timers)) {
            if (me.timers[id] < now) {
                me.screenElements[id].hide();
                delete(me.timers, id);
            }
        }
        settimer(func me.timerTrigger(), 1);
    },

    addTimer : func (duration, element) {
        if (typeof(element) == 'scalar')
            element = [ element ];
        var end = systime() + duration;
        foreach (var e; element)
            me.timers[e] = end;
    },
#}}}

    loadsvg : func () {
        me.screen = me.display.createGroup();
        me.screen.hide();
        canvas.parsesvg(me.screen, "Aircraft/Instruments-3d/zkv1000/Systems/screen.svg");
    },

    _showInitProgress : func (p,t) {
#{{{
        p.setText(t);
        if (zkv.getNode(me.role ~ 'init').getValue() != 0) {
            if (size(t) >= 10) t = '';
            settimer(func { me._showInitProgress(p, t ~ '.'); }, 0.1);
        }
        else {
            me.progress.hide();
            me.screen.show();
            var groups = {
                show : [
                    'SoftKeysTexts', 
                    'COMM', 
                    'NAV', 
                    'nav-freq-switch',
                    'comm-freq-switch',
                ],
                text: [
                    'nav1-standby-freq', 'nav1-selected-freq', 'nav1-id',
                    'nav2-standby-freq', 'nav2-selected-freq', 'nav2-id',
                    'comm1-standby-freq', 'comm1-selected-freq',
                    'comm2-standby-freq', 'comm2-selected-freq',
                ],
                hide : [ ],
                clip: [ ],
            };

            for (var k = 0; k < 12; k += 1)
                append(groups.text, sprintf("SoftKey%02i-text", k));

            if (me.role == 'PFD') {
                append(groups.show,
                    'XPDR-TIME', 
                    'FlightInstruments',
                    'Horizon',
                    'bankPointer',
                    'VSI',
                    'Rose',
                    'Heading-bug',
                    'PFD-Widgets',
                    'Trends',
                    'Airspeed-Trend-Indicator',
                    'Altitude-Trend-Indicator',
                    'OAT',
                    'IAS-bg',
                    'TAS',
                    'GSPD',
                );
                append(groups.hide,
                    'CDI',
                    'NAV1-pointer',
                    'NAV2-pointer',
                    'GPS-pointer',
                    'BRG1-pointer',
                    'BRG2-pointer',
                    'SelectedHDG-bg',
                    'SelectedHDG-bgtext',
                    'SelectedHDG-text',
                    'SelectedCRS-bg',
                    'SelectedCRS-bgtext',
                    'SelectedCRS-text',
                    'SelectedAlt-bug',
                    'SelectedAlt-top',
                    'TAS',
                    'GSPD',
                    'BARO',
                    'WindData',
                    'Reversionnary',
                    'Annunciation',
                    'Comparator',
                    'BRG1',
                    'BRG2',
                    'DME1',
                    'PFD-Map',
                    'PFD-Multilines',
                    'WindData', 'WindData-OPTN1', 'WindData-OPTN2', 'WindData-OPTN1-HDG', 'WindData-OPTN2-symbol', 'WindData-OPTN2-headwind', 'WindData-OPTN2-crosswind', 'WindData-NODATA',
                );
                append(groups.clip,
                    'SpeedLint1',
                    'SpeedTape',
                    'LintAlt',
                    'AltLint00011'
                );
                append(groups.text,
                    'SelectedAlt-text',
                    'TAS-text',
                    'GSPD-text',
                    'TIME-text',
                    'OAT-text',
                    'VSIText',
                    'Speed110',
                    'Alt11100',
                    'HDG-text',
                    'BRG1-pointer', 'BRG1-SRC-text', 'BRG1-DST-text', 'BRG1-WPID-text',
                    'BRG2-pointer', 'BRG2-SRC-text', 'BRG2-DST-text', 'BRG2-WPID-text',
                    'WindData-OPTN1-HDG-text', 'WindData-OPTN1-SPD-text',
                    'WindData-OPTN2-crosswind-text', 'WindData-OPTN2-headwind-text',
                    'AltBigC', 'AltSmallC'
                );
                for (var place = 1; place <= 6; place +=1) {
                    append(groups.text,
                        'AltBigU' ~ place,
                        'AltSmallU' ~ place,
                        'AltBigD' ~ place,
                        'AltSmallD' ~ place
                    );
                }
            }
            else
                append(groups.show, 'Header');

            me.loadGroup(groups);

            if (me.role == 'PFD') {
                me.update();
                me.updateAI();
                me.updateVSI();
                me.updateIAS();
                me.updateALT();
                me.updateHSI();
                me.updateTIME();
                me.updateOAT();
                me.updateTAS();
                me.updateBRG();
                me.timerTrigger();
            }
            me._updateRadio({auto:'nav'});
            me._updateRadio({auto:'comm'});
            me.updateSoftKeys();
            me.progress.removeAllChildren();
            me.progress = nil;
            me.showInitProgress = nil;
            me._showInitProgress = nil;
            zkv.removeChild(me.role ~ 'init');
        }
    },
#}}}

    showInitProgress : func (role) {
#{{{
        me.progress = me.display.createGroup();
        me.progress.show();
        me.progress.createChild("text", role ~ " title")
            .setTranslation(512, 384)
            .setAlignment("center-center")
            .setFont("LiberationFonts/LiberationSans-Italic.ttf")
            .setFontSize(64, 1)
            .setColor(1,1,1)
            .setText("ZKV1000 " ~ role ~ " init");

        zkv.getNode(role ~ 'init',1).setIntValue(1);

        me._showInitProgress(me.progress.createChild("text", role ~ "progress")
            .setTranslation(512, 484)
            .setAlignment("center-center")
            .setFont("LiberationFonts/LiberationSans-Bold.ttf")
            .setFontSize(128, 1)
            .setColor(1,0,0), '.');
    },
#}}}

    loadGroup : func (h) {
#{{{
        if (typeof(h) != 'hash') {
            msg_dbg(sprintf("%s need a hash, but get a %s from %s",
                    caller(0)[0],
                    typeof(h),
                    caller(1)[0]));
            return;
        }
        var setMethod = func (e, t) {
            if (t == 'hide')
                me.screenElements[e].hide();
            elsif (t == 'show')
                me.screenElements[e].show();
            elsif (t == 'rot' or t == 'trans') {
                if (! contains(me.screenElements[e], t))
                    me.screenElements[e][t] = me.screenElements[e].createTransform();
            }
            elsif (t == 'clip') {
                if (contains(me.clips, e))
                    me.screenElements[e].set("clip", me.clips[e]);
                else
                    print('no defined clip for ' ~ e);
            }
            elsif (t == 'text') {
                if (contains(me.texts, e)) {
                    if (contains(me.texts[e], 'alignment'))
                        me.screenElements[e].setAlignment(me.texts[e].alignment);
                    if (contains(me.texts[e], 'default'))
                        me.screenElements[e].setText(me.texts[e].default);
                    if (contains(me.texts[e], 'color'))
                        me.screenElements[e].setColor(me.texts[e].color);
                }
#                else
#                    print('no text format for ' ~ e);
            }
            else
                print('unknown method ' ~ t);
        };
        foreach (var todo; keys(h)) {
            if (typeof(h[todo]) != 'vector') h[todo] = [ h[todo] ];
            foreach (var id; h[todo]) {
                if (! contains(me.screenElements, id)) {
                    me.screenElements[id] = me.screen.getElementById(id);
                    if (me.screenElements[id] != nil)
                        setMethod(id, todo);
                    else
                        print('SVG ID ' ~ id ~ ' not found');
                }
                else
                    setMethod(id, todo);
            }
        }
    },
#}}}

    clips : {
#{{{
        PitchScale   : "rect(70,664,370,256)",
        SpeedLint1   : "rect(252,226,318,204)",
        SpeedTape    : "rect(115,239,455,156)",
        LintAlt      : "rect(115,808,455,704)",
        AltLint00011 : "rect(252,804,318,771)",
    },
#}}}

    texts : {
#{{{
        VSIText : {
            alignment: "right-bottom", default : num('0'),
        },
        Speed110 : {
            alignment : 'left-bottom'
        },
        Alt11100 : {
            alignment:'left-bottom'
        },
        "HDG-text" : {
            default: '---°'
        },
        'nav1-standby-freq' : {
            color: [0, 1, 1],
        },
        'nav1-id' : {
            default: ''
        },
        'nav2-id' : {
            default: ''
        },
        'TAS-text' : {
            alignment : 'right-bottom',
        },
        'GSPD-text' : {
            alignment : 'right-bottom',
        },
    },
#}}}

    updateSoftKeys : func {
# update SoftKeys boxes {{{
        # grey background code = #353939
        # on PFD the last boxes are always BACK and ALERTS
        if (me.role == 'PFD') {
            me.screenElements[sprintf("SoftKey%02i-text", 11)]
                .setText('ALERTS');
            if (size(me.device.softkeys.path) != 0)
                me.screenElements[sprintf("SoftKey%02i-text", 10)]
                    .setText('BACK');
        }

        var path = keyMap[me.role];
        foreach (var p; me.device.softkeys.path)
            path = path[p];

        # feeding with empty menus the first boxes
        var start = (contains(path, 'first')) ? path.first : 0;
        for (var k = 0; k < start; k+=1) {
            me.screenElements[sprintf("SoftKey%02i-text", k)]
                .setText('');
        }
        # filling with the content the next boxes
        forindex (var k; path.texts) {
            var i = k + start;
            me.screenElements[sprintf("SoftKey%02i-text", i)]
                .setText(path.texts[k]);
        }
        # feeding the last boxes with empty string
        var end = (me.role == 'PFD') ? 10 : 12;
        if (size(path.texts) + start < end) {
            start = size(path.texts) + start;
            for (var k = start; k < end; k += 1)
                me.screenElements[sprintf("SoftKey%02i-text", k)]
                    .setText('');
        }
    },
#}}}

    update : func {
# updates roll, pitch, VSI, IAS, altitude and HDG {{{
        me._updated_roll = getprop('/orientation/roll-deg');
        me._updated_pitch = getprop('orientation/pitch-deg');
        me._updated_vsi = getprop('/instrumentation/vertical-speed-indicator/indicated-speed-fpm');
        me._updated_ias = getprop('/velocities/airspeed-kt');
        me._updated_alt = getprop('/instrumentation/altimeter/indicated-altitude-ft');
        me._updated_hdg = getprop('/orientation/heading-deg');
        settimer(func me.update(), 0.05);
     },
    _updated_roll : 0,
    _updated_pitch : 0,
    _updated_vsi : 0,
    _updated_ias : 0,
    _updated_alt : 0,
    _updated_hdg : 0,
#}}}

    updateAI: func(){
#{{{
        var pitch = me._updated_pitch;
        var roll = me._updated_roll;
        if (pitch > 80)
            pitch = 80;
        elsif (pitch < -80)
            pitch = -80;
        me.screenElements.Horizon
            .setRotation(-roll * D2R)
            .setTranslation(0, pitch * 6.8571428);
        me.screenElements.bankPointer
            .setRotation(-roll * D2R);
        settimer(func me.updateAI(), 0.1);
    },
#}}}

    updateVSI: func () {
# animate VSI {{{
        var vsi = me._updated_vsi;
        me.screenElements.VSIText
            .setText(num(math.round(vsi, 10)));
        if (vsi > 4500)
            vsi = 4500;
        elsif (vsi < -4500)
            vsi = -4500;
        me.screenElements.VSI
            .setTranslation(0, vsi * -0.03465);
        settimer(func me.updateVSI(), 0.1);
    },
#}}}

    updateIAS: func () {
# animates the IAS lint (PFD) {{{
        var ias = me._updated_ias;
        if (ias >= 10)
            me.screenElements.Speed110
                .setText(sprintf("% 2u",num(math.floor(ias/10))));
        else
            me.screenElements.Speed110
                .setText('');
        me.screenElements.SpeedLint1
            .setTranslation(0,(math.mod(ias,10) + (ias >= 10)*10) * 36);
        me.screenElements.SpeedTape
            .setTranslation(0,ias * 5.711);
        if (ias > me._ias_vne and ! me._ias_already_exceeded) { # easier than .getColorFill
            me._ias_already_exceeded = 1;
            me.screenElements['IAS-bg']
                .setColorFill(1,0,0);
        }
        elsif (ias < me._ias_vne and me._ias_already_exceeded) { # easier than .getColorFill
            me._ias_already_exceeded = 0;
            me.screenElements['IAS-bg']
                .setColorFill(0,0,0);
        }
        var now = systime();
        # estimated speed in 6s
        var Sy = 6 * (ias - me._last_ias_kt) / (now - me._last_ias_s);
        if (abs(Sy) > 30)
            Sy = 30 * abs(Sy)/Sy; # = -30 or 30
        me.screenElements['Airspeed-Trend-Indicator']
            .setScale(1,Sy)
            .setTranslation(0, -284.5 * (Sy - 1));
        me._last_ias_kt = ias;
        me._last_ias_s = now;
        settimer(func me.updateIAS(), 0.1);
    },
    _last_ias_kt : 0,
    _last_ias_s : systime(),
    _ias_vne : 999,
    _ias_already_exceeded : 0,
#}}}

    updateTAS: func {
# updates the True Airspeed and GroundSpeed indicators {{{
        me.screenElements['TAS-text']
            .setText(sprintf('%iKT', getprop('/instrumentation/airspeed-indicator/true-speed-kt')));
        me.screenElements['GSPD-text']
            .setText(sprintf('%iKT', getprop('/velocities/groundspeed-kt')));
        settimer(func me.updateTAS(), 0.5);
    },
#}}}

    updateALT: func () {
# animates the altitude lint (PFD) {{{
        var alt = me._updated_alt;
        if (alt < 0)
            me.screenElements.Alt11100
                .setText(sprintf("% 3i",math.ceil(alt/100)));
        elsif (alt < 100)
            me.screenElements.Alt11100
                .setText('');
        else
            me.screenElements.Alt11100
                .setText(sprintf("% 3i",math.floor(alt/100)));
        me.screenElements.AltLint00011
            .setTranslation(0,math.fmod(alt,100) * 1.24);

        # From Farmin/G1000 http://wiki.flightgear.org/Project_Farmin/FG1000
        if (alt> -1000 and alt< 1000000) {
            var Offset10 = 0;
            var Offset100 = 0;
            var Offset1000 = 0;
            if (alt< 0) {
                var Ne = 1;
                var alt= -alt;
            }
            else
                var Ne = 0;

            var Alt10       = math.mod(alt,100);
            var Alt100      = int(math.mod(alt/100,10));
            var Alt1000     = int(math.mod(alt/1000,10));
            var Alt10000    = int(math.mod(alt/10000,10));
            var Alt20       = math.mod(Alt10,20)/20;
            if (Alt10 >= 80)
                var Alt100 += Alt20;

            if (Alt10 >= 80 and Alt100 >= 9)
                var Alt1000 += Alt20;

            if (Alt10 >= 80 and Alt100 >= 9 and Alt1000 >= 9)
                var Alt10000 += Alt20;

            if (alt> 100)
                var Offset10 = 100;

            if (alt> 1000)
                var Offset100 = 10;

            if (alt> 10000)
                var Offset1000 = 10;

            if (!Ne) {
                me.screenElements.LintAlt.setTranslation(0,(math.mod(alt,100))*0.57375);
                var altCentral = (int(alt/100)*100);
            }
            elsif (Ne) {
                me.screenElements.LintAlt.setTranslation(0,(math.mod(alt,100))*-0.57375);
                var altCentral = -(int(alt/100)*100);
            }
            me.screenElements["AltBigC"].setText("");
            me.screenElements["AltSmallC"].setText("");
            for (var place = 1; place <= 6; place += 1) {
                var altUP = altCentral + (place*100);
                var offset = -30.078;
                if (altUP < 0) {
                    var altUP = -altUP;
                    var prefix = "-";
                    var offset += 15.039;
                }
                else
                    var prefix = "";

                if (altUP == 0) {
                    var AltBigUP    = "";
                    var AltSmallUP  = "0";

                }
                elsif (math.mod(altUP,500) == 0 and altUP != 0) {
                    var AltBigUP    = sprintf(prefix~"%1d", altUP);
                    var AltSmallUP  = "";
                }
                elsif (altUP < 1000 and (math.mod(altUP,500))) {
                    var AltBigUP    = "";
                    var AltSmallUP  = sprintf(prefix~"%1d", int(math.mod(altUP,1000)));
                    var offset = -30.078;
                }
                elsif ((altUP < 10000) and (altUP >= 1000) and (math.mod(altUP,500))) {
                    var AltBigUP    = sprintf(prefix~"%1d", int(altUP/1000));
                    var AltSmallUP  = sprintf("%1d", int(math.mod(altUP,1000)));
                    var offset += 15.039;
                }
                else {
                    var AltBigUP    = sprintf(prefix~"%1d", int(altUP/1000));
                    var mod = int(math.mod(altUP,1000));
                    var AltSmallUP  = sprintf("%1d", mod);
                    var offset += 30.078;
                }

                me.screenElements["AltBigU"~place].setText(AltBigUP);
                me.screenElements["AltSmallU"~place].setText(AltSmallUP);
                me.screenElements["AltSmallU"~place].setTranslation(offset,0);
                var altDOWN = altCentral - (place*100);
                var offset = -30.078;
                if (altDOWN < 0) {
                    var altDOWN = -altDOWN;
                    var prefix = "-";
                    var offset += 15.039;
                }
                else
                    var prefix = "";

                if (altDOWN == 0) {
                    var AltBigDOWN  = "";
                    var AltSmallDOWN    = "0";
                }
                elsif (math.mod(altDOWN,500) == 0 and altDOWN != 0) {
                    var AltBigDOWN  = sprintf(prefix~"%1d", altDOWN);
                    var AltSmallDOWN    = "";
                }
                elsif (altDOWN < 1000 and (math.mod(altDOWN,500))) {
                    var AltBigDOWN  = "";
                    var AltSmallDOWN    = sprintf(prefix~"%1d", int(math.mod(altDOWN,1000)));
                    var offset = -30.078;
                }
                elsif ((altDOWN < 10000) and (altDOWN >= 1000) and (math.mod(altDOWN,500))) {
                    var AltBigDOWN  = sprintf(prefix~"%1d", int(altDOWN/1000));
                    var AltSmallDOWN    = sprintf("%1d", int(math.mod(altDOWN,1000)));
                    var offset += 15.039;
                }
                else {
                    var AltBigDOWN  = sprintf(prefix~"%1d", int(altDOWN/1000));
                    var mod = int(math.mod(altDOWN,1000));
                    var AltSmallDOWN    = sprintf("%1d", mod);
                    var offset += 30.078;
                }
                me.screenElements["AltBigD"~place].setText(AltBigDOWN);
                me.screenElements["AltSmallD"~place].setText(AltSmallDOWN);
                me.screenElements["AltSmallD"~place].setTranslation(offset,0);
            }
        }
        me.updateSelectedALT();
        var now = systime();
        # altitude in 6s
        var Sy = .3 * (alt - me._last_alt_ft) / (now - me._last_alt_s); # scale = 1/20ft
        if (abs(Sy) > 15)
            Sy = 15 * abs(Sy)/Sy; # = -15 or 15
        me.screenElements['Altitude-Trend-Indicator']
            .setScale(1,Sy)
            .setTranslation(0, -284.5 * (Sy - 1));
        me._last_alt_ft = alt;
        me._last_alt_s = now;
        settimer(func me.updateALT(), 0.2);
    },
    _last_alt_ft : 0,
    _last_alt_s  : systime(),
#}}}

    updateHSI : func () {
# rotates the compass (PFD) {{{
        var hdg = me._updated_hdg;
        me.screenElements.Rose
            .setRotation(-hdg * D2R);
        me.screenElements['HDG-text']
            .setText(sprintf("%03u°", hdg));
        settimer(func me.updateHSI(), 0.1);
    },
#}}}

    updateHDG : func (hdg) {
# moves the heading bug and display heading-deg for 3 seconds (PFD) {{{
        if (me.role == 'MFD')
            return;
        me.screenElements['Heading-bug']
            .setRotation(hdg * D2R);
        me.screenElements['SelectedHDG-bg']
            .show();
        me.screenElements['SelectedHDG-bgtext']
            .show();
        me.screenElements['SelectedHDG-text']
            .setText(sprintf('%03d°%s', hdg, ''))
            .show();
        me.addTimer(3, ['SelectedHDG-text', 'SelectedHDG-bgtext', 'SelectedHDG-bg']);
    },
#}}}

    updateCRS : func (crs) {
# TODO: update display for NAV/GPS/BRG courses {{{
        if (me.role == 'MFD')
            return;
        me.screenElements['SelectedCRS-bg']
            .show();
        me.screenElements['SelectedCRS-bgtext']
            .show();
        me.screenElements['SelectedCRS-text']
            .setText(sprintf('%03d°%s', crs, ''))
            .show();
        me.addTimer(3, ['SelectedCRS-text', 'SelectedCRS-bgtext', 'SelectedCRS-bg']);
    },
#}}}

    updateSelectedALT : func {
# animation for altitude section, called via updatedALT {{{
        var selected_alt = getprop('/instrumentation/zkv1000/afcs/selected-alt-ft');
        var delta_alt = me._updated_alt - selected_alt;
        if (abs(delta_alt) > 300)
            delta_alt = 300 * abs(delta_alt)/delta_alt;
        me.screenElements['SelectedAlt-top']
            .setVisible(abs(delta_alt) > 100);
        me.screenElements['SelectedAlt-bug']
            .setTranslation(0, delta_alt * 0.567); # 170/300 = 0.567
        me.screenElements['SelectedAlt-text']
            .setText(sprintf("%i", selected_alt));
    },
#}}}

    _updateRadio: func {
# common parts for NAV/LOC/COMM radios{{{
        # arg[0]._r = <comm|nav>
        if (contains(arg[0], "active")) {
            if (arg[0]['active'] == 'none') {
                me.screenElements[arg[0]._r ~ '1-selected-freq']
                    .setColor(1,1,1);
                me.screenElements[arg[0]._r ~ '2-selected-freq']
                    .setColor(1,1,1);
            }
            else {
                me.screenElements[arg[0]._r ~ arg[0]['active'] ~ '-selected-freq']
                    .setColor(0,1,0);
                me.screenElements[arg[0]._r ~ arg[0].inactive ~ '-selected-freq']
                    .setColor(1,1,1);
            }
        }
        if (contains(arg[0], 'tune')) {
            # n = 0 -> NAV1/COMM1
            # n = 1 -> NAV1/COMM2
            me.screenElements[arg[0]._r ~ '-freq-switch']
                .setTranslation(0, arg[0].tune * 25);
            me.screenElements[arg[0]._r ~ (arg[0].tune + 1) ~ '-standby-freq']
                .setColor(0,1,1);
            me.screenElements[arg[0]._r ~ ((arg[0].tune == 0) + 1) ~ '-standby-freq']
                .setColor(1,1,1);
        }
        if (contains(arg[0], 'refresh')) {
            # rafraichi une seule ligne NAV1/COMM1 ou NAV2/COMM2
            var fmt = (arg[0]._r == 'nav') ? '%.2f' : '%.3f';
            me.screenElements[arg[0]._r ~ arg[0].refresh ~ '-selected-freq']
                .setText(sprintf(fmt, getprop('/instrumentation/'
                               ~ arg[0]._r ~ '[' ~ (arg[0].refresh - 1) ~ ']/frequencies/selected-mhz')));
            me.screenElements[arg[0]._r ~ arg[0].refresh ~ '-standby-freq']
                .setText(sprintf(fmt, getprop('/instrumentation/'
                               ~ arg[0]._r ~ '[' ~ (arg[0].refresh - 1) ~ ']/frequencies/standby-mhz')));
        }
        if (contains(arg[0], 'set')) {
            # positionne la valeur modifiée, les listeners "trigguent" en permanence ces propriétés, donc exit
            var n = getprop('/instrumentation/zkv1000/radios/' ~ arg[0]._r ~ '-tune');
            var fmt = (arg[0]._r == 'nav') ? '%.2f' : '%.3f';
            me.screenElements[arg[0]._r ~ (n + 1) ~ '-standby-freq']
                .setText(sprintf(fmt, getprop('/instrumentation/' ~ arg[0]._r ~ '[' ~ n ~ ']/frequencies/standby-mhz')));
        }
        if (contains(arg[0], 'auto')) {
            # pour rafraichir automagiquement, toutes les deux secondes un refresh pour un NAV
            var radio = arg[0].auto;
            me._updateRadio({refresh: 1, _r: radio});
            settimer(func me._updateRadio({refresh: 2, _r: radio}), 1);
            settimer(func me._updateRadio({auto: radio}), 2);
        }
    },
#}}}

    updateNAV : func {
# update NAV/LOC rodios display upper left (PFD/MFD){{{
        # made active via menu
        if (contains(arg[0], "active")) {
            if (arg[0]['active'] == 'none') {
                arg[0]._r = 'nav';
                me._updateRadio(arg[0]);
                me.screenElements['nav1-id']
                    .setColor(1,1,1);
                me.screenElements['nav2-id']
                    .setColor(1,1,1);
                me.screenElements['NAV1-pointer']
                    .hide();
                me.screenElements['NAV2-pointer']
                    .hide();
            }
            else {
                arg[0]._r = 'nav';
                arg[0].inactive = (arg[0]['active'] == 1) + 1;
                me._updateRadio(arg[0]);
                me.screenElements['nav' ~ arg[0]['active'] ~ '-id']
                    .setColor(0,1,0);
                me.screenElements['NAV' ~ arg[0]['active'] ~ '-pointer']
                    .show();
                me.screenElements['nav' ~ arg[0].inactive ~ '-id']
                    .setColor(1,1,1);
#                me.screenElements['HDI']
#                    .setRotation();
#                me.screenElements['NAV' ~ inactive ~ '-pointer']
#                    .hide();
#                foreach (var e; [ 'FROM', 'TO', 'CDI' ])
#                    me.screenElements['NAV' ~ inactive ~ '-' ~ e]
#                        .hide();
            }
        }
        elsif (contains(arg[0], 'nav-id')) {
            # TODO: récupérer la valeur via les paramètres transmis du listener
            if (arg[0].val == nil)
                arg[0].val = '';
            me.screenElements["nav" ~ arg[0]['nav-id'] ~ "-id"]
                    .setText(arg[0].val);
        }
        else {
            arg[0]._r = 'nav';
            me._updateRadio(arg[0]);
        }
    },
#}}}

    updateCOMM: func {
# update COMM radios display upper right (PFD/MFD){{{
        arg[0]._r = 'comm';
        me._updateRadio(arg[0]);
    },
#}}}

    updateTIME : func {
# updates the displayed time botoom left {{{
        me.screenElements['TIME-text']
            .setText(getprop('/sim/time/gmt-string'));
        settimer(func me.updateTIME(), 1);
    },
#}}}

    updateOAT : func {
# update OAT display on normal and reversionnary modes (every 3s) {{{
        var tmp = getprop('/environment/temperature-deg' ~ me._oat_unit);
        me.screenElements['OAT-text']
            .setText(sprintf((abs(tmp) < 10) ? "%.1f %s" : "%i %s", tmp, (me._oat_unit == 'c') ? '°C' : 'F'));
        settimer(func me.updateOAT(), 3);
    },
    _oat_unit : 'c',
#}}}

    updateWindData : func {
# update the window text and arrows for OPTN1/2 {{{
        if (me._winddata_optn == 0)
            return;
        if (me._updated_ias < 30) {
            me.screenElements['WindData-NODATA']
                .hide();
            var wind_hdg = getprop('/environment/wind-from-heading-deg');
            var wind_spd = getprop('/environment/wind-speed-kt');
            var alpha = wind_hdg - me._updated_hdg;
            if (me._winddata_optn == 1) {
                me.screenElements['WindData-OPTN1-HDG']
                    .setRotation((alpha + 180) * D2R)
                    .show();
                me.screenElements['WindData-OPTN1-HDG-text']
                    .setText(sprintf("%03i°", wind_hdg))
                    .show();
                me.screenElements['WindData-OPTN1-SPD-text']
                    .setText(int(wind_spd) ~ 'KT')
                    .show();
            }
            else { # me._winddata_optn == 2
                alpha *= D2R;
                var Vt = wind_spd * math.sin(alpha);
                var Ve = wind_spd * math.cos(alpha);
                if (Vt != 0) {
                    me.screenElements['WindData-OPTN2-crosswind-text']
                        .setText(sprintf('%i', abs(Vt)))
                        .show();
                    me.screenElements['WindData-OPTN2-crosswind']
                        .setScale(-abs(Vt)/Vt, 1)
                        .setTranslation(-35 * (abs(Vt)/Vt + 1), 0)
                        .show();
                }
                if (Ve != 0) {
                    me.screenElements['WindData-OPTN2-headwind-text']
                        .setText(sprintf('%i', abs(Ve)))
                        .show();
                    me.screenElements['WindData-OPTN2-headwind']
                        .setScale(1, abs(Ve)/Ve)
                        .setTranslation(0, 515 * (1 - abs(Ve)/Ve))
                        .show();
                }
            }
        }
        else {
            foreach (var e; [
                    'WindData-OPTN1-HDG',
                    'WindData-OPTN1-HDG-text',
                    'WindData-OPTN1-SPD-text',
                    'WindData-OPTN2-crosswind-text',
                    'WindData-OPTN2-crosswind',
                    'WindData-OPTN2-headwind-text',
                    'WindData-OPTN2-headwind'
            ])
                me.screenElements[e].hide();
            me.screenElements['WindData-NODATA'].show();
        }
        settimer(func me.updateWindData(), 0.5);
    },
    _winddata_optn : 0,
#}}}

    updateBRG : func {
# displays and update
        foreach (var brg; [1, 2]) {
            var source = 'brg' ~ brg ~ '-source';

            var dev = radios.getNode(source).getValue();
            var el  = 'BRG' ~ brg;
            if (dev != 'OFF') {
                var info = {
                    pointer : nil,
                    id : 'NO DATA',
                    hdg : nil,
                    dst : '--.-NM'
                };
                if (left(dev, 3) == 'NAV') {
                    info.pointer = getprop('/instrumentation/nav[' ~ (brg - 1) ~ ']/in-range');
                    if (info.pointer) {
                        info.id  = getprop('/instrumentation/nav[' ~ (brg - 1) ~ ']/nav-id');
                        info.hdg = getprop('/instrumentation/nav[' ~ (brg - 1) ~ ']/heading-deg');
                        info.dst = sprintf('%.1d', getprop('/instrumentation/nav[' ~ (brg - 1) ~ ']/nav-distance') / 1852); # m -> /1852
                    }
                }
                elsif (dev == 'GPS') {
                    info.pointer = props.getNode('/instrumentation/gps/wp').getChild('wp[1])');
                    if (info.pointer) {
                        info.id  = getprop('/instrumentation/gps/wp/wp[1]/ID');
                        info.hdg = getprop('/instrumentation/gps/wp/wp[1]/bearing-mag-deg');
                        info.dst = sprintf('%.1d', getprop('/instrumentation/gps/wp/wp[1]/distance-nm'));
                    }
                }
                else { # there are 2 available ADF in FG, but instrument manage only 1
                    info.pointer = getprop('/instrumentation/adf/in-range');
                    if (info.pointer) {
                        info.id  = getprop('/instrumentation/adf/ident');
                        info.hdg = getprop('/instrumentation/adf/indicated-bearing-deg');
                    }
                }

                if (info.pointer)
                    me.screenElements[el ~ '-pointer']
                        .setRotation(-info.hdg-me._updated_hdg * D2R)
                        .show();
                else
                    me.screenElements[el ~ '-pointer']
                        .hide();
                me.screenElements[el ~ '-SRC-text']
                    .setText(dev);
                me.screenElements[el ~ '-DST-text']
                    .setText(info.dst);
                me.screenElements[el ~ '-WPID-text']
                    .setText(info.id);
                me.screenElements['BRG' ~ brg]
                    .show();
            }
            else {
                me.screenElements['BRG' ~ brg]
                    .hide();
            }
        }
        settimer(func me.updateBRG(), 1);
    },
};

var keyMap = {
# softkeys map for PFD and MFD {{{
    PFD : {
        first : 1,
        texts : ['INSET', 'SENSOR', 'PFD', 'OBS', 'CDI', 'DME', 'XPDR', 'IDENT', 'TMR/REF', 'NRST' ],
        INSET : {
            texts : ['OFF', 'DCLTR', 'WXLGND', 'TRAFFIC', 'TOPO', 'TERRAIN', 'STRMSCP', 'NEXRAD-C', 'XM LTNG', 'METAR'],
        },
        SENSOR : {
            first : 2,
            texts : [ 'ADC1', 'ADC2', '', 'AHRS1', 'AHRS2'],
        },
        PFD : {
            texts : [ 'SYN VIS', 'DFLTS', 'AOA/WIND', 'DME', 'BRG1', 'HSI FMT', 'BRG2', '', 'ALT UNIT', 'STD BARO' ],
            'SYN VIS' : {
                texts : [ 'PATHWAY', 'SYN TERR', 'HR2NHDG', 'APTSIGNS', 'FPM'],
            },
            'AOA/WIND' : {
                first : 4,
                texts : ['AOA', 'WIND'],
                AOA : {
                    first : 5,
                    texts : ['AOA ON', 'AOA AUTO'],
                },
                WIND : {
                    first : 2,
                    texts : ['OPTN1', 'OPTN2', '', 'OFF'],
                },
            },
            'HSI FMT' : {
                first : 6,
                texts : ['360 HSI', 'ARC HSI'],
            },
            'ALT UNIT' : {
                first : 5,
                texts : ['METERS', '', 'IN', 'HPA'],
            },
        },
        XPDR : {
            first : 2,
            texts : ['STBY', 'ON', 'ALT', '', 'VFR', 'CODE', 'IDENT'],
            CODE : {
                texts : ['0', '1', '2', '3', '4', '5', '6', '7', 'IDENT', 'BKSP'],
            },
        },
    },
    MFD : {
        texts : ['ENGINE', '', 'MAP', '', '', '', '', '', '', 'DCLTR', 'SHW CHRT', 'CHKLIST'],
        MAP : {
            texts: ['TRAFFIC', 'PROFILE', 'TOPO', 'TERRAIN', 'AIRWAYS', 'STRMSCP','NEXRAD-C', 'XM LTNG', 'METAR', 'LEGEND', 'BACK'],
        },
        CHKLIST : {
            texts : ['ENGINE', '', '', '', '', 'DONE', '', '', '', '', 'EXIT', 'EMERGCY'],
        },
        ENGINE : {
            texts : ['ENGINE', 'ANTI-ICE', '', 'DCLTR', '', 'ASSIST', '', '', '', '', 'FUEL'],
            'ANTI-ICE' : {
                texts : ['LEFT', 'AUTO', 'RIGHT', '', '', '', '', '', '', '', '', 'BACK'],
            },
            FUEL : {
                first : 1,
                texts : ['FULL', 'TABS', '', '', '', '', '', '', '', 'UNDO', 'ENTER'],
            },
        },
    },
};
#}}}