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 nRTMC;

namespace 高速锁存
{
    public partial class Form1 : Form
    {
        private ushort cardConnectNo = 0; //卡的连接号
        private UInt32 functionReturn = 0; //用于接收函数返回值

        public Form1()
        {
            InitializeComponent();
            
        }

        /// <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);

                    comboBox_HSChan.SelectedIndex = 0; //默认选择cmp0

                    numericUpDown1.Maximum = totalInNum; //输入口索引最大值

                    comboBox_generalIO.Items.Clear();
                    for (int i = 0; i < totalInNum; i++)
                    {
                        comboBox_generalIO.Items.Add("Input" + i);
                    }
                    comboBox_generalIO.SelectedIndex = 0; //默认选择Input0

                    comboBox_latchAxis.Items.Clear();
                    for (int i = 0; i < totalOutNum; i++)
                    {
                        comboBox_latchAxis.Items.Add("Axis" + i);
                    }
                    comboBox_latchAxis.SelectedIndex = 0;//默认选择Axis0
                    //------------------------------------------------------------------------------------------------------------
                    #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); //关卡释放资源
        }

        /// <summary>
        /// 锁存对象选择属性更改时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_home_CheckedChanged(object sender, EventArgs e)
        {
            string radioButtonName = (sender as RadioButton).Name;
            switch (radioButtonName)
            {
                case "radioButton_home":
                    radioButton_single.Checked = true;
                    radioButton_lowLevel.Checked = true;

                    label_HSChan.Enabled = false;
                    comboBox_HSChan.Enabled = false;

                    label_generalIo.Enabled = false;
                    comboBox_generalIO.Enabled = false;

                    radioButton_continuous.Enabled = false;

                    radioButton_doubleLevel.Enabled = false;
                    break;
                case "radioButton_EZ":
                    radioButton_single.Checked = true;
                    radioButton_lowLevel.Checked = true;

                    label_HSChan.Enabled = false;
                    comboBox_HSChan.Enabled = false;

                    label_generalIo.Enabled = false;
                    comboBox_generalIO.Enabled = false;

                    radioButton_continuous.Enabled = false;

                    radioButton_doubleLevel.Enabled = false;
                    break;
                case "radioButton_HSInput":
                    label_HSChan.Enabled = true;
                    comboBox_HSChan.Enabled = true;

                    label_generalIo.Enabled = false;
                    comboBox_generalIO.Enabled = false;

                    radioButton_continuous.Enabled = true;

                    radioButton_doubleLevel.Enabled = true;
                    break;
                case "radioButton_generalInput":
                    label_HSChan.Enabled = false;
                    comboBox_HSChan.Enabled = false;

                    label_generalIo.Enabled = true;
                    comboBox_generalIO.Enabled = true;

                    radioButton_continuous.Enabled = true;

                    radioButton_doubleLevel.Enabled = true;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 启动锁存按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_start_Click(object sender, EventArgs e)
        {
            UInt16 cmpObject = 0; //锁存信号源
            UInt16 cmpChan = 0; //锁存通道(锁存信号源为高速输入口时为0，2)
            UInt16 cmpAxis = 0;//锁存轴
            UInt16 cmpMode = 0;//锁存模式
            UInt16 cmpLogic = 0;//锁存逻辑
            UInt16 cmpPosSource = 0;//锁存位置源

            if (radioButton_home.Checked) cmpObject = 0;
            else if (radioButton_EZ.Checked) cmpObject = 1;
            else if (radioButton_HSInput.Checked)
            {
                cmpObject = 2;
                cmpChan = (ushort)comboBox_HSChan.SelectedIndex;
            }
            else if (radioButton_generalInput.Checked)
            {
                cmpObject = 3;
                cmpChan = (ushort)comboBox_generalIO.SelectedIndex;
            }
            cmpAxis = (ushort)comboBox_latchAxis.SelectedIndex;

            if (radioButton_single.Checked) cmpMode = 0;
            else if (radioButton_continuous.Checked) cmpMode = 1;

            if (radioButton_lowLevel.Checked) cmpLogic = 0;
            else if (radioButton_hightLevel.Checked) cmpLogic = 1;
            else if (radioButton_doubleLevel.Checked) cmpLogic = 2;

            if (radioButton_posCmd.Checked) cmpPosSource = 0;
            else if (radioButton_posEncode.Checked) cmpPosSource = 1;

            RTMC.MC_StartCapture(cardConnectNo,
                cmpObject,
                cmpAxis,
                cmpChan,
                cmpChan,
                cmpMode,
                cmpLogic,
                cmpPosSource);

            //启动锁存之后启动相关轴的连续运动
            RTMC.MC_Power(cardConnectNo, cmpAxis, 1); //任何运动都需要轴使能

            RTMC.MC_MoveVelocity(cardConnectNo, cmpAxis, 100, 100, 1000, 0.1, 0.1, 0, 1000000);//启动速度运动
            
        }

        /// <summary>
        /// 停止锁存按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_stop_Click(object sender, EventArgs e)
        {
            UInt16 cmpObject = 0;
            UInt16 cmpChan = 0;
            UInt16 cmpAxis = 0;

            if (radioButton_home.Checked) cmpObject = 0;
            else if (radioButton_EZ.Checked) cmpObject = 1;
            else if (radioButton_HSInput.Checked)
            {
                cmpObject = 2;
                cmpChan = (ushort)comboBox_HSChan.SelectedIndex;
            }
            else if (radioButton_generalInput.Checked)
            {
                cmpObject = 3;
                cmpChan = (ushort)comboBox_generalIO.SelectedIndex;
            }
            cmpAxis = (ushort)comboBox_latchAxis.SelectedIndex;

            RTMC.MC_StopCapture(cardConnectNo, //停止锁存器
                cmpObject,
                cmpAxis,
                cmpChan);

            RTMC.MC_StopEmgImd(cardConnectNo);//如果有轴在运动中就停止
        }

        /// <summary>
        /// 重启锁存器按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_restartLatch_Click(object sender, EventArgs e)
        {
            UInt16 cmpObject = 0;
            UInt16 cmpChan = 0;
            UInt16 cmpAxis = 0;

            if (radioButton_home.Checked) cmpObject = 0;
            else if (radioButton_EZ.Checked) cmpObject = 1;
            else if (radioButton_HSInput.Checked)
            {
                cmpObject = 2;
                cmpChan = (ushort)comboBox_HSChan.SelectedIndex;
            }
            else if (radioButton_generalInput.Checked)
            {
                cmpObject = 3;
                cmpChan = (ushort)comboBox_generalIO.SelectedIndex;
            }
            cmpAxis = (ushort)comboBox_latchAxis.SelectedIndex;

            RTMC.MC_ReStartCapture(cardConnectNo, //重启锁存器
                cmpObject,
                cmpAxis,
                cmpChan);
        }

        /// <summary>
        /// 定时器更新界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            #region 轴状态
            ushort axis0 = ((ushort)comboBox_latchAxis.SelectedIndex);
            groupBox4.Text = "轴" + axis0;
            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;
            }
            #endregion

            #region IO状态
            uint inputIndex = Convert.ToUInt32(numericUpDown1.Value);
            byte ioState = 0;
            RTMC.MC_GetInIoBit(cardConnectNo, inputIndex, ref ioState);
            if (ioState == 0) pictureBox1.BackColor = Color.Green;
            else pictureBox1.BackColor = Color.Red;

            #endregion

            

        }

