AutoForwBackw.cs 2.7 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FlyADBase;
using Misc;

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

        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        FlyAD7 flyad;

        private bool fbenable = false;
        public bool FB_Enable
        {
            get { return fbenable; }
            set
            {
                if (fbenable != value)
                {
                    fbenable = value;
                    PropertyChanged.Notify(this, "FB_Enable");
                }
            }
        }
        private int fb_pos1 = 1000;
        private int fb_pos2 = 3000;
        public int FB_Pos1
        {
            get { return fb_pos1; }
            set
            {
                if (fb_pos1 != value)
                {
                    fb_pos1 = value;
                    PropertyChanged.Notify(this, "FB_Pos1");
                }
            }
        }
        public int FB_Pos2
        {
            get { return fb_pos2; }
            set
            {
                if (fb_pos2 != value)
                {
                    fb_pos2 = value;
                    PropertyChanged.Notify(this, "FB_Pos2");
                }
            }
        }

        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()
        {
            timer.Interval = 1000;
            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;
    }
}