﻿using CWaveformGeneratorSDKCs;

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

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;    // C-WaveformGeneratorの上限チャネル数
        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short g_appid;                                 // アプリアクセス用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;
            //----------------------------------------
            // 波形タイプコンボボックスの作成
            //----------------------------------------
            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("任意");    //データファイルの読み方のサンプルはfileサンプルへ。
            //----------------------------------------
            // 波形タイプコンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // チャネル番号コンボボックスの作成
            // C-WaveformGeneratorの上限チャネル数
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // チャネル番号コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_AoChannel.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);
        }

        //================================================================================
        // デバイス設定ボタンをクリックした際の処理になります
        // デバイス設定のダイアログを開きます
        //================================================================================
        private void button_DeviceSetting_Click(object sender, EventArgs e)
        {
            DeviceSetting deviceinfodlg     = new DeviceSetting();
            ushort        aochannels        = 1;    // チャネル数
            float         frequency         = 1;    // 周波数
            ushort        unit_window       = 0;    // 周波数単位コンボボックス
            ushort        frequency_type    = 0;    // 周波数or周期
            ushort        frequency_unit    = 0;    // Hz(sec) or kHz(msec)
            ushort        oscillation_type  = 0;    // 発振タイプ
            ushort        oscillation_start = 1;    // 発振波数
            float         oscillation_stop  = 1;    // 発振停止波数or発振停止時間
            int           ret;                      // 戻り値取得用
            string        error_string;             // エラーコード文字列

            //----------------------------------------
            // 戻り値を空白にしておく
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // デバイス情報の取得を行う
            //----------------------------------------
            ret = gen.GetDeviceSetting(g_appid, out aochannels, out frequency, out frequency_type, out frequency_unit);
            unit_window = (ushort)((frequency_type << 1) + frequency_unit);
            //----------------------------------------
            // 発振の取得を行う
            //----------------------------------------
            ret = gen.GetOscillation(g_appid, out oscillation_type, out oscillation_start, out oscillation_stop);
            //----------------------------------------
            // デバイス設定のダイアログへ
            //----------------------------------------
            deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop);
            //----------------------------------------
            // OKを押された場合、処理を行う
            //----------------------------------------
            if (deviceinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // デバイス設定のダイアログから
                //----------------------------------------
                deviceinfodlg.devinfo_data_get(ref aochannels, ref frequency, ref unit_window, ref oscillation_type, ref oscillation_start, ref oscillation_stop);
                //----------------------------------------
                // 周波数単位から、タイプと単位に変更
                //----------------------------------------
                frequency_type = (ushort)((unit_window & 0x02) >> 1);
                frequency_unit = (ushort)(unit_window & 0x01);
                //----------------------------------------
                // デバイス情報の設定を行う
                //----------------------------------------
                ret = gen.SetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit);
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // エラー文字列取得
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // エラーコード,メッセージを表示
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetDeviceSetting = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // 発振の設定を行う
                //----------------------------------------
                ret = gen.SetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop);
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // エラー文字列取得
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // エラーコード,メッセージを表示
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetOscillation = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // デバイス設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnNum.Text = "デバイス設定：正常終了";
            }
            deviceinfodlg.Dispose();
        }

        //================================================================================
        // チャネル設定ボタンをクリックした際の処理になります
        // チャネル設定のダイアログを開きます
        //================================================================================
        private void button_ChannelSetting_Click(object sender, EventArgs e)
        {
            ChannelSetting channelinfodlg    = new ChannelSetting();
            ushort         aochannel         = 0;    // チャネル番号
            float          amplitude         = 0;    // 振幅
            float          offset            = 0;    // オフセット
            ushort         amplitude_type    = 0;    // 振幅タイプ
            ushort         amplitude_unit    = 0;    // 振幅単位
            float          duty              = 0;    // Duty
            float          phase             = 0;    // 位相
            ushort         sweep_type        = 0;    // 簡易スイープ
            ushort         oscillation_time  = 1;    // スイープ時間
            float          oscillation_start = 0;    // スイープ変化前
            float          oscillation_stop  = 0;    // スイープ変化後
            int            ret;                      // 戻り値取得用
            ushort         wave_type         = 0;    // 波形タイプ
            string         error_string;             // エラーコード文字列

            //----------------------------------------
            // 戻り値を空白にしておく
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (ushort)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // 波形タイプを取得を行う
            //----------------------------------------
            ret = gen.GetWaveType(g_appid, aochannel, out wave_type);
            //----------------------------------------
            // チャネル情報の取得を行う
            //----------------------------------------
            ret = gen.GetChannelSetting(g_appid, aochannel, out amplitude, out offset, out amplitude_type, out amplitude_unit, out duty, out phase);
            //----------------------------------------
            // 簡易スイープの取得を行う
            //----------------------------------------
            ret = gen.GetAoSweep(g_appid, aochannel, out sweep_type, out oscillation_time, out oscillation_start, out oscillation_stop);
            //----------------------------------------
            // チャネル設定のダイアログへ
            //----------------------------------------
            channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscillation_time, oscillation_start, oscillation_stop, wave_type);
            //----------------------------------------
            // OKを押された場合、処理を行う
            //----------------------------------------
            if (channelinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // チャネル設定のダイアログから
                //----------------------------------------
                channelinfodlg.chinfo_data_get(ref aochannel, ref amplitude, ref offset, ref amplitude_type,
                                               ref amplitude_unit, ref duty, ref phase, ref sweep_type,
                                               ref oscillation_time, ref oscillation_start, ref oscillation_stop);
                //----------------------------------------
                // チャネル情報の設定を行う
                //----------------------------------------
                ret = gen.SetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase);
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // エラー文字列取得
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // エラーコード,メッセージを表示
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetChannelSetting = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // 簡易スイープの設定を行う
                //----------------------------------------
                ret = gen.SetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop);
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0)
                {
                    //----------------------------------------
                    // エラー文字列取得
                    //----------------------------------------
                    gen.GetErrorString(ret, out error_string);
                    //----------------------------------------
                    // エラーコード,メッセージを表示
                    //----------------------------------------
                    textBox_ReturnNum.Text = string.Format("GenSetAoSweep = {0} : {1} ", ret, error_string);
                    return;
                }
                //----------------------------------------
                // チャネル設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnNum.Text = "チャネル設定：正常終了";
            }
            channelinfodlg.Dispose();
        }

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

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

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

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