﻿using CdioCs;

namespace Trigger
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.TextBox[]  m_UpCountText   = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[]  m_DownCountText = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.CheckBox[] m_TriggerCheck  = new System.Windows.Forms.CheckBox[8];

        /// <summary>
        /// Create instance Cdio class
        /// </summary>
        Cdio dio = new Cdio();

        public short m_Id;
        public int[] m_UpCount   = new int[8];
        public int[] m_DownCount = new int[8];

        public Form1()
        {
            InitializeComponent();

            m_UpCountText[0] = textBox_UpCount_0;
            m_UpCountText[1] = textBox_UpCount_1;
            m_UpCountText[2] = textBox_UpCount_2;
            m_UpCountText[3] = textBox_UpCount_3;
            m_UpCountText[4] = textBox_UpCount_4;
            m_UpCountText[5] = textBox_UpCount_5;
            m_UpCountText[6] = textBox_UpCount_6;
            m_UpCountText[7] = textBox_UpCount_7;

            m_DownCountText[0] = textBox_DownCount_0;
            m_DownCountText[1] = textBox_DownCount_1;
            m_DownCountText[2] = textBox_DownCount_2;
            m_DownCountText[3] = textBox_DownCount_3;
            m_DownCountText[4] = textBox_DownCount_4;
            m_DownCountText[5] = textBox_DownCount_5;
            m_DownCountText[6] = textBox_DownCount_6;
            m_DownCountText[7] = textBox_DownCount_7;

            m_TriggerCheck[0] = checkBox_Trigger_0;
            m_TriggerCheck[1] = checkBox_Trigger_1;
            m_TriggerCheck[2] = checkBox_Trigger_2;
            m_TriggerCheck[3] = checkBox_Trigger_3;
            m_TriggerCheck[4] = checkBox_Trigger_4;
            m_TriggerCheck[5] = checkBox_Trigger_5;
            m_TriggerCheck[6] = checkBox_Trigger_6;
            m_TriggerCheck[7] = checkBox_Trigger_7;

            short i;
            for (i = 0; i < 8; i++)
                m_TriggerCheck[i].Checked = true;

            textBox_ReturnCode.Text = "";
        }

        private void button_DioInit_Click(object sender, System.EventArgs e)
        {
            // Initialization handling
            int Ret;
            Ret = dio.Init(textBox_DeviceName.Text, out m_Id);

            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_Trigger_Start_Click(object sender, System.EventArgs e)
        {
            int   Ret = 0;
            short TrgBit;
            short TrgKind;
            int   Tim;
            //-----------------------------
            // Parameters initialization
            //-----------------------------
            for (TrgBit = 0; TrgBit < 8; TrgBit++)
            {
                m_UpCount[TrgBit] = 0;      // Up counter
                m_DownCount[TrgBit] = 0;    // Down counter
            }
            //-----------------------------
            // Witch check
            //-----------------------------
            Tim = 100;                                                                  // Observe in cycle 100ms
            TrgKind = (short)CdioConst.DIO_TRG_RISE | (short)CdioConst.DIO_TRG_FALL;    // Observe both of Up/Down
            for (TrgBit = 0; TrgBit < 8; TrgBit++)
            {
                //-----------------------------
                // Trigger start
                //-----------------------------
                if (m_TriggerCheck[TrgBit].Checked == true)
                {
                    Ret = dio.NotifyTrg(m_Id, TrgBit, TrgKind, Tim, this.Handle);
                    if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
                    {
                        break;
                    }
                }
                //-----------------------------
                // Tigger stop
                //-----------------------------
                else
                {
                    Ret = dio.StopNotifyTrg(m_Id, TrgBit);
                    if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
                    {
                        break;
                    }
                }
            }
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void button_DioExit_Click(object sender, System.EventArgs e)
        {
            // Exit handling of device
            int Ret;
            Ret = dio.Exit(m_Id);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void button_Exit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case ((int)CdioConst.DIOM_TRIGGER):
                    int TrgBit = m.LParam.ToInt32() & 0x0000ffff;
                    int TrgKind = (int)((m.LParam.ToInt32() & 0x0ffff0000) >> 16);
                    if ((TrgKind & (int)CdioConst.DIO_TRG_RISE) == (int)CdioConst.DIO_TRG_RISE)
                    {
                        m_UpCount[TrgBit]++;
                    }
                    if ((TrgKind & (int)CdioConst.DIO_TRG_FALL) == (int)CdioConst.DIO_TRG_FALL)
                    {
                        m_DownCount[TrgBit]++;
                    }
                    m_UpCountText[TrgBit].Text = System.Convert.ToString(m_UpCount[TrgBit]);
                    m_DownCountText[TrgBit].Text = System.Convert.ToString(m_DownCount[TrgBit]);
                    break;
            }
            base.WndProc(ref m);
        }

        private void CheckProcess(short BitNo)
        {
            int   Ret = 0;
            int   Tim;                 // 100ms周期で監視
            short Kind;                // アップダウン両方を監視

            Tim = 100;
            Kind = (short)CdioConst.DIO_TRG_RISE | (short)CdioConst.DIO_TRG_FALL;

            // トリガ開始
            if (m_TriggerCheck[BitNo].Checked == true)
                Ret = dio.NotifyTrg(m_Id, BitNo, Kind, Tim, this.Handle);

            //またはトリガ停止
            else
                Ret = dio.StopNotifyTrg(m_Id, BitNo);


            //エラー処理
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void checkBox_Trigger_0_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(0);
        }

        private void checkBox_Trigger_1_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(1);
        }

        private void checkBox_Trigger_2_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(2);
        }

        private void checkBox_Trigger_3_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(3);
        }

        private void checkBox_Trigger_4_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(4);
        }

        private void checkBox_Trigger_5_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(5);
        }

        private void checkBox_Trigger_6_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(6);
        }

        private void checkBox_Trigger_7_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckProcess(7);
        }
    }
}