﻿Imports System.Text

Public Class Events

    Dim OutputEvent As Integer = CNT_OEVENT_GENOUT
    Dim RF0 As Integer = CNT_INP_NONE
    Dim RF1 As Integer = CNT_INP_NONE
    Dim PresetReg As Integer = &H1000S


    '===================================================
    ' Dialog initialization
    '===================================================
    Private Sub frmEvent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
        '===================================================
        ' Initialize combo boxes
        '===================================================
        'Signal input event
        With cboInputEvent
            .Items.Clear()
            .Items.Insert(0, "General Input")
            .Items.Insert(1, "Counter Start/Stop")
            .Items.Insert(2, "Counter Preset")
            .Items.Insert(3, "Counter Clear")
        End With

        'Counter Event
        With cboCount
            .Items.Clear()
            .Items.Insert(0, "None")
            .Items.Insert(1, "Preset")
            .Items.Insert(2, "Clear")
        End With

        'RF0 rising/falling
        With cboRF0
            .Items.Clear()
            .Items.Insert(0, "None")
            .Items.Insert(1, "Rising")
            .Items.Insert(2, "Falling")
        End With

        'RF1 rising/falling
        With cboRF1
            .Items.Clear()
            .Items.Insert(0, "None")
            .Items.Insert(1, "Rising")
            .Items.Insert(2, "Falling")
        End With

        'Pulse width
        With cboPulseLength
            .Items.Clear()
            .Items.Insert(&H0S, "0")
            .Items.Insert(&H1S, "1")
            .Items.Insert(&H2S, "2")
            .Items.Insert(&H3S, "3")
            .Items.Insert(&H4S, "4")
            .Items.Insert(&H5S, "5")
            .Items.Insert(&H6S, "6")
            .Items.Insert(&H7S, "7")
            .Items.Insert(&H8S, "8")
            .Items.Insert(&H9S, "9")
            .Items.Insert(&HAS, "A")
            .Items.Insert(&HBS, "B")
            .Items.Insert(&HCS, "C")
            .Items.Insert(&HDS, "D")
            .Items.Insert(&HES, "E")
            .Items.Insert(&HFS, "F")
        End With

        cboInputEvent.SelectedIndex = 0
        cboCount.SelectedIndex = 0
        cboRF0.SelectedIndex = 2
        cboRF1.SelectedIndex = 2
        cboPulseLength.SelectedIndex = 0
        txtPresetReg.Text = "1000"

        '===================================================
        ' Initialize data
        '===================================================
        ' Input event signal
        cboInputEvent_SelectedIndexChanged(cboInputEvent, New System.EventArgs())

        'Output event signal
        If OutputEvent = CNT_OEVENT_GENOUT Then
            rdoGenOutput.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = False
            txtChNo.Enabled = True
            txtData.Enabled = True
            btnOutPulse.Enabled = True
        End If
        If OutputEvent And CNT_OEVENT_MATCH0 Then
            rdoCountMatch0.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_MATCH1 Then
            rdoCountMatch1.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_AIERR Then
            rdoAiError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_FLTERR Then
            rdoFilterError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If

        If OutputEvent And CNT_OEVENT_ALMERR Then
            rdoAlarmError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        txtChNo.Text = "0"
        txtData.Text = "1"
    End Sub

    '---------------------------------------------
    ' Setting completion processing
    '---------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim dwInputEvent As Integer
        Dim wPulseLength As Short
        Dim dwCountEvent As Integer
        Dim ChNo As Short
        Dim ret As Integer
        Dim str As String
        Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)

        '-------------------------------
        ' Get setting information
        '-------------------------------
        ' Input event signal
        ' Input Event
        dwInputEvent = cboInputEvent.SelectedIndex
        ' Preset Reg
        ' In order to set the Preset Reg value to signed, convert it once through hexadecimal characters
        PresetReg = Convert.ToInt32(Hex(Val("&H" & txtPresetReg.Text)), 16)
        ' RF0
        RF0 = cboRF0.SelectedIndex
        ' RF1
        RF1 = cboRF1.SelectedIndex


        ' Output event signal
        OutputEvent = CNT_OEVENT_GENOUT

        If rdoCountMatch0.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_MATCH0

        ElseIf rdoCountMatch1.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_MATCH1

        ElseIf rdoAiError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_AIERR

        ElseIf rdoFilterError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_FLTERR

        ElseIf rdoAlarmError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_ALMERR
        End If

        ' Pulse width
        Select Case cboPulseLength.SelectedIndex
            Case 0
                wPulseLength = &H0S
            Case 1
                wPulseLength = &H1S
            Case 2
                wPulseLength = &H2S
            Case 3
                wPulseLength = &H3S
            Case 4
                wPulseLength = &H4S
        End Select
        ' Count match event
        dwCountEvent = cboCount.SelectedIndex

        '-------------------------------
        ' Set the set information to device
        '-------------------------------
        For ChNo = 0 To Form1.ChannelNum - 1
            '---------------------------------------------
            ' Set hardware event of control input signal
            '---------------------------------------------
            ret = CntSetInputHardwareEvent(Form1.Id, ChNo, dwInputEvent, RF0, RF1, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetInputHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
            '---------------------------------------------
            ' Set preset register
            '---------------------------------------------
            If dwInputEvent = CNT_IEVENT_PRESET Or dwCountEvent = CNT_MATCH_PRESET Then
                ret = CntSetPresetRegister(Form1.Id, ChNo, PresetReg, 0)
                If ret <> 0 Then
                    CntGetErrorString(ret, error_string)
                    str = String.Format("CntSetPresetRegister : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                    MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                    GoTo ERR_END
                End If
            End If
            '---------------------------------------------
            ' Set hardware event of control output signal
            '---------------------------------------------
            ret = CntSetOutputHardwareEvent(Form1.Id, ChNo, 0, OutputEvent, wPulseLength)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOutputHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
            '---------------------------------------------
            ' Set hardware event of count match
            '---------------------------------------------
            ret = CntSetCountMatchHardwareEvent(Form1.Id, ChNo, 0, dwCountEvent, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCountMatchHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
        Next ChNo
ERR_END:
        Me.Hide()
    End Sub

    '---------------------------------------------
    ' Cancel processing
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    '---------------------------------------------
    ' Set event signal input
    '---------------------------------------------
    Private Sub cboInputEvent_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboInputEvent.SelectedIndexChanged
        If cboInputEvent.Text = "General Input" Then
            cboRF0.SelectedIndex = 0
            cboRF1.SelectedIndex = 0
            cboRF0.Enabled = False
            cboRF1.Enabled = False
            txtPresetReg.Enabled = False
        End If
        If cboInputEvent.Text = "Counter Start/Stop" Then
            cboRF0.Enabled = True
            cboRF1.Enabled = True
            txtPresetReg.Enabled = False
        End If
        If cboInputEvent.Text = "Counter Preset" Then
            cboRF0.Enabled = True
            cboRF1.SelectedIndex = 0
            cboRF1.Enabled = False
            txtPresetReg.Enabled = True
        End If
        If cboInputEvent.Text = "Counter Clear" Then
            cboRF0.Enabled = True
            cboRF1.SelectedIndex = 0
            cboRF1.Enabled = False
            txtPresetReg.Enabled = False
        End If
        If cboCount.Text = "Preset" Then
            txtPresetReg.Enabled = True
        End If
    End Sub

    Private Sub cboCount_Selendok(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoGenOutput.Click
        cboInputEvent_SelectedIndexChanged(cboInputEvent, New System.EventArgs())
    End Sub

    '---------------------------------------------
    ' Set event signal output
    '---------------------------------------------
    Private Sub rdoGenOutput_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoGenOutput.Click

        cboPulseLength.Enabled = False
        txtChNo.Enabled = True
        txtData.Enabled = True
        btnOutPulse.Enabled = True

    End Sub

    Private Sub rdoCountMatch0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoCountMatch0.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoCountMatch1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoCountMatch1.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoAiError_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoAiError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoFilterError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoFilterError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoAlarmError_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoAlarmError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    '---------------------------------------------
    ' Signal output processing
    '---------------------------------------------
    Private Sub btnOutPulse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutPulse.Click
        Dim ChNo As Short
        Dim Data As Byte
        Dim ret As Integer
        Dim str As String
        Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)

        ChNo = Val(txtChNo.Text)
        Data = CByte(txtData.Text)
        '---------------------------------------------
        ' General output processing
        '---------------------------------------------
        ret = CntOutputDOBit(Form1.Id, ChNo, 0, Data)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntOutPulse : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
            MsgBox(str, MsgBoxStyle.OkOnly, "Error")
        End If
    End Sub

    Private Sub frmEvent_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class