using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.ComponentModel;
using FObjBase;
using Misc;
using System.Collections.ObjectModel;
namespace FlyADBase
{
    /// <summary>
    /// AD盒提供的功能
    /// </summary>
    public interface IFlyAD : INotifyPropertyChanged
    {
        /// <summary>
        /// AD盒地址
        /// </summary>
        string Addr { get; set; }
        
        /// <summary>
        /// 从flyad7 获取 的systick 转换的时间
        /// </summary>
        DateTime Now { get; }

        /// <summary>
        /// AD盒版本号 版本1 没有systick;版本2 有systick;
        /// 通过通讯读取
        /// </summary>
        int Version { get; }

        /// <summary>
        /// 硬件版本，设置的！！！
        /// </summary>
        int HardwareVersion { get; set; }

        /// <summary>
        /// 脉冲
        /// </summary>
        int Position { get; }

        /// <summary>
        /// 速度 pps
        /// </summary>
        int Speed { get; }

        /// <summary>
        /// 纵向脉冲，也叫主轴脉冲
        /// </summary>
        int Position2 { get; }

        /// <summary>
        /// 纵向速度
        /// </summary>
        int Speed2 { get; }

        /// <summary>
        /// 1000ms从AD盒获取(或推送)1个AD值。此AD值只用于调试，定点
        /// </summary>
        int AD { get; }

        /// <summary>
        /// 1000ms从AD盒获取(或推送)1个AD值。此AD值只用于调试，定点
        /// </summary>
        int AD2 { get; }

        /// <summary>
        /// AD值最大值 ，恒定返回65535
        /// </summary>
        int ADMax { get; }
        
        /// <summary>
        /// 输入口状态,只有12位有效
        /// </summary>
        UInt16 IStatus { get; }

        /// <summary>
        /// 输出口状态,只有4位有效
        /// </summary>
        UInt16 OStatus { get; }

        /// <summary>
        /// Out0 = 0, SetOutput(0x0001,0x0000);
        /// Out0 = 1, Out1 = 0, SetOutput(0x0003,0x0001);
        /// </summary>
        /// <param name="mask"></param>
        /// <param name="enable"></param>
        void SetOutput(UInt16 mask, UInt16 enable);

        /// <summary>
        /// 脉冲/grid, 最大1000个grid
        /// </summary>
        UInt16 PosOfGrid { set; get; }

        #region 运动参数


        /// <summary>
        /// 电机类型
        /// </summary>
        MOTORTYPE MotorType { set; get; }

        /// <summary>
        /// 脉冲比例 分母(电机脉冲)
        /// </summary>
        UInt16 Ratio01 { get; set; }
        /// <summary>
        /// 脉冲比例 分子(编码器脉冲)
        /// </summary>
        UInt16 Ratio02 { get; set; }

        /// <summary>
        /// 脉冲平移(0位处脉冲值)
        /// </summary>
        Int16 PosOffset { set; get; }

        /// <summary>
        /// 手动速度 pps
        /// </summary>
        UInt32 JogVelocity { set; get; }
        #endregion

        #region 速度参数
        /// <summary>
        /// 设置速度参数
        /// </summary>
        /// <param name="sv">开始速度 pps</param>
        /// <param name="atime">加速时间 ms</param>
        /// <param name="dtime">减速时间 ms</param>
        /// <param name="hspeed1">归0 第1段速 pps</param>
        /// <param name="hspeed2">归0 第2段速 pps</param>
        void SetPosParam(UInt32 sv, UInt32 atime, UInt32 dtime, UInt32 hspeed1, UInt32 hspeed2);
        /// <summary>
        /// 设置速度
        /// </summary>
        /// <param name="velocity"></param>
        void SetVelocity(UInt32 velocity);
        /// <summary>
        /// runto 速度 pps
        /// </summary>
        UInt32 Velocity { get; }
        /// <summary>
        /// 开始速度 pps
        /// </summary>
        UInt32 SVelocity { get; }
        /// <summary>
        /// 加速时间 ms
        /// </summary>
        UInt32 ATime { get; }
        /// <summary>
        /// 减速时间 ms
        /// </summary>
        UInt32 DTime { get; }
        /// <summary>
        /// 归0 第1段速 pps
        /// </summary>
        UInt32 HVelocity1 { get; }
        /// <summary>
        /// 归0 第2段速 pps
        /// </summary>
        UInt32 HVelocity2 { get; }
        
        #endregion


        /// <summary>
        /// timegrid 事件; 版本1: 1.28s 一次; 版本2: 0.2s一次， 
        /// </summary>
        event TimeGridEventHandler TimeGridEvent;

