' - Encoder full -
' This example simply displays the Encoder info if it is attached and displays the acceleration data for each axis as it is 
' changing in pretty much raw form.  It also allows for modifying the sensitivity of each axis that is available on the attached
' Encoder.
'
' Please note that this example was designed to work with only one Phidget Encoder connected. For an example using multiple 
' Phidget Encoders, please see a "multiple" example in the Encoder Examples folder.
'
' Copyright 2007 Phidgets Inc.  
' This work is licensed under the Creative Commons Attribution 2.5 Canada License. 
' To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/

Public Class Form1
    Dim WithEvents phidgetEncoder As Phidgets.Encoder
    Dim inputArray As ArrayList
    Const velQueueSize As Integer = 50
    Dim velData As Queue

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    'initalize the phidget device and call open
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        velData = New Queue(velQueueSize)

        inputArray = New ArrayList
        inputArray.Add(input0Chk)
        inputArray.Add(input1Chk)
        inputArray.Add(input2Chk)
        inputArray.Add(input3Chk)

        Dim i As Integer
        For i = 0 To 3
            DirectCast(inputArray(i), CheckBox).Visible = False
            DirectCast(inputArray(i), CheckBox).AutoCheck = False
            DirectCast(inputArray(i), CheckBox).Enabled = True
        Next

        encoderCmb.Enabled = False
        enabledChk.Enabled = False


        'To reduce code complexity we assume that there is one PhidgetEncoder
        'attached to the PC before the program is run.
        phidgetEncoder = New Phidgets.Encoder
        phidgetEncoder.open()
        'Defaults for text fields
        attachedTxt.Text = "Not Connected"
    End Sub

    'Encoder attach event handler
    Private Sub phidgetEncoder_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles phidgetEncoder.Attach
        'When the Phidget Encoder attaches update the form text boxes
        serialTxt.Text = (Str(phidgetEncoder.SerialNumber))

        attachedTxt.Text = phidgetEncoder.Attached.ToString()
        nameTxt.Text = phidgetEncoder.Name.ToString()
        serialTxt.Text = phidgetEncoder.SerialNumber.ToString()
        versionTxt.Text = phidgetEncoder.Version.ToString()
        numEncodersTxt.Text = phidgetEncoder.encoders.Count.ToString()
        numInputsTxt.Text = phidgetEncoder.inputs.Count.ToString()

        Dim i As Integer
        For i = 0 To phidgetEncoder.inputs.Count
            DirectCast(inputArray(i), CheckBox).Visible = True
        Next

        For i = 0 To phidgetEncoder.encoders.Count
            encoderCmb.Items.Add(i)
        Next

        encoderCmb.SelectedIndex = 0
        encoderCmb.Enabled = True

        If phidgetEncoder.ID = Phidgets.Phidget.PhidgetID.ENCODER_HS_4ENCODER Then
            enabledChk.Enabled = True
            timeChangeLbl.Text = "Time since last change (us):"
        ElseIf phidgetEncoder.ID = Phidgets.Phidget.PhidgetID.ENCODER_HS_1ENCODER Or phidgetEncoder.ID = Phidgets.Phidget.PhidgetID.ENCODER_1ENCODER_1INPUT Then
            enabledChk.Enabled = False
            timeChangeLbl.Text = "Time since last change (ms):"
            indexPositionTxt.Text = "Unsupported"
        End If

    End Sub

    'Encoder detach event handler
    Private Sub phidgetEncoder_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles phidgetEncoder.Detach
        attachedTxt.Text = phidgetEncoder.Attached.ToString()
        nameTxt.Text = ""
        serialTxt.Text = ""
        versionTxt.Text = ""
        numEncodersTxt.Text = ""
        numInputsTxt.Text = ""
        positionTxt.Text = ""
        encoderPositionTxt.Text = ""
        timeTxt.Text = ""
        velocityTxt.Text = ""

        Dim i As Integer
        For i = 0 To phidgetEncoder.inputs.Count
            DirectCast(inputArray(i), CheckBox).Visible = False
        Next

        encoderCmb.Enabled = False
        encoderCmb.Items.Clear()
        enabledChk.Enabled = False
        enabledChk.Checked = False
        indexPositionTxt.Clear()

    End Sub

    'Encoder error event handler
    Private Sub phidgetEncoder_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles phidgetEncoder.Error
        MessageBox.Show(e.Description)
    End Sub

    Private Sub phidgetEncoder_InputChange(ByVal sender As Object, ByVal e As Phidgets.Events.InputChangeEventArgs) Handles phidgetEncoder.InputChange
        DirectCast(inputArray(e.Index), CheckBox).Checked = e.Value
    End Sub

    Private Sub phidgetEncoder_PositionChange(ByVal sender As Object, ByVal e As Phidgets.Events.EncoderPositionChangeEventArgs) Handles phidgetEncoder.PositionChange
        Dim index As Integer = DirectCast(encoderCmb.SelectedItem, Integer)

        If index = e.Index Then
            positionTxt.Text = e.PositionChange.ToString()

            Try
                timeTxt.Text = e.Time.ToString()
            Catch
                timeTxt.Text = "Unknown"
            End Try

            encoderPositionTxt.Text = phidgetEncoder.encoders(e.Index).ToString()

            Dim veloc As Double = 0
            Try
                veloc = ((CType(e.PositionChange, Double) * 1000000) / (CType(e.Time, Double) * 4))
            Catch

            End Try

            If velData.Count = velQueueSize Then
                velData.Dequeue()
            End If
            velData.Enqueue(veloc)

            Dim d As Double
            Dim averageVel As Double = 0
            Dim velCount As Integer = 0

            For Each d In velData
                averageVel += d
                velCount += 1
            Next
            averageVel /= velCount
            velocityTxt.Text = averageVel.ToString("0.#")

            If phidgetEncoder.ID = Phidgets.Phidget.PhidgetID.ENCODER_HS_4ENCODER Then
                Try
                    indexPositionTxt.Text = phidgetEncoder.encodersWithEnable(e.Index).IndexPosition.ToString()
                Catch ex As Phidgets.PhidgetException
                    indexPositionTxt.Text = "Unknown"
                End Try
            End If
        End If
    End Sub

    'When the application is being terminated, close the Phidget
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'If the form closes and the PhidgetEncoder is attached Close it.
        If phidgetEncoder.Attached = True Then
            RemoveHandler phidgetEncoder.Attach, AddressOf phidgetEncoder_Attach
            RemoveHandler phidgetEncoder.Detach, AddressOf phidgetEncoder_Detach
            RemoveHandler phidgetEncoder.Error, AddressOf phidgetEncoder_Error
            RemoveHandler phidgetEncoder.InputChange, AddressOf phidgetEncoder_InputChange
            RemoveHandler phidgetEncoder.PositionChange, AddressOf phidgetEncoder_PositionChange

            Application.DoEvents()

            phidgetEncoder.close()
        End If
    End Sub

    Private Sub encoderCmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles encoderCmb.SelectedIndexChanged
        Dim index As Integer = DirectCast(encoderCmb.SelectedItem, Integer)

        positionTxt.Text = ""
        timeTxt.Text = ""
        velocityTxt.Text = ""
        velData.Clear()

        enabledChk.Checked = phidgetEncoder.encodersWithEnable(index).Enabled
        Try
            encoderPositionTxt.Text = phidgetEncoder.encodersWithEnable(index).Position.ToString()
        Catch ex As Phidgets.PhidgetException
            encoderPositionTxt.Text = "Unknown"
        End Try

        If phidgetEncoder.ID = Phidgets.Phidget.PhidgetID.ENCODER_HS_4ENCODER Then
            Try
                indexPositionTxt.Text = phidgetEncoder.encodersWithEnable(index).IndexPosition.ToString()
            Catch ex As Phidgets.PhidgetException
                indexPositionTxt.Text = "Unknown"
            End Try
        End If
    End Sub

    Private Sub enabledChk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles enabledChk.CheckedChanged
        Try
            Dim index As Integer = DirectCast(encoderCmb.SelectedItem, Integer)
            phidgetEncoder.encodersWithEnable(index).Enabled = enabledChk.Checked
        Catch

        End Try
    End Sub
End Class