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

namespace AoGeneratingInfiniteDev
{
    public partial class Condition : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short[] g_start_trigger = {0,     // ソフトウェア
                                          1,     // 外部トリガ立ち上がり
                                          2};    // 外部トリガ立ち下がり
        public short[] g_clock_type = {   0,     // 内部クロック
                                          1};    // 外部クロック
        public short[] g_clock_edge = {   0,     // 立ち下がりエッジ
                                          1};    // 立ち上がりエッジ
        public short[] g_stop_trigger = { 1,     // 外部トリガ立ち上がり
                                          2};    // 外部トリガ立ち下がり

        public Condition()
        {
            InitializeComponent();
        }

        private void Condition_Load(object sender, EventArgs e)
        {
            string error_string;           // エラーコード文字列
            string message_string;         // メッセージ表示用
            int    ret;                    // 戻り値取得用
            int    index;                  // コンボボックスのインデックス
            short  ao_start_trigger;       // 変換開始条件
            short  ao_stop_trigger;        // 変換停止条件
            short  ao_clock_type;          // クロックの種類
            float  ao_generating_clock;    // ジェネレーティングクロック
            short  ao_clock_edge;          // 外部クロックの入力タイミング
            short  err_flag;               // 関数の実行エラーフラグ

            //----------------------------------------
            // パラメータの初期化
            //----------------------------------------
            err_flag       = 0;
            message_string = "";
            //----------------------------------------
            // アナログ出力変換開始条件用コンボボックスの作成
            //----------------------------------------
            comboBox_StartCondition.Items.Add("ソフトウェア");
            comboBox_StartCondition.Items.Add("外部トリガ立ち上がり");
            comboBox_StartCondition.Items.Add("外部トリガ立ち下がり");
            //----------------------------------------
            // アナログ出力変換開始条件を取得
            //----------------------------------------
            ret = aio.GetAoStartTrigger(Form1.g_id, out ao_start_trigger);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // エラーメッセージ生成
                    //--------------------
                    message_string = "aio.GetAoStartTrigger = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_StartCondition.Enabled = false;
                }
                //--------------------
                // アナログ出力変換開始条件用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_StartCondition.SelectedIndex = 0;
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 変換開始条件からインデックスに変換
                //--------------------
                index = 0;
                switch (ao_start_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    case 2:
                        index = 2;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // アナログ出力変換開始条件用コンボボックスの選択を変更
                //--------------------
                comboBox_StartCondition.SelectedIndex = index;
            }
            //----------------------------------------
            // アナログ出力クロック種類用コンボボックスの作成
            //----------------------------------------
            comboBox_ClockType.Items.Add("内部クロック");
            comboBox_ClockType.Items.Add("外部クロック");
            //----------------------------------------
            // クロックの種類を取得
            //----------------------------------------
            ret = aio.GetAoClockType(Form1.g_id, out ao_clock_type);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // エラーメッセージ生成
                    //--------------------
                    message_string = "aio.GetAoClockType = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_ClockType.Enabled = false;
                }
                //--------------------
                // アナログ出力クロック種類用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_ClockType.SelectedIndex = 0;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // クロック種類からインデックスに変換
                //--------------------
                index = 0;
                switch (ao_clock_type)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // アナログ出力クロック種類用コンボボックスの選択を変更
                //--------------------
                comboBox_ClockType.SelectedIndex = index;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // ジェネレーティングクロックを取得
            //----------------------------------------
            ret = aio.GetAoSamplingClock(Form1.g_id, out ao_generating_clock);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // エラーメッセージ生成
                    //--------------------
                    message_string = "aio.GetAoSamplingClock = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    textBox_GeneratingClock.Enabled = false;
                }
                //--------------------
                // ジェネレーティングクロックの初期表示を更新
                //--------------------
                textBox_GeneratingClock.Text = "1000";
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // ジェネレーティングクロックを更新
                //--------------------
                textBox_GeneratingClock.Text = Convert.ToDouble(ao_generating_clock).ToString("F6");
            }
            //----------------------------------------
            // アナログ出力外部クロック入力タイミング用コンボボックスの作成
            //----------------------------------------
            comboBox_ExternalClockInputTiming.Items.Add("立ち下がりエッジ");
            comboBox_ExternalClockInputTiming.Items.Add("立ち上がりエッジ");
            //----------------------------------------
            // 外部クロックの場合
            //----------------------------------------
            if (ao_clock_type == 1)
            {
                //----------------------------------------
                // 外部クロックの入力タイミングを取得
                //----------------------------------------
                ret = aio.GetAoClockEdge(Form1.g_id, out ao_clock_edge);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // エラーメッセージ生成
                        //--------------------
                        message_string = "aio.GetAoClockEdge = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        comboBox_ExternalClockInputTiming.Enabled = false;
                    }
                    //----------------------------------------
                    // 外部クロック入力タイミング用コンボボックスの選択を0番に変更
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = 0;
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 入力タイミングからインデックスに変換
                    //--------------------
                    index = 0;
                    switch (ao_clock_edge)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //----------------------------------------
                    // 外部クロック入力タイミング用コンボボックスの選択を変更
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // 外部クロック以外の場合
            //----------------------------------------
            else
            {
                //----------------------------------------
                // アナログ出力外部クロック入力タイミング用コンボボックスの選択を0番に変更
                //----------------------------------------
                comboBox_ExternalClockInputTiming.SelectedIndex = 0;
            }
            //----------------------------------------
            // アナログ出力変換停止条件用コンボボックスの作成
            //----------------------------------------
            comboBox_StopCondition.Items.Add("外部トリガ立ち上がり");
            comboBox_StopCondition.Items.Add("外部トリガ立ち下がり");
            //----------------------------------------
            // アナログ出力変換停止条件を取得
            //----------------------------------------
            ret = aio.GetAoStopTrigger(Form1.g_id, out ao_stop_trigger);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // エラーメッセージ生成
                    //--------------------
                    message_string = "aio.GetAoStopTrigger = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_StopCondition.Enabled = false;
                }
                //--------------------
                // アナログ出力変換停止条件用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_StopCondition.SelectedIndex = 0;
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 変換停止条件からインデックスに変換
                //--------------------
                index = 0;
                switch (ao_stop_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 0;
                        break;
                    case 2:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // アナログ出力変換停止条件用コンボボックスの選択を変更
                //--------------------
                comboBox_StopCondition.SelectedIndex = index;
            }
            //----------------------------------------
            // エラーが発生していた場合メッセージを表示
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "エラー", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // クロック種類コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_ClockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // コンボボックスのインデックス

            //----------------------------------------
            // コンボボックスからクロック種類のインデックスを取得
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // クロック種類による分岐処理
            //----------------------------------------
            switch (g_clock_type[index])
            {
                case 0:     // 内部クロック
                    textBox_GeneratingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    break;
                case 1:     // 外部クロック
                    textBox_GeneratingClock.Enabled = false;
                    comboBox_ExternalClockInputTiming.Enabled = true;
                    break;
                default:
                    textBox_GeneratingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // OKボタンをクリックした際の処理になります
        // ジェネレーティング条件の設定を行います
        //================================================================================
        private void button_OK_Click(object sender, EventArgs e)
        {
            string error_string;           // エラーコード文字列
            string text_string;            // テキストボックス更新用
            int    ret;                    // 戻り値取得用
            int    index;                  // コンボボックスのインデックス
            float  ao_generating_clock;    // ジェネレーティングクロック

            //----------------------------------------
            // アナログ出力の変換データ転送方式をデバイスバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoTransferMode(Form1.g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.SetAoTransferMode = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // データ格納用メモリ形式をFIFOに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoMemoryType(Form1.g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.SetAoMemoryType = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // コンボボックスから変換開始条件設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // アナログ出力変換開始条件の設定
            //----------------------------------------
            ret = aio.SetAoStartTrigger(Form1.g_id, g_start_trigger[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.SetAoStartTrigger = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // コンボボックスからクロック種類設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // アナログ出力クロック種類の設定
            //----------------------------------------
            ret = aio.SetAoClockType(Form1.g_id, g_clock_type[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.SetAoClockType = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // 内部クロックの場合
            //----------------------------------------
            if (g_clock_type[index] == 0)
            {
                //----------------------------------------
                // テキストボックスからジェネレーティングクロックを取得
                //----------------------------------------
                if (float.TryParse(textBox_GeneratingClock.Text, out ao_generating_clock) == false)
                {
                    ao_generating_clock = 1000;
                }
                //----------------------------------------
                // アナログ出力ジェネレーティングクロックの設定
                //----------------------------------------
                ret = aio.SetAoSamplingClock(Form1.g_id, ao_generating_clock);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = "aio.SetAoSamplingClock = " + ret.ToString() + "：" + error_string;
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // 外部クロックの場合
            //----------------------------------------
            else if (g_clock_type[index] == 1)
            {
                //----------------------------------------
                // コンボボックスから外部クロック入力タイミング設定値のインデックスを取得
                //----------------------------------------
                index = comboBox_ExternalClockInputTiming.SelectedIndex;
                //----------------------------------------
                // 外部クロックの入力タイミングの設定
                //----------------------------------------
                ret = aio.SetAoClockEdge(Form1.g_id, g_clock_edge[index]);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = "aio.AioSetAoClockEdge = " + ret.ToString() + "：" + error_string;
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // コンボボックスから変換停止条件設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // アナログ出力変換停止条件の設定
            //----------------------------------------
            ret = aio.SetAoStopTrigger(Form1.g_id, g_stop_trigger[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.AioSetAoStopTrigger = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // OK処理
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Cancelボタンをクリックした際の処理になります
        // ダイアログを終了します
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel処理
            //----------------------------------------
            this.Close();
        }
    }
}
