﻿Public Class ChannelSetting
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_SWEEPTIME As Integer = 10
    Public Const WAVETYPE_SINE As Integer = 0
    Public Const WAVETYPE_SQUARE As Integer = 4
    '----------------------------------------
    ' Variable definition
    '----------------------------------------
    Dim m_aochannel As Short
    Dim m_amplitude As Single
    Dim m_offset As Single
    Dim m_amplitude_type As Short
    Dim m_amplitude_unit As Short
    Dim m_duty As Single
    Dim m_phase As Single
    Dim m_sweep_type As Short
    Dim m_sweep_time As Short
    Dim m_sweep_start As Single
    Dim m_sweep_stop As Single
    Dim m_wave_type As Short

    '----------------------------------------
    ' Data transfer (Set)
    '----------------------------------------
    Sub chinfo_data_set(aochannel As Short, amplitude As Single, offset As Single, amplitudetype As Short,
                        amplitudeunit As Short, duty As Single, phase As Single, sweeptype As Short,
                        sweeptime As Short, sweepstart As Single, sweepstop As Single, wavetype As Short)
        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

    '----------------------------------------
    ' Data transfer (Get)
    '----------------------------------------
    Sub chinfo_data_get(ByRef aochannel As Short, ByRef amplitude As Single, ByRef offset As Single, ByRef amplitudetype As Short,
                        ByRef amplitudeunit As Short, ByRef duty As Single, ByRef phase As Single, ByRef sweeptype As Short,
                        ByRef sweeptime As Short, 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

    '----------------------------------------
    ' Initial display
    ' Value is current setting value
    '----------------------------------------
    Private Sub ChannelSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Update the display of channel number
        '----------------------------------------
        label_ChannelNo.Text = m_aochannel.ToString()
        '----------------------------------------
        ' Update the initial display of amplitude
        '----------------------------------------
        textBox_Amplitude.Text = String.Format("{0:F3}", m_amplitude)
        '----------------------------------------
        ' Update the initial display of offset
        '----------------------------------------
        textBox_Offset.Text = String.Format("{0:F3}", m_offset)
        '----------------------------------------
        ' Update the initial display of duty
        '----------------------------------------
        textBox_Duty.Text = String.Format("{0:F2}", m_duty)
        '----------------------------------------
        ' Update the initial display of phase
        '----------------------------------------
        textBox_Phase.Text = String.Format("{0:F2}", m_phase)
        '----------------------------------------
        ' Update the initial display of sweep before change
        '----------------------------------------
        textBox_SweepStart.Text = String.Format("{0:F2}", m_sweep_start)
        '----------------------------------------
        ' Update the initial display of sweep after change
        '----------------------------------------
        textBox_SweepStop.Text = String.Format("{0:F2}", m_sweep_stop)
        '----------------------------------------
        ' Create the combobox for amplitude type
        '----------------------------------------
        comboBox_AmplitudeType.Items.Add("Amplitude")
        comboBox_AmplitudeType.Items.Add("Vp-p")
        '----------------------------------------
        ' Change the selection of amplitude type combobox
        '----------------------------------------
        comboBox_AmplitudeType.SelectedIndex = m_amplitude_type
        '----------------------------------------
        ' Create the combobox for amplitude unit
        '----------------------------------------
        comboBox_AmplitudeUnit.Items.Add("V or mA")
        comboBox_AmplitudeUnit.Items.Add("LSB")
        '----------------------------------------
        ' Change the selection of amplitude unit combobox
        '----------------------------------------
        comboBox_AmplitudeUnit.SelectedIndex = m_amplitude_unit
        '----------------------------------------
        ' Create the combobox for simple sweep
        '----------------------------------------
        comboBox_SweepType.Items.Add("None")
        '----------------------------------------
        ' For Sinewave, create options for [Frequency sweep], [Amplitude sweep], and [DC offset sweep]
        '----------------------------------------
        If (m_wave_type = WAVETYPE_SINE) Then
            comboBox_SweepType.Items.Add("Frequency sweep")
            comboBox_SweepType.Items.Add("Amplitude sweep")
            comboBox_SweepType.Items.Add("DC offset sweep")
            '----------------------------------------
            ' For Square, create options for [Duty ratio sweep]
            '----------------------------------------
        ElseIf (m_wave_type = WAVETYPE_SQUARE) Then
            comboBox_SweepType.Items.Add("Duty ratio sweep")
        End If
        '----------------------------------------
        ' Change the selection of simple sweep combobox
        '----------------------------------------
        comboBox_SweepType.SelectedIndex = m_sweep_type
        '----------------------------------------
        ' Create the combobox for sweep time
        '----------------------------------------
        For i = 0 To (CWAVEFORMGENERATOR_SWEEPTIME - 1)
            comboBox_SweepTime.Items.Add((i + 1).ToString())
        Next i
        '----------------------------------------
        ' Change the selection of sweep time combobox
        '----------------------------------------
        comboBox_SweepTime.SelectedIndex = (m_sweep_time - 1)
    End Sub

    '----------------------------------------
    ' It is the process when the OK button is clicked
    '----------------------------------------
    Private Sub button_Ok_Click(sender As Object, e As EventArgs) Handles button_Ok.Click
        Dim pre_amplitude As Single     ' Amplitude
        Dim pre_offset As Single        ' Offset
        Dim pre_amplitude_unit As Short ' Amplitude unit
        Dim pre_amplitude_type As Short ' Amplitude type
        Dim pre_duty As Single          ' Duty ratio
        Dim pre_phase As Single         ' Phase
        Dim pre_sweep_type As Short     ' Simple sweep
        Dim pre_sweep_time As Short     ' Sweep time
        Dim pre_sweep_start As Single   ' Sweep before change
        Dim pre_sweep_stop As Single    ' Sweep after change

        '----------------------------------------
        ' Get the amplitude from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_Amplitude.Text, pre_amplitude) = False) Then
            pre_amplitude = 0
        End If
        '----------------------------------------
        ' Get the offset from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_Offset.Text, pre_offset) = False) Then
            pre_offset = 0
        End If
        '----------------------------------------
        ' Get the duty ratio from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_Duty.Text, pre_duty) = False) Then
            pre_duty = 0
        End If
        '----------------------------------------
        ' Get the phase from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_Phase.Text, pre_phase) = False) Then
            pre_phase = 0
        End If
        '----------------------------------------
        ' Get the amplitude unit from combobox
        '----------------------------------------
        pre_amplitude_unit = comboBox_AmplitudeUnit.SelectedIndex
        '----------------------------------------
        ' Get the amplitude type from combobox
        '----------------------------------------
        pre_amplitude_type = comboBox_AmplitudeType.SelectedIndex
        '----------------------------------------
        ' Get the sweep before change from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_SweepStart.Text, pre_sweep_start) = False) Then
            pre_sweep_start = 0
        End If
        '----------------------------------------
        ' Get the sweep after change from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_SweepStop.Text, pre_sweep_stop) = False) Then
            pre_sweep_stop = 0
        End If
        '----------------------------------------
        ' Get the simple sweep from combobox
        '----------------------------------------
        pre_sweep_type = comboBox_SweepType.SelectedIndex
        '----------------------------------------
        ' Get the sweep time from combobox
        '----------------------------------------
        pre_sweep_time = (comboBox_SweepTime.SelectedIndex + 1)

        '----------------------------------------
        ' Transfer the value set in the child dialog to the main
        ' Set on main
        '----------------------------------------
        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

    '----------------------------------------
    ' It is the process when the Cancel button is clicked
    '----------------------------------------
    Private Sub button_Cancel_Click(sender As Object, e As EventArgs) Handles button_Cancel.Click
        Me.Close()
    End Sub
End Class