﻿using System.Globalization;
using System.Text;

namespace Counter
{
    public partial class Comp : Form
    {
        //---------------------------------------------------------
        // Global variable
        //---------------------------------------------------------
        Int32 Ret;                                          // Return value
        StringBuilder szError = new StringBuilder(256);     // Error string
        string szReturnCode = new string(' ', 256);         // The string for error
        string szText = new string(' ', 100);               // Temporary string
        static Int32[] CompValue = new Int32[8];            // Comparison value
        static Int32 PulseWidth = 0;                        // Pulse width
                                                            // Additional data
        TextBox[] m_Comp = new TextBox[8];                  // Controls of comparison value

        public Comp()
        {
            InitializeComponent();
        }

        //---------------------------------------------------------
        // Initialize dialog
        //---------------------------------------------------------
        private void Comp_Load(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // Initialize screen items
            //-----------------------------------
            short i;
            rdoComp0.Checked = true;
            m_Comp[0] = txtComp0;
            m_Comp[1] = txtComp1;
            m_Comp[2] = txtComp2;
            m_Comp[3] = txtComp3;
            m_Comp[4] = txtComp4;
            m_Comp[5] = txtComp5;
            m_Comp[6] = txtComp6;
            m_Comp[7] = txtComp7;
            for (i = 0; i < 8; i++)
            {
                if (i >= CNT.Common.ChannelNum)
                {
                    m_Comp[i].Enabled = false;
                }
            }
            //-----------------------------------
            // Get and display the count-match pulse
            //-----------------------------------
            Int32 PlsWidth;
            PlsWidth = 0;
            Ret = CNT.Ccapcnt.CntGetPulseWidth(CNT.Common.Id, 0, ref PlsWidth);
            if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
            {
                PlsWidth = 0;
            }
            szText = string.Format("{0:X}", PlsWidth);
            txtPulse.Text = szText;
            //-----------------------------------
            // Get and display count comparison value
            //-----------------------------------
            for (i = 0; i < CNT.Common.ChannelNum; i++)
            {
                CompValue[i] = 0;
                Ret = CNT.Ccapcnt.CntGetCountMatchReg(CNT.Common.Id, i, 0, ref CompValue[i]);
                szText = string.Format("{0:d}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // Process OK button
        //---------------------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            short i;
            //-----------------------------------
            // Get setting value from screen
            //-----------------------------------
            for (i = 0; i < 8; i++)
            {
                szText = m_Comp[i].Text;
                try
                {
                    if (rdoComp0.Checked == true)
                    {
                        CompValue[i] = Int32.Parse(szText, NumberStyles.Integer);
                    }
                    else
                    {
                        CompValue[i] = Int32.Parse(szText, NumberStyles.HexNumber);
                    }
                }
                catch (System.FormatException)
                {
                    CompValue[i] = 0;
                }
            }
            szText = txtPulse.Text;
            try
            {
                PulseWidth = Int32.Parse(szText, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                PulseWidth = 0;
            }
            //-----------------------------------
            // Each channel
            //-----------------------------------
            for (i = 0; i < CNT.Common.ChannelNum; i++)
            {
                //-----------------------------------
                // Set comparison value
                //-----------------------------------
                Ret = CNT.Ccapcnt.CntSetCountMatchEvent(CNT.Common.Id, i, 0, CompValue[i], 1);
                //-----------------------------
                // Process error
                //-----------------------------
                if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
                {
                    CNT.Ccapcnt.CntGetErrorString(Ret, szError);
                    szReturnCode = "CntSetCountMatchEvent : " + szError;
                    MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
                    break;
                }
                //-----------------------------------
                // Set pulse width
                //-----------------------------------
                Ret = CNT.Ccapcnt.CntSetPulseWidth(CNT.Common.Id, i, PulseWidth);
                //-----------------------------
                // Process error
                //-----------------------------
                if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
                {
                    CNT.Ccapcnt.CntGetErrorString(Ret, szError);
                    szReturnCode = "CntSetPulseWidth : " + szError;
                    MessageBox.Show(szReturnCode, "Error", MessageBoxButtons.OK);
                    break;
                }
            }
            //-----------------------------------
            // Set pulse width
            //-----------------------------------
            this.Close();
        }

        //---------------------------------------------------------
        // Process Cancel button
        //---------------------------------------------------------
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        //---------------------------------------------------------
        // Change the display of the comparison value
        //---------------------------------------------------------
        // Dec
        private void rdoComp0_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //-----------------------------------
            // Refresh screen information
            //-----------------------------------
            short i;
            for (i = 0; i < 8; i++)
            {
                szText = string.Format("{0:d}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }

        // Hex
        private void rdoComp1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //-----------------------------------
            // Refresh screen information
            //-----------------------------------
            short i;
            for (i = 0; i < 8; i++)
            {
                szText = string.Format("{0:X8}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }
    }
}
