using nRTMC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 一维比较
{
    public partial class Form1 : Form
    {
        private ushort cardConnectNo = 0; //卡的连接号
        private UInt32 functionReturn = 0; //用于接收函数返回值

        enum enum_Compare_Dim
        {
            Compare_1D = 1, //一维软件比较
            Compare_2D = 2 //二维软件比较
        }

        public Form1()
        {
            InitializeComponent();
            #region 软件比较
            string[] axisNumStr = new string[12];
            for (int i = 0; i < 12; i++)
            {
                axisNumStr[i] = "轴" + i;
            }
            comboBox_axisNo.Items.AddRange(axisNumStr);
            comboBox_axisNo.SelectedIndex = 0;

            comboBox_compareSource.Items.AddRange(new object[] { "指令位置", "反馈位置" });
            comboBox_compareSource.SelectedIndex = 0;

            comboBox_cmpMode.Items.AddRange(new object[] { "小于", "大于等于" });
            comboBox_cmpMode.SelectedIndex = 0;

            comboBox_triggerMode.Items.AddRange(new object[] { "IO输出低", "IO输出高", "IO输出反向", "IO输出脉冲", "停止轴" });
            comboBox_triggerMode.SelectedIndex = 0;

            string[] outputIOStr = new string[2];
            for (int i = 0; i < 2; i++) //界面上只演示2个Output口的状态
            {
                outputIOStr[i] = "OutPut " + i;
            }
            comboBox_triggerPrm.Items.Clear();
            comboBox_triggerPrm.Items.AddRange(outputIOStr);
            comboBox_triggerPrm.SelectedIndex = 0;
            #endregion


            #region 高速比较

            string[] axisNumStrHS = new string[12];
            for (int i = 0; i < 12; i++)
            {
                axisNumStrHS[i] = "轴" + i;
            }
            comboBox_HSCmpAxis.Items.AddRange(axisNumStrHS);
            comboBox_HSCmpAxis.SelectedIndex = 0;

            comboBox_HSCmpsourceSelect.Items.AddRange(new object[] { "指令位置", "反馈位置" });
            comboBox_HSCmpsourceSelect.SelectedIndex = 0;

            comboBox_HSCmpMode.Items.AddRange(new object[] { "小于比较点", "大于比较点", "等增量比较", "FIFO点比较输出" });
            comboBox_HSCmpMode.SelectedIndex = 0;

            comboBox_HSCmpTriggerMode.Items.AddRange(new object[] { "IO输出低", "IO输出高", "IO输出脉冲" });
            comboBox_HSCmpTriggerMode.SelectedIndex = 0;

            #endregion
        }


        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 开卡
            ushort pciNum = 0; //用于接收开卡成功的卡数
            ushort[] pcieIndex = new ushort[12];
            functionReturn = RTMC.MC_Open(0, nRTMC.class_Controller_Connect_Type.CONNECTION_PCI, "", ref pciNum, pcieIndex);
            if (functionReturn == 0)//函数成功执行
            {
                if (pciNum > 0)//至少一张卡被成功开卡
                {
                    cardConnectNo = pcieIndex[0]; //如果有多卡的情况下这里也只用卡1演示

                    #region 获取卡信息
                    //--------------------------获取卡信息------------------------------------------------------------------------
                    ushort totalAxisNum = 0; //用于获取卡的轴数
                    ushort linerNum = 0;//用于获取卡的插补系个数
                    ushort totalInNum = 0;//用于获取卡的输入口数量
                    ushort totalOutNum = 0;//用于获取卡的输出口数量
                    ushort totalPWMNum = 0;//用于获取卡的PWM通道数量
                    functionReturn = RTMC.MC_GetTotalAxesAndIONum(cardConnectNo, ref totalAxisNum, ref linerNum, ref totalInNum, ref totalOutNum, ref totalPWMNum);

                    //------------------------------------------------------------------------------------------------------------
                    #endregion

                    timer1.Start(); //启动界面监控定时器
                }
                else
                {
                    MessageBox.Show("未找到卡!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("开卡失败!");
                return;
            }
            #endregion
        }


        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            timer1.Dispose();
            RTMC.MC_Close(cardConnectNo); //关卡释放资源
        }

        #region 软件比较
        /// <summary>
        /// 软件比较触发模式选项栏选择改变时引发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_triggerMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Windows.Forms.ComboBox useCombobox = sender as System.Windows.Forms.ComboBox;
            //这里如果触发模式选择了停止轴的话，触发参数的选项栏就该改为轴号选择
            if (useCombobox.SelectedIndex == 4)
            {
                comboBox_triggerPrm.Items.Clear();
                string[] axisNumStr = new string[12];
                for (int i = 0; i < 12; i++)
                {
                    axisNumStr[i] = "轴" + i;
                }
                comboBox_triggerPrm.Items.AddRange(axisNumStr);
                comboBox_triggerPrm.SelectedIndex = 0;
            }
            else
            {
                comboBox_triggerPrm.Items.Clear();
                string[] outputIOStr = new string[2];
                for (int i = 0; i < 2; i++)
                {
                    outputIOStr[i] = "OutPut " + i;
                }
                comboBox_triggerPrm.Items.AddRange(outputIOStr);
                comboBox_triggerPrm.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 启动比较按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ushort cmpChan = ((ushort)numericUpDown1.Value); //比较通道
            ushort cmpAxis = Convert.ToUInt16(comboBox_axisNo.SelectedIndex); //比较轴号
            ushort posSrc = Convert.ToUInt16(comboBox_compareSource.SelectedIndex); //比较位置源
            double pos = Convert.ToDouble(numericUpDown_cmpPos.Value); //比较点位置
            ushort cmpMode = Convert.ToUInt16(comboBox_cmpMode.SelectedIndex); //比较模式
            ushort triggerMode = Convert.ToUInt16(comboBox_triggerMode.SelectedIndex); //触发模式
            ushort triggerPrm = Convert.ToUInt16(comboBox_triggerPrm.SelectedIndex); //触发参数
            double outTime = Convert.ToDouble(numericUpDown_ioOutTime.Value); //输出时间

            uint rtVal = RTMC.MC_StartSoft1DCmp(cardConnectNo, //启动一维软件比较
                cmpChan,
                cmpAxis,
                posSrc,
                1,
                new double[] { pos },
                cmpMode,
                triggerMode,
                triggerPrm,
                outTime
                );

            //启动比较之后启动相关轴运动
            ushort axis0 = ((ushort)comboBox_axisNo.SelectedIndex);
            RTMC.MC_Power(cardConnectNo, axis0, 1);
            RTMC.MC_MoveVelocity(cardConnectNo, axis0, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
            groupBox4.Text = "轴" + axis0;

            if(comboBox_triggerMode.SelectedIndex == 4)
            {
                ushort axis1 = ((ushort)comboBox_triggerPrm.SelectedIndex);
                RTMC.MC_Power(cardConnectNo, axis1, 1);
                RTMC.MC_MoveVelocity(cardConnectNo, axis1, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
                groupBox5.Text = "轴" + axis1;
            }

        }
        /// <summary>
        /// 停止比较按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            RTMC.MC_StopSoftCmp(cardConnectNo, ((ushort)enum_Compare_Dim.Compare_1D), ((ushort)numericUpDown1.Value)); //停止一维软件比较
            RTMC.MC_StopEmgImd(cardConnectNo);//停止所有轴的运动
        }

        /// <summary>
        /// 比较器清零按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            uint rtVal = RTMC.MC_ClearSoftCmp(cardConnectNo, ((ushort)enum_Compare_Dim.Compare_1D), ((ushort)numericUpDown1.Value)); //比较器清零
        }

        /// <summary>
        /// output0按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo,0,1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 0, 0);
            }
        }

        /// <summary>
        /// output1按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (pictureBox2.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 1, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 1, 0);
            }
        }

        /// <summary>
        /// 位置清除按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            ushort axis0 = ((ushort)comboBox_axisNo.SelectedIndex);
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axis0, 0);

            if (comboBox_triggerMode.SelectedIndex == 4)
            {
                ushort axis1 = ((ushort)comboBox_triggerPrm.SelectedIndex);
                RTMC.MC_SetAxisCmdPos(cardConnectNo, axis1, 0);
            }
        }

        #endregion

        #region 高速比较
        /// <summary>
        /// 启动比较按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button12_Click(object sender, EventArgs e)
        {

            ushort cmpAxis = Convert.ToUInt16(comboBox_HSCmpAxis.SelectedIndex);//轴号
            ushort posSrc = Convert.ToUInt16(comboBox_HSCmpsourceSelect.SelectedIndex);//位置源

            ushort cmpMode = Convert.ToUInt16(comboBox_HSCmpMode.SelectedIndex);//比较模式
            ushort cmpCount = 1;
            if (cmpMode == 3) cmpCount = 2;
            double[] posArr = new double[cmpCount];
            posArr[0] = Convert.ToDouble(numericUpDown4.Value);//比较位置
            if (cmpCount == 2) posArr[1] = Convert.ToDouble(numericUpDown_incrementValue.Value); //增量值

            ushort triggerMode = Convert.ToUInt16(comboBox_HSCmpTriggerMode.SelectedIndex); //触发模式
            ushort triggerPrm = Convert.ToUInt16(numericUpDown2.Value); //触发参数
            double outTime = Convert.ToDouble(numericUpDown3.Value); //IO输出时间

            uint rtVal = RTMC.MC_StartHS1DCmp(cardConnectNo,
                ((ushort)numericUpDown2.Value),
                cmpAxis,
                posSrc,
                cmpCount,
                posArr,
                cmpMode,
                triggerMode,
                triggerPrm,
                outTime);

            //启动比较之后启动相关轴运动
            ushort axis0 = ((ushort)comboBox_HSCmpAxis.SelectedIndex);
            RTMC.MC_Power(cardConnectNo, axis0, 1);
            RTMC.MC_MoveVelocity(cardConnectNo, axis0, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
            groupBox10.Text = "轴" + axis0;
        }

        /// <summary>
        /// 停止比较按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            RTMC.MC_StopHSCmp(cardConnectNo,((ushort)enum_Compare_Dim.Compare_1D), ((ushort)numericUpDown2.Value));
            RTMC.MC_StopEmgImd(cardConnectNo);//停止所有轴的运动

        }

        /// <summary>
        /// 比较器清零按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            uint rtVal = RTMC.MC_ClearHSCmp(cardConnectNo, ((ushort)enum_Compare_Dim.Compare_1D), ((ushort)numericUpDown2.Value));
        }

        /// <summary>
        /// output12按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            if (pictureBox4.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 12, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 12, 0);
            }
        }

        /// <summary>
        /// output13按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            if (pictureBox3.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 13, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 13, 0);
            }
        }

        /// <summary>
        /// output14按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            if (pictureBox5.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 14, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 14, 0);
            }
        }

        /// <summary>
        /// output15按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            if (pictureBox6.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 15, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 15, 0);
            }
        }

        /// <summary>
        /// 比较输出模式改成等增量比较时开发增量值输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_HSCmpMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as System.Windows.Forms.ComboBox).Text == "等增量比较")
            {
                numericUpDown_incrementValue.Enabled = true;
            }
            else
            {
                numericUpDown_incrementValue.Enabled = false;
                numericUpDown_incrementValue.Value = 0;
            }
        }

        /// <summary>
        /// 位置清除按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button14_Click(object sender, EventArgs e)
        {
            ushort axis0 = ((ushort)comboBox_HSCmpAxis.SelectedIndex);
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axis0, 0);
        }

        #endregion
        /// <summary>
        /// 定时器更新界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            #region 软件比较
            #region 比较器状态
            ushort enable = 0;
            double running_pos = 0;
            UInt32 completed_points = 0;
            UInt32 remained_points = 0;
            uint rtVal = RTMC.MC_GetSoftCmpStatus(cardConnectNo,
                ((ushort)enum_Compare_Dim.Compare_1D), //比较维数
                ((ushort)numericUpDown1.Value), //比较通道
                ref enable, //比较器使能
                ref running_pos, //当前比较点位置
                ref completed_points, //已完成比较点
                ref remained_points); //剩余比较点

            switch (enable)
            {
                case 0:
                    textBox_enableState.Text = "未启用";
                    break;
                case 1:
                    textBox_enableState.Text = "使能";
                    break;
                default:
                    textBox_enableState.Text = "未定义";
                    break;
            }

            textBox_runningPoint.Text = running_pos.ToString();
            textBox_yetPoints.Text = completed_points.ToString();
            textBox_usablePoints.Text = remained_points.ToString();

            #endregion

            #region 轴状态
            ushort axis0 = ((ushort)comboBox_axisNo.SelectedIndex);
            double cmdPos = 0;
            RTMC.MC_GetAxisCmdPos(cardConnectNo, axis0, ref cmdPos);//获取当前指令位置
            textBox2.Text = cmdPos.ToString();

            double cmdSpeed = 0;
            RTMC.MC_GetAxisCmdSpeed(cardConnectNo, axis0, ref cmdSpeed); //获取当前指令速度
            textBox1.Text = cmdSpeed.ToString();

            ushort checkDone = 1;
            RTMC.MC_GetAxisCheckDone(cardConnectNo, axis0, ref checkDone);//获取轴运动状态
            switch (checkDone)
            {
                case 0:
                    textBox3.Text = "运动中";
                    break;
                case 1:
                    textBox3.Text = "空闲";
                    break;
                default:
                    textBox3.Text = checkDone.ToString();
                    break;
            }

            if(comboBox_triggerMode.SelectedIndex == 4)
            {
                ushort axis1 = ((ushort)comboBox_triggerPrm.SelectedIndex);
                RTMC.MC_GetAxisCmdPos(cardConnectNo, axis1, ref cmdPos);//获取当前指令位置
                textBox5.Text = cmdPos.ToString();

                RTMC.MC_GetAxisCmdSpeed(cardConnectNo, axis1, ref cmdSpeed); //获取当前指令速度
                textBox6.Text = cmdSpeed.ToString();

                RTMC.MC_GetAxisCheckDone(cardConnectNo, axis1, ref checkDone);//获取轴运动状态
                switch (checkDone)
                {
                    case 0:
                        textBox4.Text = "运动中";
                        break;
                    case 1:
                        textBox4.Text = "空闲";
                        break;
                    default:
                        textBox4.Text = checkDone.ToString();
                        break;
                }
            }
            #endregion

            #region 输出口状态
            byte ioState = 0;
            RTMC.MC_GetOutIoBit(cardConnectNo, 0, ref ioState);
            if (ioState == 0) pictureBox1.BackColor = Color.Green;
            else pictureBox1.BackColor = Color.Red;

            RTMC.MC_GetOutIoBit(cardConnectNo, 1, ref ioState);
            if (ioState == 0) pictureBox2.BackColor = Color.Green;
            else pictureBox2.BackColor = Color.Red;

            #endregion
            #endregion

            #region 高速比较
            #region 比较器状态

            ushort HSenable = 0;
            double HSrunning_pos = 0;
            UInt32 HScompleted_points = 0;
            UInt32 HSremained_points = 0;
            rtVal = RTMC.MC_GetHSCmpStatus(cardConnectNo,
                ((ushort)enum_Compare_Dim.Compare_1D),
                ((ushort)numericUpDown2.Value),
                ref HSenable,
                ref HSrunning_pos,
                ref HScompleted_points,
                ref HSremained_points);
            switch (HSenable)
            {
                case 0:
                    textBox14.Text = "未启用";
                    break;
                case 1:
                    textBox14.Text = "使能";
                    break;
                default:
                    textBox14.Text = "未定义";
                    break;
            }

            textBox13.Text = HSrunning_pos.ToString();
            textBox15.Text = HScompleted_points.ToString();
            textBox16.Text = HSremained_points.ToString();

            #endregion

            #region 轴状态
            ushort HSaxis0 = ((ushort)comboBox_HSCmpAxis.SelectedIndex);
            double HScmdPos = 0;
            RTMC.MC_GetAxisCmdPos(cardConnectNo, axis0, ref HScmdPos);//获取当前指令位置
            textBox10.Text = HScmdPos.ToString();

            double HScmdSpeed = 0;
            RTMC.MC_GetAxisCmdSpeed(cardConnectNo, axis0, ref HScmdSpeed); //获取当前指令速度
            textBox12.Text = HScmdSpeed.ToString();

            ushort HSCheckDone = 0;
            RTMC.MC_GetAxisCheckDone(cardConnectNo, axis0, ref HSCheckDone);//获取轴运动状态
            switch (HSCheckDone)
            {
                case 0:
                    textBox11.Text = "运动中";
                    break;
                case 1:
                    textBox11.Text = "空闲";
                    break;
                default:
                    textBox11.Text = HSCheckDone.ToString();
                    break;
            }

            #endregion

            #region 输出口状态

            byte HSIoState = 0;
            RTMC.MC_GetOutIoBit(cardConnectNo, 12, ref HSIoState);
            if (HSIoState == 0) pictureBox4.BackColor = Color.Green;
            else pictureBox4.BackColor = Color.Red;

            RTMC.MC_GetOutIoBit(cardConnectNo, 13, ref HSIoState);
            if (HSIoState == 0) pictureBox3.BackColor = Color.Green;
            else pictureBox3.BackColor = Color.Red;

            RTMC.MC_GetOutIoBit(cardConnectNo, 14, ref HSIoState);
            if (HSIoState == 0) pictureBox5.BackColor = Color.Green;
            else pictureBox5.BackColor = Color.Red;

            RTMC.MC_GetOutIoBit(cardConnectNo, 15, ref HSIoState);
            if (HSIoState == 0) pictureBox6.BackColor = Color.Green;
            else pictureBox6.BackColor = Color.Red;

            #endregion


            #endregion
        }

        
    }
}
