#!/usr/bin/perl
#!/usr/bin/perl -d:ptkdb

###############################################################################
#
# libmktime.pl -  MK Mission Cockpit - Timer for GUI Frontend
#
# Copyright (C) 2009  Rainer Walther  (rainerwalther-mail@web.de)
#
# Creative Commons Lizenz mit den Zusaetzen (by, nc, sa)
#
# Es ist Ihnen gestattet: 
#     * das Werk vervielfältigen, verbreiten und öffentlich zugänglich machen
#     * Abwandlungen bzw. Bearbeitungen des Inhaltes anfertigen
# 
# Zu den folgenden Bedingungen:
#     * Namensnennung.
#       Sie müssen den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
#     * Keine kommerzielle Nutzung.
#       Dieses Werk darf nicht für kommerzielle Zwecke verwendet werden.
#     * Weitergabe unter gleichen Bedingungen.
#       Wenn Sie den lizenzierten Inhalt bearbeiten oder in anderer Weise umgestalten,
#       verändern oder als Grundlage für einen anderen Inhalt verwenden,
#       dürfen Sie den neu entstandenen Inhalt nur unter Verwendung von Lizenzbedingungen
#       weitergeben, die mit denen dieses Lizenzvertrages identisch oder vergleichbar sind.
# 
# Im Falle einer Verbreitung müssen Sie anderen die Lizenzbedingungen, unter welche dieses
# Werk fällt, mitteilen. Am Einfachsten ist es, einen Link auf diese Seite einzubinden.
# 
# Jede der vorgenannten Bedingungen kann aufgehoben werden, sofern Sie die Einwilligung
# des Rechteinhabers dazu erhalten.
# 
# Diese Lizenz lässt die Urheberpersönlichkeitsrechte unberührt.
# 
# Weitere Details zur Lizenzbestimmung gibt es hier:
#   Kurzform: http://creativecommons.org/licenses/by-nc-sa/3.0/de/
#   Komplett: http://creativecommons.org/licenses/by-nc-sa/3.0/de/legalcode
#
###############################################################################
# 2009-08-09 0.2.5 rw Timer moved from mkcockpit.pl
#                     Optional Player home-pos in map configuration
# 2009-08-23 0.2.6 rw Tracking-Antenna Icon
#                     POI heading control
# 2009-10-11 0.2.7 rw Tracker control changed
#
###############################################################################

$Version{'libmktimer.pl'}  = "0.2.7 - 2009-10-11";

use Math::Trig;

#
# Timer: 5s
#
$main->repeat (5000, sub 
    {
    if ( ! $MkSendWp )
        {
        # Abfragefrequenz OSD und Debug regelmäßig neu einstellen, falls Übertragungsfehler
        $MkSendQueue->enqueue( "o", "$AddrNC", pack ("C", 10) );   # Frequenz OSD Datensatz, * 10ms
        $MkSendQueue->enqueue( "d", "$AddrNC", pack ("C", 10) );   # Frequenz MK Debug Datensatz, * 10ms
        $MkSendQueue->enqueue( "v", "$AddrNC", "");   # Version
        $MkSendQueue->enqueue( "e", "$AddrNC", "");   # Error Text Request
        }

    lock (%MkOsd);              # until end of block

    # Draw Operation Radius Border
    $map_canvas->delete('Map-Border-OperatingRadius');
    if ( &HomePosIsValid() )
        {
        my $Radius = $MkOsd{'OperatingRadius'};
        my $H_Lat = $MkOsd{'HomePos_Lat'};
        my $H_Lon = $MkOsd{'HomePos_Lon'};
        my $Angel = &MapAngel();

        my ($T_Lat, $T_Lon) = &MapGpsAt ($H_Lat, $H_Lon, $Radius, $Angel -90); 
        my ($R_Lat, $R_Lon) = &MapGpsAt ($H_Lat, $H_Lon, $Radius, $Angel); 
        my ($B_Lat, $B_Lon) = &MapGpsAt ($H_Lat, $H_Lon, $Radius, $Angel + 90); 
        my ($L_Lat, $L_Lon) = &MapGpsAt ($H_Lat, $H_Lon, $Radius, $Angel + 180); 

        my ($T_x, $T_y) = &MapGps2XY ($T_Lat, $T_Lon);
        my ($R_x, $R_y) = &MapGps2XY ($R_Lat, $R_Lon);
        my ($B_x, $B_y) = &MapGps2XY ($B_Lat, $B_Lon);
        my ($L_x, $L_y) = &MapGps2XY ($L_Lat, $L_Lon);

        $map_canvas->createArc ( $L_x, $B_y, $R_x, $T_y,
                                 '-tags' => 'Map-Border-OperatingRadius',
                                 '-extent' => '359',
                                 '-start' => '0', 
                                 '-style' => 'chord',
                                 '-outline' => $Cfg->{'mkcockpit'}->{'ColorAirfield'},
                                 '-width' => '1',
                               );
        $map_canvas->raise('Map-Border-OperatingRadius', 'Map');  # Border above Map
        }

    });



