﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CdioCs;

namespace DiCounter
{
    public partial class DiCounter : Form
    {
        /// <summary>
        /// Create instance Cdio class
        /// </summary>
        Cdio dio = new Cdio();

        //==========================================
        // グローバル変数
        //==========================================
        public short        dev_id;                 // デバイスID
        public short        count_ch_num;           // デバイスのカウンタチャネル数
        public static short MAX_COUNT_CH = 16;      // 最大カウントチャネル数
        public static short DISP_MAX_COUNT_CH = 8;  // 最大カウントチャネル数

        private System.Windows.Forms.ComboBox[] m_CountEdge         = new System.Windows.Forms.ComboBox[MAX_COUNT_CH];  // カウントエッジコントロール
        private System.Windows.Forms.TextBox[]  m_CompareCountValue = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // 比較カウント値コントロール
        private System.Windows.Forms.TextBox[]  m_PresetValue       = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // プリセット値コントロール
        private System.Windows.Forms.TextBox[]  m_CountValue        = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // カウント値コントロール
        private System.Windows.Forms.TextBox[]  m_CountStatus       = new System.Windows.Forms.TextBox[MAX_COUNT_CH];   // カウントステータスコントロール

        

        public DiCounter()
        {
            InitializeComponent();

            //-----------------------------------
            //	コントロール配列設定
            //-----------------------------------
            //-----------------------------------
            // カウントエッジコントロール
            //-----------------------------------
            m_CountEdge[0] = comboBox_CountEdge0;
            m_CountEdge[1] = comboBox_CountEdge1;
            m_CountEdge[2] = comboBox_CountEdge2;
            m_CountEdge[3] = comboBox_CountEdge3;
            m_CountEdge[4] = comboBox_CountEdge4;
            m_CountEdge[5] = comboBox_CountEdge5;
            m_CountEdge[6] = comboBox_CountEdge6;
            m_CountEdge[7] = comboBox_CountEdge7;
            //-----------------------------------
            // 比較カウント値コントロール
            //-----------------------------------
            m_CompareCountValue[0] = textBox_CompareCountValue0;
            m_CompareCountValue[1] = textBox_CompareCountValue1;
            m_CompareCountValue[2] = textBox_CompareCountValue2;
            m_CompareCountValue[3] = textBox_CompareCountValue3;
            m_CompareCountValue[4] = textBox_CompareCountValue4;
            m_CompareCountValue[5] = textBox_CompareCountValue5;
            m_CompareCountValue[6] = textBox_CompareCountValue6;
            m_CompareCountValue[7] = textBox_CompareCountValue7;
            //-----------------------------------
            // プリセット値コントロール
            //-----------------------------------
            m_PresetValue[0] = textBox_PresetValue0;
            m_PresetValue[1] = textBox_PresetValue1;
            m_PresetValue[2] = textBox_PresetValue2;
            m_PresetValue[3] = textBox_PresetValue3;
            m_PresetValue[4] = textBox_PresetValue4;
            m_PresetValue[5] = textBox_PresetValue5;
            m_PresetValue[6] = textBox_PresetValue6;
            m_PresetValue[7] = textBox_PresetValue7;
            //-----------------------------------
            // カウント値コントロール
            //-----------------------------------
            m_CountValue[0] = textBox_CountValue0;
            m_CountValue[1] = textBox_CountValue1;
            m_CountValue[2] = textBox_CountValue2;
            m_CountValue[3] = textBox_CountValue3;
            m_CountValue[4] = textBox_CountValue4;
            m_CountValue[5] = textBox_CountValue5;
            m_CountValue[6] = textBox_CountValue6;
            m_CountValue[7] = textBox_CountValue7;
            //-----------------------------------
            // カウントステータスコントロール
            //-----------------------------------
            m_CountStatus[0] = textBox_CountStatus0;
            m_CountStatus[1] = textBox_CountStatus1;
            m_CountStatus[2] = textBox_CountStatus2;
            m_CountStatus[3] = textBox_CountStatus3;
            m_CountStatus[4] = textBox_CountStatus4;
            m_CountStatus[5] = textBox_CountStatus5;
            m_CountStatus[6] = textBox_CountStatus6;
            m_CountStatus[7] = textBox_CountStatus7;
        }

