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

namespace Counter
{
    public partial class Mode : Form
    {
        public struct StrNum
        {
            public string Str;
            public short Num;
            public StrNum(string Str, short Num)
            {
                this.Str = Str;
                this.Num = Num;
            }
        };

        // Z相使用方法
        static StrNum[] ZPhaseMode =
        {
            new StrNum("未使用",             (short)CcntConst.CNT_ZPHASE_NOT_USE),
            new StrNum("1回",                (short)CcntConst.CNT_ZPHASE_NEXT_ONE),
            new StrNum("毎回",               (short)CcntConst.CNT_ZPHASE_EVERY_TIME),
            new StrNum("End",                0)
        };
        // Z相論理
        static StrNum[] ZPhaseLogic =
        {
            new StrNum("正論理",             (short)CcntConst.CNT_ZLOGIC_POSITIVE),
            new StrNum("負論理",             (short)CcntConst.CNT_ZLOGIC_NEGATIVE),
            new StrNum("End",                0)
        };
        // 外部信号源
        static StrNum[] SignalSource =
        {
            new StrNum("フォトカプラ絶縁",   (short)CcntConst.CNT_SIGTYPE_ISOLATE),
            new StrNum("TTL",                (short)CcntConst.CNT_SIGTYPE_TTL),
            new StrNum("差動",               (short)CcntConst.CNT_SIGTYPE_LINERECEIVER),
            new StrNum("End",                0)
        };
        // カウント方向
        static StrNum[] CountDirection =
        {
            new StrNum("ダウンカウント",     (short)CcntConst.CNT_DIR_DOWN),
            new StrNum("アップカウント",     (short)CcntConst.CNT_DIR_UP),
            new StrNum("End",                0)
        };
        // 動作モード
        static StrNum[] Phase =
        {
            new StrNum("単相",               (short)CcntConst.CNT_MODE_1PHASE),
            new StrNum("2相",                (short)CcntConst.CNT_MODE_2PHASE),
            new StrNum("ゲートコントロール", (short)CcntConst.CNT_MODE_GATECONTROL),
            new StrNum("End",       0)
        };
        // 逓倍
        static StrNum[] Mul =
        {
            new StrNum("1逓倍",              (short)CcntConst.CNT_MUL_X1),
            new StrNum("2逓倍",              (short)CcntConst.CNT_MUL_X2),
            new StrNum("4逓倍",              (short)CcntConst.CNT_MUL_X4),
            new StrNum("End",                0)
        };
        // 同期/非同期クリア
        static StrNum[] SyncClear =
        {
            new StrNum("非同期クリア",      (short)CcntConst.CNT_CLR_ASYNC),
            new StrNum("同期クリア",        (short)CcntConst.CNT_CLR_SYNC),
            new StrNum("End",               0)
        };
        // デジタルフィルタ値
        static StrNum[] Filter =
        {
            new StrNum("0",                 0x00),
            new StrNum("1",                 0x01),
            new StrNum("2",                 0x02),
            new StrNum("3",                 0x03),
            new StrNum("4",                 0x04),
            new StrNum("5",                 0x05),
            new StrNum("6",                 0x06),
            new StrNum("7",                 0x07),
            new StrNum("8",                 0x08),
            new StrNum("9",                 0x09),
            new StrNum("A",                 0x0a),
            new StrNum("B",                 0x0b),
            new StrNum("C",                 0x0c),
            new StrNum("D",                 0x0d),
            new StrNum("E",                 0x0e),
            new StrNum("F",                 0x0f),
            new StrNum("10",                0x10),
            new StrNum("11",                0x11),
            new StrNum("12",                0x12),
            new StrNum("13",                0x13),
            new StrNum("14",                0x14),
            new StrNum("End",               0)
        };

        // データ初期化
        static string szZPhaseMode     = "未使用";
        static string szZPhaseLogic    = "負論理";
        static string szSignalSource   = "差動";
        static string szCountDirection = "アップカウント";
        static string szPhase          = "2相";
        static string szMul            = "1逓倍";
        static string szSyncClear      = "非同期クリア";
        static string szFilter         = "0";

        Ccnt          cnt = new Ccnt();

        public static short str_to_num(StrNum[] strnum, string str)
        {
            short i;

            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (string.Compare(strnum[i].Str, str) == 0)
                {
                    return strnum[i].Num;
                }
            }
            return 0;
        }