        /// <summary>
        /// 获取锁存结果按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            #region 锁存器结果
            UInt16 cmpObject = 0;
            UInt16 cmpChan = 0;
            UInt16 cmpAxis = 0;

            if (radioButton_home.Checked) cmpObject = 0;
            else if (radioButton_EZ.Checked) cmpObject = 1;
            else if (radioButton_HSInput.Checked)
            {
                cmpObject = 2;
                cmpChan = (ushort)comboBox_HSChan.SelectedIndex;
            }
            else if (radioButton_generalInput.Checked)
            {
                cmpObject = 3;
                cmpChan = (ushort)comboBox_generalIO.SelectedIndex;
            }
            cmpAxis = (ushort)comboBox_latchAxis.SelectedIndex;

            uint cmpCount = 0;
            RTMC.MC_GetCaptureCount(cardConnectNo, //获取锁存到的位置个数
                cmpObject,
                cmpAxis,
                cmpChan,
                ref cmpCount);

            ushort realCount = 0;
            double[] posArr = new double[255];
            RTMC.MC_GetCapturePos(cardConnectNo,
                cmpObject,
                cmpAxis,
                cmpChan,
                (ushort)cmpCount,
                ref realCount,
                posArr);


            string cmpObjectStr = "";
            string cmpChanStr = "";
            string cmpAxisStr = "";
            if (radioButton_home.Checked) { cmpObjectStr = radioButton_home.Text; }
            else if (radioButton_EZ.Checked) { cmpObjectStr = radioButton_EZ.Text; }
            else if (radioButton_HSInput.Checked)
            {
                cmpObjectStr = radioButton_HSInput.Text;
                cmpChanStr = comboBox_HSChan.Text;

            }
            else if (radioButton_generalInput.Checked)
            {
                cmpObjectStr = radioButton_generalInput.Text;
                cmpChanStr = comboBox_generalIO.Text;
            }
            cmpAxisStr = comboBox_latchAxis.Text;
            for (int i = 0; i < realCount; i++)
            {
                textBox4.AppendText(cmpObjectStr + cmpAxisStr + cmpChanStr + posArr[i] + "\n"); //锁存结果显示在Textbox
            }

            #endregion
        }


        /// <summary>
        /// 锁存结果清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ushort axis0 = (ushort)comboBox_latchAxis.SelectedIndex; //轴位置清除
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axis0, 0);

            textBox4.Clear(); //文本框清除
        }
    }
}
