AutoForwBackw.cs 1.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FlyADBase;
using Misc;
8
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
9 10 11 12 13 14 15 16 17

namespace Flyad7_WPF
{
    /// <summary>
    /// 来回走
    /// </summary>
    public class AutoForwBackw : INotifyPropertyChanged
    {

18
        DispatcherTimer timer;
潘栩锋's avatar
潘栩锋 committed
19 20
        FlyAD7 flyad;

21 22 23 24 25

        public bool FB_Enable { get; set; }
        public int FB_Pos1 { get; set; } = 1000;

        public int FB_Pos2 { get; set; } = 3000;
潘栩锋's avatar
潘栩锋 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        public AutoForwBackw(FlyAD7 flyad)
        {
            this.flyad = flyad;
            FB_init();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (!flyad.IsConnected)
            {
                FB_Enable = false;
            }

            if (flyad.DriveStatus != DRIVE_MAN_STATUS.RUNNING)
            {
                if (Math.Abs(FB_Pos1 - flyad.Position) > Math.Abs(FB_Pos2 - flyad.Position))
                {
                    flyad.Runto(FB_Pos1);
                }
                else
                {
                    flyad.Runto(FB_Pos2);
                }
            }

        }
        void FB_init()
        {
54 55
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
潘栩锋's avatar
潘栩锋 committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            timer.Tick += new EventHandler(timer_Tick);
            FB_Enable = false;
            this.PropertyChanged += new PropertyChangedEventHandler(MainWindow_PropertyChanged);
        }

        void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FB_Enable")
            {
                if (FB_Enable)
                {
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}