﻿using CaioCs;

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

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int MAX_AI_RANGE_NUM = 32;    // アナログ入力レンジ配列最大要素数

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short   g_id;                                                                  // デバイスアクセス用ID
        public int     g_ai_range_index;                                                      // レンジのインデックス
        public short[] g_ai_range = new short[MAX_AI_RANGE_NUM]{(short)CaioConst.PM10,        // ±10V
                                                                (short)CaioConst.PM5,         // ±5V
                                                                (short)CaioConst.PM25,        // ±2.5V
                                                                (short)CaioConst.PM125,       // ±1.25V
                                                                (short)CaioConst.PM1,         // ±1V
                                                                (short)CaioConst.PM0625,      // ±0.625V
                                                                (short)CaioConst.PM05,        // ±0.5V
                                                                (short)CaioConst.PM03125,     // ±0.3125V
                                                                (short)CaioConst.PM025,       // ±0.25V
                                                                (short)CaioConst.PM0125,      // ±0.125V
                                                                (short)CaioConst.PM01,        // ±0.1V
                                                                (short)CaioConst.PM005,       // ±0.05V
                                                                (short)CaioConst.PM0025,      // ±0.025V
                                                                (short)CaioConst.PM00125,     // ±0.0125V
                                                                (short)CaioConst.PM001,       // ±0.01V
                                                                (short)CaioConst.P10,         // 0～10V
                                                                (short)CaioConst.P5,          // 0～5V
                                                                (short)CaioConst.P4095,       // 0～4.095V
                                                                (short)CaioConst.P25,         // 0～2.5V
                                                                (short)CaioConst.P125,        // 0～1.25V
                                                                (short)CaioConst.P1,          // 0～1V
                                                                (short)CaioConst.P05,         // 0～0.5V
                                                                (short)CaioConst.P025,        // 0～0.25V
                                                                (short)CaioConst.P01,         // 0～0.1V
                                                                (short)CaioConst.P005,        // 0～0.05V
                                                                (short)CaioConst.P0025,       // 0～0.025V
                                                                (short)CaioConst.P00125,      // 0～0.0125V
                                                                (short)CaioConst.P001,        // 0～0.01V
                                                                (short)CaioConst.P20MA,       // 0～20mA
                                                                (short)CaioConst.P4TO20MA,    // 4～20mA
                                                                (short)CaioConst.PM20MA,      // ±20mA
                                                                (short)CaioConst.P1TO5};      // 1～5V

        public Form1()
        {
            InitializeComponent();
        }

        private void AiSimple_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // 指定チャネル入力のチャネル番号の初期表示を更新
            //----------------------------------------
            textBox_ChNo.Text = "0";
            //----------------------------------------
            // 複数チャネル入力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // アナログ入力レンジ用コンボボックスの作成
            //----------------------------------------
            comboBox_RangeValue.Items.Add("-10 - +10V");
            comboBox_RangeValue.Items.Add("-5 - +5V");
            comboBox_RangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_RangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_RangeValue.Items.Add("-1 - +1V");
            comboBox_RangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_RangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_RangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_RangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_RangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_RangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_RangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_RangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_RangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_RangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +10V");
            comboBox_RangeValue.Items.Add("0 - +5V");
            comboBox_RangeValue.Items.Add("0 - +4.095V");
            comboBox_RangeValue.Items.Add("0 - +2.5V");
            comboBox_RangeValue.Items.Add("0 - +1.25V");
            comboBox_RangeValue.Items.Add("0 - +1V");
            comboBox_RangeValue.Items.Add("0 - +0.5V");
            comboBox_RangeValue.Items.Add("0 - +0.25V");
            comboBox_RangeValue.Items.Add("0 - +0.1V");
            comboBox_RangeValue.Items.Add("0 - +0.05V");
            comboBox_RangeValue.Items.Add("0 - +0.025V");
            comboBox_RangeValue.Items.Add("0 - +0.0125V");
            comboBox_RangeValue.Items.Add("0 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +20mA");
            comboBox_RangeValue.Items.Add("4 - +20mA");
            comboBox_RangeValue.Items.Add("-20 - +20mA");
            comboBox_RangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // アナログ入力レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 0;
            //----------------------------------------
            // アナログ入力方式用コンボボックスの作成
            //----------------------------------------
            comboBox_InpMethod.Items.Add("シングルエンド入力");
            comboBox_InpMethod.Items.Add("差動入力");
            //----------------------------------------
            // アナログ入力方式用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_InpMethod.SelectedIndex = 0;
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id             = 0;
            g_ai_range_index = 0;
        }

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

            //----------------------------------------
            // デバイス名をテキストボックスから取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス初期化
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetAiRangeAllボタンをクリックした際の処理になります
        // アナログ入力の入力レンジの設定を行います
        //================================================================================
        private void button_SetAiRangeAll_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // GUIの情報を更新
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // コンボボックスからレンジ設定値のインデックスを取得
            //----------------------------------------
            g_ai_range_index = comboBox_RangeValue.SelectedIndex;
            //----------------------------------------
            // アナログ入力レンジの設定(全チャネル)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_id, g_ai_range[g_ai_range_index]);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiRangeAll = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetAiInputMethodボタンをクリックした際の処理になります
        // アナログ入力の入力方式の設定を行います
        //================================================================================
        private void button_SetAiInpMethod_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    input_method;    // 入力方式

            //----------------------------------------
            // GUIの情報を更新
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // コンボボックスから入力方式設定値を取得
            //----------------------------------------
            input_method = comboBox_InpMethod.SelectedIndex;
            //----------------------------------------
            // アナログ入力方式の設定
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_id, (short)input_method);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiInputMethod = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSingleAiExボタンをクリックした際の処理になります
        // 入力データ(電圧または電流)を指定チャネルから1回入力します
        //================================================================================
        private void button_SingleAiEx_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            float  ai_data;         // 入力データ
            short  ai_channel;      // チャネル番号

            //----------------------------------------
            // テキストボックスからチャネル番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChNo.Text, out ai_channel) == false)
            {
                ai_channel = 0;
            }
            //----------------------------------------
            // 指定チャネルのAD変換データを取得
            //----------------------------------------
            ret = aio.SingleAiEx(g_id, ai_channel, out ai_data);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // エラー文字列取得
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // メッセージ表示
            //--------------------
            textBox_ReturnCode.Text = "aio.SingleAiEx = " + ret.ToString() + "：" + error_string;
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // 指定チャネルのAD変換データを表示
            //----------------------------------------
            if ((g_ai_range[g_ai_range_index] == (short)CaioConst.P20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.P4TO20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.PM20MA))
            {
                text_string = string.Format("{0:F5}mA", ai_data);
            }
            else
            {
                text_string = string.Format("{0:F5}V", ai_data);
            }
            //--------------------
            // 変換データ表示を更新
            //--------------------
            textBox_SingleAiData.Text = text_string;
        }

        //================================================================================
        // AioMultiAiExボタンをクリックした際の処理になります
        // 入力データ(電圧または電流)を複数チャネルから1回入力します
        //================================================================================
        private void button_MultiAiEx_Click(object sender, EventArgs e)
        {
            string    error_string;    // エラーコード文字列
            string    text_string;     // テキストボックス更新用
            int       ret;             // 戻り値取得用
            float[]   ai_data;         // 入力データ
            short     i;               // 入力データ表示ループ用
            short     ai_channels;     // チャネル数

            //----------------------------------------
            // テキストボックスからチャネル数を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // AD変換データ格納配列の確保
            //----------------------------------------
            try
            {
                ai_data = new float[ai_channels];
            }
            //----------------------------------------
            // AD変換データ格納配列の確保に失敗した場合
            //----------------------------------------
            catch
            {
                textBox_ReturnCode.Text = "AD変換データ格納配列の確保に失敗しました";
                return;
            }
            //----------------------------------------
            // 複数チャネルのAD変換データを取得
            //----------------------------------------
            ret = aio.MultiAiEx(g_id, ai_channels, ai_data);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // エラー文字列取得
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // メッセージ表示
            //--------------------
            textBox_ReturnCode.Text = "aio.MultiAiEx = " + ret.ToString() + "：" + error_string;
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // AD変換データを表示
            //----------------------------------------
            //--------------------
            // AD変換データ表示用ListBoxをクリア
            //--------------------
            listBox_MultiInpData.Items.Clear();
            //--------------------
            // 設定チャネル数まで処理を繰り返す
            //--------------------
            for (i = 0; i < ai_channels; i++)
            {
                //--------------------
                // 表示データの生成
                //--------------------
                if ((g_ai_range[g_ai_range_index] == (short)CaioConst.P20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.P4TO20MA) || (g_ai_range[g_ai_range_index] == (short)CaioConst.PM20MA))
                {
                    text_string = string.Format("{0,3}              {1:F5}mA", i, ai_data[i]);
                }
                else
                {
                    text_string = string.Format("{0,3}              {1:F5}V", i, ai_data[i]);
                }
                //--------------------
                // 変換データ表示を更新
                //--------------------
                listBox_MultiInpData.Items.Add(text_string);
            }
        }

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

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

        //================================================================================
        // 閉じるボタンをクリックした際の処理になります
        // ダイアログを終了します
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }
    }
}