﻿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 AoSimple
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

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

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short   g_id;                                                                  // デバイスアクセス用ID
        public short[] g_ao_range = new short[MAX_AO_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.P1TO5 };     // 1～5V

        public Form1()
        {
            InitializeComponent();
        }

        private void AoSimple_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // 指定チャネル出力のチャネル番号の初期表示を更新
            //----------------------------------------
            textBox_ChNo.Text = "0";
            //----------------------------------------
            // 指定チャネル出力のデータの初期表示を更新
            //----------------------------------------
            textBox_SingleAoData.Text = "0.0";
            //----------------------------------------
            // 複数チャネル出力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // 複数チャネル出力のデータの初期表示を更新
            //----------------------------------------
            textBox_MultiAoDataCh0.Text = "0.0";
            textBox_MultiAoDataCh1.Text = "0.0";
            textBox_MultiAoDataCh2.Text = "0.0";
            textBox_MultiAoDataCh3.Text = "0.0";
            //----------------------------------------
            // アナログ出力レンジ用コンボボックスの作成
            //----------------------------------------
            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("1 - +5V");
            //----------------------------------------
            // アナログ出力レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 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;
        }

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

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

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

            //----------------------------------------
            // テキストボックスからチャネル番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChNo.Text, out ao_channel) == false)
            {
                ao_channel = 0;
            }
            //----------------------------------------
            // テキストボックスから出力データを取得
            //----------------------------------------
            if (float.TryParse(textBox_SingleAoData.Text, out ao_data) == false)
            {
                ao_data = 0.0f;
            }
            //----------------------------------------
            // 指定チャネルのDA変換データを出力
            //----------------------------------------
            ret = aio.SingleAoEx(g_id, ao_channel, ao_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SingleAoEx = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioMultiAoExボタンをクリックした際の処理になります
        // 出力データ(電圧または電流)を複数チャネルから1回出力します
        //================================================================================
        private void button_MultiAoEx_Click(object sender, EventArgs e)
        {
            string  error_string;    // エラーコード文字列
            int     ret;             // 戻り値取得用
            float[] ao_data;         // 出力データ
            short   ao_channels;     // チャネル数

            //----------------------------------------
            // テキストボックスからチャネル数を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ao_channels) == false)
            {
                ao_channels = 1;
            }
            //----------------------------------------
            // DA変換データ格納配列の確保
            //----------------------------------------
            try
            {
                ao_data = new float[ao_channels];
            }
            //----------------------------------------
            // DA変換データ格納配列の確保に失敗した場合
            //----------------------------------------
            catch
            {
                textBox_ReturnCode.Text = "DA変換データ格納配列の確保に失敗しました";
                return;
            }
            //----------------------------------------
            // DA変換データを初期化
            //----------------------------------------
            Array.Clear(ao_data, 0, ao_channels);
            //----------------------------------------
            // テキストボックスからDA変換データを取得
            //----------------------------------------
            if (0 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh0.Text, out ao_data[0]) == false)
                {
                    ao_data[0] = 0.0f;
                }
            }
            if (1 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh1.Text, out ao_data[1]) == false)
                {
                    ao_data[1] = 0.0f;
                }
            }
            if (2 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh2.Text, out ao_data[2]) == false)
                {
                    ao_data[2] = 0.0f;
                }
            }
            if (3 < ao_channels)
            {
                if (float.TryParse(textBox_MultiAoDataCh3.Text, out ao_data[3]) == false)
                {
                    ao_data[3] = 0.0f;
                }
            }
            //----------------------------------------
            // 指定チャネルのDA変換データを出力
            //----------------------------------------
            ret = aio.MultiAoEx(g_id, ao_channels, ao_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.MultiAoEx = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // DA変換データ格納配列の解放
            //----------------------------------------
            ao_data = null;
        }

        //================================================================================
        // 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();
        }
    }
}
