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

namespace CapacitanceSingleMeasure
{
    public partial class CapacitanceSingleMeasure : Form
    {
        //----------------------------------------
        // クラスCzmインスタンス作成
        //----------------------------------------
        Czm zm = new Czm();

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short g_id;    // デバイスアクセス用ID

        public CapacitanceSingleMeasure()
        {
            InitializeComponent();
        }

        private void CapacitanceSingleMeasure_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "ZM000";
            //----------------------------------------
            // チャネル番号用コンボボックスの作成
            //----------------------------------------
            comboBox_ChannelNumber.Items.Add("CH1");
            comboBox_ChannelNumber.Items.Add("CH2");
            //----------------------------------------
            // チャネル番号用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_ChannelNumber.SelectedIndex = 0;
            //----------------------------------------
            // 周波数用コンボボックスの作成
            //----------------------------------------
            comboBox_Frequency.Items.Add("120 Hz");
            comboBox_Frequency.Items.Add("1 kHz");
            //----------------------------------------
            // 周波数用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Frequency.SelectedIndex = 0;
            //----------------------------------------
            // 信号レベル用コンボボックスの作成
            //----------------------------------------
            comboBox_Level.Items.Add("0.5 V");
            comboBox_Level.Items.Add("1 V");
            //----------------------------------------
            // 信号レベル用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Level.SelectedIndex = 0;
            //----------------------------------------
            // 測定スピード用コンボボックスの作成
            //----------------------------------------
            comboBox_Speed.Items.Add("FAST");
            comboBox_Speed.Items.Add("NORMAL");
            comboBox_Speed.Items.Add("SLOW");
            //----------------------------------------
            // 測定スピード用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Speed.SelectedIndex = 0;
            //----------------------------------------
            // 平均化回数の初期表示を更新
            //----------------------------------------
            textBox_AverageCount.Text = "1";
            //----------------------------------------
            // 測定レンジ用コンボボックスの作成
            //----------------------------------------
            comboBox_Range.Items.Add("4 nF");
            comboBox_Range.Items.Add("40 nF");
            comboBox_Range.Items.Add("400 nF");
            comboBox_Range.Items.Add("4 μF");
            comboBox_Range.Items.Add("40 μF");
            comboBox_Range.Items.Add("400 μF");
            comboBox_Range.Items.Add("1.4 mF");
            //----------------------------------------
            // 測定レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Range.SelectedIndex = 0;
            //----------------------------------------
            // 等価回路モード用コンボボックスの作成
            //----------------------------------------
            comboBox_Circuit.Items.Add("AUTO");
            comboBox_Circuit.Items.Add("直列等価回路");
            comboBox_Circuit.Items.Add("並列等価回路");
            //----------------------------------------
            // 等価回路モード用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Circuit.SelectedIndex = 0;
            //----------------------------------------
            // 測定開始ディレイの初期表示を更新
            //----------------------------------------
            textBox_StartDelay.Text = "2";
            //----------------------------------------
            // トリガ同期出力用コンボボックスの作成
            //----------------------------------------
            comboBox_TriggerSyncEnable.Items.Add("無効");
            comboBox_TriggerSyncEnable.Items.Add("有効");
            //----------------------------------------
            // トリガ同期出力用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_TriggerSyncEnable.SelectedIndex = 0;
            //----------------------------------------
            // 測定信号出力ディレイの初期表示を更新
            //----------------------------------------
            textBox_SignalOutputDelay.Text = "0";
            //----------------------------------------
            // コンタクトチェック用コンボボックスの作成
            //----------------------------------------
            comboBox_ContactCheck.Items.Add("無効");
            comboBox_ContactCheck.Items.Add("測定前");
            comboBox_ContactCheck.Items.Add("測定後");
            comboBox_ContactCheck.Items.Add("測定前後");
            //----------------------------------------
            // コンタクトチェック用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_ContactCheck.SelectedIndex = 0;
        }