        //=========================================================
        // ダイアログ初期化
        //=========================================================
        private void DiCounter_Load(object sender, EventArgs e)
        {
            //-----------------------------------
            //	画面表示の初期化
            //-----------------------------------
            short   ch_no;
            string  cstr;

            //-----------------------------------
            // デバイス名
            //-----------------------------------
            textBox_DeviceName.Text = "DIO000";
            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < DISP_MAX_COUNT_CH; ch_no++){
                //-----------------------------------
                // カウントエッジコントロール
                //-----------------------------------
                m_CountEdge[ch_no].Items.Add("立上り");
                m_CountEdge[ch_no].Items.Add("立下り");
                m_CountEdge[ch_no].Items.Add("両方");
                m_CountEdge[ch_no].SelectedIndex = 0;
                //-----------------------------------
                // 比較カウント値コントロール
                //-----------------------------------
                cstr = string.Format("{0:d}", ((ch_no + 1) * 100));
                m_CompareCountValue[ch_no].Text = cstr;
                //-----------------------------------
                // プリセット値コントロール
                //-----------------------------------
                cstr = string.Format("{0:d}", (ch_no * 100));
                m_PresetValue[ch_no].Text = cstr;
                //-----------------------------------
                // カウント値コントロール
                //-----------------------------------
                m_CountValue[ch_no].Text = "-";
                //-----------------------------------
                // カウントステータスコントロール
                //-----------------------------------
                m_CountStatus[ch_no].Text = "-";
            }
        }

        //=========================================================
        // DioInit()
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            int     ret;
            string  device_name;
            string  error_string;
            string  return_string;
            short   ch_no;

