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

namespace MultiChannelWave
{
    public partial class ChannelSetting : Form
    {
        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int CWAVEFORMGENERATOR_SWEEPTIME = 10;
        public const int WAVETYPE_SINE = 0;
        public const int WAVETYPE_SQUARE = 4;
        //----------------------------------------
        // 変数定義
        //----------------------------------------
        short m_aochannel;
        float m_amplitude;
        float m_offset;
        short m_amplitude_type;
        short m_amplitude_unit;
        float m_duty;
        float m_phase;
        short m_sweep_type;
        short m_sweep_time;
        float m_sweep_start;
        float m_sweep_stop;
        short m_wave_type;

        public ChannelSetting()
        {
            InitializeComponent();
        }

        //----------------------------------------
        // データの受け渡し（設定）
        //----------------------------------------
        internal void chinfo_data_set( short aochannel, float amplitude, float offset, short amplitudetype,
                                       short amplitudeunit, float duty, float phase, short sweeptype,
                                       short sweeptime, float sweepstart, float sweepstop, short wavetype)
        {
            m_aochannel         = aochannel;
            m_amplitude         = amplitude;
            m_offset            = offset;
            m_amplitude_type    = amplitudetype;
            m_amplitude_unit    = amplitudeunit;
            m_duty              = duty;
            m_phase             = phase;
            m_sweep_type        = sweeptype;
            m_sweep_time        = sweeptime;
            m_sweep_start       = sweepstart;
            m_sweep_stop        = sweepstop;
            m_wave_type         = wavetype;
        }

        //----------------------------------------
        // データの受け渡し（取得）
        //----------------------------------------
        internal void chinfo_data_get( ref short aochannel, ref float amplitude, ref float offset, ref short amplitudetype,
                                       ref short amplitudeunit, ref float duty, ref float phase, ref short sweeptype,
                                       ref short sweeptime, ref float sweepstart, ref float sweepstop)
        {
            aochannel           = m_aochannel;
            amplitude           = m_amplitude;
            offset              = m_offset;
            amplitudetype       = m_amplitude_type;
            amplitudeunit       = m_amplitude_unit;
            duty                = m_duty;
            phase               = m_phase;
            sweeptype           = m_sweep_type;
            sweeptime           = m_sweep_time;
            sweepstart          = m_sweep_start;
            sweepstop           = m_sweep_stop;
        }

