﻿Public Class DeviceSetting
    Dim m_aochannels As Short
    Dim m_frequency As Single
    Dim m_frequencyunit_window As Short
    Dim m_oscilation_type As Short
    Dim m_oscilation_start As Short
    Dim m_oscilation_stop As Single

    '----------------------------------------
    ' Data transfer (Set)
    '----------------------------------------
    Sub devinfo_data_set(aochannels As Short, frequency As Single, frequencyunit As Short, oscilationtype As Short, oscilationstart As Short, oscilationstop As Single)
        m_aochannels = aochannels
        m_frequency = frequency
        m_frequencyunit_window = frequencyunit
        m_oscilation_type = oscilationtype
        m_oscilation_start = oscilationstart
        m_oscilation_stop = oscilationstop
    End Sub

    '----------------------------------------
    ' Data transfer (Get)
    '----------------------------------------
    Sub devinfo_data_get(ByRef aochannels As Short, ByRef frequency As Single, ByRef frequencyunit As Short, ByRef oscilationtype As Short, ByRef oscilationstart As Short, ByRef oscilationstop As Single)
        aochannels = m_aochannels
        frequency = m_frequency
        frequencyunit = m_frequencyunit_window
        oscilationtype = m_oscilation_type
        oscilationstart = m_oscilation_start
        oscilationstop = m_oscilation_stop
    End Sub

    '----------------------------------------
    ' Initial display
    ' Value is current setting value
    '----------------------------------------
    Private Sub DeviceSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Update the initial display of frequency
        '----------------------------------------
        textBox_Frequency.Text = String.Format("{0:F4}", m_frequency)
        '----------------------------------------
        ' Update the initial display of number of oscilation waveform
        '----------------------------------------
        textBox_StartOscilation.Text = String.Format("{0}", m_oscilation_start)
        '----------------------------------------
        ' Update the initial display of number of oscilation stop waveform or time of oscillation stop
        '----------------------------------------
        textBox_StopOscilation.Text = String.Format("{0:F4}", m_oscilation_stop)
        '----------------------------------------
        ' Create the combobox for number of channels
        '----------------------------------------
        For i = 0 To (Form1.CWAVEFORMGENERATOR_CHANNELS - 1)
            comboBox_ChannelNum.Items.Add((i + 1).ToString())
        Next i
        '----------------------------------------
        ' Change the selection of number of channels combobox
        '----------------------------------------
        comboBox_ChannelNum.SelectedIndex = (m_aochannels - 1)
        '----------------------------------------
        ' Create the combobox for frequency unit
        '----------------------------------------
        comboBox_FrequencyUnit.Items.Add("Hz")
        comboBox_FrequencyUnit.Items.Add("kHz")
        comboBox_FrequencyUnit.Items.Add("sec")
        comboBox_FrequencyUnit.Items.Add("msec")
        '----------------------------------------
        ' Change the selection of frequency unit combobox
        '----------------------------------------
        comboBox_FrequencyUnit.SelectedIndex = m_frequencyunit_window
        '-------------------
        ' Create the combobox for oscilation type
        '-------------------
        comboBox_OscilationType.Items.Add("None")
        comboBox_OscilationType.Items.Add("Auto burst")
        comboBox_OscilationType.Items.Add("Trigger burst(rising edge)")
        comboBox_OscilationType.Items.Add("Trigger burst(falling edge)")
        '-------------------
        ' Change the selection of oscilation type combobox
        '-------------------
        comboBox_OscilationType.SelectedIndex = m_oscilation_type
    End Sub

    '================================================================================
    ' It is the process when the OK button is clicked
    ' Set the setting value
    '================================================================================
    Private Sub button_Ok_Click(sender As Object, e As EventArgs) Handles button_Ok.Click
        Dim pre_frequency As Single         ' Frequency
        Dim pre_oscilation_start As Short   ' Oscilation
        Dim pre_oscilation_stop As Single   ' Stop
        Dim pre_channel_num As Short        ' Number of channel
        Dim pre_frequency_unit As Short     ' Frequency unit
        Dim pre_oscilation_type As Short    ' Oscilation type

        '----------------------------------------
        ' Get the number of channel from combobox
        ' Number of channel starts from 1, so +1
        '----------------------------------------
        pre_channel_num = (comboBox_ChannelNum.SelectedIndex + 1)
        '----------------------------------------
        ' Get the frequency from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_Frequency.Text, pre_frequency) = False) Then
            pre_frequency = 0
        End If
        '----------------------------------------
        ' Get the frequency unit from combobox
        '----------------------------------------
        pre_frequency_unit = comboBox_FrequencyUnit.SelectedIndex
        '----------------------------------------
        ' Get the oscilation type from combobox
        '----------------------------------------
        pre_oscilation_type = comboBox_OscilationType.SelectedIndex
        '----------------------------------------
        ' Get the oscilation from textbox
        '----------------------------------------
        If (Short.TryParse(textBox_StartOscilation.Text, pre_oscilation_start) = False) Then
            pre_oscilation_start = 0
        End If
        '----------------------------------------
        ' Get the stop from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_StopOscilation.Text, pre_oscilation_stop) = False) Then
            pre_oscilation_stop = 0
        End If

        '----------------------------------------
        ' Transfer the value set in the child dialog to the main
        ' Set on main
        '----------------------------------------
        m_aochannels = pre_channel_num
        m_frequency = pre_frequency
        m_frequencyunit_window = pre_frequency_unit
        m_oscilation_type = pre_oscilation_type
        m_oscilation_start = pre_oscilation_start
        m_oscilation_stop = pre_oscilation_stop
        Me.Close()
    End Sub

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