        public static string num_to_str(StrNum[] strnum, short num)
        {
            short i;

            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (strnum[i].Num == num)
                {
                    return strnum[i].Str;
                }
            }
            return strnum[0].Str;
        }

        public Mode()
        {
            InitializeComponent();
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void frmMode_Load(object sender, System.EventArgs e)
        {
            //===================================================
            // コンボボックス初期化
            //===================================================
            short i;
            // Z相使用方法
            for (i = 0; string.Compare(ZPhaseMode[i].Str, "End") != 0; i++)
            {
                cboZPhaseMode.Items.Add(ZPhaseMode[i].Str);
            }
            // Z相論理
            for (i = 0; string.Compare(ZPhaseLogic[i].Str, "End") != 0; i++)
            {
                cboZPhaseLogic.Items.Add(ZPhaseLogic[i].Str);
            }
            // 外部信号源
            for (i = 0; string.Compare(SignalSource[i].Str, "End") != 0; i++)
            {
                cboSignalSource.Items.Add(SignalSource[i].Str);
            }
            // カウント方向
            for (i = 0; string.Compare(CountDirection[i].Str, "End") != 0; i++)
            {
                cboCountDirection.Items.Add(CountDirection[i].Str);
            }
            // 動作モード
            for (i = 0; string.Compare(Phase[i].Str, "End") != 0; i++)
            {
                cboPhase.Items.Add(Phase[i].Str);
            }
            // 逓倍
            for (i = 0; string.Compare(Mul[i].Str, "End") != 0; i++)
            {
                cboMul.Items.Add(Mul[i].Str);
            }
            // 同期/非同期クリア
            for (i = 0; string.Compare(SyncClear[i].Str, "End") != 0; i++)
            {
                cboSyncClear.Items.Add(SyncClear[i].Str);
            }
            // デジタルフィルタ値
            for (i = 0; string.Compare(Filter[i].Str, "End") != 0; i++)
            {
                cboFilter.Items.Add(Filter[i].Str);
            }
            //-------------------------------
            // 初期データのセット
            //-------------------------------
            cboZPhaseMode.Text     = szZPhaseMode;
            cboZPhaseLogic.Text    = szZPhaseLogic;
            cboSignalSource.Text   = szSignalSource;
            cboCountDirection.Text = szCountDirection;
            cboPhase.Text          = szPhase;
            cboMul.Text            = szMul;
            cboSyncClear.Text      = szSyncClear;
            cboFilter.Text         = szFilter;

        }

        //---------------------------------------------
        // 設定完了処理
        //---------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            short  ChNo;
            int    ret;
            string str;
            string error_string = new string(' ', 256);

            short  wZPhaseMode;
            short  wZPhaseLogic;
            short  wSignalSource;
            short  wCountDirection;
            short  wPhase;
            short  wMul;
            short  wSyncClear;
            short  wFilter;

            //-------------------------------
            // 設定情報の取得
            //-------------------------------
            szZPhaseMode     = cboZPhaseMode.Text;
            szZPhaseLogic    = cboZPhaseLogic.Text;
            szSignalSource   = cboSignalSource.Text;
            szCountDirection = cboCountDirection.Text;
            szPhase          = cboPhase.Text;
            szMul            = cboMul.Text;
            szSyncClear      = cboSyncClear.Text;
            szFilter         = cboFilter.Text;

            wZPhaseMode     = str_to_num(ZPhaseMode, szZPhaseMode);
            wZPhaseLogic    = str_to_num(ZPhaseLogic, szZPhaseLogic);
            wSignalSource   = str_to_num(SignalSource, szSignalSource);
            wCountDirection = str_to_num(CountDirection, szCountDirection);
            wPhase          = str_to_num(Phase, szPhase);
            wMul            = str_to_num(Mul, szMul);
            wSyncClear      = str_to_num(SyncClear, szSyncClear);
            wFilter         = str_to_num(Filter, szFilter);

            //-------------------------------
            // 設定情報をデバイスへセット
            //-------------------------------
            for (ChNo = 0; ChNo < Form1.ChannelNum; ChNo++)
            {
                //---------------------------------------------
                // Z相使用方法 設定
                //---------------------------------------------
                ret = cnt.SetZMode(Form1.Id, ChNo, wZPhaseMode);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetZMode : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
                //---------------------------------------------
                // Z相論理 設定
                //---------------------------------------------
                ret = cnt.SetZLogic(Form1.Id, ChNo, wZPhaseLogic);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetZLogic : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
                //---------------------------------------------
                // 外部信号源 設定
                //---------------------------------------------
                ret = cnt.SelectChannelSignal(Form1.Id, ChNo, wSignalSource);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSelectChannelSignal : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
                //---------------------------------------------
                // カウント方向 設定
                //---------------------------------------------
                ret = cnt.SetCountDirection(Form1.Id, ChNo, wCountDirection);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetCountDirection : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
                //---------------------------------------------
                // 動作モード 設定
                //---------------------------------------------
                ret = cnt.SetOperationMode(Form1.Id, ChNo, wPhase, wMul, wSyncClear);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetOperationMode : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
                //---------------------------------------------
                // デジタルフィルタ値 設定
                //---------------------------------------------
                ret = cnt.SetDigitalFilter(Form1.Id, ChNo, wFilter);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetOperationMode : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    break;
                }
            }
            this.Close();
        }

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