﻿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 CdioCs;

namespace Sync
{
    public partial class MCondition : Form
    {
        struct StrToNum
        {                                                   // structure of condition and parameters
            public string Str;
            public short  Num;
            public short  ExtSig;
            public StrToNum(string Str, short Num, short ExtSig)
            {
                this.Str    = Str;
                this.Num    = Num;
                this.ExtSig = ExtSig;
            }
        };
        // Start Condition
        static StrToNum[] StartTrgTable =
        {
            new StrToNum("Software Start", (short)CdioConst.DIODM_START_SOFT,     (short)CdioConst.DIODM_EXT_START_SOFT_IN),
            new StrToNum("External UP",    (short)CdioConst.DIODM_START_EXT_RISE, (short)CdioConst.DIODM_EXT_START_EXT_RISE_IN),
            new StrToNum("External Down",  (short)CdioConst.DIODM_START_EXT_FALL, (short)CdioConst.DIODM_EXT_START_EXT_FALL_IN),
            new StrToNum("Pattern",        (short)CdioConst.DIODM_START_PATTERN,  (short)CdioConst.DIODM_EXT_START_PATTERN_IN),
            new StrToNum("End",            0,                                     0)
        };
        // Clock Condition
        static StrToNum[] ClockTrgTable =
        {
            new StrToNum("Internal Clock", (short)CdioConst.DIODM_CLK_CLOCK,      (short)CdioConst.DIODM_EXT_CLOCK_IN),
            new StrToNum("External Clock", (short)CdioConst.DIODM_CLK_EXT_TRG,    (short)CdioConst.DIODM_EXT_EXT_TRG_IN),
            new StrToNum("Hand Shake",     (short)CdioConst.DIODM_CLK_HANDSHAKE,  (short)CdioConst.DIODM_EXT_HANDSHAKE_IN),
            new StrToNum("End",            0,                                     0)
        };
        // Stop Condition
        static StrToNum[] StopTrgTable =
        {
            new StrToNum("Software Stop",  (short)CdioConst.DIODM_STOP_SOFT,      (short)CdioConst.DIODM_EXT_STOP_SOFT_IN),
            new StrToNum("External UP",    (short)CdioConst.DIODM_STOP_EXT_RISE,  (short)CdioConst.DIODM_EXT_STOP_EXT_RISE_IN),
            new StrToNum("External Down",  (short)CdioConst.DIODM_STOP_EXT_FALL,  (short)CdioConst.DIODM_EXT_STOP_EXT_FALL_IN),
            new StrToNum("Number",         (short)CdioConst.DIODM_STOP_NUM,       (short)CdioConst.DIODM_EXT_TRNSNUM_IN),
            new StrToNum("End",            0,                                     0)
        };

        Cdio                dio            = new Cdio();
        static string       szStart        = "Software Start";
        static string       szClock        = "Internal Clock";
        static string       szStop         = "Software Stop";
        static uint         Pattern        = 0x55;
        static uint         Mask           = 0xFF;
        static uint         InternalClock  = 1000;
        static uint         StopNumber     = 1000;
        public static uint  M_NumberOfData = 500;
        public static short M_IsRing       = (short)CdioConst.DIODM_WRITE_ONCE;

        public MCondition()
        {
            InitializeComponent();
        }

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

            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;

            strTmp = string.Format("{0:X}", Pattern);
            txtCmpPtn.Text = strTmp;
            strTmp = string.Format("{0:X}", Mask);
            txtPtnMask.Text = strTmp;
            strTmp = string.Format("{0:d}", InternalClock);
            txtInternalClock.Text = strTmp;
            strTmp = string.Format("{0:d}", StopNumber);
            txtStopNum.Text = strTmp;
            strTmp = string.Format("{0:d}", M_NumberOfData);
            txtNumEvent.Text = strTmp;
            if (M_IsRing == (short)CdioConst.DIODM_WRITE_ONCE)
            {
                rdoOnce.Checked = true;
                rdoRing.Checked = false;
            }
            else
            {
                rdoOnce.Checked = false;
                rdoRing.Checked = true;
            }

            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 szBuff = new string(' ', 100);
            //--------------------------------
            // Pattern と Maskの有効、無効を切り替え
            //--------------------------------
            szBuff = cboStartTrg.Text;
            if (string.Compare(szBuff, "Pattern") == 0)
            {
                txtCmpPtn.Enabled  = true;
                txtPtnMask.Enabled = true;
            }
            else
            {
                txtCmpPtn.Enabled  = false;
                txtPtnMask.Enabled = false;
            }
        }


