﻿using System.Globalization;
using System.Text;

namespace Counter
{
    public partial class Comp : Form
    {
        //---------------------------------------------------------
        // グローバル変数
        //---------------------------------------------------------
        Int32          Ret;                                    // 戻り値
        StringBuilder  szError = new StringBuilder(256);       // エラー文字列
        string         szReturnCode = new string(' ', 256);    // 表示エラー文字列
        string         szText = new string(' ', 100);          // テンポラリ文字列
        static Int32[] CompValue = new Int32[8];               // 比較値
        static Int32   PulseWidth = 0;                         // パルス長
        // 追加データ
        TextBox[]      m_Comp = new TextBox[8];                // 比較値コントロール

        public Comp()
        {
            InitializeComponent();
        }

        //---------------------------------------------------------
        // ダイアログ初期化
        //---------------------------------------------------------
        private void Comp_Load(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // 画面アイテムの初期化
            //-----------------------------------
            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 >= Common.ChannelNum)
                {
                    m_Comp[i].Enabled = false;
                }
            }
            //-----------------------------------
            // カウント一致パルスの取得と表示
            //-----------------------------------
            int PlsWidth;
            PlsWidth = 0;
            Ret = CNT.Ccapcnt.CntGetPulseWidth(Common.Id, 0, ref PlsWidth);
            if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
            {
                PlsWidth = 0;
            }
            szText = string.Format("{0:X}", PlsWidth);
            txtPulse.Text = szText;
            //-----------------------------------
            // カウント比較値の取得と表示
            //-----------------------------------
            for (i = 0; i < Common.ChannelNum; i++)
            {
                CompValue[i] = 0;
                Ret = CNT.Ccapcnt.CntGetCountMatchReg(Common.Id, i, 0, ref CompValue[i]);
                szText = string.Format("{0:d}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // OKボタンの処理
        //---------------------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            short i;
            //-----------------------------------
            // 画面から設定値取得
            //-----------------------------------
            for (i = 0; i < 8; i++)
            {
                szText = m_Comp[i].Text;
                try
                {
                    if (rdoComp0.Checked == true)
                    {
                        CompValue[i] = Int32.Parse(szText);
                    }
                    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;
            }
            //-----------------------------------
            // 各チャネル毎に
            //-----------------------------------
            for (i = 0; i < Common.ChannelNum; i++)
            {
                //-----------------------------------
                // 比較値設定
                //-----------------------------------
                Ret = CNT.Ccapcnt.CntSetCountMatchEvent(Common.Id, i, 0, CompValue[i], 1);
                //-----------------------------
                // エラー処理
                //-----------------------------
                if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
                {
                    CNT.Ccapcnt.CntGetErrorString(Ret, szError);
                    szReturnCode = "CntSetCountMatchEvent : " + szError;
                    MessageBox.Show(szReturnCode, "エラー", MessageBoxButtons.OK);
                    break;
                }
                //-----------------------------------
                // パルス長設定
                //-----------------------------------
                Ret = CNT.Ccapcnt.CntSetPulseWidth(Common.Id, i, PulseWidth);
                //-----------------------------
                // エラー処理
                //-----------------------------
                if (Ret != CNT.Ccapcnt.CNT_ERR_SUCCESS)
                {
                    CNT.Ccapcnt.CntGetErrorString(Ret, szError);
                    szReturnCode = "CntSetPulseWidth : " + szError;
                    MessageBox.Show(szReturnCode, "エラー", MessageBoxButtons.OK);
                    break;
                }
            }
            //-----------------------------------
            // パルス長設定
            //-----------------------------------
            this.Close();
        }

        //---------------------------------------------------------
        // キャンセルボタンの処理
        //---------------------------------------------------------
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        //---------------------------------------------------------
        // 比較値の表示変更
        //---------------------------------------------------------
        // 10進数
        private void rdoComp0_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //-----------------------------------
            // 画面情報を更新
            //-----------------------------------
            short i;
            for (i = 0; i < 8; i++)
            {
                szText = string.Format("{0:d}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }

        // 16進数
        private void rdoComp1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //-----------------------------------
            // 画面情報を更新
            //-----------------------------------
            short i;
            for (i = 0; i < 8; i++)
            {
                szText = string.Format("{0:X8}", CompValue[i]);
                m_Comp[i].Text = szText;
            }
        }
    }
}
