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

namespace MultiChannelWave
{
    public partial class Form1 : Form
    {
        CCWaveformGeneratorClass c_fgen;

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // CWaveformGeneratorの上限チャネル数


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // C-WaveFormGeneratorをSDKで呼び出すための初期処理
            //----------------------------------------
            try
            {
                //----------------------------------------
                // C-WaveformGeneratorのクラスを作成
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // エラーの時、エラーメッセージを表示
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                this.Close();
                return;
            }

            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            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;
        }

        //================================================================================
        // OpenApplicationボタンをクリックした際の処理になります
        // アプリの初期化処理を行います
        //================================================================================
        private void button_OpenApplicaion_Click(object sender, EventArgs e)
        {
            string  device_name;        // デバイス名
            string  arg1;               // 予約
            short   openmode;           // 画面サイズ
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // テキストボックスからデバイス名を取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // コンボボックスから画面サイズを取得
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // アプリの初期化処理を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // DeviceSettingボタンをクリックした際の処理になります
        // デバイス設定のダイアログを開きます
        //================================================================================
        private void button_DeviceSetting_Click(object sender, EventArgs e)
        {
            DeviceSetting   deviceinfodlg       = new DeviceSetting();
            short           aochannels          = 1;    // チャネル数
            float           frequency           = 1;    // 周波数
            short           unit_window         = 0;    // 周波数単位コンボボックス
            short           frequency_type      = 0;    // 周波数or周期
            short           frequency_unit      = 0;    // Hz(sec) or kHz(msec)
            short           oscilation_type     = 0;    // 発振タイプ
            short           oscilation_start    = 1;    // 発振波数
            float           oscilation_stop     = 1;    // 発振停止波数or発振停止時間
            short           arg1                = 0;    // 予約
            long            ret;                        // 戻り値取得用

            //----------------------------------------
            // 戻り値を空白にしておく
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // デバイス情報の取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetDeviceSetting(ref aochannels, ref frequency, ref frequency_type, ref frequency_unit, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            unit_window = (short)((frequency_type << 1) + frequency_unit);
            //----------------------------------------
            // 発振の取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetOscilation(ref oscilation_type, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // デバイス設定のダイアログへ
            //----------------------------------------
            deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscilation_type, oscilation_start, oscilation_stop);
            //----------------------------------------
            // OKを押された場合、処理を行う
            //----------------------------------------
            if (deviceinfodlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // デバイス設定のダイアログから
                //----------------------------------------
                deviceinfodlg.devinfo_data_get(ref aochannels, ref frequency, ref unit_window, ref oscilation_type, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // 周波数単位から、タイプと単位に変更
                //----------------------------------------
                frequency_type = (short)((unit_window & 0x02) >> 1);
                frequency_unit = (short)(unit_window & 0x01);
                //----------------------------------------
                // arg1（予約）
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // デバイス情報の設定を行う
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetDeviceSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // 発振の設定を行う
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetOscilation = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // 条件設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnNum.Text = "デバイス設定：正常終了";
            }
            deviceinfodlg.Dispose();
        }

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

            //----------------------------------------
            // 戻り値を空白にしておく
            //----------------------------------------
            textBox_ReturnNum.Text = "";
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // 波形タイプを取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetWaveType(aochannel, ref wave_type, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // チャネル情報の取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetChannelSetting(aochannel, ref amplitude, ref offset, ref amplitude_type, ref amplitude_unit, ref duty, ref phase, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // 簡易スイープの取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetAoSweep(aochannel, ref sweep_type, ref oscilation_time, ref oscilation_start, ref oscilation_stop, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // チャネル設定のダイアログへ
            //----------------------------------------
            channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscilation_time, oscilation_start, oscilation_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 oscilation_time, ref oscilation_start, ref oscilation_stop);
                //----------------------------------------
                // arg1（予約）
                //----------------------------------------
                arg1 = 0;
                //----------------------------------------
                // チャネル情報の設定を行う
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetChannelSetting = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // 簡易スイープの設定を行う
                //----------------------------------------
                try
                {
                    ret = c_fgen.SetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // 正常終了ではない場合、エラーコードを表示
                //----------------------------------------
                if (ret != 0) {
                    textBox_ReturnNum.Text = string.Format("SetAoSweep = {0} ", ret);
                    return;
                }
                //----------------------------------------
                // 条件設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnNum.Text = "チャネル設定：正常終了";
            }
            channelinfodlg.Dispose();
        }

        //================================================================================
        // SetWaveTypeボタンをクリックした際の処理になります
        // 波形タイプの設定を行います
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wave_type;          // 波形タイプ
            short   aochannel;          // チャネル番号
            short   arg1;               // 予約
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // コンボボックスから波形タイプを取得
            //----------------------------------------
            wave_type = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // 波形タイプの設定を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wave_type, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // StartOperationボタンをクリックした際の処理になります
        // アプリの出力開始を行います
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // 予約
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // アプリの出力開始を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // GetApplicationStatusボタンをクリックした際の処理になります
        // 動作状況の取得を行います
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;        // ステータス
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // 動作状況の取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // ステータスを表示
            //----------------------------------------
            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();
            }
        }

        //================================================================================
        // StopOperationボタンをクリックした際の処理になります
        // アプリの出力停止を行います
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;               // 予約
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // アプリの出力停止を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // CloseApplicationボタンをクリックした際の処理になります
        // アプリの終了処理を行います
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;               // 予約
            long    ret;                // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // アプリの終了処理を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // 閉じるボタンをクリックした際の処理になります
        // アプリケーションの終了処理を行います
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;             // 予約

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // デバイスの終了処理を行う
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }
    }
}