        //===================================================
        // クロック条件設定変更
        //===================================================
        private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Internal Clockの有効、無効を切り替え
            //--------------------------------
            szBuff = cboClockTrg.Text;
            if (string.Compare(szBuff, "Internal Clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // ストップ条件設定変更
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Stop Numberの有効、無効を切り替え
            //--------------------------------
            szBuff = cboStopTrg.Text;
            if (string.Compare(szBuff, "Number") == 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  ExtSig1, ExtSig2, ExtSig3;
            short  i;
            short  Direction = (short)CdioConst.DIODM_DIR_IN;
            int    ret;
            string strMsg;

            //--------------------------------
            // 表示データを取得
            //--------------------------------
            szStart = cboStartTrg.Text;
            szClock = cboClockTrg.Text;
            szStop  = cboStopTrg.Text;
            szTmp   = txtCmpPtn.Text;
            try
            {
                Pattern = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                Pattern = 0x55;
            }
            szTmp = txtPtnMask.Text;
            try
            {
                Mask = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                Mask = 0xFF;
            }
            szTmp = txtInternalClock.Text;
            try
            {
                InternalClock = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                InternalClock = 1000;
            }
            szTmp = txtStopNum.Text;
            try
            {
                StopNumber = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                StopNumber = 1000;
            }
            szTmp = txtNumEvent.Text;
            try
            {
                M_NumberOfData = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                M_NumberOfData = 500;
            }
            if (rdoOnce.Checked == true)
            {
                M_IsRing = (short)CdioConst.DIODM_WRITE_ONCE;
            }
            else
            {
                M_IsRing = (short)CdioConst.DIODM_WRITE_RING;
            }
            //--------------------------------
            // 設定値を数値に変換
            //--------------------------------
            StartTrg = 0;
            ExtSig2  = 0;
            for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStart, StartTrgTable[i].Str) == 0)
                {
                    StartTrg = StartTrgTable[i].Num;
                    ExtSig2  = StartTrgTable[i].ExtSig;
                    break;
                }
            }
            ClockTrg = 0;
            ExtSig1  = 0;
            for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szClock, ClockTrgTable[i].Str) == 0)
                {
                    ClockTrg = ClockTrgTable[i].Num;
                    ExtSig1  = ClockTrgTable[i].ExtSig;
                    break;
                }
            }
            StopTrg = 0;
            ExtSig3 = 0;
            for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStop, StopTrgTable[i].Str) == 0)
                {
                    StopTrg = StopTrgTable[i].Num;
                    ExtSig3 = StopTrgTable[i].ExtSig;
                    break;
                }
            }
            //--------------------------------
            // サンプリング条件を設定
            //--------------------------------
            // 入出力方向設定(全点入力)
            ret = dio.DmSetDirection(Form1.M_Id, (short)CdioConst.PI_32);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetDirection ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // マスター設定
            ret = dio.DmSetMaster(Form1.M_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetMaster ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スタート条件設定
            ret = dio.DmSetStartTrigger(Form1.M_Id, Direction, StartTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // クロック条件設定
            ret = dio.DmSetClockTrigger(Form1.M_Id, Direction, ClockTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // ストップ条件設定
            ret = dio.DmSetStopTrigger(Form1.M_Id, Direction, StopTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // パターン/マスク
            ret = dio.DmSetStartPattern(Form1.M_Id, Pattern, Mask);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStartPattern ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 内部クロック値設定
            ret = dio.DmSetInternalClock(Form1.M_Id, Direction, InternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetInternalClock ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 停止個数設定
            ret = dio.DmSetStopNumber(Form1.M_Id, Direction, StopNumber);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStopNumber ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }

            this.Close();
        }

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