﻿Imports System.Text

Public Class Condition
    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Dim g_start_trigger() As Short = {0,    ' ソフトウェア
                                      1,    ' 外部トリガ立ち上がり
                                      2}    ' 外部トリガ立ち下がり
    Dim g_clock_type() As Short = {0,       ' 内部クロック
                                   1}       ' 外部クロック
    Dim g_clock_edge() As Short = {0,       ' 立ち下がりエッジ
                                   1}       ' 立ち上がりエッジ
    Dim g_stop_trigger() As Short = {1,     ' 外部トリガ立ち上がり
                                     2}     ' 外部トリガ立ち下がり

    Private Sub Condition_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim message_string As String                      ' メッセージ表示用
        Dim ret As Integer                                ' 戻り値取得用
        Dim index As Integer                              ' コンボボックスのインデックス
        Dim ao_start_trigger As Short                     ' 変換開始条件
        Dim ao_stop_trigger As Short                      ' 変換停止条件
        Dim ao_clock_type As Short                        ' クロックの種類
        Dim ao_generating_clock As Single                 ' ジェネレーティングクロック
        Dim ao_clock_edge As Short                        ' 外部クロックの入力タイミング
        Dim err_flag As Short                             ' 関数の実行エラーフラグ

        '----------------------------------------
        ' パラメータの初期化
        '----------------------------------------
        err_flag = 0
        message_string = ""
        '----------------------------------------
        ' アナログ出力変換開始条件用コンボボックスの作成
        '----------------------------------------
        ComboBox_StartCondition.Items.Add("ソフトウェア")
        ComboBox_StartCondition.Items.Add("外部トリガ立ち上がり")
        ComboBox_StartCondition.Items.Add("外部トリガ立ち下がり")
        '----------------------------------------
        ' アナログ出力変換開始条件を取得
        '----------------------------------------
        ret = AioGetAoStartTrigger(Form1.g_id, ao_start_trigger)
        '----------------------------------------
        ' 異常終了の処理
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' エラーメッセージ生成
                '--------------------
                message_string = "AioGetAoStartTrigger = " & ret & "：" & error_string.ToString() + vbCrLf + vbCrLf + "※エラーが発生したため表示を更新できませんでした。"
                err_flag = 1
            Else
                '--------------------
                ' コントロールを無効に設定
                '--------------------
                ComboBox_StartCondition.Enabled = False
            End If
            '--------------------
            ' アナログ出力変換開始条件用コンボボックスの選択を0番に変更
            '--------------------
            ComboBox_StartCondition.SelectedIndex = 0
        '----------------------------------------
        ' 正常終了の処理
        '----------------------------------------
        Else
            '--------------------
            ' 変換開始条件からインデックスに変換
            '--------------------
            index = 0
            Select Case ao_start_trigger
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case 2
                    index = 2
                Case Else
                    index = 0
            End Select
            '--------------------
            ' アナログ出力変換開始条件用コンボボックスの選択を変更
            '--------------------
            ComboBox_StartCondition.SelectedIndex = index
        End If
        '----------------------------------------
        ' アナログ出力クロック種類用コンボボックスの作成
        '----------------------------------------
        ComboBox_ClockType.Items.Add("内部クロック")
        ComboBox_ClockType.Items.Add("外部クロック")
        '----------------------------------------
        ' クロックの種類を取得
        '----------------------------------------
        ret = AioGetAoClockType(Form1.g_id, ao_clock_type)
        '----------------------------------------
        ' 異常終了の処理
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' エラーメッセージ生成
                '--------------------
                message_string = "AioGetAoClockType = " & ret & "：" & error_string.ToString() + vbCrLf + vbCrLf + "※エラーが発生したため表示を更新できませんでした。"
                err_flag = 1
            Else
                '--------------------
                ' コントロールを無効に設定
                '--------------------
                ComboBox_ClockType.Enabled = False
            End If
            '--------------------
            ' アナログ出力クロック種類用コンボボックスの選択を0番に変更
            '--------------------
            ComboBox_ClockType.SelectedIndex = 0
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        '----------------------------------------
        ' 正常終了の処理
        '----------------------------------------
        Else
            '--------------------
            ' クロック種類からインデックスに変換
            '--------------------
            index = 0
            Select Case ao_clock_type
                Case 0
                    index = 0
                Case 1
                    index = 1
                Case Else
                    index = 0
            End Select
            '--------------------
            ' アナログ出力クロック種類用コンボボックスの選択を変更
            '--------------------
            ComboBox_ClockType.SelectedIndex = index
            ComboBox_ClockType_SelectedIndexChanged(New Object(), New EventArgs())
        End If
        '----------------------------------------
        ' ジェネレーティングクロックを取得
        '----------------------------------------
        ret = AioGetAoSamplingClock(Form1.g_id, ao_generating_clock)
        '----------------------------------------
        ' 異常終了の処理
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' エラーメッセージ生成
                '--------------------
                message_string = "AioGetAoSamplingClock = " & ret & "：" & error_string.ToString() + vbCrLf + vbCrLf + "※エラーが発生したため表示を更新できませんでした。"
                err_flag = 1
            Else
                '--------------------
                ' コントロールを無効に設定
                '--------------------
                TextBox_GeneratingClock.Enabled = False
            End If
            '--------------------
            ' ジェネレーティングクロックの初期表示を更新
            '--------------------
            TextBox_GeneratingClock.Text = "1000"
        '----------------------------------------
        ' 正常終了の処理
        '----------------------------------------
        Else
            '--------------------
            ' ジェネレーティングクロックを更新
            '--------------------
            TextBox_GeneratingClock.Text = Convert.ToDouble(ao_generating_clock).ToString("F6")
        End If
        '----------------------------------------
        ' アナログ出力外部クロック入力タイミング用コンボボックスの作成
        '----------------------------------------
        ComboBox_ExternalClockInputTiming.Items.Add("立ち下がりエッジ")
        ComboBox_ExternalClockInputTiming.Items.Add("立ち上がりエッジ")
        '----------------------------------------
        ' 外部クロックの場合
        '----------------------------------------
        If (ao_clock_type = 1) Then
            '----------------------------------------
            ' 外部クロックの入力タイミングを取得
            '----------------------------------------
            ret = AioGetAoClockEdge(Form1.g_id, ao_clock_edge)
            '----------------------------------------
            ' 異常終了の処理
            '----------------------------------------
            If (ret <> 0) Then
                If (ret <> 20001) Then
                    '--------------------
                    ' エラー文字列取得
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' エラーメッセージ生成
                    '--------------------
                    message_string = "AioGetAoClockEdge = " & ret & "：" & error_string.ToString() + vbCrLf + vbCrLf + "※エラーが発生したため表示を更新できませんでした。"
                    err_flag = 1
                Else
                    '--------------------
                    ' コントロールを無効に設定
                    '--------------------
                    ComboBox_ExternalClockInputTiming.Enabled = False
                End If
                '----------------------------------------
                ' 外部クロック入力タイミング用コンボボックスの選択を0番に変更
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = 0
            '----------------------------------------
            ' 正常終了の処理
            '----------------------------------------
            Else
                '--------------------
                ' 入力タイミングからインデックスに変換
                '--------------------
                index = 0
                Select Case ao_clock_edge
                    Case 0
                        index = 0
                    Case 1
                        index = 1
                    Case Else
                        index = 0
                End Select
                '----------------------------------------
                ' 外部クロック入力タイミング用コンボボックスの選択を変更
                '----------------------------------------
                ComboBox_ExternalClockInputTiming.SelectedIndex = index
            End If
        '----------------------------------------
        ' 外部クロック以外の場合
        '----------------------------------------
        Else
            '----------------------------------------
            ' アナログ出力外部クロック入力タイミング用コンボボックスの選択を0番に変更
            '----------------------------------------
            ComboBox_ExternalClockInputTiming.SelectedIndex = 0
        End If
        '----------------------------------------
        ' アナログ出力変換停止条件用コンボボックスの作成
        '----------------------------------------
        ComboBox_StopCondition.Items.Add("外部トリガ立ち上がり")
        ComboBox_StopCondition.Items.Add("外部トリガ立ち下がり")
        '----------------------------------------
        ' アナログ出力変換停止条件を取得
        '----------------------------------------
        ret = AioGetAoStopTrigger(Form1.g_id, ao_stop_trigger)
        '----------------------------------------
        ' 異常終了の処理
        '----------------------------------------
        If (ret <> 0) Then
            If (ret <> 20001) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' エラーメッセージ生成
                '--------------------
                message_string = "AioGetAoStopTrigger = " & ret & "：" & error_string.ToString() + vbCrLf + vbCrLf + "※エラーが発生したため表示を更新できませんでした。"
                err_flag = 1
            Else
                '--------------------
                ' コントロールを無効に設定
                '--------------------
                ComboBox_StopCondition.Enabled = False
            End If
            '--------------------
            ' アナログ出力変換停止条件用コンボボックスの選択を0番に変更
            '--------------------
            ComboBox_StopCondition.SelectedIndex = 0
        '----------------------------------------
        ' 正常終了の処理
        '----------------------------------------
        Else
            '--------------------
            ' 変換停止条件からインデックスに変換
            '--------------------
            index = 0
            Select Case ao_stop_trigger
                Case 0
                    index = 0
                Case 1
                    index = 0
                Case 2
                    index = 1
                Case Else
                    index = 0
            End Select
            '--------------------
            ' アナログ出力変換停止条件用コンボボックスの選択を変更
            '--------------------
            ComboBox_StopCondition.SelectedIndex = index
        End If
        '----------------------------------------
        ' エラーが発生していた場合メッセージを表示
        '----------------------------------------
        If (err_flag <> 0) Then
            MessageBox.Show(message_string, "エラー", MessageBoxButtons.OK)
        End If
    End Sub

    '================================================================================
    ' クロック種類コンボボックスのインデックスが変更された際の処理になります
    ' 使用可能コントロールを更新します
    '================================================================================
    Private Sub ComboBox_ClockType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_ClockType.SelectedIndexChanged
        Dim index As Integer    ' コンボボックスのインデックス

        '----------------------------------------
        ' コンボボックスからクロック種類のインデックスを取得
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' クロック種類による分岐処理
        '----------------------------------------
        Select Case g_clock_type(index)
            Case 0    ' 内部クロック
                TextBox_GeneratingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
            Case 1    ' 外部クロック
                TextBox_GeneratingClock.Enabled = False
                ComboBox_ExternalClockInputTiming.Enabled = True
            Case Else
                TextBox_GeneratingClock.Enabled = True
                ComboBox_ExternalClockInputTiming.Enabled = False
        End Select
    End Sub

    '================================================================================
    ' OKボタンをクリックした際の処理になります
    ' ジェネレーティング条件の設定を行います
    '================================================================================
    Private Sub Button_OK_Click(sender As Object, e As EventArgs) Handles Button_OK.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim index As Integer                              ' コンボボックスのインデックス
        Dim ao_generating_clock As Single                 ' ジェネレーティングクロック

        '----------------------------------------
        ' アナログ出力の変換データ転送方式をデバイスバッファモードに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAoTransferMode(Form1.g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            text_string = "AioSetAoTransferMode = " & ret & "：" & error_string.ToString()
            MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' データ格納用メモリ形式をFIFOに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAoMemoryType(Form1.g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            text_string = "AioSetAoMemoryType = " & ret & "：" & error_string.ToString()
            MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' コンボボックスから変換開始条件設定値のインデックスを取得
        '----------------------------------------
        index = ComboBox_StartCondition.SelectedIndex
        '----------------------------------------
        ' アナログ出力変換開始条件の設定
        '----------------------------------------
        ret = AioSetAoStartTrigger(Form1.g_id, g_start_trigger(index))
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            text_string = "AioSetAoStartTrigger = " & ret & "：" & error_string.ToString()
            MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' コンボボックスからクロック種類設定値のインデックスを取得
        '----------------------------------------
        index = ComboBox_ClockType.SelectedIndex
        '----------------------------------------
        ' アナログ出力クロック種類の設定
        '----------------------------------------
        ret = AioSetAoClockType(Form1.g_id, g_clock_type(index))
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            text_string = "AioSetAoClockType = " & ret & "：" & error_string.ToString()
            MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' 内部クロックの場合
        '----------------------------------------
        If (g_clock_type(index) = 0) Then
            '----------------------------------------
            ' テキストボックスからジェネレーティングクロックを取得
            '----------------------------------------
            If (Single.TryParse(TextBox_GeneratingClock.Text, ao_generating_clock) = False) Then
                ao_generating_clock = 1000
            End If
            '----------------------------------------
            ' アナログ出力ジェネレーティングクロックの設定
            '----------------------------------------
            ret = AioSetAoSamplingClock(Form1.g_id, ao_generating_clock)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            '--------------------
            ' 正常終了でなければ処理を抜ける
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                text_string = "AioSetAoSamplingClock = " & ret & "：" & error_string.ToString()
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
                Exit Sub
            End If
        '----------------------------------------
        ' 外部クロックの場合
        '----------------------------------------
        ElseIf (g_clock_type(index) = 1) Then
            '----------------------------------------
            ' コンボボックスから外部クロック入力タイミング設定値のインデックスを取得
            '----------------------------------------
            index = ComboBox_ExternalClockInputTiming.SelectedIndex
            '----------------------------------------
            ' 外部クロックの入力タイミングの設定
            '----------------------------------------
            ret = AioSetAoClockEdge(Form1.g_id, g_clock_edge(index))
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            '--------------------
            ' 正常終了でなくかつ使用不可でもなければ処理を抜ける
            '--------------------
            If ((ret <> 0) And (ret <> 20001)) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                text_string = "AioSetAoClockEdge = " & ret & "：" & error_string.ToString()
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
                Exit Sub
            End If
        End If
        '----------------------------------------
        ' コンボボックスから変換停止条件設定値のインデックスを取得
        '----------------------------------------
        index = ComboBox_StopCondition.SelectedIndex
        '----------------------------------------
        ' アナログ出力変換停止条件の設定
        '----------------------------------------
        ret = AioSetAoStopTrigger(Form1.g_id, g_stop_trigger(index))
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        '--------------------
        ' 正常終了でなければ処理を抜ける
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            text_string = "AioSetAoStopTrigger = " & ret & "：" & error_string.ToString()
            MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK)
            Exit Sub
        End If
        '----------------------------------------
        ' OK処理
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' Cancelボタンをクリックした際の処理になります
    ' ダイアログを終了します
    '================================================================================
    Private Sub Button_Cancel_Click(sender As Object, e As EventArgs) Handles Button_Cancel.Click
        '----------------------------------------
        ' Cancel処理
        '----------------------------------------
        Me.Close()
    End Sub
End Class