﻿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 Infinite
{
    public partial class frmCondition : Form
    {
        Ccnt cnt = new Ccnt();

        // Start Condition
        static frmMode.StrNum[] StartTrg =
        {
            new frmMode.StrNum("Software Start",                            (ushort)CcntConst.CNTS_START_SOFT               ),
            new frmMode.StrNum("External Signal Rising",                    (ushort)CcntConst.CNTS_START_EXT_RISE           ),
            new frmMode.StrNum("External Signal Falling",                   (ushort)CcntConst.CNTS_START_EXT_FALL           ),
            new frmMode.StrNum("Count-match Ch0 Compare-register 0",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH0_REG0  ),
            new frmMode.StrNum("Count-match Ch1 Compare-register 0",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH1_REG0  ),
            new frmMode.StrNum("Count-match Ch2 Compare-register 0",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH2_REG0  ),
            new frmMode.StrNum("Count-match Ch3 Compare-register 0",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH3_REG1  ),
            new frmMode.StrNum("Count-match Ch0 Compare-register 1",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH0_REG1  ),
            new frmMode.StrNum("Count-match Ch1 Compare-register 1",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH1_REG1  ),
            new frmMode.StrNum("Count-match Ch2 Compare-register 1",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH2_REG1  ),
            new frmMode.StrNum("Count-match Ch3 Compare-register 1",        (ushort)CcntConst.CNTS_START_CNTMATCH_CH3_REG1  ),
            new frmMode.StrNum("End",                                       0                                               )
        };

        // Clock Condition
        static frmMode.StrNum[] ClockTrg =
        {
            new frmMode.StrNum("Internal Clock",                (ushort)CcntConst.CNTS_CLOCK_CLOCK     ),
            new frmMode.StrNum("External Clock",                (ushort)CcntConst.CNTS_CLOCK_EXT_TRG   ),
            new frmMode.StrNum("End",                           0                                      )
        };

        // Stop Condition
        static frmMode.StrNum[] StopTrg =
        {
            new frmMode.StrNum("Software Stop",                             (ushort)CcntConst.CNTS_STOP_SOFT               ),
            new frmMode.StrNum("External Signal Rising",                    (ushort)CcntConst.CNTS_STOP_EXT_RISE           ),
            new frmMode.StrNum("External Signal Falling",                   (ushort)CcntConst.CNTS_STOP_EXT_FALL           ),
            new frmMode.StrNum("Stop Transfer by Specified Times",          (ushort)CcntConst.CNTS_STOP_NUM                ),
            new frmMode.StrNum("Count-match Ch0 Compare-register 0",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0  ),
            new frmMode.StrNum("Count-match Ch1 Compare-register 0",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0  ),
            new frmMode.StrNum("Count-match Ch2 Compare-register 0",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0  ),
            new frmMode.StrNum("Count-match Ch3 Compare-register 0",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0  ),
            new frmMode.StrNum("Count-match Ch0 Compare-register 1",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1  ),
            new frmMode.StrNum("Count-match Ch1 Compare-register 1",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1  ),
            new frmMode.StrNum("Count-match Ch2 Compare-register 1",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1  ),
            new frmMode.StrNum("Count-match Ch3 Compare-register 1",        (ushort)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1  ),
            new frmMode.StrNum("End",                                       0                                              )
        };

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

        string szStart = "Software Start";
        string szClock = "Internal Clock";
        string szStop = "Software Stop";
        int Mask = 0xFF;
        uint InternalClock = 100000;
        uint StopNumber = 1000;
        int NumberOfData = 500;
        short StartCountMode = (short)CcntConst.CNTS_START_CNT_WITH_SAMP;
        short StopCountMode = (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP;
        short Id;

        public frmCondition()
        {
            InitializeComponent();
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void frmCondition_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // Initialize dialog display
            //--------------------------------
            int i;

            for (i = 0; string.Compare(StartTrg[i].Str, "End") != 0; i++)
            {
                cboStartTrg.Items.Add(StartTrg[i].Str);
            }
            for (i = 0; string.Compare(ClockTrg[i].Str, "End") != 0; i++)
            {
                cboClockTrg.Items.Add(ClockTrg[i].Str);
            }
            for (i = 0; string.Compare(StopTrg[i].Str, "End") != 0; i++)
            {
                cboStopTrg.Items.Add(StopTrg[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;
            }
            txtCompStart.Text = string.Format("{0:X}", StartComp);
            txtInternalClock.Text = string.Format("{0:d}", InternalClock);
            txtStopNum.Text = string.Format("{0:d}", StopNumber);
            txtNumEvent.Text = string.Format("{0:d}", NumberOfData);
            txtCompStop.Text = string.Format("{0:X}", StopComp);

            Id = Form1.Id;

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

        //===================================================
        // Change start condition setting
        //===================================================
        private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStartTrg.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStart.Enabled = true;
            }
            else
            {
                txtCompStart.Enabled = false;
            }
        }

        //===================================================
        // Change clock condition setting
        //===================================================
        private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            //--------------------------------
            // Change validity of internal clock value
            //--------------------------------
            szBuf = cboClockTrg.Text;
            if (string.Compare(szBuf, "Internal Clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // Change stop condition setting
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            //--------------------------------
            // Change validity of stop number
            //--------------------------------
            szBuf = cboStopTrg.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStop.Enabled = true;
            }
            else
            {
                txtCompStop.Enabled = false;
            }
            if (string.Compare(szBuf, "Stop Transfer by Specified Times") == 0)
            {
                txtStopNum.Enabled = true;
            }
            else
            {
                txtStopNum.Enabled = false;
            }
        }

        //===================================================
        // OK processing
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string error_string = new string(' ', 256);
            string szTmp = new string(' ', 100);
            short wStartTrg;
            short wClockTrg;
            short wStopTrg;
            int ret;
            string strMsg;
            short CompChNo;
            short CompRegNo;

            //--------------------------------
            // Get displayed data
            //--------------------------------
            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;
            StartComp = uint.Parse(szTmp, NumberStyles.HexNumber);
            szTmp = txtCompStop.Text;
            StopComp = uint.Parse(szTmp, NumberStyles.HexNumber);

            Mask = int.Parse(szTmp, NumberStyles.HexNumber);
            szTmp = txtInternalClock.Text;
            InternalClock = uint.Parse(szTmp);
            szTmp = txtStopNum.Text;
            StopNumber = uint.Parse(szTmp);
            szTmp = txtNumEvent.Text;
            NumberOfData = int.Parse(szTmp);

            //--------------------------------
            // Convert the set value to numerical value
            //--------------------------------
            wStartTrg = 0;
            wStartTrg = (short)frmMode.str_to_num(StartTrg, szStart);

            wClockTrg = 0;
            wClockTrg = (short)frmMode.str_to_num(ClockTrg, szClock);

            wStopTrg = 0;
            wStopTrg = (short)frmMode.str_to_num(StopTrg, szStop);
            //--------------------------------
            // Set sampling condition
            //--------------------------------
            // StandAlone
            ret = cnt.SetStandAlone(Id);
            if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetStandAlone ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                return;
            }
            // Set start condition
            ret = cnt.SetSamplingStartTrigger(Id, wStartTrg, StartCountMode, (short)Form1.CHANNEL_NUM);
            if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                return;
            }
            switch (wStartTrg)
            {
                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(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, "", MessageBoxButtons.OK);
                }
            }

            // Set clock condition
            ret = cnt.SetSamplingClockTrigger(Id, wClockTrg);
            if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                return;
            }
            // Set internal clock value
            if (wClockTrg == (short)CcntConst.CNTS_CLOCK_CLOCK)
            {
                ret = cnt.SetSamplingInternalClock(Id, InternalClock, (short)CcntConst.CNTS_TIM_UNIT_NS);
                if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                    return;
                }
            }

            // Set stop condition
            ret = cnt.SetSamplingStopTrigger(Id, wStopTrg, StopCountMode, (short)Form1.CHANNEL_NUM);
            if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                return;
            }
            switch (wStopTrg)
            {
                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(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, "", MessageBoxButtons.OK);
                }
            }
            // Set stop number
            if (wStopTrg == (short)CcntConst.CNTS_STOP_NUM)
            {
                ret = cnt.SetSamplingStopNumber(Id, StopNumber);
                if (ret != (short)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, "", MessageBoxButtons.OK);
                    return;
                }
            }
            this.Close();
        }

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