        /// <summary>
        /// runto 中，每经过1个grid 推送数据
        /// </summary>
        event MiniGridEventHandler MiniGridEvent;

        /// <summary>
        /// 输入状态改变事件
        /// </summary>
        event IStatusChangedEventHandler IStatusChangedEvent;

        /// <summary>
        /// 指令状态改变事件
        /// </summary>
        event DriveStatusChangedEventHandler DriveStatusChangedEvent;

        /// <summary>
        /// 脉冲1改变事件
        /// </summary>
        event PositionChangedEventHandler PositionChangedEvent;

        /// <summary>
        /// 脉冲2改变事件
        /// </summary>
        event PositionChangedEventHandler Position2ChangedEvent;


        #region 运行动作
        /// <summary>
        /// 运行至指定位置
        /// </summary>
        /// <param name="to">脉冲</param>
        void Runto(int to);
        /// <summary>
        /// 归原点
        /// </summary>
        void Origin();
        /// <summary>
        /// 停止
        /// </summary>
        void Stop();
        /// <summary>
        /// 反向，直到撞了限位
        /// </summary>
        void Backward();
        /// <summary>
        /// 正行，直到撞了限位
        /// </summary>
        void Forward();

        /// <summary>
        /// 当前运行指令
        /// </summary>
        DRIVE_MAN_ORDER DriveOrder { get; }
        /// <summary>
        /// 之前运行动作的状态
        /// </summary>
        DRIVE_MAN_STATUS DriveStatus { get; }
        #endregion
    }



    /// <summary>
    /// 电机类型
    /// </summary>
    public enum MOTORTYPE
    {
        /// <summary>
        /// 没电机
        /// </summary>
        NULL = 0,
        /// <summary>
        /// 异步电机，通过I1~I3控制 正反转
        /// </summary>
        VF0 = 1,
        /// <summary>
        /// 伺服 或 步进电机，通过 S,P, 控制驱动器
        /// </summary>
        SERVO = 2
    }
    /// <summary>
    /// 使用码区状态
    /// </summary>
    public enum AREA_STATUS
    {
        /// <summary>
        /// 没有读过密码区
        /// </summary>
        NOTLOAD = 0,
        /// <summary>
        /// 读出错
        /// </summary>
        RDERR = 1,
        /// <summary>
        /// 非有效密码区 没有0xAA55 或 0x5A5A 标志
        /// </summary>
        NOTVALID = 2,
        /// <summary>
        /// 写出错
        /// </summary>
        WRERR = 3,
        /// <summary>
        /// 有效密码区
        /// </summary>
        VALID = 4,
        /// <summary>
        /// 有效密码区，应回写EEPROM
        /// </summary>
        VALID_DIRTY = 5,
        /// <summary>
        /// 有效密码区，但回写EEPROM失败
        /// </summary>
        VALID_WRERR = 6
    }
    /// <summary>
    /// 使用码区操作结果
    /// </summary>

    public enum AREA_ERR
    {
        /// <summary>
        /// 没有错误
        /// </summary>
        NO_ERR = 0,
        /// <summary>
        /// 重复使用码
        /// </summary>
        DUP_ACCESS = 1,
        /// <summary>
        /// 无效使用码
        /// </summary>
        ERR_ACCESS = 2
    }
    /// <summary>
    /// 运行状态
    /// </summary>
    public enum DRIVE_MAN_STATUS
    {
        /// <summary>
        /// 正常运行结束
        /// </summary>
        STOP = 0,
        /// <summary>
        /// 正在运行
        /// </summary>
        RUNNING = 1,
        /// <summary>
        /// 指令结束（收到停止指令）
        /// </summary>
        STOP_MANUAL = 2,
        /// <summary>
        /// 运行结束，限位开关闭合
        /// </summary>
        LIMIT = 3
    }
    /// <summary>
    /// 运行动作
    /// </summary>
    public enum DRIVE_MAN_ORDER
    {
        /// <summary>
        /// 闲置
        /// </summary>
        IDLE = 'I',
        /// <summary>
        /// 运行到
        /// </summary>
        RUNTO = 'R',
        /// <summary>
        /// 归原点
        /// </summary>
        ORIGIN = 'O',
        /// <summary>
        /// 后退
        /// </summary>
        BACKWORD = 'B',
        /// <summary>
        /// 前进
        /// </summary>
        FORWORD = 'F',
        /// <summary>
        /// 停止
        /// </summary>
        STOP = 'S',
        /// <summary>
        /// 同步
        /// </summary>
        SYNC = 'D',

        /// <summary>
        /// 未知状态
        /// </summary>
        SYNC2 = 'P'
    }
}
