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

namespace pulse_measurement
{
    public partial class pulse_measurement : Form
    {
        //----------------------------------------
        // クラスCdioインスタンス作成
        //----------------------------------------
        Cdio dio = new Cdio();

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int MAX_PULSE_MEASUREMENT_CH = 4;                            // 最大パルス計測チャネル数

        //----------------------------------------
        // グローバル変数
        //----------------------------------------
        public short dev_id;                                                      // デバイスID
        public ComboBox[] m_Logic = new ComboBox[MAX_PULSE_MEASUREMENT_CH];       // パルス計測論理値コントロール
        public ComboBox[] m_TimeUnit = new ComboBox[MAX_PULSE_MEASUREMENT_CH];    // パルス計測時間単位値コントロール
        public TextBox[] m_Period = new TextBox[MAX_PULSE_MEASUREMENT_CH];        // パルス計測周期値コントロール
        public TextBox[] m_TimeValue = new TextBox[MAX_PULSE_MEASUREMENT_CH];     // パルス計測時間値コントロール
        public TextBox[] m_Status = new TextBox[MAX_PULSE_MEASUREMENT_CH];        // パルス計測ステータス値コントロール
        public TextBox[] m_Error = new TextBox[MAX_PULSE_MEASUREMENT_CH];         // パルス計測エラー値コントロール

        public pulse_measurement()
        {
            InitializeComponent();
        }

        //=========================================================
        // ダイアログ初期化
        //=========================================================
        private void pulse_measurement_Load(object sender, EventArgs e)
        {
            short ch_no;

            //----------------------------------------
            // コントロール配列設定
            //----------------------------------------
            //--------------------
            // パルス計測論理コントロール
            //--------------------
            m_Logic[0] = comboBox_Logic0;
            m_Logic[1] = comboBox_Logic1;
            m_Logic[2] = comboBox_Logic2;
            m_Logic[3] = comboBox_Logic3;
            //--------------------
            // パルス計測周期コントロール
            //--------------------
            m_Period[0] = textBox_Period0;
            m_Period[1] = textBox_Period1;
            m_Period[2] = textBox_Period2;
            m_Period[3] = textBox_Period3;
            //--------------------
            // パルス計測時間コントロール
            //--------------------
            m_TimeValue[0] = textBox_TimeValue0;
            m_TimeValue[1] = textBox_TimeValue1;
            m_TimeValue[2] = textBox_TimeValue2;
            m_TimeValue[3] = textBox_TimeValue3;
            //--------------------
            // パルス計測時間単位コントロール
            //--------------------
            m_TimeUnit[0] = comboBox_TimeUnit0;
            m_TimeUnit[1] = comboBox_TimeUnit1;
            m_TimeUnit[2] = comboBox_TimeUnit2;
            m_TimeUnit[3] = comboBox_TimeUnit3;
            //--------------------
            // パルス計測ステータスコントロール
            //--------------------
            m_Status[0] = textBox_Status0;
            m_Status[1] = textBox_Status1;
            m_Status[2] = textBox_Status2;
            m_Status[3] = textBox_Status3;
            //--------------------
            // パルス計測エラーコントロール
            //--------------------
            m_Error[0] = textBox_Error0;
            m_Error[1] = textBox_Error1;
            m_Error[2] = textBox_Error2;
            m_Error[3] = textBox_Error3;
            //----------------------------------------
            // 画面表示の初期化
            //----------------------------------------
            //--------------------
            // デバイス名
            //--------------------
            textBox_DeviceName.Text = "DIO000";
            //--------------------
            // チャネル数
            //--------------------
            comboBox_ChannelNum.Items.Add("1");
            comboBox_ChannelNum.Items.Add("2");
            comboBox_ChannelNum.Items.Add("3");
            comboBox_ChannelNum.Items.Add("4");
            comboBox_ChannelNum.SelectedItem = "4";
            //--------------------
            // 各チャネルごとに
            //--------------------
            for (ch_no = 0; ch_no < MAX_PULSE_MEASUREMENT_CH; ch_no++)
            {
                //--------------------
                // パルス計測論理コントロール
                //--------------------
                m_Logic[ch_no].Items.Add("OFF時間");
                m_Logic[ch_no].Items.Add("ON時間");
                m_Logic[ch_no].SelectedItem = "OFF時間";
                //--------------------
                // パルス計測周期コントロール
                //--------------------
                m_Period[ch_no].Text = "0";
                //--------------------
                // パルス計測時間単位コントロール
                //--------------------
                m_TimeUnit[ch_no].Items.Add("sec");
                m_TimeUnit[ch_no].Items.Add("msec");
                m_TimeUnit[ch_no].Items.Add("usec");
                m_TimeUnit[ch_no].Items.Add("nsec");
                m_TimeUnit[ch_no].SelectedItem = "nsec";
                //--------------------
                // パルス計測時間コントロール
                //--------------------
                m_TimeValue[ch_no].Text = "";
                //--------------------
                // パルス計測ステータスコントロール
                //--------------------
                m_Status[ch_no].Text = "";
                //--------------------
                // パルス計測エラーコントロール
                //--------------------
                m_Error[ch_no].Text = "";
            }
        }

