﻿using CWaveformGeneratorSDKCs;

namespace FileOperation
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // クラスCWaveformGeneratorSDKインスタンス作成
        //----------------------------------------
        CWaveformGeneratorSDK gen = new CWaveformGeneratorSDK();

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;    // 最大チャネル数
        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short g_appid = 0;                             // アプリアクセス用ID

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // 画面サイズコンボボックスの作成
            //----------------------------------------
            comboBox_OpenMode.Items.Add("最小化");
            comboBox_OpenMode.Items.Add("通常");
            comboBox_OpenMode.Items.Add("最大化");
            //----------------------------------------
            // 画面サイズ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // チャネル番号コンボボックスの作成
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // チャネル番号コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
            //----------------------------------------
            // 波形タイプコンボボックスの作成
            //----------------------------------------
            comboBox_WaveType.Items.Add("正弦");
            comboBox_WaveType.Items.Add("固定");
            comboBox_WaveType.Items.Add("三角");
            comboBox_WaveType.Items.Add("のこぎり");
            comboBox_WaveType.Items.Add("矩形");
            comboBox_WaveType.Items.Add("ノイズ");
            comboBox_WaveType.Items.Add("任意");
            //----------------------------------------
            // 波形タイプコンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // データ保存コンボボックスの作成
            //----------------------------------------
            comboBox_Param.Items.Add("単チャネル");
            comboBox_Param.Items.Add("複数チャネル");
            //----------------------------------------
            // データ保存コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Param.SelectedIndex = 0;
        }

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

            //----------------------------------------
            // テキストボックスからデバイス名を取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // コンボボックスから画面サイズを取得
            //----------------------------------------
            openmode = (ushort)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // アプリの初期化処理を行う
            //----------------------------------------
            ret = gen.OpenApplication(device_name, out g_appid, openmode);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenOpenApplication = {0} : {1} ", ret, error_string);
        }

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

            //----------------------------------------
            // テキストボックスからデバイス名を取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス名の設定
            //----------------------------------------
            ret = gen.SetDeviceName(g_appid, device_name);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetDeviceName = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // ...ボタンをクリックした際の処理になります  (設定ファイル)
        // iniファイルを選択するダイアログを表示します
        //================================================================================
        private void button_SettingSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // ファイル名が存在するかどうかをチェックしない
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // ファイルのパスが存在する場合は、ファイルのパスをダイアログの初期パスに設定します
            //----------------------------------------
            if (textBox_SettingFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_SettingFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // ファイルのパスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // 選択したパスを表示
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // GenExportSettingFileボタンをクリックした際の処理になります
        // 設定をiniファイルに保存します
        //================================================================================
        private void button_GenExportSettingFile_Click(object sender, EventArgs e)
        {
            string filepath;        // ファイルパス
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // テキストボックスから設定ファイルパスを取得
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // 設定をiniファイルに保存
            //----------------------------------------
            ret = gen.ExportSettingFile(g_appid, filepath);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenExportSettingFile = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // GenImportSettingFileボタンをクリックした際の処理になります
        // iniファイルの設定を読み込みます。
        //================================================================================
        private void button_GenImportSettingFile_Click(object sender, EventArgs e)
        {
            string filepath;        // ファイルパス
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // テキストボックスから設定ファイルパスを取得
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // iniファイルの設定読み出しを行う
            //----------------------------------------
            ret = gen.ImportSettingFile(g_appid, filepath);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenImportSettingFile = {0} : {1} ", ret, error_string);
        }

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

            //----------------------------------------
            // コンボボックスから波形タイプを取得
            //----------------------------------------
            wavetype = (ushort)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // 波形タイプの設定を行う
            //----------------------------------------
            ret = gen.SetWaveType(g_appid, aochannel, wavetype);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetWaveType = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // ...ボタンをクリックした際の処理になります (データファイル)
        // csvファイルを選択するダイアログを表示します
        //================================================================================
        private void button_ArSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // ファイル名が存在するかどうかをチェックしない
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // パスが存在する場合は、現在のパスをダイアログの初期パスに設定します
            //----------------------------------------
            if (textBox_ArbitraryDataFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // パスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // 選択したパスを表示
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // GenSetArbitraryDataFileボタンをクリックした際の処理になります
        // データファイル（csvファイル）パスを設定します
        //================================================================================
        private void button_GenSetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // ファイルパス
            ushort aochannel;       // チャネル番号
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // テキストボックスからデータファイルパスを取得
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // データファイルパスの設定
            //----------------------------------------
            ret = gen.SetArbitraryDataFile(g_appid, aochannel, filepath);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSetArbitraryDataFile = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // GenGetArbitraryDataFileボタンをクリックした際の処理になります
        // 設定されているデータファイル（csvファイル）パスを取得します
        //================================================================================
        private void button_GenGetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // ファイルパス
            ushort aochannel;       // チャネル番号
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // 設定されているチャネルを取得
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // データファイルパスの取得
            //----------------------------------------
            ret = gen.GetArbitraryDataFile(g_appid, aochannel, out filepath);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenGetArbitraryDataFile = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // データファイルパスを表示
            //----------------------------------------
            textBox_ArbitraryDataFile.Text = filepath;
        }

        //================================================================================
        // GenSaveDataFileボタンをクリックした際の処理になります
        // 指定したチャネルのデータをcsvファイルに保存します
        //================================================================================
        private void button_GenSaveDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // ファイルパス
            short  aochannel;       // チャネル番号
            short  param;           // 単数or複数
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // テキストボックスからデータファイル名を取得
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // コンボボックスからチャネルのパラメータ（単数or複数）を取得
            //----------------------------------------
            param = (short)comboBox_Param.SelectedIndex;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // データファイルの設定
            //----------------------------------------
            //-------------------
            // 単数チャネルの場合、第4引数をチャネル番号にする
            //-------------------
            if (param == 0)
            {
                ret = gen.SaveDataFile(g_appid, filepath, param, aochannel);
            }
            //-------------------
            // 複数チャネルの場合、第4引数を0にする
            //-------------------
            else
            {
                ret = gen.SaveDataFile(g_appid, filepath, param, 0);
            }
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenSaveDataFile = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // GenStartOperationボタンをクリックした際の処理になります
        // アプリの出力開始を行います
        //================================================================================
        private void button_GenStartOperation_Click(object sender, EventArgs e)
        {
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // アプリの出力開始を行う
            //----------------------------------------
            ret = gen.StartOperation(g_appid);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenStartOperation = {0} : {1} ", ret, error_string);
        }

        //================================================================================
        // GenGetApplicationStatusボタンをクリックした際の処理になります
        // 動作状況の取得を行います
        //================================================================================
        private void button_GenGetApplicationStatus_Click(object sender, EventArgs e)
        {
            uint   status = 0;      // ステータス
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // 動作状況の取得を行う
            //----------------------------------------
            ret = gen.GetApplicationStatus(g_appid, out status);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenGetApplicationStatus = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // ステータスを表示
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(停止中)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(動作中)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(開始トリガ待ち)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(動作中、開始トリガ待ち)";
            }
            else
            {
                textBox_Status.Text = string.Format("{0} ", status);
            }
        }

        //================================================================================
        // GenStopOperationボタンをクリックした際の処理になります
        // アプリの出力停止を行います
        //================================================================================
        private void button_GenStopOperation_Click(object sender, EventArgs e)
        {
            int    ret;             // 戻り値取得用
            string error_string;    // エラーコード文字列

            //----------------------------------------
            // アプリの出力停止を行う
            //----------------------------------------
            ret = gen.StopOperation(g_appid);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenStopOperation = {0} : {1} ", ret, error_string);
        }

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

            //----------------------------------------
            // デバイスの終了処理を行う
            //----------------------------------------
            ret = gen.CloseApplication(g_appid, out status);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            gen.GetErrorString(ret, out error_string);
            //----------------------------------------
            // エラーコード,メッセージを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GenCloseApplication = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // ステータスを表示
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "0(正常終了)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "1(C-WaveformGenerator終了エラー)";
            }
            else
            {
                textBox_Status.Text = string.Format("{0} ", status);
            }
        }

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