        //----------------------------------------
        // 初期表示
        // 値は現在の設定値
        //----------------------------------------
        private void ChannelSetting_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // チャネル番号の表示を更新
            //----------------------------------------
            label_ChannelNo.Text = m_aochannel.ToString();
            //----------------------------------------
            // 振幅の初期表示を更新
            //----------------------------------------
            textBox_Amplitude.Text = string.Format("{0:F3}", m_amplitude);
            //----------------------------------------
            // オフセットの初期表示を更新
            //----------------------------------------
            textBox_Offset.Text = string.Format("{0:F3}", m_offset);
            //----------------------------------------
            // Duty(シンメトリ)の初期表示を更新
            //----------------------------------------
            textBox_Duty.Text = string.Format("{0:F2}", m_duty);
            //----------------------------------------
            // 位相の初期表示を更新
            //----------------------------------------
            textBox_Phase.Text = string.Format("{0:F2}", m_phase);
            //----------------------------------------
            // スイープ変化前の初期表示を更新
            //----------------------------------------
            textBox_SweepStart.Text = string.Format("{0:F2}", m_sweep_start);
            //----------------------------------------
            // スイープ変化後の初期表示を更新
            //----------------------------------------
            textBox_SweepStop.Text = string.Format("{0:F2}", m_sweep_stop);
            //----------------------------------------
            // 振幅タイプコンボボックスの作成
            //----------------------------------------
            comboBox_AmplitudeType.Items.Add("振幅");
            comboBox_AmplitudeType.Items.Add("Vp-p");
            //----------------------------------------
            // 振幅タイプコンボボックスの選択を変更
            //----------------------------------------
            comboBox_AmplitudeType.SelectedIndex = m_amplitude_type;
            //----------------------------------------
            // 振幅単位コンボボックスの作成
            //----------------------------------------
            comboBox_AmplitudeUnit.Items.Add("V or mA");
            comboBox_AmplitudeUnit.Items.Add("LSB");
            //----------------------------------------
            // 振幅単位コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_AmplitudeUnit.SelectedIndex = m_amplitude_unit;
            //----------------------------------------
            // 簡易スイープコンボボックスの作成
            //----------------------------------------
            comboBox_SweepType.Items.Add("なし");
            //----------------------------------------
            // サイン波の場合、「周波数スイープ」「振幅スイープ」「DCオフセットスイープ」の選択肢作成
            //----------------------------------------
            if (m_wave_type == WAVETYPE_SINE)
            {
                comboBox_SweepType.Items.Add("周波数スイープ");
                comboBox_SweepType.Items.Add("振幅スイープ");
                comboBox_SweepType.Items.Add("DCオフセットスイープ");
            }
            //----------------------------------------
            // 矩形波の場合、「デューティスイープ」の選択肢作成
            //----------------------------------------
            else if (m_wave_type == WAVETYPE_SQUARE)
            {
                comboBox_SweepType.Items.Add("デューティスイープ");
            }
            //----------------------------------------
            // 簡易スイープコンボボックスの選択を変更
            //----------------------------------------
            comboBox_SweepType.SelectedIndex = m_sweep_type;
            //----------------------------------------
            // スイープ時間コンボボックスの作成
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_SWEEPTIME; i++)
            {
                comboBox_SweepTime.Items.Add((i+1).ToString());
            }
            //----------------------------------------
            // スイープ時間コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_SweepTime.SelectedIndex = (m_sweep_time - 1);
        }

        //----------------------------------------
        // OKボタンを押したときの処理
        //----------------------------------------
        private void button_Ok_Click(object sender, EventArgs e)
        {
            float   pre_amplitude;      // 振幅
            float   pre_offset;         // オフセット
            short   pre_amplitude_unit; // 振幅単位
            short   pre_amplitude_type; // 振幅タイプ
            float   pre_duty;           // Duty比
            float   pre_phase;          // 位相
            short   pre_sweep_type;     // 簡易スイープ
            short   pre_sweep_time;     // スイープ時間
            float   pre_sweep_start;    // スイープ変化前
            float   pre_sweep_stop;     // スイープ変化後

            //----------------------------------------
            // テキストボックスから振幅を取得
            //----------------------------------------
            if (float.TryParse(textBox_Amplitude.Text, out pre_amplitude) == false)
            {
                pre_amplitude = 0;
            }
            //----------------------------------------
            // テキストボックスからオフセットを取得
            //----------------------------------------
            if (float.TryParse(textBox_Offset.Text, out pre_offset) == false)
            {
                pre_offset = 0;
            }
            //----------------------------------------
            // テキストボックスからDuty比を取得
            //----------------------------------------
            if (float.TryParse(textBox_Duty.Text, out pre_duty) == false)
            {
                pre_duty = 0;
            }
            //----------------------------------------
            // テキストボックスから位相を取得
            //----------------------------------------
            if (float.TryParse(textBox_Phase.Text, out pre_phase) == false)
            {
                pre_phase = 0;
            }
            //----------------------------------------
            // コンボボックスから振幅単位を取得
            //----------------------------------------
            pre_amplitude_unit = (short)comboBox_AmplitudeUnit.SelectedIndex;
            //----------------------------------------
            // コンボボックスから振幅タイプを取得
            //----------------------------------------
            pre_amplitude_type = (short)comboBox_AmplitudeType.SelectedIndex;
            //----------------------------------------
            // テキストボックスからスイープ変化前を取得
            //----------------------------------------
            if (float.TryParse(textBox_SweepStart.Text, out pre_sweep_start) == false)
            {
                pre_sweep_start = 0;
            }
            //----------------------------------------
            // テキストボックスからスイープ変化後を取得
            //----------------------------------------
            if (float.TryParse(textBox_SweepStop.Text, out pre_sweep_stop) == false)
            {
                pre_sweep_stop = 0;
            }
            //----------------------------------------
            // コンボボックスから簡易スイープを取得
            //----------------------------------------
            pre_sweep_type = (short)comboBox_SweepType.SelectedIndex;
            //----------------------------------------
            // コンボボックスからスイープタイプを取得
            //----------------------------------------
            pre_sweep_time = (short)(comboBox_SweepTime.SelectedIndex + 1);

            //----------------------------------------
            // 子ダイアログで設定した値をメインに渡す
            // メインで設定を行う
            //----------------------------------------
            m_amplitude         = pre_amplitude;
            m_offset            = pre_offset;
            m_amplitude_type    = pre_amplitude_type;
            m_amplitude_unit    = pre_amplitude_unit;
            m_duty              = pre_duty;
            m_phase             = pre_phase;
            m_sweep_type        = pre_sweep_type;
            m_sweep_time        = pre_sweep_time;
            m_sweep_start       = pre_sweep_start;
            m_sweep_stop        = pre_sweep_stop;
            this.Close();
        }

        //----------------------------------------
        // Cancelボタンを押したときの処理
        //----------------------------------------
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