        //=========================================================
        // DioInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //=========================================================
        private void button_DioInit_Click(object sender, EventArgs e)
        {
            string device_name;
            string error_string;
            int    ret;

            //----------------------------------------
            // デバイス名取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // 初期化処理、デバイスID取得
            //----------------------------------------
            ret = dio.Init(device_name, out dev_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.Init = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioSetPulseMeasurementSettingボタンをクリックした際の処理になります
        // デバイスのパルス計測設定を行います
        //=========================================================
        private void button_DioSetPulseMeasurementSetting_Click(object sender, EventArgs e)
        {
            string   cstr;
            string   error_string;
            ushort[] pulse_measurement_target = new ushort[MAX_PULSE_MEASUREMENT_CH];
            ushort[] pulse_measurement_period = new ushort[MAX_PULSE_MEASUREMENT_CH];
            short[]  ch_no_array = new short[MAX_PULSE_MEASUREMENT_CH];
            int      ret;
            short    ch_no;
            short    pulse_measurement_ch_num;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pulse_measurement_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // パルス計測論理指定値取得
                //----------------------------------------
                cstr = m_Logic[ch_no].SelectedItem.ToString();
                if (string.Equals(cstr, "OFF時間"))
                {
                    pulse_measurement_target[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TARGET_OFF_TIME;
                }
                else
                {
                    pulse_measurement_target[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TARGET_ON_TIME;
                }
                //----------------------------------------
                // パルス計測周期指定値取得
                //----------------------------------------
                if (ushort.TryParse(m_Period[ch_no].Text, out pulse_measurement_period[ch_no]) == false)
                {
                    pulse_measurement_period[ch_no] = 0;
                }
            }
            //----------------------------------------
            // パルス計測設定処理
            //----------------------------------------
            ret = dio.SetPulseMeasurementSetting(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_target, pulse_measurement_period);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.SetPulseMeasurementSetting = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioStartPulseMeasurementボタンをクリックした際の処理になります
        // パルス計測を開始します
        //=========================================================
        private void button_DioStartPulseMeasurement_Click(object sender, EventArgs e)
        {
            string  error_string;
            short[] ch_no_array = new short[MAX_PULSE_MEASUREMENT_CH];
            int     ret;
            short   ch_no;
            short   pulse_measurement_ch_num;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pulse_measurement_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // パルス計測開始処理
            //----------------------------------------
            ret = dio.StartPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StartPulseMeasurement = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioReadPulseMeasurementDataボタンをクリックした際の処理になります
        // パルス計測データを取得します
        //=========================================================
        private void button_DioReadPulseMeasurementData_Click(object sender, EventArgs e)
        {
            string   cstr;
            string   error_string;
            ulong[]  pulse_measurement_time_value = new ulong[MAX_PULSE_MEASUREMENT_CH];
            ushort[] pulse_measurement_status = new ushort[MAX_PULSE_MEASUREMENT_CH];
            ushort[] pulse_measurement_error = new ushort[MAX_PULSE_MEASUREMENT_CH];
            ushort[] pulse_measurement_time_unit = new ushort[MAX_PULSE_MEASUREMENT_CH];
            short[]  ch_no_array = new short[MAX_PULSE_MEASUREMENT_CH];
            int      ret;
            short    ch_no;
            short    pulse_measurement_ch_num;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pulse_measurement_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
                //----------------------------------------
                // パルス計測時間初期化
                //----------------------------------------
                pulse_measurement_time_value[ch_no] = 0;
                //----------------------------------------
                // パルス計測時間単位指定値取得
                //----------------------------------------
                cstr = m_TimeUnit[ch_no].SelectedItem.ToString();
                if (string.Equals(cstr, "sec"))
                {
                    pulse_measurement_time_unit[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TIME_UNIT_S;
                }
                else if (string.Equals(cstr, "msec"))
                {
                    pulse_measurement_time_unit[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TIME_UNIT_MS;
                }
                else if (string.Equals(cstr, "usec"))
                {
                    pulse_measurement_time_unit[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TIME_UNIT_US;
                }
                else
                {
                    pulse_measurement_time_unit[ch_no] = (ushort)CdioConst.DIO_PULSE_MEASUREMENT_TIME_UNIT_NS;
                }
            }
            //----------------------------------------
            // パルス計測データ取得処理
            //----------------------------------------
            ret = dio.ReadPulseMeasurementData(dev_id, ch_no_array, pulse_measurement_ch_num, pulse_measurement_status, pulse_measurement_error, pulse_measurement_time_value, pulse_measurement_time_unit);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.ReadPulseMeasurementData = {0} : {1}", ret, error_string);
            //----------------------------------------
            // エラーの場合処理を終了
            //----------------------------------------
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++)
            {
                //----------------------------------------
                // パルス計測ステータス表示更新
                //----------------------------------------
                m_Status[ch_no].Text = pulse_measurement_status[ch_no].ToString("X");
                //----------------------------------------
                // パルス計測エラー表示更新
                //----------------------------------------
                m_Error[ch_no].Text = pulse_measurement_error[ch_no].ToString("X");
                //----------------------------------------
                // パルス計測時間表示更新
                //----------------------------------------
                m_TimeValue[ch_no].Text = pulse_measurement_time_value[ch_no].ToString();
            }
        }

        //=========================================================
        // DioStopPulseMeasurementボタンをクリックした際の処理になります
        // パルス計測を停止します
        //=========================================================
        private void button_DioStopPulseMeasurement_Click(object sender, EventArgs e)
        {
            string  error_string;
            short[] ch_no_array = new short[MAX_PULSE_MEASUREMENT_CH];
            int     ret;
            short   ch_no;
            short   pulse_measurement_ch_num;

            //----------------------------------------
            // チャネル数の取得
            //----------------------------------------
            short.TryParse(comboBox_ChannelNum.SelectedItem.ToString(), out pulse_measurement_ch_num);
            //----------------------------------------
            // 各チャネルごとに
            //----------------------------------------
            for (ch_no = 0; ch_no < pulse_measurement_ch_num; ch_no++)
            {
                //----------------------------------------
                // チャネル番号格納
                //----------------------------------------
                ch_no_array[ch_no] = ch_no;
            }
            //----------------------------------------
            // パルス計測停止処理
            //----------------------------------------
            ret = dio.StopPulseMeasurement(dev_id, ch_no_array, pulse_measurement_ch_num);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            dio.GetErrorString(ret, out error_string);
            textBox_ReturnCode.Text = string.Format("dio.StopPulseMeasurement = {0} : {1}", ret, error_string);
        }

        //=========================================================
        // DioExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //=========================================================
        private void button_DioExit_Click(object sender, EventArgs e)
        {
            string error_string;
            int    ret;

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

        //=========================================================
        // 閉じるをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //=========================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理
            //----------------------------------------
            dio.Exit(dev_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            Close();
        }
    }
}
