﻿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
    {
        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)
        };

        Cdio dio = new Cdio();
        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();
        }

        //===================================================
        // Dialog init
        //===================================================
        private void Condition_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // Init
            //--------------------------------
            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 button handle
        //===================================================
        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;
            //--------------------------------
            // Get the displayed data
            //--------------------------------
            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;
            }
            //--------------------------------
            // Convert data to number
            //--------------------------------
            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;
                }
            }
            //--------------------------------
            // Set sampling condition
            //--------------------------------
            // Specify input/output direction (32-point input)
            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;
            }
            // Start alone
            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;
            }
            // Set start condition
            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;
            }
            // Set clock condition
            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;
            }
            // Set stop condition
            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;
            }
            // Pattern/Mask
            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;
            }
            // Set internal clock
            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;
            }
            // Set stop number
            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();
        }

        //===================================================
        // Cancel button handle
        //===================================================
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }


        //===================================================
        // Modify start condition
        //===================================================
        private void cboStart_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Pattern and Mask
            //--------------------------------
            szBuff = cboStart.Text;
            if (string.Compare(szBuff, "Pattern") == 0)
            {
                txtPattern.Enabled = true;
                txtMask.Enabled = true;
            }
            else
            {
                txtPattern.Enabled = false;
                txtMask.Enabled = false;
            }
        }

        //===================================================
        // Modify clock condition
        //===================================================
        private void cboClock_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Internal Clock
            //--------------------------------
            szBuff = cboClock.Text;
            if (string.Compare(szBuff, "Internal Clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // Modify stop condition
        //===================================================
        private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Stop Number
            //--------------------------------
            szBuff = cboStop.Text;
            if (string.Compare(szBuff, "Number") == 0)
            {
                txtStopNumber.Enabled = true;
            }
            else
            {
                txtStopNumber.Enabled = false;
            }
        }
    }
}
