Programming, IO

Input Interrupt

Current Languages

Description

A simple example of servicing the onInterrupt event of the COM library to detect when inputs occur. Uses the EI command to arm the interrupts.

Screenshot of input interrupt example
images/inputinterrupt.png

Concepts

Details/Special Instructions

Source

'VB Example
Public Class Form1
    WithEvents g As New Galil.Galil
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            g.address = "" 'display dialog for choosing a connection
            PrintStatus(g.connection, System.Drawing.Color.Green)
            Arm()
            PrintStatus("Waiting for inputs 1-8...", Color.Brown)
        Catch ex As Exception
            PrintStatus(ex.Message, System.Drawing.Color.Red)
        End Try
    End Sub


    'event that runs when the library gets in interrupt from the controller
    Private Sub onInterrupt(status As Integer) Handles g.onInterrupt
        Select Case status
            Case 225 'input 1
                InputHandler(1)
            Case 226 'input 2
                InputHandler(2)
            Case 227 'input 3
                InputHandler(3)
            Case 228 'input 4
                InputHandler(4)
            Case 229 'input 5
                InputHandler(5)
            Case 230 'input 6
                InputHandler(6)
            Case 231 'input 7
                InputHandler(7)
            Case 232 'input 8
                InputHandler(8)
            Case Else
                PrintStatus("Unexpected interrupt " + status.ToString, System.Drawing.Color.Orange)
        End Select
    End Sub

    Private Sub InputHandler(input As Byte)
        PrintStatus("Input occured: " + input.ToString, System.Drawing.Color.Blue)
        Arm(True)
    End Sub

    Private Sub Arm(Optional Debounce As Boolean = False)
        Try
            If Debounce Then
                Application.DoEvents()
                Threading.Thread.Sleep(250) 'debounce the input before arming again
            End If
            g.command("EI $8000,$FF") 'set input interrupt mask
            PrintStatus("Armed input interrupts.", Color.Green)
        Catch ex As Exception
            PrintStatus("Arming input interrupts failed. " + ex.Message, Color.Red)
        End Try

    End Sub


    'helper function for printing messages
    Private Sub PrintStatus(ByRef Msg As String, ByRef Color As Color)
        RichTextBox1.SelectionColor = Color
        RichTextBox1.AppendText(Msg + vbCrLf)
    End Sub
End Class