﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public Const CWAVEFORMGENERATOR_CHANNELS As Integer = 16    ' C-WaveformGeneratorの上限チャネル数
    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Public g_appid As Short                                     ' アプリアクセス用ID

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        textBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' 画面サイズコンボボックスの作成
        '----------------------------------------
        comboBox_OpenMode.Items.Add("最小化")
        comboBox_OpenMode.Items.Add("通常")
        comboBox_OpenMode.Items.Add("最大化")
        '----------------------------------------
        ' 画面サイズ用コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_OpenMode.SelectedIndex = 0
        '----------------------------------------
        ' 波形タイプコンボボックスの作成
        '----------------------------------------
        comboBox_WaveType.Items.Add("正弦")
        comboBox_WaveType.Items.Add("固定")
        comboBox_WaveType.Items.Add("三角")
        comboBox_WaveType.Items.Add("のこぎり")
        comboBox_WaveType.Items.Add("矩形")
        comboBox_WaveType.Items.Add("ノイズ")
        comboBox_WaveType.Items.Add("任意")    'データファイルの読み方のサンプルはfileサンプルへ。
        '----------------------------------------
        ' 波形タイプコンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_WaveType.SelectedIndex = 0
        '----------------------------------------
        ' チャネル番号コンボボックスの作成
        ' C-WaveformGeneratorの上限チャネル数
        '----------------------------------------
        For i = 0 To (CWAVEFORMGENERATOR_CHANNELS - 1)
            comboBox_AoChannel.Items.Add(i.ToString())
        Next i
        '----------------------------------------
        ' チャネル番号コンボボックスの選択を0番に変更
        '----------------------------------------
        comboBox_AoChannel.SelectedIndex = 0
    End Sub

    '================================================================================
    ' GenOpenApplicationボタンをクリックした際の処理になります
    ' アプリの初期化処理を行います
    '================================================================================
    Private Sub button_GenOpenApplication_Click(sender As Object, e As EventArgs) Handles button_GenOpenApplication.Click
        Dim device_name As String                         ' デバイス名
        Dim openmode As UShort                            ' 画面サイズ
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' テキストボックスからデバイス名を取得
        '----------------------------------------
        device_name = textBox_DeviceName.Text
        '----------------------------------------
        ' コンボボックスから画面サイズを取得
        '----------------------------------------
        openmode = comboBox_OpenMode.SelectedIndex
        '----------------------------------------
        ' アプリの初期化処理を行う
        '----------------------------------------
        ret = GenOpenApplication(device_name, g_appid, openmode)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenOpenApplication = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' デバイス設定ボタンをクリックした際の処理になります
    ' デバイス設定のダイアログを開きます
    '================================================================================
    Private Sub button_DeviceSetting_Click(sender As Object, e As EventArgs) Handles button_DeviceSetting.Click
        Dim deviceinfodlg As New DeviceSetting
        Dim aochannels As UShort = 1                      ' チャネル数
        Dim frequency As Single = 1                       ' 周波数
        Dim unit_window As Short = 0                      ' 周波数単位コンボボックス
        Dim frequency_type As UShort = 0                  ' 周波数or周期
        Dim frequency_unit As UShort = 0                  ' Hz(sec) Or kHz(msec)
        Dim oscillation_type As UShort = 0                ' 発振タイプ
        Dim oscillation_start As UShort = 1               ' 発振波数
        Dim oscillation_stop As Single = 1                ' 発振停止波数or発振停止時間
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' 戻り値を空白にしておく
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' デバイス情報の取得を行う
        '----------------------------------------
        ret = GenGetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit)
        unit_window = ((frequency_type << 1) + frequency_unit)
        '----------------------------------------
        ' 発振の取得を行う
        '----------------------------------------
        ret = GenGetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' デバイス設定のダイアログへ
        '----------------------------------------
        deviceinfodlg.devinfo_data_set(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' OKを押された場合、処理を行う
        '----------------------------------------
        If (deviceinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' デバイス設定のダイアログから
            '----------------------------------------
            deviceinfodlg.devinfo_data_get(aochannels, frequency, unit_window, oscillation_type, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' 周波数単位から、タイプと単位に変更
            '----------------------------------------
            frequency_type = ((unit_window And &H2S) >> 1)
            frequency_unit = (unit_window And &H1S)
            '----------------------------------------
            ' デバイス情報の設定を行う
            '----------------------------------------
            ret = GenSetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit)
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' エラー文字列取得
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' エラーコード,メッセージを表示
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetDeviceSetting = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' 発振の設定を行う
            '----------------------------------------
            ret = GenSetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' エラー文字列取得
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' エラーコード,メッセージを表示
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetOscillation = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' デバイス設定完了のメッセージ表示
            '----------------------------------------
            textBox_ReturnNum.Text = "デバイス設定：正常終了"
        End If
        deviceinfodlg.Dispose()
    End Sub

    '================================================================================
    ' チャネル設定ボタンをクリックした際の処理になります
    ' チャネル設定のダイアログを開きます
    '================================================================================
    Private Sub button_ChannelSetting_Click(sender As Object, e As EventArgs) Handles button_ChannelSetting.Click
        Dim channelinfodlg As New ChannelSetting
        Dim aochannel As UShort = 0                       ' チャネル番号
        Dim amplitude As Single = 0                       ' 振幅
        Dim offset As Single = 0                          ' オフセット
        Dim amplitude_type As UShort = 0                  ' 振幅タイプ
        Dim amplitude_unit As UShort = 0                  ' 振幅単位
        Dim duty As Single = 0                            ' Duty
        Dim phase As Single = 0                           ' 位相
        Dim sweep_type As UShort = 0                      ' 簡易スイープ
        Dim oscillation_time As UShort = 1                ' スイープ時間
        Dim oscillation_start As Single = 0               ' スイープ変化前
        Dim oscillation_stop As Single = 0                ' スイープ変化後
        Dim ret As Integer                                ' 戻り値取得用
        Dim wave_type As UShort = 0                       ' 波形タイプ
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' 戻り値を空白にしておく
        '----------------------------------------
        textBox_ReturnNum.Text = ""
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' 波形タイプを取得を行う
        '----------------------------------------
        ret = GenGetWaveType(g_appid, aochannel, wave_type)
        '----------------------------------------
        ' チャネル情報の取得を行う
        '----------------------------------------
        ret = GenGetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase)
        '----------------------------------------
        ' 簡易スイープの取得を行う
        '----------------------------------------
        ret = GenGetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop)
        '----------------------------------------
        ' チャネル設定のダイアログへ
        '----------------------------------------
        channelinfodlg.chinfo_data_set(aochannel, amplitude, offset, amplitude_type,
                                        amplitude_unit, duty, phase, sweep_type,
                                        oscillation_time, oscillation_start, oscillation_stop, wave_type)
        '----------------------------------------
        ' OKを押された場合、処理を行う
        '----------------------------------------
        If (channelinfodlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' チャネル設定のダイアログから
            '----------------------------------------
            channelinfodlg.chinfo_data_get(aochannel, amplitude, offset, amplitude_type,
                                           amplitude_unit, duty, phase, sweep_type,
                                           oscillation_time, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' チャネル情報の設定を行う
            '----------------------------------------
            ret = GenSetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase)
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' エラー文字列取得
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' エラーコード,メッセージを表示
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetChannelSetting = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' 簡易スイープの設定を行う
            '----------------------------------------
            ret = GenSetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop)
            '----------------------------------------
            ' 正常終了ではない場合、エラーコードを表示
            '----------------------------------------
            If (ret <> 0) Then
                '----------------------------------------
                ' エラー文字列取得
                '----------------------------------------
                GenGetErrorString(ret, error_string)
                '----------------------------------------
                ' エラーコード,メッセージを表示
                '----------------------------------------
                textBox_ReturnNum.Text = String.Format("GenSetAoSweep = {0} : {1} ", ret, error_string)
                Return
            End If
            '----------------------------------------
            ' チャネル設定完了のメッセージ表示
            '----------------------------------------
            textBox_ReturnNum.Text = "チャネル設定：正常終了"
        End If
        channelinfodlg.Dispose()
    End Sub

    '================================================================================
    ' GenSetWaveTypeボタンをクリックした際の処理になります
    ' 波形タイプの設定を行います
    '================================================================================
    Private Sub button_GenSetWaveType_Click(sender As Object, e As EventArgs) Handles button_GenSetWaveType.Click
        Dim wave_type As UShort                           ' 波形タイプ
        Dim aochannel As UShort                           ' チャネル番号
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' コンボボックスから波形タイプを取得
        '----------------------------------------
        wave_type = comboBox_WaveType.SelectedIndex
        '----------------------------------------
        ' コンボボックスからチャネル番号を取得
        '----------------------------------------
        aochannel = comboBox_AoChannel.SelectedIndex
        '----------------------------------------
        ' 波形タイプの設定を行う
        '----------------------------------------
        ret = GenSetWaveType(g_appid, aochannel, wave_type)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenSetWaveType = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' GenStartOperationボタンをクリックした際の処理になります
    ' アプリの出力開始を行います
    '================================================================================
    Private Sub button_GenStartOperation_Click(sender As Object, e As EventArgs) Handles button_GenStartOperation.Click
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' アプリの出力開始をを行う
        '----------------------------------------
        ret = GenStartOperation(g_appid)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenStartOperation = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' GenGetApplicationStatusボタンをクリックした際の処理になります
    ' 動作状況の取得を行います
    '================================================================================
    Private Sub button_GenGetApplicationStatus_Click(sender As Object, e As EventArgs) Handles button_GenGetApplicationStatus.Click
        Dim status As UInteger = 0                        ' ステータス
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' 動作状況の取得を行う
        '----------------------------------------
        ret = GenGetApplicationStatus(g_appid, status)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenGetApplicationStatus = {0} : {1} ", ret, error_string)
        '----------------------------------------
        ' ステータスを表示
        '----------------------------------------
        If (status = 0) Then
            textBox_Status.Text = "00H(停止中)"
        ElseIf (status = 1) Then
            textBox_Status.Text = "01H(動作中)"
        ElseIf (status = 2) Then
            textBox_Status.Text = "02H(開始トリガ待ち)"
        ElseIf (status = 3) Then
            textBox_Status.Text = "03H(動作中、開始トリガ待ち)"
        Else
            textBox_Status.Text = status.ToString()
        End If
    End Sub

    '================================================================================
    ' GenStopOperationボタンをクリックした際の処理になります
    ' アプリの出力停止を行います
    '================================================================================
    Private Sub button_GenStopOperation_Click(sender As Object, e As EventArgs) Handles button_GenStopOperation.Click
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' アプリの出力停止を行う
        '----------------------------------------
        ret = GenStopOperation(g_appid)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenStopOperation = {0} : {1} ", ret, error_string)
    End Sub

    '================================================================================
    ' GenCloseApplicationボタンをクリックした際の処理になります
    ' アプリの終了処理を行います
    '================================================================================
    Private Sub button_GenCloseApplication_Click(sender As Object, e As EventArgs) Handles button_GenCloseApplication.Click
        Dim status As UInteger = 0                        ' ステータス
        Dim ret As Integer                                ' 戻り値取得用
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列

        '----------------------------------------
        ' デバイスの終了処理を行う
        '----------------------------------------
        ret = GenCloseApplication(g_appid, status)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        GenGetErrorString(ret, error_string)
        '----------------------------------------
        ' エラーコード,メッセージを表示
        '----------------------------------------
        textBox_ReturnNum.Text = String.Format("GenCloseApplication = {0} : {1} ", ret, error_string)
        '----------------------------------------
        ' ステータスを表示
        '----------------------------------------
        If (status = 0) Then
            textBox_Status.Text = "0(正常終了)"
        ElseIf (status = 1) Then
            textBox_Status.Text = "1(C-WaveformGenerator終了エラー)"
        Else
            textBox_Status.Text = status.ToString()
        End If
    End Sub

    '================================================================================
    ' 閉じるボタンをクリックした際の処理になります
    ' アプリケーションの終了処理を行います
    '================================================================================
    Private Sub button_Exit_Click(sender As Object, e As EventArgs) Handles button_Exit.Click
        '----------------------------------------
        ' ダイアログを終了する
        '----------------------------------------
        Me.Close()
    End Sub
End Class
