﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using CcntCs;

namespace Sampling
{
    public partial class ConditionDlg : Form
    {
        public struct StrToNum
        {                                                   // structure of condition and parameters
            public string Str;
            public short  Num;
            public StrToNum(string Str, short Num)
            {
                this.Str = Str;
                this.Num = Num;
            }
        };
        // Start Condition
        StrToNum[] StartTrgTable =
        {
        new StrToNum("ソフトウェアスタート",                 (short)CcntConst.CNTS_START_SOFT                ),
        new StrToNum("外部信号立ち上がり",                   (short)CcntConst.CNTS_START_EXT_RISE            ),
        new StrToNum("外部信号立ち下がり",                   (short)CcntConst.CNTS_START_EXT_FALL            ),
        new StrToNum("カウント一致CH0 比較レジスタ0",        (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0   ),
        new StrToNum("カウント一致CH1 比較レジスタ0",        (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0   ),
        new StrToNum("カウント一致CH2 比較レジスタ0",        (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0   ),
        new StrToNum("カウント一致CH3 比較レジスタ0",        (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0   ),
        new StrToNum("カウント一致CH0 比較レジスタ1",        (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1   ),
        new StrToNum("カウント一致CH1 比較レジスタ1",        (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1   ),
        new StrToNum("カウント一致CH2 比較レジスタ1",        (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1   ),
        new StrToNum("カウント一致CH3 比較レジスタ1",        (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1   ),
        new StrToNum("End",                                  0                                               )
        };
        // Clock Condition
        StrToNum[] ClockTrgTable =
        {
            new StrToNum("内部クロック",                     (short)CcntConst.CNTS_CLOCK_CLOCK               ),
            new StrToNum("外部クロック",                     (short)CcntConst.CNTS_CLOCK_EXT_TRG             ),
            new StrToNum("End",                              0                                               )
        };
        // Stop Condition
        StrToNum[] StopTrgTable =
        {
            new StrToNum("ソフトウェアストップ",             (short)CcntConst.CNTS_STOP_SOFT                 ),
            new StrToNum("外部信号立ち上がり",               (short)CcntConst.CNTS_STOP_EXT_RISE             ),
            new StrToNum("外部信号立ち下がり",               (short)CcntConst.CNTS_STOP_EXT_FALL             ),
            new StrToNum("指定個数転送完了",                 (short)CcntConst.CNTS_STOP_NUM                  ),
            new StrToNum("カウント一致CH0 比較レジスタ0",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0    ),
            new StrToNum("カウント一致CH1 比較レジスタ0",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0    ),
            new StrToNum("カウント一致CH2 比較レジスタ0",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0    ),
            new StrToNum("カウント一致CH3 比較レジスタ0",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0    ),
            new StrToNum("カウント一致CH0 比較レジスタ1",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1    ),
            new StrToNum("カウント一致CH1 比較レジスタ1",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1    ),
            new StrToNum("カウント一致CH2 比較レジスタ1",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1    ),
            new StrToNum("カウント一致CH3 比較レジスタ1",    (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1    ),
            new StrToNum("End",                              0                                               )
        };

        static uint        StartComp      = 0x100;
        static uint        StopComp       = 0x200;

        static string      szStart        = "ソフトウェアスタート";
        static string      szClock        = "内部クロック";
        static string      szStop         = "ソフトウェアストップ";
        static uint        InternalClock  = 100000;
        static uint        StopNumber     = 1000;
        public static uint NumberOfData   = 500;
        static short       StartCountMode = (short)CcntConst.CNTS_START_CNT_WITH_SAMP;
        static short       StopCountMode  = (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP;

        Ccnt               cnt            = new Ccnt();

        public ConditionDlg()
        {
            InitializeComponent();
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void ConditionDlg_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // 表示初期化
            //--------------------------------
            uint i;
            string szBuf = new string(' ', 256);

            for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++)
            {
                cboStartTrg.Items.Add(StartTrgTable[i].Str);
            }
            for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++)
            {
                cboClockTrg.Items.Add(ClockTrgTable[i].Str);
            }
            for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++)
            {
                cboStopTrg.Items.Add(StopTrgTable[i].Str);
            }

            cboStartTrg.Text = szStart;
            cboClockTrg.Text = szClock;
            cboStopTrg.Text  = szStop;

            if (StartCountMode == (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP)
            {
                rdoStartCntNotWithSamp.Checked = true;
                rdoStartCntWithSamp.Checked    = false;
            }
            else
            {
                rdoStartCntNotWithSamp.Checked = false;
                rdoStartCntWithSamp.Checked    = true;
            }
            if (StopCountMode == (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP)
            {
                rdoStopCntNotWithSamp.Checked = true;
                rdoStopCntWithSamp.Checked    = false;
            }
            else
            {
                rdoStopCntNotWithSamp.Checked = false;
                rdoStopCntWithSamp.Checked    = true;
            }

            szBuf = string.Format("{0:X}", StartComp);
            txtCompStart.Text = szBuf;
            txtInternalClock.Text = InternalClock.ToString();
            txtStopNum.Text = StopNumber.ToString();
            txtNumEvent.Text = NumberOfData.ToString();
            szBuf = string.Format("{0:X}", StopComp);
            txtCompStop.Text = szBuf;

            cboStartTrg_SelectedIndexChanged(cboStartTrg, new System.EventArgs());
            cboClockTrg_SelectedIndexChanged(cboClockTrg, new System.EventArgs());
            cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
        }

        //===================================================
        // スタート条件設定変更
        //===================================================
        private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStartTrg.Text;
            if (szBuf.StartsWith("カウント一致"))
            {
                txtCompStart.Enabled = true;
            }
            else
            {
                txtCompStart.Enabled = false;
            }
        }

        //===================================================
        // クロック条件設定変更
        //===================================================
        private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            //--------------------------------
            // 内部クロック値の有効/無効を切り替え
            //--------------------------------
            szBuf = cboClockTrg.Text;

            if (string.Compare(szBuf, "内部クロック") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // ストップ条件設定変更
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStopTrg.Text;
            if (szBuf.StartsWith("カウント一致"))
            {
                txtCompStop.Enabled = true;
            }
            else
            {
                txtCompStop.Enabled = false;
            }
            if (string.Compare(szBuf, "指定個数転送完了") == 0)
            {
                txtStopNum.Enabled = true;
            }
            else
            {
                txtStopNum.Enabled = false;
            }
        }

        //===================================================
        // OKボタン処理
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string error_string = new string(' ', 256);
            string szTmp = new string(' ', 100);
            short  StartTrg;
            short  ClockTrg;
            short  StopTrg;
            short  i;
            int    ret;
            string strMsg;
            short  CompChNo;
            short  CompRegNo;

            //--------------------------------
            // 表示データを取得
            //--------------------------------
            szStart = cboStartTrg.Text;
            szClock = cboClockTrg.Text;
            szStop  = cboStopTrg.Text;

            if (rdoStartCntNotWithSamp.Checked == true)
            {
                StartCountMode = (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP;
            }
            else
            {
                StartCountMode = (short)CcntConst.CNTS_START_CNT_WITH_SAMP;
            }
            if (rdoStopCntNotWithSamp.Checked == true)
            {
                StopCountMode = (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP;
            }
            else
            {
                StopCountMode = (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP;
            }

            szTmp = txtCompStart.Text;
            try
            {
                StartComp = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StartComp = 0x100;
            }
            szTmp = txtCompStop.Text;
            try
            {
                StopComp = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StopComp = 0x200;
            }
            szTmp = txtInternalClock.Text;
            try
            {
                InternalClock = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                InternalClock = 100000;
            }
            szTmp = txtStopNum.Text;
            try
            {
                StopNumber = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                StopNumber = 1000;
            }
            szTmp = txtNumEvent.Text;
            try
            {
                NumberOfData = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                NumberOfData = 500;
            }

            //--------------------------------
            // 設定値を数値に変換
            //--------------------------------
            StartTrg = 0;
            for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStart, StartTrgTable[i].Str) == 0)
                {
                    StartTrg = StartTrgTable[i].Num;
                    break;
                }
            }
            ClockTrg = 0;
            for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szClock, ClockTrgTable[i].Str) == 0)
                {
                    ClockTrg = ClockTrgTable[i].Num;
                    break;
                }
            }
            StopTrg = 0;
            for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStop, StopTrgTable[i].Str) == 0)
                {
                    StopTrg = StopTrgTable[i].Num;
                    break;
                }
            }
            //--------------------------------
            // サンプリング条件を設定
            //--------------------------------
            // スタンドアローン
            ret = cnt.SetStandAlone(Form1.Id);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetStandAlone ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スタート条件設定
            ret = cnt.SetSamplingStartTrigger(Form1.Id, StartTrg, StartCountMode, Form1.CHANNEL_NUM);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            switch (StartTrg)
            {
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0: CompChNo = 0;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0: CompChNo = 1;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0: CompChNo = 2;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0: CompChNo = 3;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG0: CompChNo = 4;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG0: CompChNo = 5;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG0: CompChNo = 6;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG0: CompChNo = 7;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1: CompChNo = 0;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1: CompChNo = 1;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1: CompChNo = 2;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1: CompChNo = 3;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG1: CompChNo = 4;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG1: CompChNo = 5;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG1: CompChNo = 6;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG1: CompChNo = 7;     CompRegNo = 1;     break;
                default:                                            CompChNo = 0xfff; CompRegNo = 0xfff; break;
            }
            if (CompChNo != 0xfff && CompRegNo != 0xfff)
            {
                ret = cnt.NotifyCountUp(Form1.Id, CompChNo, CompRegNo, StartComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            // クロック条件設定
            ret = cnt.SetSamplingClockTrigger(Form1.Id, ClockTrg);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 内部クロック値設定
            if (ClockTrg == (short)CcntConst.CNTS_CLOCK_CLOCK)
            {
                ret = cnt.SetSamplingInternalClock(Form1.Id, InternalClock, (short)CcntConst.CNTS_TIM_UNIT_NS);
                if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                    return;
                }
            }

            // ストップ条件設定
            ret = cnt.SetSamplingStopTrigger(Form1.Id, StopTrg, StopCountMode, Form1.CHANNEL_NUM);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            switch (StopTrg)
            {
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0: CompChNo = 0;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0: CompChNo = 1;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0: CompChNo = 2;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0: CompChNo = 3;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG0: CompChNo = 4;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG0: CompChNo = 5;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG0: CompChNo = 6;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG0: CompChNo = 7;     CompRegNo = 0;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1: CompChNo = 0;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1: CompChNo = 1;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1: CompChNo = 2;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1: CompChNo = 3;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG1: CompChNo = 4;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG1: CompChNo = 5;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG1: CompChNo = 6;     CompRegNo = 1;     break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG1: CompChNo = 7;     CompRegNo = 1;     break;
                default:                                           CompChNo = 0xfff; CompRegNo = 0xfff; break;
            }
            if (CompChNo != 0xfff && CompRegNo != 0xfff)
            {
                ret = cnt.NotifyCountUp(Form1.Id, CompChNo, CompRegNo, StopComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            // 停止個数設定
            if (StopTrg == (short)CcntConst.CNTS_STOP_NUM)
            {
                ret = cnt.SetSamplingStopNumber(Form1.Id, StopNumber);
                if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                    return;
                }
            }
            this.Close();
        }

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