﻿Option Strict Off
Option Explicit On
Imports System.Text

Public Class Form1
    Dim Id As Short ' デバイスID
    Dim Data() As Single    ' 取得データの配列
    Dim Records As Integer  ' 取得レコード数
    Dim RecordLength As Integer ' レコード長
    Dim Channels As Short   ' チャネル数
    Dim TriggerChannel As Short ' トリガソースチャネル
    Dim Range As Single ' 入力レンジ
    Dim bmp As Bitmap    ' ビットマップ

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bmp = New Bitmap(Graph.Size.Width, Graph.Size.Height)     ' グラフと同サイズのビットマップを生成
    End Sub

    '==================================
    ' 初期化処理
    '==================================
    Private Sub CommandInit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInit.Click
        Dim Ret As Integer     ' 戻り値
        Dim Ret2 As Integer    ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        '----------------------------------
        ' 初期化処理
        '----------------------------------
        Ret = DigInit(TextDevice.Text, Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigInit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' デバイスのリセット
        '----------------------------------
        Ret = DigResetDevice(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigResetDevice = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "初期化処理 : 正常終了"
    End Sub

    '==================================
    ' 収集開始
    '==================================
    Private Sub CommandStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStart.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        '----------------------------------
        ' オートセットアップを実行
        '----------------------------------
        Ret = DigAutoSetup(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigAutoSetup = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' セットアップ情報を取得/表示
        '----------------------------------
        CommandInfo_Click(CommandInfo, New System.EventArgs())

        '----------------------------------
        ' データ領域を再確保
        '----------------------------------
        ReDim Data(RecordLength * Channels)

        '----------------------------------
        ' 動作開始
        '----------------------------------
        Ret = DigStart(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigStart = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextInfo.Text = "収集開始 : 正常終了"

        '----------------------------------
        ' 動作中ステータスがOFFになるまでポーリング
        '----------------------------------
        Dim Status As Integer

        Do
            System.Windows.Forms.Application.DoEvents()          ' ダイアログ操作を有効にするための処理
            '----------------------------------
            ' ドライバメモリ内レコード数を取得
            '----------------------------------
            Ret = DigGetDriverMemoryRecords(Id, Records)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If

            '----------------------------------
            ' ドライバメモリ内にレコードが存在すれば、新しい方からデータを取得
            '----------------------------------
            If Records > 0 Or Records = -1 Then
                Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
                If Ret <> 0 Then
                    Ret2 = DigGetErrorString(Ret, ErrorString)
                    TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                    Exit Sub
                End If
                '----------------------------------
                ' グラフの描画
                '----------------------------------
                DrawGraph()
            End If
            '----------------------------------
            ' ステータスの取得
            '----------------------------------
            Ret = DigGetStatus(Id, Status)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            LabelStatus.Text = Hex(Status) & "H"
        Loop While (Status And DIG_STATUS_BUSY) = DIG_STATUS_BUSY

        '----------------------------------
        ' ドライバメモリ内レコード数を取得
        '----------------------------------
        Ret = DigGetDriverMemoryRecords(Id, Records)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDriverMemoryRecords = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        '----------------------------------
        ' ドライバメモリ内にレコードが存在すれば、新しい方からデータを取得
        '----------------------------------
        If Records > 0 Or Records - -1 Then
            Ret = DigReadLatestRecords(Id, DIG_RECORD_DATA, 1, Data, Nothing, Nothing)
            If Ret <> 0 Then
                Ret2 = DigGetErrorString(Ret, ErrorString)
                TextInfo.Text = "DigReadLatestRecords = " & Ret & " : " & ErrorString.ToString()
                Exit Sub
            End If
            '----------------------------------
            ' グラフの描画
            '----------------------------------
            DrawGraph()
        End If

    End Sub

    '==================================
    ' 情報取得
    '==================================
    Private Sub CommandInfo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandInfo.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列
        Dim Status As Integer      ' ステータス
        Dim SampleRate As Single          ' サンプルレート
        Dim Duty As Single      ' デューティ比
        Dim Level As Single       ' トリガレベル
        Dim Slope As Short        ' トリガ方向
        Dim Count As Integer          ' トリガ回数

        '----------------------------------
        ' ステータスの取得
        '----------------------------------
        Ret = DigGetStatus(Id, Status)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetStatus = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        LabelStatus.Text = Hex(Status) & "H"
        '-------------------------------------
        ' DigAutoSetup関数で自動的に設定される情報について取得
        '-------------------------------------
        '-------------------------------
        ' サンプルレートの取得
        '-------------------------------
        Ret = DigGetSampleRate(Id, SampleRate)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = "サンプルレート：" & SampleRate & "Hz" & vbCrLf
        '-------------------------------
        ' レコード長の取得
        '-------------------------------
        Ret = DigGetRecordLength(Id, RecordLength)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetSampleRate = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "レコード長：" & RecordLength & vbCrLf
        '-------------------------------
        ' デューティ比の取得
        '-------------------------------
        Ret = DigGetDutyCycle(Id, Duty)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetDutyCycle = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "デューティ比：" & Duty & "%" & vbCrLf
        '-------------------------------
        ' 有効なチャネル数の取得
        '-------------------------------
        Ret = DigGetChannels(Id, Channels)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetChannels = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        '-------------------------------
        ' トリガ情報の取得
        '-------------------------------
        Ret = DigGetPostTriggerAiEdge(Id, TriggerChannel, Level, Slope, Count)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetPostTriggerAiEdge = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "チャネル：" & TriggerChannel & vbCrLf
        TextDeviceInfo.Text = TextDeviceInfo.Text & "トリガレベル：" & Level & "V" & vbCrLf
        '-------------------------------
        ' レンジ情報の取得
        '-------------------------------
        Ret = DigGetRange(Id, TriggerChannel, Range)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigGetRange = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If
        TextDeviceInfo.Text = TextDeviceInfo.Text & "入力レンジ：" & Range & "VPP" & vbCrLf

        TextInfo.Text = "情報取得 : 正常終了"
    End Sub

    '==================================
    ' 収集停止
    '==================================
    Private Sub CommandStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandStop.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        Ret = DigStop(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "収集停止 : 正常終了"
    End Sub


    '==================================
    ' 終了処理
    '==================================
    Private Sub CommandExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CommandExit.Click
        Dim Ret As Integer        ' 戻り値
        Dim Ret2 As Integer       ' 戻り値2
        Dim ErrorString As New StringBuilder("", 256)     ' エラー文字列

        Ret = DigExit(Id)
        If Ret <> 0 Then
            Ret2 = DigGetErrorString(Ret, ErrorString)
            TextInfo.Text = "DigExit = " & Ret & " : " & ErrorString.ToString()
            Exit Sub
        End If

        TextInfo.Text = "終了処理 : 正常終了"
    End Sub

    '==================================
    ' フォームのアンロード
    '==================================
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Dim Ret As Integer        ' 戻り値
        Ret = DigExit(Id)
    End Sub

    '==================================
    ' グラフ描画
    '==================================
    Private Sub DrawGraph()
        Dim XUnit As Single       ' X軸描画インターバル
        Dim PosX As Single        ' X軸描画位置
        Dim PosY As Single        ' Y軸描画位置
        Dim OldX As Single        ' X軸座標退避用
        Dim OldY As Single        ' Y軸座標退避用
        Dim i As Integer

        '-----------------------------------------
        ' 現在のデータをグラフに描画します。
        '-----------------------------------------
        Dim pen As New Pen(System.Drawing.Color.Red, 1)     ' 赤色ペンの作成
        Graph.Image = bmp
        Dim grp As Graphics = Graphics.FromImage(Graph.Image)       ' ビットマップから描画対象オブジェクトを生成する
        grp.Clear(Graph.BackColor)                                  ' グラフをクリア
        '----------------------------------------------------
        ' 収集データ数分だけ、描画処理を繰り返します。
        ' 有効なチャネルの内、番号の一番若いチャネルのデータのみ表示します。
        '----------------------------------------------------
        XUnit = Graph.Size.Width() / CSng(RecordLength)     ' 1データあたりの横軸移動ピクセル数を計算
        OldY = Graph.Height * ((Range / 2) - Data(0)) / Range       ' Y軸の初期値を計算
        If OldY >= Graph.Height Then
            OldY = Graph.Height - 1         ' Y軸の初期値がグラフの描画領域を超える場合、位置を修正
        End If
        If OldY < 0 Then
            OldY = 0              ' Y軸の初期値がグラフの描画領域を下回る場合、位置を修正
        End If
        OldX = 0
        For i = Channels To RecordLength * Channels Step Channels
            PosX = OldX + XUnit                     ' X軸の座標計算
            PosY = Graph.Height() * ((Range / 2) - Data(i)) / Range          ' Y軸の座標計算
            If PosY >= Graph.Height() Then
                ' Y軸の座標が描画領域を超える場合
                PosY = Graph.Height() - 1            ' 描画位置の修正

            ElseIf PosY < 0 Then
                ' Y軸の座標が描画領域を下回る場合
                PosY = 0                ' 描画位置の修正
            End If
            grp.DrawLine(pen, OldX, OldY, PosX, PosY)             ' 描画

            OldX = PosX         ' X軸の座標を描画用に保持
            OldY = PosY         ' Y軸の座標を描画用に保持
        Next

        pen.Dispose()        ' リソースの解放
        grp.Dispose()        ' リソースの解放
    End Sub
End Class
