﻿// 通常版カウンタ用サンプル
using CcntCs;

namespace Counter
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.TextBox[] m_CH = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[] m_Status = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[] m_Preset = new System.Windows.Forms.TextBox[8];

        /// <summary>
        /// クラスCcntインスタンス作成
        /// </summary>
        Ccnt cnt = new Ccnt();

        public static IntPtr m_Handle;
        public static short  m_Id;
        public static short  m_ChannelNum;
        public uint[]        PresetData = new uint[8] { 0, 100, 200, 300, 400, 500, 600, 700 };
        public int           MatchCount;

        public Form1()
        {
            InitializeComponent();

            m_CH[0] = textBox_CountValue_Channel0;
            m_CH[1] = textBox_CountValue_Channel1;
            m_CH[2] = textBox_CountValue_Channel2;
            m_CH[3] = textBox_CountValue_Channel3;
            m_CH[4] = textBox_CountValue_Channel4;
            m_CH[5] = textBox_CountValue_Channel5;
            m_CH[6] = textBox_CountValue_Channel6;
            m_CH[7] = textBox_CountValue_Channel7;

            m_Status[0] = textBox_Status_Channel0;
            m_Status[1] = textBox_Status_Channel1;
            m_Status[2] = textBox_Status_Channel2;
            m_Status[3] = textBox_Status_Channel3;
            m_Status[4] = textBox_Status_Channel4;
            m_Status[5] = textBox_Status_Channel5;
            m_Status[6] = textBox_Status_Channel6;
            m_Status[7] = textBox_Status_Channel7;

            m_Preset[0] = textBox_PresetValue_Channel0;
            m_Preset[1] = textBox_PresetValue_Channel1;
            m_Preset[2] = textBox_PresetValue_Channel2;
            m_Preset[3] = textBox_PresetValue_Channel3;
            m_Preset[4] = textBox_PresetValue_Channel4;
            m_Preset[5] = textBox_PresetValue_Channel5;
            m_Preset[6] = textBox_PresetValue_Channel6;
            m_Preset[7] = textBox_PresetValue_Channel7;

            m_Handle = this.Handle;
        }

        private void radioButton_PresetValue_Dec_CheckedChanged(object sender, System.EventArgs e)
        {

            for (short ChNo = 0; ChNo < 8; ChNo++)
            {
                m_Preset[ChNo].Text = System.Convert.ToString(PresetData[ChNo]);
            }
        }

        private void radioButton_PresetValue_Hex_CheckedChanged(object sender, System.EventArgs e)
        {
            for (short ChNo = 0; ChNo < 8; ChNo++)
            {
                m_Preset[ChNo].Text = System.Convert.ToString(PresetData[ChNo], 16);
            }
        }

        private void button_CntInit_Click(object sender, System.EventArgs e)
        {
            // 初期化処理
            int Ret;

            Ret = cnt.Init(textBox_DeviceName.Text, out m_Id);

            string ErrorString;
            cnt.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != 0)
            {
                return;
            }
            //-----------------------------
            // 総チャネル数を取得
            //-----------------------------
            Ret = cnt.GetMaxChannels(Form1.m_Id, out m_ChannelNum);

            cnt.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != 0)
            {
                m_ChannelNum = 0;
                return;
            }
            // このサンプルは8ポートを超えない
            if (m_ChannelNum > 8)
            {
                m_ChannelNum = 8;
            }
            //-----------------------------
            // カウンタ、ステータス、プリセット
            // 表示の無効化
            //-----------------------------
            for (short ChNo = m_ChannelNum; ChNo < 8; ChNo++)
            {
                m_CH[ChNo].Enabled     = false;
                m_Status[ChNo].Enabled = false;
                m_Preset[ChNo].Enabled = false;
            }

            MatchCount = 0;
        }

        private void button_ModeSet_Click(object sender, System.EventArgs e)
        {
            //---------------------------------------------
            // モード設定 ダイアログを表示
            //---------------------------------------------
            Mode ModeDlg = new Mode();
            ModeDlg.ShowDialog();
            ModeDlg.Dispose();
        }

        private void button_ComparisonSet_Click(object sender, System.EventArgs e)
        {
            //---------------------------------------------
            // 比較設定 ダイアログを表示
            //---------------------------------------------
            Comp ComparisonSetDlg = new Comp();
            ComparisonSetDlg.ShowDialog();
            ComparisonSetDlg.Dispose();
        }

        private void button_CntStartCount_Click(object sender, System.EventArgs e)
        {
            short[] ChNoArray = new short[8];
            int     Ret;
            string  ErrorString;

            //-----------------------------
            // パラメータの設定
            //-----------------------------
            for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
            {
                ChNoArray[ChNo] = ChNo;
            }
            //-----------------------------
            // カウンタスタート
            //-----------------------------
            Ret = cnt.StartCount(m_Id, ChNoArray, m_ChannelNum);
            //-----------------------------
            // エラー処理
            //-----------------------------
            cnt.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // タイマを起動
            //-----------------------------
            timer1.Start();
        }

        private void button_CntPreset_Click(object sender, System.EventArgs e)
        {
            short[] ChNoArray = new short[8];
            int     Ret;
            string  ErrorString;

            //-----------------------------
            // パラメータの設定
            //-----------------------------
            for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
            {
                ChNoArray[ChNo] = ChNo;

                try
                {
                    if (radioButton_PresetValue_Dec.Checked == true)
                    {
                        PresetData[ChNo] = System.Convert.ToUInt32(m_Preset[ChNo].Text);
                    }
                    else
                    {
                        PresetData[ChNo] = System.Convert.ToUInt32(m_Preset[ChNo].Text, 16);
                    }
                }
                catch
                {
                    PresetData[ChNo] = 0;
                }
            }
            //-----------------------------
            // プリセット
            //-----------------------------
            Ret = cnt.Preset(m_Id, ChNoArray, m_ChannelNum, PresetData);
            //-----------------------------
            // エラー処理
            //-----------------------------
            cnt.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_CntStopCount_Click(object sender, System.EventArgs e)
        {
            short[] ChNoArray = new short[8];
            int     Ret;
            string  ErrorString;

            //-----------------------------
            // パラメータの設定
            //-----------------------------
            for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
            {
                ChNoArray[ChNo] = ChNo;
            }
            //-----------------------------
            // カウンタストップ
            //-----------------------------
            Ret = cnt.StopCount(m_Id, ChNoArray, m_ChannelNum);
            //-----------------------------
            // エラー処理
            //-----------------------------
            cnt.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // タイマを停止
            //-----------------------------
            timer1.Stop();
        }

        private void button_CntExit_Click(object sender, System.EventArgs e)
        {

            int    Ret;
            string ErrorString;

            Ret = cnt.Exit(m_Id);

            cnt.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();
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            short[] ChNoArray = new short[8];
            uint[]  CountValue = new uint[8];
            int     Ret;
            short   Sts;

            //-----------------------------
            // パラメータを設定
            //-----------------------------
            for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
            {
                ChNoArray[ChNo] = ChNo;
            }
            //-----------------------------
            // カウント値を取得
            //-----------------------------
            Ret = cnt.ReadCount(m_Id, ChNoArray, m_ChannelNum, CountValue);
            if (Ret == (int)CcntConst.CNT_ERR_SUCCESS)
            {
                for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
                {
                    if (radioButtonCountValue_Dec.Checked == true)
                    {
                        m_CH[ChNo].Text = System.Convert.ToString(CountValue[ChNo]);
                    }
                    else
                    {
                        m_CH[ChNo].Text = System.Convert.ToString(CountValue[ChNo], 16);
                    }
                }
            }
            //-----------------------------
            // ステータスを取得
            //-----------------------------
            for (short ChNo = 0; ChNo < m_ChannelNum; ChNo++)
            {
                Ret = cnt.ReadStatus(m_Id, ChNo, out Sts);
                if (Ret == (int)CcntConst.CNT_ERR_SUCCESS)
                {
                    m_Status[ChNo].Text = System.Convert.ToString(Sts, 16);
                }
            }
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case ((int)CcntConst.CNTM_COUNTUP_CH0):
                case ((int)CcntConst.CNTM_COUNTUP_CH1):
                case ((int)CcntConst.CNTM_COUNTUP_CH2):
                case ((int)CcntConst.CNTM_COUNTUP_CH3):
                case ((int)CcntConst.CNTM_COUNTUP_CH4):
                case ((int)CcntConst.CNTM_COUNTUP_CH5):
                case ((int)CcntConst.CNTM_COUNTUP_CH6):
                case ((int)CcntConst.CNTM_COUNTUP_CH7):
                    MatchCount++;
                    if (((m.WParam.ToInt32() & 0x0ffff0000) >> 16) == (int)CcntConst.CNT_DIR_UP)
                    {
                        textBox_Comparison_result.Text =
                            "カウント一致!! CH" + System.Convert.ToString(m.Msg & 0x07) +
                            " Value = " + Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff) +
                            " UP Match Count = " + System.Convert.ToString(MatchCount);
                    }
                    else
                    {
                        textBox_Comparison_result.Text =
                            "カウント一致!! CH" + System.Convert.ToString(m.Msg & 0x07) +
                            " Value = " + Convert.ToUInt32(m.LParam.ToInt64() & 0x00000000ffffffff) +
                            " DOWN Match Count = " + System.Convert.ToString(MatchCount);
                    }
                    break;
            }
            base.WndProc(ref m);
        }
    }
}