﻿using CSmcCs;

namespace Event
{
    public partial class Form1 : Form
    {
        /////////////////////////////////////////////////
        // SMC access value
        /////////////////////////////////////////////////
        int dwRet;
        string ErrorString = "";
        short Id = -1;
        short AxisNo;
        int lOutPulse;
        int lCountPulse;
        string szText = "";
        string szCaption = "";
        int wBankNo;
        CSmc Smc = new CSmc();

        const int WM_STOP_EVENT = (int)CSmcConst.CSMC_MESSAGE;
        const int WM_OUTPULSE_EVENT = (int)CSmcConst.CSMC_MESSAGE + 1;
        const int WM_ENCODER_EVENT = (int)CSmcConst.CSMC_MESSAGE + 2;
        const int WM_BANK_EVENT = (int)CSmcConst.CSMC_MESSAGE + 3;

        public Form1()
        {
            InitializeComponent();
        }

        private void Event_Load(object sender, System.EventArgs e)
        {
            AxisNo = 1;

            // AxisNo ComboBox
            string wStr;
            for (int i = 0; i < 8; i++)
            {
                wStr = string.Format("{0:d}", i + 1);
                cboTargetAxis.Items.Add(wStr);
            }

            // ComboBox Initialise
            cboTargetAxis.SelectedIndex = AxisNo - 1;

            //------------
            // Initialize
            //------------
            txtDeviceName.Text = "SMC000";

            //----------------------
            // Set OutPulse to 500
            //----------------------
            lOutPulse = 500;
            wStr = string.Format("{0:d}", lOutPulse);
            txtOutPulse.Text = wStr;

            //---------------------------
            // Set Encoder Pulse to 500
            //---------------------------
            lCountPulse = 500;
            wStr = string.Format("{0:d}", lCountPulse);
            txtCountPulse.Text = wStr;

            //---------------------------
            // Set wBankNo to 3
            //---------------------------
            wBankNo = 3;
            wStr = string.Format("{0:d}", wBankNo);
            txtBankNo.Text = wStr;
        }

        private void DispComment(string func, string msg)
        {
            lblComment.Text = string.Format("{0}: {1}", func, msg);
        }

        private void chkStop_CheckedChanged(object sender, System.EventArgs e)
        {
            byte bEventMode;
            if (chkStop.Checked == true)
            {
                bEventMode = (byte)CSmcConst.CSMC_ENABLE;
            }
            else
            {
                bEventMode = (byte)CSmcConst.CSMC_DISABLE;
            }
            //----------------------------------
            // Set Event for StopMotion to Driver
            //----------------------------------
            dwRet = Smc.WStopEvent(Id, AxisNo, this.Handle, bEventMode);
            Smc.WGetErrorString(dwRet, out ErrorString);
            DispComment("SmcWStopEvent", ErrorString);
        }

        private void chkBank_CheckedChanged(object sender, System.EventArgs e)
        {
            byte bEventMode;
            if (chkBank.Checked == true)
            {
                bEventMode = (byte)CSmcConst.CSMC_ENABLE;
            }
            else
            {
                bEventMode = (byte)CSmcConst.CSMC_DISABLE;
            }
            try
            {
                wBankNo = int.Parse(txtBankNo.Text);
            }
            catch (FormatException)
            {
                wBankNo = 0;
            }

            //----------------------------------
            // Set Event for StopMotion to Driver
            //----------------------------------
            dwRet = Smc.WBankEvent(Id, AxisNo, this.Handle, bEventMode, (short)wBankNo);
            Smc.WGetErrorString(dwRet, out ErrorString);
            DispComment("SmcWBankEvent", ErrorString);
        }

