﻿Public Class DeviceSetting
    Dim m_aochannels As UShort
    Dim m_frequency As Single
    Dim m_frequencyunit_window As UShort
    Dim m_oscillation_type As UShort
    Dim m_oscillation_start As UShort
    Dim m_oscillation_stop As Single

    '================================================================================
    ' Data transfer (Set)
    '================================================================================
    Sub devinfo_data_set(aochannels As UShort, frequency As Single, frequencyunit As UShort, oscillationtype As UShort, oscillationstart As UShort, oscillationstop As Single)
        m_aochannels = aochannels
        m_frequency = frequency
        m_frequencyunit_window = frequencyunit
        m_oscillation_type = oscillationtype
        m_oscillation_start = oscillationstart
        m_oscillation_stop = oscillationstop
    End Sub

    '================================================================================
    ' Data transfer (Get)
    '================================================================================
    Sub devinfo_data_get(ByRef aochannels As UShort, ByRef frequency As Single, ByRef frequencyunit As UShort, ByRef oscillationtype As UShort, ByRef oscillationstart As UShort, ByRef oscillationstop As Single)
        aochannels = m_aochannels
        frequency = m_frequency
        frequencyunit = m_frequencyunit_window
        oscillationtype = m_oscillation_type
        oscillationstart = m_oscillation_start
        oscillationstop = m_oscillation_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 oscillation waveform
        '----------------------------------------
        textBox_StartOscillation.Text = String.Format("{0}", m_oscillation_start)
        '----------------------------------------
        ' Update the initial display of number of oscillation stop waveform or time of oscillation stop
        '----------------------------------------
        textBox_StopOscillation.Text = String.Format("{0:F4}", m_oscillation_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 oscillation type
        '----------------------------------------
        comboBox_OscillationType.Items.Add("None")
        comboBox_OscillationType.Items.Add("Auto burst")
        comboBox_OscillationType.Items.Add("Trigger burst(rising edge)")
        comboBox_OscillationType.Items.Add("Trigger burst(falling edge)")
        '----------------------------------------
        ' Change the selection of oscillation type combobox
        '----------------------------------------
        comboBox_OscillationType.SelectedIndex = m_oscillation_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_oscillation_start As UShort    ' Oscillation
        Dim pre_oscillation_stop As Single     ' Stop
        Dim pre_channel_num As UShort          ' Number of channel
        Dim pre_frequency_unit As UShort       ' Frequency unit
        Dim pre_oscillation_type As UShort     ' Oscillation 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 oscillation type from combobox
        '----------------------------------------
        pre_oscillation_type = comboBox_OscillationType.SelectedIndex
        '----------------------------------------
        ' Get the oscillation from textbox
        '----------------------------------------
        If (UShort.TryParse(textBox_StartOscillation.Text, pre_oscillation_start) = False) Then
            pre_oscillation_start = 0
        End If
        '----------------------------------------
        ' Get the stop from textbox
        '----------------------------------------
        If (Single.TryParse(textBox_StopOscillation.Text, pre_oscillation_stop) = False) Then
            pre_oscillation_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_oscillation_type = pre_oscillation_type
        m_oscillation_start = pre_oscillation_start
        m_oscillation_stop = pre_oscillation_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