﻿Public Class ChannelSetting
    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_SWEEPTIME As Integer = 10
    Public Const WAVETYPE_SINE As Integer = 0
    Public Const WAVETYPE_SQUARE As Integer = 4
    '----------------------------------------
    ' 変数定義
    '----------------------------------------
    Dim m_aochannel As UShort
    Dim m_amplitude As Single
    Dim m_offset As Single
    Dim m_amplitude_type As UShort
    Dim m_amplitude_unit As UShort
    Dim m_duty As Single
    Dim m_phase As Single
    Dim m_sweep_type As UShort
    Dim m_sweep_time As UShort
    Dim m_sweep_start As Single
    Dim m_sweep_stop As Single
    Dim m_wave_type As UShort

    '================================================================================
    ' データの受け渡し（設定）
    '================================================================================
    Sub chinfo_data_set(aochannel As UShort, amplitude As Single, offset As Single, amplitudetype As UShort,
                        amplitudeunit As UShort, duty As Single, phase As Single, sweeptype As UShort,
                        sweeptime As UShort, sweepstart As Single, sweepstop As Single, wavetype As UShort)
        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
    End Sub

    '================================================================================
    ' データの受け渡し（取得）
    '================================================================================
    Sub chinfo_data_get(ByRef aochannel As UShort, ByRef amplitude As Single, ByRef offset As Single, ByRef amplitudetype As UShort,
                        ByRef amplitudeunit As UShort, ByRef duty As Single, ByRef phase As Single, ByRef sweeptype As UShort,
                        ByRef sweeptime As UShort, ByRef sweepstart As Single, ByRef sweepstop As Single)
        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
    End Sub

    '================================================================================
    ' 初期表示
    ' 値は現在の設定値
    '================================================================================
    Private Sub ChannelSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' チャネル番号の表示を更新
        '----------------------------------------
        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")
        '----------------------------------------
        ' 振幅単位コンボボックスの選択を変更
        '----------------------------------------
        comboBox_AmplitudeUnit.SelectedIndex = m_amplitude_unit
        '----------------------------------------
        ' 簡易スイープコンボボックスの作成
        '----------------------------------------
        comboBox_SweepType.Items.Add("なし")
        '----------------------------------------
        ' サイン波の場合、「周波数スイープ」「振幅スイープ」「DCオフセットスイープ」の選択肢作成
        '----------------------------------------
        If (m_wave_type = WAVETYPE_SINE) Then
            comboBox_SweepType.Items.Add("周波数スイープ")
            comboBox_SweepType.Items.Add("振幅スイープ")
            comboBox_SweepType.Items.Add("DCオフセットスイープ")
            '----------------------------------------
            ' 矩形波の場合、「デューティスイープ」の選択肢作成
            '----------------------------------------
        ElseIf (m_wave_type = WAVETYPE_SQUARE) Then
            comboBox_SweepType.Items.Add("デューティスイープ")
        End If
        '----------------------------------------
        ' 簡易スイープコンボボックスの選択を変更
        '----------------------------------------
        comboBox_SweepType.SelectedIndex = m_sweep_type
        '----------------------------------------
        ' スイープ時間コンボボックスの作成
        '----------------------------------------
        For i = 0 To (CWAVEFORMGENERATOR_SWEEPTIME - 1)
            comboBox_SweepTime.Items.Add((i + 1).ToString())
        Next i
        '----------------------------------------
        ' スイープ時間コンボボックスの選択を変更
        '----------------------------------------
        comboBox_SweepTime.SelectedIndex = (m_sweep_time - 1)
    End Sub

    '================================================================================
    ' OKボタンを押したときの処理
    '================================================================================
    Private Sub button_Ok_Click(sender As Object, e As EventArgs) Handles button_Ok.Click
        Dim pre_amplitude As Single         ' 振幅
        Dim pre_offset As Single            ' オフセット
        Dim pre_amplitude_unit As UShort    ' 振幅単位
        Dim pre_amplitude_type As UShort    ' 振幅タイプ
        Dim pre_duty As Single              ' Duty比
        Dim pre_phase As Single             ' 位相
        Dim pre_sweep_type As UShort        ' 簡易スイープ
        Dim pre_sweep_time As UShort        ' スイープ時間
        Dim pre_sweep_start As Single       ' スイープ変化前
        Dim pre_sweep_stop As Single        ' スイープ変化後

        '----------------------------------------
        ' テキストボックスから振幅を取得
        '----------------------------------------
        If (Single.TryParse(textBox_Amplitude.Text, pre_amplitude) = False) Then
            pre_amplitude = 0
        End If
        '----------------------------------------
        ' テキストボックスからオフセットを取得
        '----------------------------------------
        If (Single.TryParse(textBox_Offset.Text, pre_offset) = False) Then
            pre_offset = 0
        End If
        '----------------------------------------
        ' テキストボックスからDuty比を取得
        '----------------------------------------
        If (Single.TryParse(textBox_Duty.Text, pre_duty) = False) Then
            pre_duty = 0
        End If
        '----------------------------------------
        ' テキストボックスから位相を取得
        '----------------------------------------
        If (Single.TryParse(textBox_Phase.Text, pre_phase) = False) Then
            pre_phase = 0
        End If
        '----------------------------------------
        ' コンボボックスから振幅単位を取得
        '----------------------------------------
        pre_amplitude_unit = comboBox_AmplitudeUnit.SelectedIndex
        '----------------------------------------
        ' コンボボックスから振幅タイプを取得
        '----------------------------------------
        pre_amplitude_type = comboBox_AmplitudeType.SelectedIndex
        '----------------------------------------
        ' テキストボックスからスイープ変化前を取得
        '----------------------------------------
        If (Single.TryParse(textBox_SweepStart.Text, pre_sweep_start) = False) Then
            pre_sweep_start = 0
        End If
        '----------------------------------------
        ' テキストボックスからスイープ変化後を取得
        '----------------------------------------
        If (Single.TryParse(textBox_SweepStop.Text, pre_sweep_stop) = False) Then
            pre_sweep_stop = 0
        End If
        '----------------------------------------
        ' コンボボックスから簡易スイープを取得
        '----------------------------------------
        pre_sweep_type = comboBox_SweepType.SelectedIndex
        '----------------------------------------
        ' コンボボックスからスイープタイプを取得
        '----------------------------------------
        pre_sweep_time = (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
        Me.Close()
    End Sub

    '================================================================================
    ' Cancelボタンを押したときの処理
    '================================================================================
    Private Sub button_Cancel_Click(sender As Object, e As EventArgs) Handles button_Cancel.Click
        Me.Close()
    End Sub
End Class