        private void chkOutPulse_CheckedChanged(object sender, System.EventArgs e)
        {
            byte bEventMode;
            try
            {
                lOutPulse = int.Parse(txtOutPulse.Text);
            }
            catch (FormatException)
            {
                lOutPulse = 0;
            }
            if (chkOutPulse.Checked == true)
            {
                bEventMode = (byte)CSmcConst.CSMC_ENABLE;
            }
            else
            {
                bEventMode = (byte)CSmcConst.CSMC_DISABLE;
            }
            //----------------------------------
            // Set Event for OutPulse to Driver
            //----------------------------------
            dwRet = Smc.WCountEvent(Id, AxisNo, this.Handle, bEventMode, (short)CSmcConst.CSMC_OUTPULSE, lOutPulse);
            Smc.WGetErrorString(dwRet, out ErrorString);
            DispComment("SmcWCountEvent", ErrorString);
        }

        private void chkCountPulse_CheckedChanged(object sender, System.EventArgs e)
        {
            byte bEventMode;
            try
            {
                lCountPulse = int.Parse(txtCountPulse.Text);
            }
            catch (FormatException)
            {
                lCountPulse = 0;
            }

            if (chkCountPulse.Checked == true)
            {
                bEventMode = (byte)CSmcConst.CSMC_ENABLE;
            }
            else
            {
                bEventMode = (byte)CSmcConst.CSMC_DISABLE;
            }

            //----------------------------------
            // Set Event for Encoder to Driver
            //----------------------------------
            dwRet = Smc.WCountEvent(Id, AxisNo, this.Handle, bEventMode, (short)CSmcConst.CSMC_ENCODER, lCountPulse);
            Smc.WGetErrorString(dwRet, out ErrorString);
            DispComment("SmcWCountEvent", ErrorString);
        }

        private void cboTargetAxis_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            AxisNo = (short)(cboTargetAxis.SelectedIndex + 1);
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            Smc.WExit(Id);

            this.Close();
        }

        private void txtOutPulse_TextChanged(object sender, System.EventArgs e)
        {
            chkOutPulse_CheckedChanged(chkOutPulse, new System.EventArgs());
        }

        private void txtCountPulse_TextChanged(object sender, System.EventArgs e)
        {
            chkCountPulse_CheckedChanged(chkCountPulse, new System.EventArgs());
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_STOP_EVENT: // Stop transfer by specified times
                    Id = (short)(m.WParam.ToInt32() & 0x0000ffff);

                    szText = string.Format("AxisNo {0:d} was stop.", m.LParam);
                    szCaption = "Stop Event";
                    MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
                    break;
                case WM_BANK_EVENT:     // Transfer stopped
                    Id = (short)(m.WParam.ToInt32() & 0x0000ffff);

                    szText = string.Format("BankNo is {0:d}:", wBankNo);
                    szCaption = "Bank Event";
                    MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
                    break;
                case WM_OUTPULSE_EVENT:
                    Id = (short)(m.WParam.ToInt32() & 0x0000ffff);

                    szText = string.Format("OutPulse is {0:d}.", lOutPulse);
                    szCaption = "OutPulse Event";
                    MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
                    break;
                case WM_ENCODER_EVENT:
                    Id = (short)(m.WParam.ToInt32() & 0x0000ffff);

                    szText = string.Format("Encoder is {0:d}.", lCountPulse);
                    szCaption = "Encoder Event";
                    MessageBox.Show(szText, szCaption, MessageBoxButtons.OK);
                    break;
            }
            base.WndProc(ref m);
        }

        private void txtDeviceName_TextChanged(object sender, System.EventArgs e)
        {
            string wStr = "OK";

            if (Id != -1)
            {
                Smc.WExit(Id);
            }
            wStr = txtDeviceName.Text;
            dwRet = Smc.WInit(wStr, out Id);
            if (dwRet != 0)
            {
                Smc.WGetErrorString(dwRet, out ErrorString);
                DispComment("SmcWInit", ErrorString);
                Id = -1;
                return;
            }

            wStr = "OK";
            lblComment.Text = wStr;
        }
    }
}