﻿using CdioCs;

namespace Interrupt
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.TextBox[] m_UpCountText = new System.Windows.Forms.TextBox[4];     // UpCounter Express
        private System.Windows.Forms.TextBox[] m_DownCountText = new System.Windows.Forms.TextBox[4];   // DownCounter Express

        Cdio dio = new Cdio();
        short m_Id;                     // Device ID
        int Ret;                        // Return Code
        string ErrorString = "";        // Error String
        int[] m_UpCount = new int[4];   // UpCounter
        int[] m_DownCount = new int[4]; // DownCounter

        public Form1()
        {
            InitializeComponent();

            //-----------------------------------
            // Screen Item Initialization
            //-----------------------------------
            txtDeviceName.Text = "DIO000";
            txtReturnCode.Text = "";
            // Up counter
            m_UpCountText[0] = txtUpCount_0;
            m_UpCountText[1] = txtUpCount_1;
            m_UpCountText[2] = txtUpCount_2;
            m_UpCountText[3] = txtUpCount_3;
            // Down counter
            m_DownCountText[0] = txtDownCount_0;
            m_DownCountText[1] = txtDownCount_1;
            m_DownCountText[2] = txtDownCount_2;
            m_DownCountText[3] = txtDownCount_3;

            rdoRise.Checked = true;
        }

        //---------------------------------------------------------
        // DioInit()
        //---------------------------------------------------------
        private void btnDioInit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Execute initialization function and get ID
            //-----------------------------
            Ret = dio.Init(txtDeviceName.Text, out m_Id);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(Ret, out ErrorString);
            txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }


        //---------------------------------------------------------
        // Interrupt observe start
        //---------------------------------------------------------
        private void btnIntStart_Click(object sender, System.EventArgs e)
        {
            short BitNo;
            short Kind;
            //-----------------------------
            // Parameters initialization
            //-----------------------------
            for (BitNo = 0; BitNo < 4; BitNo++)
            {
                m_UpCount[BitNo] = 0;       // Up counter
                m_DownCount[BitNo] = 0;     // Down counter
            }
            if (rdoRise.Checked == true)
            {
                Kind = (short)CdioConst.DIO_INT_RISE;
            }
            else
            {
                Kind = (short)CdioConst.DIO_INT_FALL;
            }
            //-----------------------------
            // Interrupt start
            //-----------------------------
            for (BitNo = 0; BitNo < 4; BitNo++)
            {
                Ret = dio.NotifyInterrupt(m_Id, BitNo, Kind, this.Handle);
                if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
                {
                    break;
                }
            }
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(Ret, out ErrorString);
            txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        //---------------------------------------------------------
        // Interrupt observe stop
        //---------------------------------------------------------
        private void btnIntStop_Click(object sender, System.EventArgs e)
        {
            short BitNo;
            for (BitNo = 0; BitNo < 4; BitNo++)
            {
                Ret = dio.NotifyInterrupt(m_Id, BitNo, (short)CdioConst.DIO_INT_NONE, this.Handle);
                if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
                {
                    break;
                }
            }
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(Ret, out ErrorString);
            txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        //---------------------------------------------------
        // DioExit()
        //---------------------------------------------------
        private void btnDioExit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Exit function execute
            //-----------------------------
            Ret = dio.Exit(m_Id);
            //-----------------------------
            // Error process
            //-----------------------------
            dio.GetErrorString(Ret, out ErrorString);
            txtReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        //---------------------------------------------------------
        // Dialog exit
        //---------------------------------------------------------
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Exit function execute
            //-----------------------------
            dio.Exit(m_Id);
            //-----------------------------
            // Dialog exit
            //-----------------------------
            this.Close();
        }

        //---------------------------------------------------------
        // Message handler
        //---------------------------------------------------------
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //-----------------------------
                // Message interpret
                //-----------------------------
                case ((int)CdioConst.DIOM_INTERRUPT):
                    int BitNo = m.LParam.ToInt32() & 0x0000ffff;
                    int Kind = (int)((m.LParam.ToInt32() & 0x0ffff0000) >> 16);
                    if ((Kind & (int)CdioConst.DIO_INT_RISE) == (int)CdioConst.DIO_INT_RISE)
                    {
                        m_UpCount[BitNo]++;
                    }
                    if ((Kind & (int)CdioConst.DIO_INT_FALL) == (int)CdioConst.DIO_INT_FALL)
                    {
                        m_DownCount[BitNo]++;
                    }
                    m_UpCountText[BitNo].Text = System.Convert.ToString(m_UpCount[BitNo]);
                    m_DownCountText[BitNo].Text = System.Convert.ToString(m_DownCount[BitNo]);
                    break;
            }
            base.WndProc(ref m);
        }
    }
}