            //-----------------------------
            // デバイス名取得
            //-----------------------------
            device_name = textBox_DeviceName.Text; 
            //-----------------------------
            // 初期化処理、デバイスID取得
            //-----------------------------
            ret = dio.Init(device_name, out dev_id);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // チャネル数取得
            //-----------------------------
            ret = dio.GetMaxCountChannels(dev_id, out count_ch_num);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
                textBox_ReturnCode.Text = return_string;
                return;
            }
            //-----------------------------------
            // デバイスに存在しないチャネル用コントロールを無効化
            //-----------------------------------
            for (ch_no = count_ch_num; ch_no < DISP_MAX_COUNT_CH; ch_no++)
            {
                //-----------------------------------
                // カウントエッジコントロール
                //-----------------------------------
                m_CountEdge[ch_no].Enabled = false;
                //-----------------------------------
                // 比較カウント値コントロール
                //-----------------------------------
                m_CompareCountValue[ch_no].Enabled = false;
                //-----------------------------------
                // プリセット値コントロール
                //-----------------------------------
                m_PresetValue[ch_no].Enabled = false;
                //-----------------------------------
                // カウント値コントロール
                //-----------------------------------
                m_CountValue[ch_no].Enabled = false;
                //-----------------------------------
                // カウントステータスコントロール
                //-----------------------------------
                m_CountStatus[ch_no].Enabled = false;
            }
            //-----------------------------------
            // カウントチャネル数を表示できる最大チャネル数に補正
            //-----------------------------------
            if (DISP_MAX_COUNT_CH < count_ch_num)
            {
                count_ch_num = DISP_MAX_COUNT_CH;
            }
        }

        //=========================================================
        // DioSetCountEdge()
        //=========================================================
        private void button_DioSetCountEdge_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];
            short[] count_edge  = new short[MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]	= ch_no;
                //-----------------------------------
                // カウントエッジ指定値取得
                //-----------------------------------
                cstr = m_CountEdge[ch_no].Text;
                if(string.Compare(cstr, "立上り") == 0){
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_UP;
                }else if(string.Compare(cstr, "立下り") == 0){
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_DOWN;
                }else{
                    count_edge[ch_no]   = (int)CdioConst.DIO_COUNT_EDGE_UP | (int)CdioConst.DIO_COUNT_EDGE_DOWN;
                }
            }
            //-----------------------------
            // カウントエッジ設定
            //-----------------------------
            ret	= dio.SetCountEdge(dev_id, ch_no_array, count_ch_num, count_edge);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioSetCountMatchValue
        //=========================================================
        private void button_DioSetCountMatchValue_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array             = new short[MAX_COUNT_CH];
            short[] count_match_register_no = new short[MAX_COUNT_CH];
            uint[]  count_match_value       = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]				= ch_no;
                //-----------------------------------
                // 比較レジスタ番号格納
                //-----------------------------------
                count_match_register_no[ch_no]	= 0;
                //-----------------------------------
                // 比較カウント指定値取得
                //-----------------------------------
                cstr = m_CompareCountValue[ch_no].Text;
                count_match_value[ch_no]        = System.Convert.ToUInt32(cstr, 10);
            }
            //-----------------------------
            // 比較レジスタ設定
            //-----------------------------
            ret	= dio.SetCountMatchValue(dev_id, ch_no_array, count_ch_num, count_match_register_no, count_match_value);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioStartCount()
        //=========================================================
        private void button_DioStartCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]	= ch_no;
            }
            //-----------------------------
            // カウントスタート
            //-----------------------------
            ret	= dio.StartCount(dev_id, ch_no_array, count_ch_num);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioReadCount()
        //=========================================================
        private void button_DioReadCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];
            uint[]  count       = new uint [MAX_COUNT_CH];
            string  cstr;
            
            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]	= ch_no;
            }
            //-----------------------------
            // カウント値リード
            //-----------------------------
            ret	= dio.ReadCount(dev_id, ch_no_array, count_ch_num, count);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------
                // カウント値表示更新
                //-----------------------------
                cstr = System.Convert.ToString(count[ch_no], 10);
                m_CountValue[ch_no].Text = cstr;
            }
        }

        //=========================================================
        // DioGetCountStatus()
        //=========================================================
        private void button_DioGetCountStatus_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];
            uint[]  count_status = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]	= ch_no;
            }
            //-----------------------------
            // カウントステータスリード
            //-----------------------------
            ret	= dio.GetCountStatus(dev_id, ch_no_array, count_ch_num, count_status);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------
                // カウントステータス表示更新
                //-----------------------------
                cstr = System.Convert.ToString(count_status[ch_no], 16);
                m_CountStatus[ch_no].Text = cstr;
            }
        }

        //=========================================================
        // DioCountPreset()
        //=========================================================
        private void button_DioCountPreset_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array  = new short[MAX_COUNT_CH];
            uint[]  preset_count = new uint [MAX_COUNT_CH];
            string  cstr;

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]		= ch_no;
                //-----------------------------------
                // プリセットカウント指定値取得
                //-----------------------------------
                cstr = m_PresetValue[ch_no].Text; 
                preset_count[ch_no]     = System.Convert.ToUInt32(cstr, 10);
            }
            //-----------------------------
            // カウント値をプリセット
            //-----------------------------
            ret	= dio.CountPreset(dev_id, ch_no_array, count_ch_num, preset_count);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioStopCount()
        //=========================================================
        private void button_DioStopCount_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;
            short   ch_no;
            short[] ch_no_array = new short[MAX_COUNT_CH];

            //-----------------------------------
            // 各チャネルごとに
            //-----------------------------------
            for(ch_no = 0; ch_no < count_ch_num; ch_no++){
                //-----------------------------------
                // チャネル番号格納
                //-----------------------------------
                ch_no_array[ch_no]	= ch_no;
            }
            //-----------------------------
            // カウントストップ
            //-----------------------------
            ret = dio.StopCount(dev_id, ch_no_array, count_ch_num);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // DioExit()
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            int     ret;
            string  error_string;
            string  return_string;

            //-----------------------------
            // 終了処理
            //-----------------------------
            ret	= dio.Exit(dev_id);
            //-----------------------------
            // エラー処理
            //-----------------------------
            dio.GetErrorString(ret, out error_string);
            return_string = string.Format("Ret = {0:d} : {1:s}", ret, error_string);
            textBox_ReturnCode.Text = return_string;
        }

        //=========================================================
        // アプリケーション終了
        //=========================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            //-----------------------------
            // ダイアログを終了する
            //-----------------------------
            this.Close();
        }
    }
}