#	
# Timer: 0.1s - Map Overlay aktualisieren
#
$frame_map_top->repeat (100, sub
    {

    # Clear old messages from this timer
    &MkMessageInit ("Timer-MapOverlay");

    lock (%MkOsd);              # until end of block

    # Aktuell gültige Karte
    %Map = %{$Maps{'Current'}};

    if ( &MkOsdIsValid() )
        {
        # Gueltige OSD Daten

        # Operation Mode
        $OperationMode = "";
        if ( &MkIsWptMode() )         { $OperationMode = "WPT"; }
        if ( $PlayerMode eq "Play" )  { $OperationMode = "Play"; }
        if ( $PlayerMode eq "Pause" ) { $OperationMode = "Paus"; }
        if ( $PlayerMode eq "Home" )  { $OperationMode = "Home"; }
        if ( &MkIsPhMode() )          { $OperationMode = "PH"; }
        if ( &MkIsFreeMode() )        { $OperationMode = "Free"; }

        my $SatsInUse = $MkOsd{'SatsInUse'};
        if ( &CurPosIsValid()  and  &HomePosIsValid() )
            {
            # ausreichender GPS Empfang

            # get x,y map coords of current position
            my ($C_x, $C_y, $C_Angel) = &MapGps2XY($MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'}, $MkOsd{'CompassHeading'});
            $MkPos_x = $C_x;
            $MkPos_y = $C_y;
		
            # rotate MK arrow
            my $dy = sin (deg2rad $C_Angel) * ($MapMkLen/2);
            my $dx = cos (deg2rad $C_Angel) * ($MapMkLen/2);
            my $x0 = $C_x - $dx;
            my $y0 = $C_y - $dy;
            my $x1 = $C_x + $dx;
            my $y1 = $C_y + $dy;
            $map_canvas->coords ('MK-Arrow', $x0, $y0, $x1, $y1);

            # Update speed vector
            my $MapAngel = &MapAngel();   # North to Map-Horizont
            my $GpsSpeedNorth = $MkNcDebug{'Analog_21'};
            my $GpsSpeedEast  = $MkNcDebug{'Analog_22'};
            my $PhiGpsSpeed = rad2deg atan2 ( $GpsSpeedEast, $GpsSpeedNorth );
            $PhiMapSpeed = $PhiGpsSpeed - $MapAngel;

            # 555 cm/s ~ 20 km/h -> Zeigerlänge = $MkSpeedLen bei 20 km/h
            my $dy = sin (deg2rad $PhiMapSpeed) * $MapMkSpeedLen * $MkOsd{'GroundSpeed'} / 555;
            my $dx = cos (deg2rad $PhiMapSpeed) * $MapMkSpeedLen * $MkOsd{'GroundSpeed'} / 555;
            my $x0 = $C_x;
            my $y0 = $C_y;
            my $x1 = $C_x + $dx;
            my $y1 = $C_y + $dy;
            $map_canvas->coords ('MK-Speed', $x0, $y0, $x1, $y1);
 
            # Update Line between Home and MK
            my ($H_x, $H_y) = &MapGps2XY($MkOsd{'HomePos_Lat'}, $MkOsd{'HomePos_Lon'});
            $map_canvas->coords ('MK-Home-Line', $H_x, $H_y, $C_x, $C_y);

            # Update Distance between Home and MK
            my ($Dist, $Bearing) = &MapGpsTo($MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'},
		                                    $MkOsd{'HomePos_Lat'}, $MkOsd{'HomePos_Lon'} );
            my $x = ($C_x - $H_x) / 2 + $H_x + 8;
            my $y = ($C_y - $H_y) / 2 + $H_y + 8;
            $map_canvas->coords ('MK-Home-Dist', $x, $y);
            $map_canvas->itemconfigure ('MK-Home-Dist',
                                        '-text' => sprintf ("%4d m", int ($Dist + 0.5) ),
                                       );

            # Update OSD - Sat dependent values
            $map_canvas->itemconfigure ('MK-OSD-Spd-Value', '-text' => sprintf ("%3d km/h", $MkOsd{'GroundSpeed'} * 0.036) );

            # Alt = average Luftdruck und Sat
            my $Alt = int ( ($MkOsd{'Altimeter'} / $Cfg->{'mkcockpit'}->{'AltFactor'} +
                             $MkOsd{'CurPos_Alt'} - $MkOsd{'HomePos_Alt'} ) / 2 + 0.5 );
            $System{'Alt'} = $Alt;
            $map_canvas->itemconfigure ('MK-OSD-Alt-Value', '-text' => sprintf ("%3d m", $Alt) );

            if ( &TargetIsValid() )
                {
                # Valid Target

                # Update Line between Target and MK
                my ($T_x, $T_y) = &MapGps2XY($MkOsd{'TargetPos_Lat'}, $MkOsd{'TargetPos_Lon'});
                $map_canvas->coords ('MK-Target-Line', $C_x, $C_y, $T_x, $T_y);

                # Update Distance between Target and MK
                my ($Dist, $Bearing) = &MapGpsTo($MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'},
		                                         $MkOsd{'TargetPos_Lat'}, $MkOsd{'TargetPos_Lon'} );
                if ( $Dist >= 25 )   
                    {
                    my $x = ($C_x - $T_x) / 2 + $T_x - 8;
                    my $y = ($C_y - $T_y) / 2 + $T_y + 8;
                    $map_canvas->coords ('MK-Target-Dist', $x, $y);
                    $map_canvas->itemconfigure ('MK-Target-Dist',
                                                '-text' => sprintf ("%4d m", int ($Dist + 0.5) ),
                                               );
                    }
                else
                    {
                    # Don't show distance < 25m
                    $map_canvas->coords ('MK-Target-Dist', 0, -100);
                    }

                # show target icon
                my $IconHeight = 48;
                my $IconWidth = 48;
                $map_canvas->coords('Target', $T_x - $IconWidth/2, $T_y - $IconHeight );

                $System{'CrossingBorder'} = 0;
                if ( &MkIsFlying()  and  &IsCrossingBorder($MkPos_x, $MkPos_y, $T_x, $T_y) )
                    {
                    # only, if MK is flying
                    $System{'CrossingBorder'} = 1;
                    &MkMessage ($Translate{'MsgCrossingBorder'}, "Timer-MapOverlay");
                    }
                }
            else
                {
                # No valid Target, move target line out of sight/canvas
                $map_canvas->coords ('MK-Target-Line', 0, -100, 0, -100);
                $map_canvas->coords ('MK-Target-Dist', 0, -100);

                # hide target icon
                $map_canvas->coords('Target', 0, -100, );
                }

            # Update Line between MK and POI
            if ( $PoiMode )
                {
                my ($P_x, $P_y) = &MapGps2XY($Poi_Lat, $Poi_Lon);
                $map_canvas->coords ('MK-POI-Line', $P_x, $P_y, $C_x, $C_y);
                }
            else
                {
                $map_canvas->coords ('MK-POI-Line', 0, -200, 0, -200);
                }
            }
        else
            {
            # kein ausreichender Sat-Empfang
            $map_canvas->itemconfigure ('MK-OSD-Spd-Value', '-text' => sprintf ("%3d km/h", 0 ) );
            }

        # Update OSD - non Sat dependent values
        $map_canvas->itemconfigure ('MK-OSD-Odo-Value', '-text' => sprintf ("%3.3f km", $OdoMeter / 1000) );
        $map_canvas->itemconfigure ('MK-OSD-Tim-Value', '-text' => sprintf ("%02d:%02d", $MkFlyingTime / 60, $MkFlyingTime % 60) );
        $map_canvas->itemconfigure ('MK-OSD-Sat-Value', '-text' => $MkOsd{'SatsInUse'} );

        # battery - OSD and warning
        my $UBat = sprintf ("%3.1f", $MkOsd{'UBat'});
        $System{'UBat'} = $UBat;
        $map_canvas->itemconfigure ('MK-OSD-Bat-Value', '-text' => "$UBat V" );

        $System{'BatWarning'} = 0;
        $map_canvas->itemconfigure ('MK-OSD-Bat-Value', '-fill' => $Cfg->{'mkcockpit'}->{'ColorOsd'});
        if ( $MkOsd{'UBat'}  <  $Cfg->{'mkcockpit'}->{'UBatWarning'} )
            {
            if ( time %2 )
                {
                $map_canvas->itemconfigure ('MK-OSD-Bat-Value', '-fill' => 'red');
                }

            &MkMessage ($Translate{'MsgBatWarning'}, "Timer-MapOverlay");
            $System{'BatWarning'} = 1;
            }


        # Display Operation Mode
        my $DisplayMode = $OperationMode;
        if ( &MkIsWptMode()  and  $OperationMode eq "Play" )
            {
            my %ModeMatrix = 
               (
               "KML-STD" => "Play KML",
               "KML-RND" => "Play KML",
               "KML-MAP" => "Play KML",
               "WPT-STD" => "Play WPT",
               "WPT-RND" => "Rand WPT",
               "WPT-MAP" => "Rand MAP",
               );
            my $Key = "${PlayerWptKmlMode}-${PlayerRandomMode}";
            $DisplayMode = $ModeMatrix{$Key};
            }

        $System{'RangeWarning'} = 0;
        if ( &MkRangeLimit() )
            {
            $DisplayMode  = "$DisplayMode" . " !!";   # Range Warning
            $System{'RangeWarning'} = 1;
            }
        $map_canvas->itemconfigure ('MK-OSD-Mode-Value', '-text' => $DisplayMode );


        # Waypoints abhaengig vom Modus NC/Player
        my $WpValue = "-- / --";
        if ( $MkOsd{'WaypointNumber'} > 0)
            {
            $WpValue = sprintf ("%d / %d", $MkOsd{'WaypointIndex'} + 1, $MkOsd{'WaypointNumber'});
            }
        if ($PlayerMode ne "Stop" and $PlayerWptKmlMode eq "WPT")
            {
            $WpValue = sprintf ("%d / %d", $WpPlayerIndex +1, scalar @Waypoints);
            }
        if ($PlayerMode ne "Stop" and $PlayerWptKmlMode eq "KML" )
            {
            my $KmlTimeBase = $Cfg->{'waypoint'}->{'KmlTimeBase'} || 1.0;
            my $CurrTime = int ($KmlPlayerIndex * $KmlTimeBase + 0.5);
            my $TotTime = int (scalar @KmlTargets * $KmlTimeBase + 0.5);
            $WpValue = sprintf ("%02d:%02d / %02d:%02d", $CurrTime / 60, $CurrTime % 60, $TotTime / 60, $TotTime % 60);
            }
        $map_canvas->itemconfigure ('MK-OSD-Wp-Value',  '-text' => "$WpValue");

        # Recording Mode
        my $RecordText = "";
        if ( $PlayerRecordMode =~ /REC/i )
            {
            my $KmlTimeBase = $Cfg->{'waypoint'}->{'KmlTimeBase'} || 1.0;
            my $TotTime = int (scalar @KmlTargets * $KmlTimeBase + 0.5);
            $RecordText = sprintf ("Recording %02d:%02d", $TotTime / 60, $TotTime % 60);
            }
        $map_canvas->itemconfigure ('MK-OSD-Rec-Value', '-text' => $RecordText );


        # Farbe MK-Zeiger abhängig vom GPS Empfang
        my $MkCol= $Cfg->{'mkcockpit'}->{'ColorMkSatNo'};
        if ( $SatsInUse >= 1 ) { $MkCol = $Cfg->{'mkcockpit'}->{'ColorMkSatLow'} ; }
        if ( $SatsInUse >= 6 ) { $MkCol = $Cfg->{'mkcockpit'}->{'ColorMkSatGood'}; }
        $map_canvas->itemconfigure ('MK-Arrow', '-fill' => $MkCol);


        # Show/Hide SatFix Icon
        if ($SatsInUse >= 6 )
            {
            $map_canvas->coords('Satellite', $MapSizeX-300, 10, );
            }
        else
            {
            # move icon out of sight
            $map_canvas->coords('Satellite', 0, -100, );
            }


        # Variometer Pointer
        my $dy = -$MkOsd{'Variometer'} * 10;
        $map_canvas->coords('Map-Variometer-Pointer', 5, $MapSizeY/2+$dy, 20, $MapSizeY/2+10+$dy, 20, $MapSizeY/2-10+$dy);

        #
        # System checks
        #

        if ( ! &MkIsMotorOn() )      { &MkMessage ($Translate{'MsgMotorOff'}, "Timer-MapOverlay"); }
        if ( ! &MkIsFlying() )       { &MkMessage ($Translate{'MsgNotFlying'}, "Timer-MapOverlay"); }
        if ( &MkIsCalibrating() )    { &MkMessage ($Translate{'MsgCalibrate'}, "Timer-MapOverlay"); }
        if ( &MkIsMotorStarting() )  { &MkMessage ($Translate{'MsgStart'}, "Timer-MapOverlay") }
        if ( &MkEmergencyLanding() ) { &MkMessage ($Translate{'MsgEmergencyLanding'}, "Timer-MapOverlay"); }
        if ( &MkRangeLimit() )       { &MkMessage ($Translate{'MsgRangeLimit'}, "Timer-MapOverlay"); }

        # RC range check
        my $RcQuality = $MkOsd{'RC_Quality'};
        $System{'RCQuality'} = "";

        if ( $RcQuality < 100 )
            {
            $System{'RCQuality'} = "NO";
            &MkMessage ($Translate{'MsgRcError'}, "Timer-MapOverlay");
            }
        elsif ( $RcQuality < 150 )
            {
            $System{'RCQuality'} = "WEAK";
            &MkMessage ($Translate{'MsgRcWarning'}, "Timer-MapOverlay");
            }

        # Sat reception quality
        if ( $SatsInUse == 0 )
            {
            &MkMessage ($Translate{'MsgNoSatReception'}, "Timer-MapOverlay");
            }
        elsif ( $SatsInUse > 0  and $SatsInUse < 6 )
            {
            &MkMessage ($Translate{'MsgWeakSatReception'}, "Timer-MapOverlay");
            }

        # MK Border check
        $System{'OutsideBorder'} = "0";
        if ( &MkIsFlying() and ! &IsInsideBorder($MkPos_x, $MkPos_y) )
            {
            # only, if MK is flying
            $System{'OutsideBorder'} = "1";
            &MkMessage ($Translate{'MsgOutsideBorder'}, "Timer-MapOverlay");
            }

        # Show Balloon, when aproaching Target
        &TargetMessageShow();

        }
    else
        {
        # keine aktuellen OSD Daten vom MK verfügbar
        &MkMessage ($Translate{'MsgNoData'}, "Timer-MapOverlay");
        }


    # Wp-Number input from keyboard
    $KbTimer++;
    if ( $CbPlayerKey ne "" )
        {
        # Key pressed
        $KbNum = "$KbNum" . "$CbPlayerKey";

        $CbPlayerKey = "";
        $KbTimer = 0;
        }
    if ( $KbTimer > 7  and $KbNum ne "" )
        {
        # number complete, set target
        my $WpIndex = sprintf ("%d", $KbNum);
        &WpTargetSet ($WpIndex - 1);

        # prepare for next number
        $KbNum = "";
        }
    

    # Show System Messages
    &MkMessageShow();

    });

#	
# Timer: 0.1s - Tracking Anzeige aktualisieren
#
if ( $Cfg->{'track'}->{'Active'} =~ /y/i )
    {
    $TrackMkCalibrateEdge = 0;  # calibrate edge detection

    $frame_map_top->repeat (100, sub
        {
        # Clear old messages from this timer
        &MkMessageInit ("Timer-Tracking");     

        lock (%MkOsd);              # until end of block
        lock (%MkTrack);            # until end of block

        # Aktuell gültige Karte
        %Map = %{$Maps{'Current'}};

        # Zeiger neu zeichnen
        my $ServoPan = @ServoPos[$MkTrack{'ServoPan'}];
        if ( $ServoPan ne ""  )
            {
            my $x0 = $TrackSizeX/2;     
            my $y0 = $TrackSizeY - 0 - $TrackOffY;
            my $x1 = $TrackSizeX/2 - ($TrackPtrLen-22) * cos( deg2rad $ServoPan);
            my $y1 = $TrackSizeY   - ($TrackPtrLen-22) * sin (deg2rad $ServoPan) - $TrackOffY;
            $track_canvas->coords ('Track-Ptr', $x0, $y0, $x1, $y1);
            }

        # Farbe Zeiger abhängig vom GPS Empfang
        my $SatsInUse = $MkOsd{'SatsInUse'};
        my $TrackPtrCol= 'red';
        if ( $SatsInUse >= 1 ) { $TrackPtrCol = 'orange'; }
        if ( $SatsInUse >= 6 ) { $TrackPtrCol = 'green'; }
        $track_canvas->itemconfigure ('Track-Ptr', '-fill' => $TrackPtrCol);

        # tracker coldstart condition at MK calibration, rising edge of signal
        if ( &MkIsCalibrating() )
            {
            if ( $TrackMkCalibrateEdge == 0 )
                {
                $TrackMkCalibrateEdge = 1;

                # send coldstart command to tracker
                $TrackQueue->enqueue("COLDSTART");
                }
            }
        else
            {
            $TrackMkCalibrateEdge = 0;
            }
        });
    }


#	
# Timer: 0.5s - Waypoint Player
#
$frame_map_top->repeat (500, sub
    {
    # Clear old messages from this timer
    &MkMessageInit ("Timer-Player");         

    lock (%MkOsd);              # until end of block

    if ( &MkIsWptMode() )
        {
        # NC is in WPT Mode 

        my $PoiHeading = $Cfg->{'waypoint'}->{'DefaultHeading'};
        if ( &CurPosIsValid()  and  $PoiMode )
            {
            $PoiHeading = &MapGpsTo ( $MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'},
                                      $Poi_Lat,  $Poi_Lon );
            }

        if ( $PlayerMode eq "Pause" )
            {
            if ( $PlayerPause_Lat ne ""  and  $PlayerPause_Lon ne "" )
                {
                # Gespeicherte Pausen-Pos senden
                &MkFlyTo ( '-lat'  => $PlayerPause_Lat,
                           '-lon'  => $PlayerPause_Lon,
                           '-holdtime' => "60",
                           '-heading' => $PoiHeading,
                           '-mode' => "Target",
                         );
                }
            }

        if ( $PlayerMode eq "Home" and &HomePosIsValid() )
            {
            # Gespeicherte oder eingestellte Home-Pos senden

            my $Home_Lat = $Map{'Home_Lat'} || $MkOsd{'HomePos_Lat'};
            my $Home_Lon = $Map{'Home_Lon'} || $MkOsd{'HomePos_Lon'};

            &MkFlyTo ( '-lat'  => $Home_Lat,
                       '-lon'  => $Home_Lon,
                       '-holdtime' => "60",
                       '-heading' => $PoiHeading,
                       '-mode' => "Target",
                     );
            }


        if ( $PlayerWptKmlMode ne 'WPT' )
            {
            # not in Wp mode
            return;
            }


        if ( $PlayerMode eq "Play"  )
            {

            if ( $PlayerRandomMode =~ /RND/i  or $PlayerRandomMode =~ /STD/i )
                {
                my $WpCnt = scalar @Waypoints;
                if ( $WpCnt > 0  and  $WpPlayerIndex < $WpCnt )
                    {
                    # Target WP-Pos senden
                    my $Wp = $Waypoints[$WpPlayerIndex];
                    my $Wp_Lon = $Wp->{'Pos_Lon'};
                    my $Wp_Lat = $Wp->{'Pos_Lat'};
                    if ( $Wp_Lat ne ""  and  $Wp_Lon ne "" )
                        {
                        &MkFlyTo ( '-lat'  => $Wp_Lat,
                                   '-lon'  => $Wp_Lon,
                                   '-holdtime' => "60",
                                   '-heading' => $PoiHeading,
                                   '-mode' => "Target",
                                 );
                        }
                    }
                }

            if ( $PlayerRandomMode =~ /MAP/i )
                {
                # Target Map-Pos senden
                &MkFlyTo ( '-x'  => $RandomTarget_x ,
                           '-y'  => $RandomTarget_y ,
                           '-holdtime' => "60",
                           '-heading' => $PoiHeading,
                           '-mode' => "Target",
                         );
                }
 
            # Ziel erreicht?
            if ( &WpCheckTargetReached() )
                {
                &WpTargetNext();
                }
            }
        }

    # WP Player Holdtime count down
    if ( $WpPlayerHoldtime > 0  )
        {
        $WpPlayerHoldtime --;
        }
    });


#	
# Timer: variabel - KML Player
#
my $KmlTimeBase = $Cfg->{'waypoint'}->{'KmlTimeBase'} || 1.0;
$KmlTimeBase *= 1000;

$frame_map_top->repeat ($KmlTimeBase, sub
    {

    # Clear old messages from this timer
    &MkMessageInit ("Timer-KMLPlayer");     

    lock (%MkOsd);              # until end of block

    if ( &CurPosIsValid() and $PlayerRecordMode =~ /REC/i )
        {
        # record current position
        push @KmlTargets, {
                          'Lat' => $MkOsd{'CurPos_Lat'},
                          'Lon' => $MkOsd{'CurPos_Lon'},
                          'Alt' => $MkOsd{'CurPos_Alt'},
                          }; 
        }


    if ( &MkIsWptMode() and  $PlayerMode eq "Play"  and  $PlayerWptKmlMode eq 'KML')
        {
        # Play KML

        # Pause, Home is handled in WPT-Timer

        my $KmlCnt = scalar @KmlTargets;
        if ( $KmlCnt > 0  and  $KmlPlayerIndex < $KmlCnt )
            {
            my $Lat = $KmlTargets[$KmlPlayerIndex]->{'Lat'};
            my $Lon = $KmlTargets[$KmlPlayerIndex]->{'Lon'};
            my $Alt = $KmlTargets[$KmlPlayerIndex]->{'Alt'};

            &MkFlyTo ( '-lat'             => $Lat,
                       '-lon'             => $Lon,
                       '-alt'             => $Alt,
                       '-holdtime'        => "60",
                       '-mode'            => "Target",
                     );

            # proceed to next Target
            $KmlPlayerIndex ++;
            if ( $KmlPlayerIndex >= $KmlCnt )
                {
                $KmlPlayerIndex = 0;
                }
            }
        }

    });


#	
# Timer: 1s
#
$frame_map_top->repeat (1000, sub
    {
    # Clear old messages from this timer
    &MkMessageInit ("Timer-Misc-1s");     

    lock (%MkOsd);              # until end of block

    # Aktuell gültige Karte
    %Map = %{$Maps{'Current'}};

    if ( &MkOsdIsValid() )
        {

        # Heartbeat MK Datenübertragung
        if ( time %2 )
            {
            $map_canvas->itemconfigure('Heartbeat', '-image' => 'HeartbeatLarge', );
            }
        else
            {
            $map_canvas->itemconfigure('Heartbeat', '-image' => 'HeartbeatSmall', );
            }

        # Flugzeit aktualisieren
        # Flugzeit selber mitzählen, da $MkOsd{'FlyingTime'} immer 0 (0.14b)
        if ( &MkIsFlying() )
            {
            $MkFlyingTime += 1;
            }

        # Update ODO-Meter
        if ( &CurPosIsValid() )
            {
            my $C_Lat = $MkOsd{'CurPos_Lat'};
            my $C_Lon = $MkOsd{'CurPos_Lon'};

            if ( $OdoFirst ne "" )
                {
                my ($Dist, $Bearing) = &MapGpsTo($C_Lat, $C_Lon, $OdoPos_Lat, $OdoPos_Lon );
                $OdoMeter += $Dist;
                }
            $OdoPos_Lat = $C_Lat;
            $OdoPos_Lon = $C_Lon;
            $OdoFirst = "1";
            }

        # # Bat capacity
        # my $Capacity = 100.0;
        # my $CapacityText = "";
        # if ( &MkIsFlying() )
        #     {
        #     if ( $UBatAverage eq "" )
        #         {
        #         $UBatAverage = $MkOsd{'UBat'};
        #         }
        #     $UBatAverage = (9.0 * $UBatAverage + $MkOsd{'UBat'}) / 10.0;
        #     $Capacity = &BatCapacity($UBatAverage);
        #     $CapacityText = sprintf ("(%d %)", int ($Capacity + 0.5) );
        #     }
        # $map_canvas->itemconfigure ('MK-OSD-Bat-Level', '-text' => $CapacityText );


        # # Estimate remaining flight time
        # my $TextTimeLeft = "";
        # if ( &MkIsFlying()  and  $Capacity <= 90 )
        #     {
        #     my $MaxTime = 100.0 / (100.0 - $Capacity) * $MkFlyingTime;
        #     my $TimeLeft = $MaxTime - $MkFlyingTime;
        #     $TextTimeLeft = sprintf ("(%d min)", int ($TimeLeft / 60.0 + 0.8) );
        #     }
        # $map_canvas->itemconfigure ('MK-OSD-Tim-Left', '-text' => $TextTimeLeft );


        # Footprint
        if ( $Cfg->{'mkcockpit'}->{'FootprintLength'} > 0 )
            {
            if ( &CurPosIsValid() )
                {
                # neuen Footprint hinten anhaengen
                my ($x, $y) = &MapGps2XY($MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'});
                push @Footprint, $x, $y;
                }

            while ( $#Footprint / 2  >  $Cfg->{'mkcockpit'}->{'FootprintLength'} )
                {
                # alte Footprints entfernen
                splice @Footprint, 0, 2;
                }

            &FootprintRedraw();
            }

        # show tracking antenna icon
        if ( $MkTrack{'HomePos_Lat'} ne ""  and  $MkTrack{'HomePos_Lon'} ne ""  )
            {
            # show antenna icon
            my ($x, $y) = &MapGps2XY($MkTrack{'HomePos_Lat'}, $MkTrack{'HomePos_Lon'});

            my $IconHeight = 48;
            my $IconWidth = 48;
            $map_canvas->coords('Track-Antenna', $x - $IconWidth/2, $y - $IconHeight );
            }
        else
            {
            # move icon out of sight
            $map_canvas->coords('Track-Antenna', 0, -50 );
            }


        # fun :-)
        if ( int rand (100) == 43 )
            {
            &TtsSpeak ('LOW', $Translate{'TtsFun'});
            }
        }

    });


#	
# Timer: 1s - TTS Sprachausgabe
#
$frame_map_top->repeat (1000, sub
    {
    # Aktuell gültige Karte
    %Map = %{$Maps{'Current'}};

    lock (%MkOsd);              # until end of block

    my $StatusInterval = $Cfg->{'tts'}->{'StatusInterval'}  || "30";   
    $SpeechTimer ++;
    my @TtsMsg;

    if ( &MkOsdIsValid() )
        {
        # Gueltige OSD Daten

        if ( $SpeechTimer % $StatusInterval == 0 )
            {
            #
            # give system status, low prio messages
            #

            # clear old low prio messages
            &TtsClear('LOW');

            # Flight time
            my $Min = int ( ($MkFlyingTime +1) / 60);
            my $Sec = int ( ($MkFlyingTime +1) % 60);
            my $Text = sprintf ("$Translate{'TtsFlightTimeMinSec'}", $Min, $Sec);
            if ( $Min == 0 )
                {
                $Text = sprintf ("$Translate{'TtsFlightTimeSec'}", $Sec);
                }
            &TtsSpeak ('LOW', "$Text");

            # Battery
            my ($Volt, $Tenth) = split '\.', $System{'UBat'};
            &TtsSpeak ('LOW', sprintf ("$Translate{'TtsBattery'}", $Volt, $Tenth));

            # Altitude
            if ( $System{'Alt'} < 0 )
                {
                &TtsSpeak ('LOW', sprintf ("$Translate{'TtsAltitudeNegative'}", abs($System{'Alt'}) ) );
                }
            else
                {
                &TtsSpeak ('LOW', sprintf ("$Translate{'TtsAltitude'}", $System{'Alt'} ) );
                }

            # Satellites
            &TtsSpeak ('LOW', sprintf ("$Translate{'TtsSatellite'}",  $MkOsd{'SatsInUse'}) );
            }

       # high prio messages
       if ( $SpeechTimer % 5 == 0 )
            {
            if ( $System{'BatWarning'} )          { push @TtsMsg, $Translate{'TtsBatteryWarning'}; }
            if ( $System{'RCQuality'} eq "WEAK" ) { push @TtsMsg, $Translate{'TtsRcWeak'}; }
            if ( $System{'RCQuality'} eq "NO" )   { push @TtsMsg, $Translate{'TtsRcNo'}; }
            if ( $System{'CrossingBorder'} )      { push @TtsMsg, $Translate{'TtsCrossingBorder'}; }
            if ( $System{'OutsideBorder'} )       { push @TtsMsg, $Translate{'TtsOutsideAirfield'}; }
            if ( $System{'RangeWarning'} )        { push @TtsMsg, $Translate{'TtsRange'}; }
            }
        }
    else
        {
        # no data link
        if ( $SpeechTimer % 5 == 0 )
            {
            push @TtsMsg, $Translate{'TtsNoDataLink'};
            }
        }

    # speak high prio messages 
    if ( scalar @TtsMsg > 0 )
        {
        # Clear pending messsages
        &TtsClear('HIGH');

        # Speak collected messages
        foreach $Msg (@TtsMsg)
            {
            &TtsSpeak ('HIGH', $Msg);
            }
        }
    });


__END__
