﻿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 Sampling
{
    public partial class Condition : Form
    {
        Cdio dio = new Cdio();

        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
        static StrToNum[] stnStart =
        {
            new StrToNum("Software Start", (short)CdioConst.DIODM_START_SOFT),
            new StrToNum("External UP",    (short)CdioConst.DIODM_START_EXT_RISE),
            new StrToNum("External Down",  (short)CdioConst.DIODM_START_EXT_FALL),
            new StrToNum("Pattern",        (short)CdioConst.DIODM_START_PATTERN),
            new StrToNum("End",            0)
        };
        // Clock Condition
        static StrToNum[] stnClock =
        {
            new StrToNum("Internal Clock", (short)CdioConst.DIODM_CLK_CLOCK),
            new StrToNum("External Clock", (short)CdioConst.DIODM_CLK_EXT_TRG),
            new StrToNum("Hand Shake",     (short)CdioConst.DIODM_CLK_HANDSHAKE),
            new StrToNum("End",            0)
        };
        // Stop Condition
        static StrToNum[] stnStop =
        {
            new StrToNum("Software Stop",  (short)CdioConst.DIODM_STOP_SOFT),
            new StrToNum("External UP",    (short)CdioConst.DIODM_STOP_EXT_RISE),
            new StrToNum("External Down",  (short)CdioConst.DIODM_STOP_EXT_FALL),
            new StrToNum("Number",         (short)CdioConst.DIODM_STOP_NUM),
            new StrToNum("End",            0)
        };

        static string       szStart         = "Software Start";
        static string       szClock         = "Internal Clock";
        static string       szStop          = "Software Stop";
        static uint        dwPattern        = 0x55;
        static uint        dwMask           = 0xFF;
        static uint        dwInternalClock  = 1000;
        static uint        dwStopNumber     = 1000;
        public static uint dwNumberOfData   = 500;    // notification count

        public Condition()
        {
            InitializeComponent();
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void Condition_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // 表示初期化
            //--------------------------------
            uint i;
            for (i = 0; string.Compare(stnStart[i].Str, "End") != 0; i++)
            {
                cboStart.Items.Add(stnStart[i].Str);
            }
            for (i = 0; string.Compare(stnClock[i].Str, "End") != 0; i++)
            {
                cboClock.Items.Add(stnClock[i].Str);
            }
            for (i = 0; string.Compare(stnStop[i].Str, "End") != 0; i++)
            {
                cboStop.Items.Add(stnStop[i].Str);
            }

            cboStart.Text = szStart;
            cboClock.Text = szClock;
            cboStop.Text  = szStop;

            txtPattern.Text       = string.Format("{0:X}", dwPattern);
            txtMask.Text          = string.Format("{0:X}", dwMask);
            txtInternalClock.Text = string.Format("{0:d}", dwInternalClock);
            txtStopNumber.Text    = string.Format("{0:d}", dwStopNumber);
            txtNumberOfData.Text  = string.Format("{0:d}", dwNumberOfData);

            cboStart_SelectedIndexChanged(cboStart, new System.EventArgs());
            cboClock_SelectedIndexChanged(cboClock, new System.EventArgs());
            cboStop_SelectedIndexChanged(cboStop, new System.EventArgs());
        }

        //===================================================
        // OKボタン処理
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string szTmp = new string(' ', 100);
            short  Start;
            short  Clock;
            short  Stop;
            short  Direction = (short)CdioConst.DIODM_DIR_IN;
            uint   i;
            int    Ret;
            string csMsg;
            string ErrorString;
            //--------------------------------
            // 表示データを取得
            //--------------------------------
            szStart = cboStart.Text;
            szClock = cboClock.Text;
            szStop  = cboStop.Text;
            szTmp   = txtPattern.Text;
            try
            {
                dwPattern = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                dwPattern = 0x55;
            }
            szTmp = txtMask.Text;
            try
            {
                dwMask = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                dwMask = 0xFF;
            }
            szTmp = txtInternalClock.Text;
            try
            {
                dwInternalClock = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                dwInternalClock = 1000;
            }
            szTmp = txtStopNumber.Text;
            try
            {
                dwStopNumber = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                dwStopNumber = 1000;
            }
            szTmp = txtNumberOfData.Text;
            try
            {
                dwNumberOfData = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                dwNumberOfData = 500;
            }
            //--------------------------------
            // 設定値を数値に変換
            //--------------------------------
            Start = 0;
            for (i = 0; string.Compare(stnStart[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStart, stnStart[i].Str) == 0)
                {
                    Start = stnStart[i].Num;
                    break;
                }
            }
            Clock = 0;
            for (i = 0; string.Compare(stnClock[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szClock, stnClock[i].Str) == 0)
                {
                    Clock = stnClock[i].Num;
                    break;
                }
            }
            Stop = 0;
            for (i = 0; string.Compare(stnStop[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStop, stnStop[i].Str) == 0)
                {
                    Stop = stnStop[i].Num;
                    break;
                }
            }
            //--------------------------------
            // サンプリング条件を設定
            //--------------------------------
            // 入出力方向設定(全点入力)
            Ret = dio.DmSetDirection(Form1.m_Id, (short)CdioConst.PI_32);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetDirection Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スタンドアローン
            Ret = dio.DmSetStandAlone(Form1.m_Id);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetStandAlone Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スタート条件設定
            Ret = dio.DmSetStartTrigger(Form1.m_Id, Direction, Start);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DmSetStartTrigger Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // クロック条件設定
            Ret = dio.DmSetClockTrigger(Form1.m_Id, Direction, Clock);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetClockTrigger Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // ストップ条件設定
            Ret = dio.DmSetStopTrigger(Form1.m_Id, Direction, Stop);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetStopTrigger Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // パターン/マスク
            Ret = dio.DmSetStartPattern(Form1.m_Id, dwPattern, dwMask);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetStartPattern Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 内部クロック値設定
            Ret = dio.DmSetInternalClock(Form1.m_Id, Direction, dwInternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetInternalClock Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 停止個数設定
            Ret = dio.DmSetStopNumber(Form1.m_Id, Direction, dwStopNumber);
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(Ret, out ErrorString);
                csMsg = "DioDmSetStopNumber Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
                MessageBox.Show(csMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }


        //===================================================
        // スタート条件設定変更
        //===================================================
        private void cboStart_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Pattern と Maskの有効、無効を切り替え
            //--------------------------------
            szBuff = cboStart.Text;
            if (string.Compare(szBuff, "Pattern") == 0)
            {
                txtPattern.Enabled = true;
                txtMask.Enabled    = true;
            }
            else
            {
                txtPattern.Enabled = false;
                txtMask.Enabled    = false;
            }
        }

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

        //===================================================
        // ストップ条件設定変更
        //===================================================
        private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Stop Numberの有効、無効を切り替え
            //--------------------------------
            szBuff = cboStop.Text;
            if (string.Compare(szBuff, "Number") == 0)
            {
                txtStopNumber.Enabled = true;
            }
            else
            {
                txtStopNumber.Enabled = false;
            }
        }
    }
}