        //================================================================================
        // ZmInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //================================================================================
        private void button_Init_Click(object sender, EventArgs e)
        {
            string device_name;     // デバイス名
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイス名をテキストボックスから取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス初期化
            //----------------------------------------
            ret = zm.Init(device_name, out g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Init = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmResetDeviceボタンをクリックした際の処理になります
        // デバイスのリセット処理を行います
        //================================================================================
        private void button_ResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイスをリセット
            //----------------------------------------
            ret = zm.ResetDevice(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.ResetDevice = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetFrequencyボタンをクリックした際の処理になります
        // 周波数の設定を行います
        //================================================================================
        private void button_SetFrequency_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort frequency;         // 周波数

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する周波数をコンボボックスから取得
            //----------------------------------------
            frequency = (ushort)comboBox_Frequency.SelectedIndex;
            //----------------------------------------
            // 周波数の設定
            //----------------------------------------
            ret = zm.SetFrequency(g_id, channel_number, frequency);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetFrequency = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetLevelボタンをクリックした際の処理になります
        // 信号レベルの設定を行います
        //================================================================================
        private void button_SetLevel_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort level;             // 信号レベル

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する信号レベルをコンボボックスから取得
            //----------------------------------------
            level = (ushort)comboBox_Level.SelectedIndex;
            //----------------------------------------
            // 信号レベルの設定
            //----------------------------------------
            ret = zm.SetLevel(g_id, channel_number, level);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetLevel = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetSpeedボタンをクリックした際の処理になります
        // 測定スピードと平均化回数の設定を行います
        //================================================================================
        private void button_SetSpeed_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort speed;             // 測定スピード
            uint   average_count;     // 平均化回数

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する測定スピードをコンボボックスから取得
            //----------------------------------------
            speed = (ushort)comboBox_Speed.SelectedIndex;
            //----------------------------------------
            // 設定する平均化回数をテキストボックスから取得
            //----------------------------------------
            if (uint.TryParse(textBox_AverageCount.Text, out average_count) == false)
            {
                average_count = 1;
            }
            //----------------------------------------
            // 測定スピードと平均化回数の設定
            //----------------------------------------
            ret = zm.SetSpeed(g_id, channel_number, speed, average_count);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSpeed = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetRangeボタンをクリックした際の処理になります
        // 測定レンジの設定を行います
        //================================================================================
        private void button_SetRange_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort range;             // 測定レンジ

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する測定レンジをコンボボックスから取得
            //----------------------------------------
            range = (ushort)(comboBox_Range.SelectedIndex + 3);
            //----------------------------------------
            // 測定レンジの設定
            //----------------------------------------
            ret = zm.SetRange(g_id, channel_number, range);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetRange = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetCircuitボタンをクリックした際の処理になります
        // 等価回路モードの設定を行います
        //================================================================================
        private void button_SetCircuit_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort circuit;           // 等価回路モード

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する等価回路モードをコンボボックスから取得
            //----------------------------------------
            circuit = (ushort)comboBox_Circuit.SelectedIndex;
            //----------------------------------------
            // 等価回路モードの設定
            //----------------------------------------
            ret = zm.SetCircuit(g_id, channel_number, circuit);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetCircuit = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetStartDelayボタンをクリックした際の処理になります
        // 測定開始ディレイの設定を行います
        //================================================================================
        private void button_SetStartDelay_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            float  start_delay;       // 測定開始ディレイ

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する測定開始ディレイをテキストボックスから取得
            //----------------------------------------
            if (float.TryParse(textBox_StartDelay.Text, out start_delay) == false)
            {
                start_delay = 2;
            }
            //----------------------------------------
            // 測定開始ディレイの設定
            //----------------------------------------
            ret = zm.SetStartDelay(g_id, channel_number, start_delay);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetStartDelay = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetTriggerSyncEnableボタンをクリックした際の処理になります
        // トリガ同期出力の設定を行います
        //================================================================================
        private void button_SetTriggerSyncEnable_Click(object sender, EventArgs e)
        {
            string error_string;           // エラーコード文字列
            int    ret;                    // 戻り値取得用
            ushort channel_number;         // チャネル番号
            ushort trigger_sync_enable;    // トリガ同期出力

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定するトリガ同期出力をコンボボックスから取得
            //----------------------------------------
            trigger_sync_enable = (ushort)comboBox_TriggerSyncEnable.SelectedIndex;
            //----------------------------------------
            // トリガ同期出力の設定
            //----------------------------------------
            ret = zm.SetTriggerSyncEnable(g_id, channel_number, trigger_sync_enable);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetTriggerSyncEnable = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetSignalOutputDelayボタンをクリックした際の処理になります
        // 測定信号出力ディレイの設定を行います
        //================================================================================
        private void button_SetSignalOutputDelay_Click(object sender, EventArgs e)
        {
            string error_string;          // エラーコード文字列
            int    ret;                   // 戻り値取得用
            ushort channel_number;        // チャネル番号
            float signal_output_delay;    // 測定信号出力ディレイ

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定する測定信号出力ディレイをテキストボックスから取得
            //----------------------------------------
            if (float.TryParse(textBox_SignalOutputDelay.Text, out signal_output_delay) == false)
            {
                signal_output_delay = 0;
            }
            //----------------------------------------
            // 測定信号出力ディレイの設定
            //----------------------------------------
            ret = zm.SetSignalOutputDelay(g_id, channel_number, signal_output_delay);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetSignalOutputDelay = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSetContactCheckボタンをクリックした際の処理になります
        // コンタクトチェックの設定を行います
        //================================================================================
        private void button_SetContactCheck_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            ushort contact_check;     // コンタクトチェック

            //----------------------------------------
            // 設定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 設定するコンタクトチェックをコンボボックスから取得
            //----------------------------------------
            contact_check = (ushort)comboBox_ContactCheck.SelectedIndex;
            //----------------------------------------
            // コンタクトチェックの設定
            //----------------------------------------
            ret = zm.SetContactCheck(g_id, channel_number, contact_check);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SetContactCheck = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmSingleCMeasureボタンをクリックした際の処理になります
        // 測定を行います
        //================================================================================
        private void button_SingleCMeasure_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            ushort channel_number;    // チャネル番号
            float  capacitance;       // 静電容量
            float  dissipation;       // 損失係数
            uint   status;            // ステータス
            ushort contact_check;     // コンタクトチェック

            //----------------------------------------
            // 測定するチャネル番号をコンボボックスから取得
            //----------------------------------------
            channel_number = (ushort)(comboBox_ChannelNumber.SelectedIndex + 1);
            //----------------------------------------
            // 測定
            //----------------------------------------
            ret = zm.SingleCMeasure(g_id, channel_number, out capacitance, out dissipation, out status);
            //----------------------------------------
            // ZmSingleCMeasure関数が正常終了したら表示を更新
            //----------------------------------------
            if (ret == (int)CzmConst.ZM_ERR_SUCCESS)
            {
                //----------------------------------------
                // 静電容量の表示を更新
                //----------------------------------------
                textBox_Capacitance.Text = string.Format("{0:e}", capacitance);
                //----------------------------------------
                // 損失係数の表示を更新
                //----------------------------------------
                textBox_Dissipation.Text = string.Format("{0:f}", dissipation);
                //----------------------------------------
                // 等価回路モードの表示を更新
                //----------------------------------------
                if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_SERIAL)
                {
                    //----------------------------------------
                    // 直列等価回路に表示を更新
                    //----------------------------------------
                    textBox_Circuit.Text = "直列等価回路";
                }
                else if (((status >> 24) & 0x03) == (uint)CzmConst.ZM_CIRCUIT_PARALLEL)
                {
                    //----------------------------------------
                    // 並列等価回路に表示を更新
                    //----------------------------------------
                    textBox_Circuit.Text = "並列等価回路";
                }
                //----------------------------------------
                // コンタクトチェックエラー表示を更新
                // コンタクトチェックが無効の箇所は「-」に更新
                //----------------------------------------
                zm.GetContactCheck(g_id, channel_number, out contact_check);
                //----------------------------------------
                // コンタクトチェック無効なので全て「-」に更新
                //----------------------------------------
                if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_DISABLE)
                {
                    textBox_BeforeL.Text = "-";
                    textBox_BeforeH.Text = "-";
                    textBox_AfterL.Text = "-";
                    textBox_AfterH.Text = "-";
                }
                //----------------------------------------
                // コンタクトチェック測定前なので測定後は「-」に更新
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_BEFORE)
                {
                    textBox_BeforeL.Text = string.Format("{0:d}", (status >> 16) & 0x01);
                    textBox_BeforeH.Text = string.Format("{0:d}", (status >> 17) & 0x01);
                    textBox_AfterL.Text = "-";
                    textBox_AfterH.Text = "-";
                }
                //----------------------------------------
                // コンタクトチェック測定後なので測定前は「-」に更新
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_AFTER)
                {
                    textBox_BeforeL.Text = "-";
                    textBox_BeforeH.Text = "-";
                    textBox_AfterL.Text = string.Format("{0:d}", (status >> 18) & 0x01);
                    textBox_AfterH.Text = string.Format("{0:d}", (status >> 19) & 0x01);
                }
                //----------------------------------------
                // コンタクトチェック測定前後なので全てステータスの値に更新
                //----------------------------------------
                else if (contact_check == (ushort)CzmConst.ZM_CONTACTCHECK_BOTH)
                {
                    textBox_BeforeL.Text = string.Format("{0:d}", (status >> 16) & 0x01);
                    textBox_BeforeH.Text = string.Format("{0:d}", (status >> 17) & 0x01);
                    textBox_AfterL.Text = string.Format("{0:d}", (status >> 18) & 0x01);
                    textBox_AfterH.Text = string.Format("{0:d}", (status >> 19) & 0x01);
                }
            }
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.SingleCMeasure = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ZmExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            ret = zm.Exit(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            zm.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnValue.Text = "zm.Exit = " + ret.ToString() + "：" + error_string;
        }

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